Skip to content

Commit

Permalink
cargo bug around build script rerun has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Apr 11, 2024
1 parent 3c86e9c commit 52c277b
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ fn main() {
let target = &*env::var("TARGET").expect("TARGET not set");
let target_arch = &*env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
let target_os = &*env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
// HACK: If --target is specified, rustflags is not applied to the build
// script itself, so the build script will not be rerun when these are changed.
// TODO: once https://github.com/rust-lang/cargo/issues/13003 is fixed,
// remove this hack in the version that contains the fix.
//
// Ideally, the build script should be rebuilt when CARGO_ENCODED_RUSTFLAGS
// is changed, but since it is an environment variable set by cargo,
// as of 1.62.0-nightly, specifying it as rerun-if-env-changed does not work.
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS");
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
println!("cargo:rerun-if-env-changed=CARGO_BUILD_RUSTFLAGS");
let mut target_upper = target.replace(|c: char| c == '-' || c == '.', "_");
target_upper.make_ascii_uppercase();
println!("cargo:rerun-if-env-changed=CARGO_TARGET_{}_RUSTFLAGS", target_upper);

let version = match rustc_version() {
Some(version) => version,
Expand All @@ -59,6 +45,22 @@ fn main() {
}
};

// 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).
if !version.probe(79, 2024, 4, 10) {
// HACK: If --target is specified, rustflags is not applied to the build
// script itself, so the build script will not be recompiled when rustflags
// is changed. That in itself is not a problem, but the old Cargo does
// not rerun the build script as well, which can be problematic.
// https://github.com/rust-lang/cargo/issues/13003
// This problem has been fixed in 1.79 so only older versions need a workaround.
println!("cargo:rerun-if-env-changed=CARGO_ENCODED_RUSTFLAGS");
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
println!("cargo:rerun-if-env-changed=CARGO_BUILD_RUSTFLAGS");
let mut target_upper = target.replace(|c: char| c == '-' || c == '.', "_");
target_upper.make_ascii_uppercase();
println!("cargo:rerun-if-env-changed=CARGO_TARGET_{}_RUSTFLAGS", target_upper);
}

// Note that this is `no_`*, not `has_*`. This allows treating as the latest
// stable rustc is used when the build script doesn't run. This is useful
// for non-cargo build systems that don't run the build script.
Expand Down

0 comments on commit 52c277b

Please sign in to comment.