110b2ab
From e8fb8c36ca0de817b3d30f603d6d6b3c56e8b0be Mon Sep 17 00:00:00 2001
46e7352
From: Josh Stone <jistone@redhat.com>
46e7352
Date: Fri, 5 Apr 2024 15:07:58 -0700
46e7352
Subject: [PATCH] bootstrap: move all of rustc's flags to `rustc_cargo`
46e7352
46e7352
This ensures that `RUSTFLAGS` will be consistent between all modes of
46e7352
building the compiler, so they won't trigger a rebuild by cargo. This
46e7352
kind of fix was started in #119414 just for LTO flags, but it's
46e7352
applicable to all kinds of flags that might be configured.
46e7352
---
46e7352
 src/bootstrap/src/core/build_steps/check.rs   |   2 +-
46e7352
 src/bootstrap/src/core/build_steps/compile.rs | 107 +++++++++---------
46e7352
 src/bootstrap/src/core/build_steps/doc.rs     |   2 +-
46e7352
 src/bootstrap/src/core/build_steps/test.rs    |   2 +-
46e7352
 4 files changed, 59 insertions(+), 54 deletions(-)
46e7352
46e7352
diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs
110b2ab
index 55180a82885b..37d91b14ca1b 100644
46e7352
--- a/src/bootstrap/src/core/build_steps/check.rs
46e7352
+++ b/src/bootstrap/src/core/build_steps/check.rs
110b2ab
@@ -296,7 +296,7 @@ fn run(self, builder: &Builder<'_>) {
46e7352
             cargo_subcommand(builder.kind),
46e7352
         );
110b2ab
 
46e7352
-        rustc_cargo(builder, &mut cargo, target, compiler.stage);
46e7352
+        rustc_cargo(builder, &mut cargo, target, &compiler);
46e7352
 
46e7352
         // For ./x.py clippy, don't run with --all-targets because
46e7352
         // linting tests and benchmarks can produce very noisy results
46e7352
diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs
110b2ab
index e03997181ee5..9420e40d6c2b 100644
46e7352
--- a/src/bootstrap/src/core/build_steps/compile.rs
46e7352
+++ b/src/bootstrap/src/core/build_steps/compile.rs
110b2ab
@@ -945,55 +945,10 @@ fn run(self, builder: &Builder<'_>) -> u32 {
110b2ab
             "build",
110b2ab
         );
46e7352
 
46e7352
-        rustc_cargo(builder, &mut cargo, target, compiler.stage);
46e7352
+        rustc_cargo(builder, &mut cargo, target, &compiler);
46e7352
 
46e7352
-        if builder.config.rust_profile_use.is_some()
46e7352
-            && builder.config.rust_profile_generate.is_some()
46e7352
-        {
46e7352
-            panic!("Cannot use and generate PGO profiles at the same time");
46e7352
-        }
46e7352
-
46e7352
-        // With LLD, we can use ICF (identical code folding) to reduce the executable size
46e7352
-        // of librustc_driver/rustc and to improve i-cache utilization.
46e7352
-        //
46e7352
-        // -Wl,[link options] doesn't work on MSVC. However, /OPT:ICF (technically /OPT:REF,ICF)
46e7352
-        // is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
46e7352
-        // https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
46e7352
-        // https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
46e7352
-        if builder.config.lld_mode.is_used() && !compiler.host.is_msvc() {
46e7352
-            cargo.rustflag("-Clink-args=-Wl,--icf=all");
46e7352
-        }
46e7352
-
46e7352
-        let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
46e7352
-            if compiler.stage == 1 {
46e7352
-                cargo.rustflag(&format!("-Cprofile-generate={path}"));
46e7352
-                // Apparently necessary to avoid overflowing the counters during
46e7352
-                // a Cargo build profile
46e7352
-                cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
46e7352
-                true
46e7352
-            } else {
46e7352
-                false
46e7352
-            }
46e7352
-        } else if let Some(path) = &builder.config.rust_profile_use {
46e7352
-            if compiler.stage == 1 {
46e7352
-                cargo.rustflag(&format!("-Cprofile-use={path}"));
46e7352
-                if builder.is_verbose() {
46e7352
-                    cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
46e7352
-                }
46e7352
-                true
46e7352
-            } else {
46e7352
-                false
46e7352
-            }
46e7352
-        } else {
46e7352
-            false
46e7352
-        };
46e7352
-        if is_collecting {
46e7352
-            // Ensure paths to Rust sources are relative, not absolute.
46e7352
-            cargo.rustflag(&format!(
46e7352
-                "-Cllvm-args=-static-func-strip-dirname-prefix={}",
46e7352
-                builder.config.src.components().count()
46e7352
-            ));
46e7352
-        }
46e7352
+        // NB: all RUSTFLAGS should be added to `rustc_cargo()` so they will be
46e7352
+        // consistently applied by check/doc/test modes too.
46e7352
 
46e7352
         for krate in &*self.crates {
46e7352
             cargo.arg("-p").arg(krate);
110b2ab
@@ -1044,7 +999,12 @@ fn run(self, builder: &Builder<'_>) -> u32 {
46e7352
     }
46e7352
 }
46e7352
 
