Skip to content

Commit

Permalink
Add option to emit rerun-if-env-changed metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-daniel authored and thomcc committed Oct 29, 2022
1 parent 3eeb50b commit 962af53
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,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 @@ -322,6 +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,
}
}

Expand Down Expand Up @@ -897,6 +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*
///
pub fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut Build {
self.cargo_metadata = cargo_metadata;
Expand Down Expand Up @@ -936,6 +939,17 @@ impl Build {
self.use_plt = Some(use_plt);
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.
///
Expand Down Expand Up @@ -2840,6 +2854,9 @@ impl Build {
}

fn getenv(&self, v: &str) -> Option<String> {
if self.emit_rerun_if_env_changed {
self.print(&format!("cargo:rerun-if-env-changed={}", v));
}
let mut cache = self.env_cache.lock().unwrap();
if let Some(val) = cache.get(v) {
return val.clone();
Expand Down

0 comments on commit 962af53

Please sign in to comment.