@@ -125,6 +125,7 @@ pub struct Build {
125125 extra_warnings : Option < bool > ,
126126 env_cache : Arc < Mutex < HashMap < String , Option < String > > > > ,
127127 apple_sdk_root_cache : Arc < Mutex < HashMap < String , OsString > > > ,
128+ emit_rerun_if_env_changed : bool ,
128129}
129130
130131/// Represents the types of errors that may occur while using cc-rs.
@@ -322,6 +323,7 @@ impl Build {
322323 warnings_into_errors : false ,
323324 env_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
324325 apple_sdk_root_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
326+ emit_rerun_if_env_changed : false ,
325327 }
326328 }
327329
@@ -897,6 +899,7 @@ impl Build {
897899 /// - `rustc-link-search=native=`*target folder*
898900 /// - When target is MSVC, the ATL-MFC libs are added via `rustc-link-search=native=`
899901 /// - When C++ is enabled, the C++ stdlib is added via `rustc-link-lib`
902+ /// - If `emit_rerun_if_env_changed` is `true`, `rerun-if-env-changed=`*env*
900903 ///
901904 pub fn cargo_metadata ( & mut self , cargo_metadata : bool ) -> & mut Build {
902905 self . cargo_metadata = cargo_metadata;
@@ -936,6 +939,17 @@ impl Build {
936939 self . use_plt = Some ( use_plt) ;
937940 self
938941 }
942+
943+ /// Define whether metadata should be emitted for cargo to detect environment
944+ /// changes that should trigger a rebuild.
945+ ///
946+ /// This has no effect if the `cargo_metadata` option is `false`.
947+ ///
948+ /// This option defaults to `false`.
949+ pub fn emit_rerun_if_env_changed ( & mut self , emit_rerun_if_env_changed : bool ) -> & mut Build {
950+ self . emit_rerun_if_env_changed = emit_rerun_if_env_changed;
951+ self
952+ }
939953
940954 /// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
941955 ///
@@ -2840,6 +2854,9 @@ impl Build {
28402854 }
28412855
28422856 fn getenv ( & self , v : & str ) -> Option < String > {
2857+ if self . emit_rerun_if_env_changed {
2858+ self . print ( & format ! ( "cargo:rerun-if-env-changed={}" , v) ) ;
2859+ }
28432860 let mut cache = self . env_cache . lock ( ) . unwrap ( ) ;
28442861 if let Some ( val) = cache. get ( v) {
28452862 return val. clone ( ) ;
0 commit comments