Skip to content

Commit 486864f

Browse files
committed
Don't statically link std into rustc_driver for windows-gnu
1 parent 736a249 commit 486864f

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Diff for: src/bootstrap/src/bin/rustc.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ fn main() {
9292
// Get the name of the crate we're compiling, if any.
9393
let crate_name = parse_value_from_args(&orig_args, "--crate-name");
9494

95-
// We want everything statically linked into `rustc_driver`, so remove `-C prefer-dynamic`
96-
if crate_name == Some("rustc_driver") && stage != "0" {
97-
// Remove `-C prefer-dynamic` to link `std` statically into `rustc_driver`
95+
// When statically linking `std` into `rustc_driver`, remove `-C prefer-dynamic`
96+
if env::var("RUSTC_LINK_STD_INTO_RUSTC_DRIVER").unwrap() == "1"
97+
&& crate_name == Some("rustc_driver")
98+
&& stage != "0"
99+
{
98100
if let Some(pos) = args.iter().enumerate().position(|(i, a)| {
99101
a == "-C" && args.get(i + 1).map(|a| a == "prefer-dynamic").unwrap_or(false)
100102
}) {

Diff for: src/bootstrap/src/core/build_steps/compile.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1830,15 +1830,25 @@ impl Step for Assemble {
18301830
})
18311831
.collect::<HashSet<_>>();
18321832

1833+
let link_std_into_rustc_driver = builder.link_std_into_rustc_driver(target_compiler.host);
18331834
let sysroot = builder.sysroot(target_compiler);
18341835
let rustc_libdir = builder.rustc_libdir(target_compiler);
18351836
t!(fs::create_dir_all(&rustc_libdir));
18361837
let src_libdir = builder.sysroot_libdir(build_compiler, host);
18371838
for f in builder.read_dir(&src_libdir) {
18381839
let filename = f.file_name().into_string().unwrap();
1840+
1841+
// For the later stages which gets distributed only copy over the
1842+
// `rustc_driver` library so we don't end up with an extra copy of `std`.
1843+
// If we're not statically linking `std` into `rustc_driver`, just copy every library
1844+
// to ensure `std` is included.
1845+
// We still need `std` for the initial stage as the bootstrap compiler may not
1846+
// have the new `rustc_private` linking behavior.
18391847
let can_be_rustc_dep = filename.starts_with("rustc_driver-")
18401848
|| filename.starts_with("librustc_driver-")
1841-
|| build_compiler.stage == 0;
1849+
|| build_compiler.stage == 0
1850+
|| !link_std_into_rustc_driver;
1851+
18421852
if can_be_rustc_dep
18431853
&& (is_dylib(&filename) || is_debug_info(&filename))
18441854
&& !proc_macros.contains(&filename)

Diff for: src/bootstrap/src/core/builder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,12 @@ impl<'a> Builder<'a> {
11061106
StepDescription::run(v, self, paths);
11071107
}
11081108

1109+
/// Returns if `std` should be statically linked into `rustc_driver`.
1110+
/// It's currently not done on `windows-gnu` due to linker bugs.
1111+
pub fn link_std_into_rustc_driver(&self, target: TargetSelection) -> bool {
1112+
!target.triple.ends_with("-windows-gnu")
1113+
}
1114+
11091115
/// Obtain a compiler at a given stage and for a given host (i.e., this is the target that the
11101116
/// compiler will run on, *not* the target it will build code for). Explicitly does not take
11111117
/// `Compiler` since all `Compiler` instances are meant to be obtained through this function,
@@ -2165,6 +2171,14 @@ impl<'a> Builder<'a> {
21652171
if matches!(mode, Mode::Std) {
21662172
rustflags.arg("-Cprefer-dynamic");
21672173
}
2174+
if matches!(mode, Mode::Rustc) && !self.link_std_into_rustc_driver(target) {
2175+
rustflags.arg("-Cprefer-dynamic");
2176+
}
2177+
2178+
cargo.env(
2179+
"RUSTC_LINK_STD_INTO_RUSTC_DRIVER",
2180+
if self.link_std_into_rustc_driver(target) { "1" } else { "0" },
2181+
);
21682182

21692183
// When building incrementally we default to a lower ThinLTO import limit
21702184
// (unless explicitly specified otherwise). This will produce a somewhat

0 commit comments

Comments
 (0)