Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default emit_rerun_if_env_changed to true #738

Merged
merged 2 commits into from Oct 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/lib.rs
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2854,11 +2854,25 @@ impl Build {
}

fn getenv(&self, v: &str) -> Option<String> {
// 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();
Expand Down