46e7352
-pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection, stage: u32) {
46e7352
+pub fn rustc_cargo(
46e7352
+    builder: &Builder<'_>,
46e7352
+    cargo: &mut Cargo,
46e7352
+    target: TargetSelection,
46e7352
+    compiler: &Compiler,
46e7352
+) {
46e7352
     cargo
46e7352
         .arg("--features")
110b2ab
         .arg(builder.rustc_features(builder.kind, target))
110b2ab
@@ -1055,7 +1015,7 @@ pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelec
46e7352
 
46e7352
     // We currently don't support cross-crate LTO in stage0. This also isn't hugely necessary
46e7352
     // and may just be a time sink.
46e7352
-    if stage != 0 {
46e7352
+    if compiler.stage != 0 {
46e7352
         match builder.config.rust_lto {
46e7352
             RustcLto::Thin | RustcLto::Fat => {
46e7352
                 // Since using LTO for optimizing dylibs is currently experimental,
110b2ab
@@ -1081,7 +1041,52 @@ pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelec
46e7352
         cargo.rustflag("-Clto=off");
46e7352
     }
46e7352
 
46e7352
-    rustc_cargo_env(builder, cargo, target, stage);
46e7352
+    // With LLD, we can use ICF (identical code folding) to reduce the executable size
46e7352
+    // of librustc_driver/rustc and to improve i-cache utilization.
46e7352
+    //
46e7352
+    // -Wl,[link options] doesn't work on MSVC. However, /OPT:ICF (technically /OPT:REF,ICF)
46e7352
+    // is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
46e7352
+    // https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
46e7352
+    // https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
46e7352
+    if builder.config.lld_mode.is_used() && !compiler.host.is_msvc() {
46e7352
+        cargo.rustflag("-Clink-args=-Wl,--icf=all");
46e7352
+    }
46e7352
+
46e7352
+    if builder.config.rust_profile_use.is_some() && builder.config.rust_profile_generate.is_some() {
46e7352
+        panic!("Cannot use and generate PGO profiles at the same time");
46e7352
+    }
46e7352
+    let is_collecting = if let Some(path) = &builder.config.rust_profile_generate {
46e7352
+        if compiler.stage == 1 {
46e7352
+            cargo.rustflag(&format!("-Cprofile-generate={path}"));
46e7352
+            // Apparently necessary to avoid overflowing the counters during
46e7352
+            // a Cargo build profile
46e7352
+            cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
46e7352
+            true
46e7352
+        } else {
46e7352
+            false
46e7352
+        }
46e7352
+    } else if let Some(path) = &builder.config.rust_profile_use {
46e7352
+        if compiler.stage == 1 {
46e7352
+            cargo.rustflag(&format!("-Cprofile-use={path}"));
46e7352
+            if builder.is_verbose() {
46e7352
+                cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
46e7352
+            }
46e7352
+            true
46e7352
+        } else {
46e7352
+            false
46e7352
+        }
46e7352
+    } else {
46e7352
+        false
46e7352
+    };
46e7352
+    if is_collecting {
46e7352
+        // Ensure paths to Rust sources are relative, not absolute.
46e7352
+        cargo.rustflag(&format!(
46e7352
+            "-Cllvm-args=-static-func-strip-dirname-prefix={}",
46e7352
+            builder.config.src.components().count()
46e7352
+        ));
46e7352
+    }
46e7352
+
46e7352
+    rustc_cargo_env(builder, cargo, target, compiler.stage);
46e7352
 }
46e7352
 
46e7352
 pub fn rustc_cargo_env(
46e7352
diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs
110b2ab
index 51b5cdc05657..a22cbeacf016 100644
46e7352
--- a/src/bootstrap/src/core/build_steps/doc.rs
46e7352
+++ b/src/bootstrap/src/core/build_steps/doc.rs
110b2ab
@@ -804,7 +804,7 @@ fn run(self, builder: &Builder<'_>) {
110b2ab
         // see https://github.com/rust-lang/rust/pull/122066#issuecomment-1983049222
110b2ab
         // cargo.rustdocflag("--generate-link-to-definition");
110b2ab
 
46e7352
-        compile::rustc_cargo(builder, &mut cargo, target, compiler.stage);
46e7352
+        compile::rustc_cargo(builder, &mut cargo, target, &compiler);
46e7352
         cargo.arg("-Zunstable-options");
46e7352
         cargo.arg("-Zskip-rustdoc-fingerprint");
46e7352
 
46e7352
diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs
110b2ab
index bacf5f0d33ce..5277c38a4ad0 100644
46e7352
--- a/src/bootstrap/src/core/build_steps/test.rs
46e7352
+++ b/src/bootstrap/src/core/build_steps/test.rs
110b2ab
@@ -2671,7 +2671,7 @@ fn run(self, builder: &Builder<'_>) {
46e7352
                 }
46e7352
             }
46e7352
             Mode::Rustc => {
46e7352
-                compile::rustc_cargo(builder, &mut cargo, target, compiler.stage);
46e7352
+                compile::rustc_cargo(builder, &mut cargo, target, &compiler);
46e7352
             }
46e7352
             _ => panic!("can only test libraries"),
46e7352
         };
46e7352
-- 
46e7352
2.44.0
46e7352