@@ -125,6 +125,7 @@ pub struct Build {
125
125
extra_warnings : Option < bool > ,
126
126
env_cache : Arc < Mutex < HashMap < String , Option < String > > > > ,
127
127
apple_sdk_root_cache : Arc < Mutex < HashMap < String , OsString > > > ,
128
+ emit_rerun_if_env_changed : bool ,
128
129
}
129
130
130
131
/// Represents the types of errors that may occur while using cc-rs.
@@ -322,6 +323,7 @@ impl Build {
322
323
warnings_into_errors : false ,
323
324
env_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
324
325
apple_sdk_root_cache : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
326
+ emit_rerun_if_env_changed : false ,
325
327
}
326
328
}
327
329
@@ -897,6 +899,7 @@ impl Build {
897
899
/// - `rustc-link-search=native=`*target folder*
898
900
/// - When target is MSVC, the ATL-MFC libs are added via `rustc-link-search=native=`
899
901
/// - 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*
900
903
///
901
904
pub fn cargo_metadata ( & mut self , cargo_metadata : bool ) -> & mut Build {
902
905
self . cargo_metadata = cargo_metadata;
@@ -936,6 +939,17 @@ impl Build {
936
939
self . use_plt = Some ( use_plt) ;
937
940
self
938
941
}
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
+ }
939
953
940
954
/// Configures whether the /MT flag or the /MD flag will be passed to msvc build tools.
941
955
///
@@ -2840,6 +2854,9 @@ impl Build {
2840
2854
}
2841
2855
2842
2856
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
+ }
2843
2860
let mut cache = self . env_cache . lock ( ) . unwrap ( ) ;
2844
2861
if let Some ( val) = cache. get ( v) {
2845
2862
return val. clone ( ) ;
0 commit comments