Skip to content

Commit

Permalink
Auto merge of #13560 - heisen-li:build_flag, r=weihanglo
Browse files Browse the repository at this point in the history
[fix]:Build script not rerun when target rustflags change

### What does this PR try to resolve?

Fixes #13003
  • Loading branch information
bors committed Apr 8, 2024
2 parents 3d16d65 + db7afeb commit bd1cf58
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit bd1cf58

Please sign in to comment.