diff --git a/src/lib.rs b/src/lib.rs index e76a757fb..57f31ae40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -323,7 +323,7 @@ impl Build { warnings_into_errors: false, env_cache: Arc::new(Mutex::new(HashMap::new())), apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())), - emit_rerun_if_env_changed: false, + emit_rerun_if_env_changed: true, } } @@ -899,7 +899,7 @@ impl Build { /// - `rustc-link-search=native=`*target folder* /// - When target is MSVC, the ATL-MFC libs are added via `rustc-link-search=native=` /// - When C++ is enabled, the C++ stdlib is added via `rustc-link-lib` - /// - If `emit_rerun_if_env_changed` is `true`, `rerun-if-env-changed=`*env* + /// - If `emit_rerun_if_env_changed` is not `false`, `rerun-if-env-changed=`*env* /// pub fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut Build { self.cargo_metadata = cargo_metadata; @@ -945,7 +945,7 @@ impl Build { /// /// This has no effect if the `cargo_metadata` option is `false`. /// - /// This option defaults to `false`. + /// This option defaults to `true`. pub fn emit_rerun_if_env_changed(&mut self, emit_rerun_if_env_changed: bool) -> &mut Build { self.emit_rerun_if_env_changed = emit_rerun_if_env_changed; self @@ -2854,11 +2854,25 @@ impl Build { } fn getenv(&self, v: &str) -> Option { + // Returns true for environment variables cargo sets for build scripts: + // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts + // + // This handles more of the vars than we actually use (it tries to check + // complete-ish set), just to avoid needing maintenance if/when new + // calls to `getenv`/`getenv_unwrap` are added. + fn provided_by_cargo(envvar: &str) -> bool { + match envvar { + v if v.starts_with("CARGO") || v.starts_with("RUSTC") => true, + "HOST" | "TARGET" | "RUSTDOC" | "OUT_DIR" | "OPT_LEVEL" | "DEBUG" | "PROFILE" + | "NUM_JOBS" | "RUSTFLAGS" => true, + _ => false, + } + } let mut cache = self.env_cache.lock().unwrap(); if let Some(val) = cache.get(v) { return val.clone(); } - if self.emit_rerun_if_env_changed { + if self.emit_rerun_if_env_changed && !provided_by_cargo(v) { self.print(&format!("cargo:rerun-if-env-changed={}", v)); } let r = env::var(v).ok();