From cdda9008a375db062c3a69125c263ccf61323aa7 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Fri, 8 Mar 2024 20:33:22 +0800 Subject: [PATCH 1/2] fix: show build script didnt rerun when target rustflags changed --- tests/testsuite/build_script.rs | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index a60c59b0b25..8da34c44075 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -5527,3 +5527,57 @@ fn test_old_syntax_with_old_msrv() { p.cargo("build -v").run(); p.cargo("run -v").with_stdout("foo\n").run(); } + +#[cargo_test] +fn build_script_rerun_when_target_rustflags_change() { + let target = rustc_host(); + let p = project() + .file( + "src/main.rs", + r#" + fn main() { + #[cfg(enable)] + println!("hello"); + } + "#, + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") { + if !rustflags.is_empty() { + println!("cargo::rustc-cfg=enable") + } + } + } + "#, + ) + .build(); + + p.cargo("run --target") + .arg(&target) + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([..]) +[FINISHED] [..] +[RUNNING] [..] +", + ) + .run(); + + p.cargo("run --target") + .arg(&target) + .env("RUSTFLAGS", "-C opt-level=3") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([..]) +[FINISHED] [..] +[RUNNING] [..] +", + ) + .with_stdout("") + .run(); +} From db7afeba4e415626d78eb3f2e6005fa303f82f59 Mon Sep 17 00:00:00 2001 From: heisen-li Date: Sun, 7 Apr 2024 19:58:23 +0800 Subject: [PATCH 2/2] fix: rerun build script when target rustflags changed fixes #13003 --- src/cargo/core/compiler/fingerprint/mod.rs | 3 +++ tests/testsuite/build_script.rs | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index 4fdbed220c1..f3389b49f58 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -1526,11 +1526,14 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change .collect::>>()? }; + let rustflags = build_runner.bcx.rustflags_args(unit).to_vec(); + Ok(Fingerprint { local: Mutex::new(local), rustc: util::hash_u64(&build_runner.bcx.rustc().verbose_version), deps, outputs: if overridden { Vec::new() } else { vec![output] }, + rustflags, // Most of the other info is blank here as we don't really include it // in the execution of the build script, but... this may be a latent diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 8da34c44075..2f75826bd59 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -5578,6 +5578,9 @@ fn build_script_rerun_when_target_rustflags_change() { [RUNNING] [..] ", ) - .with_stdout("") + .with_stdout( + "\ +hello", + ) .run(); }