Skip to content

Add option to emit rerun-if-env-changed metadata #701

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

Merged
merged 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct Build {
extra_warnings: Option<bool>,
env_cache: Arc<Mutex<HashMap<String, Option<String>>>>,
apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>,
emit_rerun_if_env_changed: bool,
}

/// Represents the types of errors that may occur while using cc-rs.
Expand Down Expand Up @@ -320,6 +321,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,
}
}

Expand Down Expand Up @@ -892,6 +894,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*
///
pub fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut Build {
self.cargo_metadata = cargo_metadata;
Expand Down Expand Up @@ -922,6 +925,17 @@ impl Build {
self
}

/// Define whether metadata should be emitted for cargo to detect environment
/// changes that should trigger a rebuild.
///
/// This has no effect if the `cargo_metadata` option is `false`.
///
/// This option defaults to `false`.
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
}

/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
///
/// This option defaults to `false`, and affect only msvc targets.
Expand Down Expand Up @@ -2792,6 +2806,9 @@ impl Build {
if let Some(val) = cache.get(v) {
return val.clone();
}
if self.emit_rerun_if_env_changed {
self.print(&format!("cargo:rerun-if-env-changed={}", v));
}
let r = env::var(v).ok();
self.print(&format!("{} = {:?}", v, r));
cache.insert(v.to_string(), r.clone());
Expand Down