-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Lower link args to
@
-files on Windows more
When spawning a linker rustc has historically been known to blow OS limits for the command line being too large, notably on Windows. This is especially true of incremental compilation where there can be dozens of object files per compilation. The compiler currently has logic for detecting a failure to spawn and instead passing arguments via a file instead, but this failure detection only triggers if a process actually fails to spawn. Unfortunately on Windows we've got something else to worry about which is `cmd.exe`. The compiler may be running a linker through `cmd.exe` where `cmd.exe` has a limit of 8192 on the command line vs 32k on `CreateProcess`. Moreso rustc actually succeeds in spawning `cmd.exe` today, it's just that after it's running `cmd.exe` fails to spawn its child, which rustc doesn't currently detect. Consequently this commit updates the logic for the spawning the linker on Windows to instead have a heuristic to see if we need to pass arguments via a file. This heuristic is an overly pessimistic and "inaccurate" calculation which just calls `len` on a bunch of `OsString` instances (where `len` is not precisely the length in u16 elements). This number, when exceeding the 6k threshold, will force rustc to always pass arguments through a file. This strategy should avoid us trying to parse the output on Windows of the linker to see if it successfully spawned yet failed to actually sub-spawn the linker. We may just be passing arguments through files a little more commonly now... The motivation for this commit was a recent bug in Gecko [1] when beta testing, notably when incremental compilation was enabled it blew out the limit on `cmd.exe`. This commit will also fix #46999 as well though as emscripten uses a bat script as well (and we're blowing the limit there). [1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1430886 Closes #46999 (cherry picked from commit 66366f9)
- Loading branch information
1 parent
7973dc9
commit 590924b
Showing
6 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-include ../tools.mk | ||
|
||
all: | ||
$(RUSTC) foo.rs -g | ||
cp foo.bat $(TMPDIR)/ | ||
OUT_DIR="$(TMPDIR)" RUSTC="$(RUSTC_ORIGINAL)" $(call RUN,foo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
%MY_LINKER% %* |
96 changes: 96 additions & 0 deletions
96
src/test/run-make/long-linker-command-lines-cmd-exe/foo.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Like the `long-linker-command-lines` test this test attempts to blow | ||
// a command line limit for running the linker. Unlike that test, however, | ||
// this test is testing `cmd.exe` specifically rather than the OS. | ||
// | ||
// Unfortunately `cmd.exe` has a 8192 limit which is relatively small | ||
// in the grand scheme of things and anyone sripting rustc's linker | ||
// is probably using a `*.bat` script and is likely to hit this limit. | ||
// | ||
// This test uses a `foo.bat` script as the linker which just simply | ||
// delegates back to this program. The compiler should use a lower | ||
// limit for arguments before passing everything via `@`, which | ||
// means that everything should still succeed here. | ||
|
||
use std::env; | ||
use std::fs::{self, File}; | ||
use std::io::{BufWriter, Write, Read}; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
if !cfg!(windows) { | ||
return | ||
} | ||
|
||
let tmpdir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
let ok = tmpdir.join("ok"); | ||
let not_ok = tmpdir.join("not_ok"); | ||
if env::var("YOU_ARE_A_LINKER").is_ok() { | ||
match env::args().find(|a| a.contains("@")) { | ||
Some(file) => { fs::copy(&file[1..], &ok).unwrap(); } | ||
None => { File::create(¬_ok).unwrap(); } | ||
} | ||
return | ||
} | ||
|
||
let rustc = env::var_os("RUSTC").unwrap_or("rustc".into()); | ||
let me = env::current_exe().unwrap(); | ||
let bat = me.parent() | ||
.unwrap() | ||
.join("foo.bat"); | ||
let bat_linker = format!("linker={}", bat.display()); | ||
for i in (1..).map(|i| i * 10) { | ||
println!("attempt: {}", i); | ||
|
||
let file = tmpdir.join("bar.rs"); | ||
let mut f = BufWriter::new(File::create(&file).unwrap()); | ||
let mut lib_name = String::new(); | ||
for _ in 0..i { | ||
lib_name.push_str("foo"); | ||
} | ||
for j in 0..i { | ||
writeln!(f, "#[link(name = \"{}{}\")]", lib_name, j).unwrap(); | ||
} | ||
writeln!(f, "extern {{}}\nfn main() {{}}").unwrap(); | ||
f.into_inner().unwrap(); | ||
|
||
drop(fs::remove_file(&ok)); | ||
drop(fs::remove_file(¬_ok)); | ||
let status = Command::new(&rustc) | ||
.arg(&file) | ||
.arg("-C").arg(&bat_linker) | ||
.arg("--out-dir").arg(&tmpdir) | ||
.env("YOU_ARE_A_LINKER", "1") | ||
.env("MY_LINKER", &me) | ||
.status() | ||
.unwrap(); | ||
|
||
if !status.success() { | ||
panic!("rustc didn't succeed: {}", status); | ||
} | ||
|
||
if !ok.exists() { | ||
assert!(not_ok.exists()); | ||
continue | ||
} | ||
|
||
let mut contents = String::new(); | ||
File::open(&ok).unwrap().read_to_string(&mut contents).unwrap(); | ||
|
||
for j in 0..i { | ||
assert!(contents.contains(&format!("{}{}", lib_name, j))); | ||
} | ||
|
||
break | ||
} | ||
} |