Skip to content

Commit 91ec716

Browse files
committed
cargo bug around build script rerun has been fixed
1 parent ca86d5f commit 91ec716

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

build.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@ fn main() {
1515
let target = &*env::var("TARGET").expect("TARGET not set");
1616
let target_arch = &*env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
1717
let target_os = &*env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
18-
// HACK: If --target is specified, rustflags is not applied to the build
19-
// script itself, so the build script will not be rerun when these are changed.
20-
// TODO: once https://github.com/rust-lang/cargo/issues/13003 is fixed,
21-
// remove this hack in the version that contains the fix.
22-
//
23-
// Ideally, the build script should be rebuilt when CARGO_ENCODED_RUSTFLAGS
24-
// is changed, but since it is an environment variable set by cargo,
25-
// as of 1.62.0-nightly, specifying it as rerun-if-env-changed does not work.
26-
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS");
27-
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
28-
println!("cargo:rerun-if-env-changed=CARGO_BUILD_RUSTFLAGS");
29-
let mut target_upper = target.replace(['-', '.'], "_");
30-
target_upper.make_ascii_uppercase();
31-
println!("cargo:rerun-if-env-changed=CARGO_TARGET_{target_upper}_RUSTFLAGS");
3218

3319
let version = match rustc_version() {
3420
Some(version) => version,
@@ -42,6 +28,22 @@ fn main() {
4228
}
4329
};
4430

31+
// https://github.com/rust-lang/rust/pull/123745 (includes https://github.com/rust-lang/cargo/pull/13560) merged in Rust 1.79 (nightly-2024-04-11).
32+
if !version.probe(79, 2024, 4, 10) {
33+
// HACK: If --target is specified, rustflags is not applied to the build
34+
// script itself, so the build script will not be recompiled when rustflags
35+
// is changed. That in itself is not a problem, but the old Cargo does
36+
// not rerun the build script as well, which can be problematic.
37+
// https://github.com/rust-lang/cargo/issues/13003
38+
// This problem has been fixed in 1.79 so only older versions need a workaround.
39+
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS");
40+
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
41+
println!("cargo:rerun-if-env-changed=CARGO_BUILD_RUSTFLAGS");
42+
let mut target_upper = target.replace(['-', '.'], "_");
43+
target_upper.make_ascii_uppercase();
44+
println!("cargo:rerun-if-env-changed=CARGO_TARGET_{target_upper}_RUSTFLAGS");
45+
}
46+
4547
// Note that this is `no_`*, not `has_*`. This allows treating as the latest
4648
// stable rustc is used when the build script doesn't run. This is useful
4749
// for non-cargo build systems that don't run the build script.

0 commit comments

Comments
 (0)