Auto merge of #13560 - heisen-li:build_flag, r=weihanglo

[fix]:Build script not rerun when target rustflags change

### What does this PR try to resolve?

Fixes https://github.com/rust-lang/cargo/issues/13003
This commit is contained in:
bors 2024-04-08 15:54:04 +00:00
commit bd1cf584af
2 changed files with 60 additions and 0 deletions

View File

@ -1526,11 +1526,14 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
.collect::<CargoResult<Vec<_>>>()?
};
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

View File

@ -5537,3 +5537,60 @@ 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(
"\
hello",
)
.run();
}