99use std:: path:: PathBuf ;
1010
1111use crate :: ident:: { is_ascii_ident, is_crate_name, is_feature_name} ;
12+ use crate :: output:: rerun_if_env_changed;
13+
14+ /// [`ProcessEnv`] wrapper that implicit calls [`rerun_if_env_changed`]
15+ const ENV : RerunIfEnvChanged < ProcessEnv > = RerunIfEnvChanged :: new ( ) ;
1216
1317/// Abstraction over environment variables
1418trait Env {
@@ -41,6 +45,28 @@ impl Env for ProcessEnv {
4145 }
4246}
4347
48+ /// [`Env`] wrapper that implicitly calls [`rerun_if_env_changed`]
49+ struct RerunIfEnvChanged < E : Env > ( E ) ;
50+
51+ impl RerunIfEnvChanged < ProcessEnv > {
52+ const fn new ( ) -> Self {
53+ Self ( ProcessEnv )
54+ }
55+ }
56+
57+ impl < E : Env > Env for RerunIfEnvChanged < E > {
58+ #[ track_caller]
59+ fn get ( & self , key : & str ) -> Option < std:: ffi:: OsString > {
60+ rerun_if_env_changed ( key) ;
61+ self . 0 . get ( key)
62+ }
63+
64+ #[ track_caller]
65+ fn is_present ( & self , key : & str ) -> bool {
66+ self . get ( key) . is_some ( )
67+ }
68+ }
69+
4470/// Path to the `cargo` binary performing the build.
4571#[ track_caller]
4672pub fn cargo ( ) -> PathBuf {
@@ -60,8 +86,7 @@ pub fn cargo_manifest_dir() -> PathBuf {
6086/// The path to the manifest of your package.
6187#[ track_caller]
6288pub fn cargo_manifest_path ( ) -> PathBuf {
63- ProcessEnv
64- . get ( "CARGO_MANIFEST_PATH" )
89+ ENV . get ( "CARGO_MANIFEST_PATH" )
6590 . map ( to_path)
6691 . unwrap_or_else ( || {
6792 let mut path = cargo_manifest_dir ( ) ;
@@ -73,7 +98,7 @@ pub fn cargo_manifest_path() -> PathBuf {
7398/// The manifest `links` value.
7499#[ track_caller]
75100pub fn cargo_manifest_links ( ) -> Option < String > {
76- ProcessEnv . get ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
101+ ENV . get ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
77102}
78103
79104/// Contains parameters needed for Cargo’s [jobserver] implementation to parallelize
@@ -88,7 +113,7 @@ pub fn cargo_manifest_links() -> Option<String> {
88113/// [jobserver]: https://www.gnu.org/software/make/manual/html_node/Job-Slots.html
89114#[ track_caller]
90115pub fn cargo_makeflags ( ) -> Option < String > {
91- ProcessEnv . get ( "CARGO_MAKEFLAGS" ) . map ( to_string)
116+ ENV . get ( "CARGO_MAKEFLAGS" ) . map ( to_string)
92117}
93118
94119/// For each activated feature of the package being built, this will be `true`.
@@ -99,7 +124,7 @@ pub fn cargo_feature(name: &str) -> bool {
99124 }
100125 let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
101126 let key = format ! ( "CARGO_FEATURE_{name}" ) ;
102- ProcessEnv . is_present ( & key)
127+ ENV . is_present ( & key)
103128}
104129
105130/// For each [configuration option] of the package being built, this will contain
@@ -113,7 +138,7 @@ pub fn cargo_feature(name: &str) -> bool {
113138#[ track_caller]
114139pub fn cargo_cfg ( cfg : & str ) -> Option < Vec < String > > {
115140 let var = cargo_cfg_var ( cfg) ;
116- ProcessEnv . get ( & var) . map ( |v| to_strings ( v, ',' ) )
141+ ENV . get ( & var) . map ( |v| to_strings ( v, ',' ) )
117142}
118143
119144#[ track_caller]
@@ -143,7 +168,7 @@ mod cfg {
143168 #[ cfg( any( ) ) ]
144169 #[ track_caller]
145170 pub fn cargo_cfg_clippy ( ) -> bool {
146- ProcessEnv . is_present ( "CARGO_CFG_CLIPPY" )
171+ ENV . is_present ( "CARGO_CFG_CLIPPY" )
147172 }
148173
149174 /// If we are compiling with debug assertions enabled.
@@ -154,25 +179,25 @@ mod cfg {
154179 #[ cfg( any( ) ) ]
155180 #[ track_caller]
156181 pub fn cargo_cfg_debug_assertions ( ) -> bool {
157- ProcessEnv . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
182+ ENV . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
158183 }
159184
160185 #[ cfg( any( ) ) ]
161186 #[ track_caller]
162187 pub fn cargo_cfg_doc ( ) -> bool {
163- ProcessEnv . is_present ( "CARGO_CFG_DOC" )
188+ ENV . is_present ( "CARGO_CFG_DOC" )
164189 }
165190
166191 #[ cfg( any( ) ) ]
167192 #[ track_caller]
168193 pub fn cargo_cfg_docsrs ( ) -> bool {
169- ProcessEnv . is_present ( "CARGO_CFG_DOCSRS" )
194+ ENV . is_present ( "CARGO_CFG_DOCSRS" )
170195 }
171196
172197 #[ cfg( any( ) ) ]
173198 #[ track_caller]
174199 pub fn cargo_cfg_doctest ( ) -> bool {
175- ProcessEnv . is_present ( "CARGO_CFG_DOCTEST" )
200+ ENV . is_present ( "CARGO_CFG_DOCTEST" )
176201 }
177202
178203 /// The level of detail provided by derived [`Debug`] implementations.
@@ -186,15 +211,15 @@ mod cfg {
186211 #[ cfg( any( ) ) ]
187212 #[ track_caller]
188213 pub fn cargo_cfg_miri ( ) -> bool {
189- ProcessEnv . is_present ( "CARGO_CFG_MIRI" )
214+ ENV . is_present ( "CARGO_CFG_MIRI" )
190215 }
191216
192217 /// If we are compiling with overflow checks enabled.
193218 #[ doc = unstable ! ( cfg_overflow_checks, 111466 ) ]
194219 #[ cfg( feature = "unstable" ) ]
195220 #[ track_caller]
196221 pub fn cargo_cfg_overflow_checks ( ) -> bool {
197- ProcessEnv . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
222+ ENV . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
198223 }
199224
200225 /// The [panic strategy](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#panic).
@@ -206,7 +231,7 @@ mod cfg {
206231 /// If the crate is being compiled as a procedural macro.
207232 #[ track_caller]
208233 pub fn cargo_cfg_proc_macro ( ) -> bool {
209- ProcessEnv . is_present ( "CARGO_CFG_PROC_MACRO" )
234+ ENV . is_present ( "CARGO_CFG_PROC_MACRO" )
210235 }
211236
212237 /// The target relocation model.
@@ -220,33 +245,31 @@ mod cfg {
220245 #[ cfg( any( ) ) ]
221246 #[ track_caller]
222247 pub fn cargo_cfg_rustfmt ( ) -> bool {
223- ProcessEnv . is_present ( "CARGO_CFG_RUSTFMT" )
248+ ENV . is_present ( "CARGO_CFG_RUSTFMT" )
224249 }
225250
226251 /// Sanitizers enabled for the crate being compiled.
227252 #[ doc = unstable ! ( cfg_sanitize, 39699 ) ]
228253 #[ cfg( feature = "unstable" ) ]
229254 #[ track_caller]
230255 pub fn cargo_cfg_sanitize ( ) -> Option < Vec < String > > {
231- ProcessEnv
232- . get ( "CARGO_CFG_SANITIZE" )
233- . map ( |v| to_strings ( v, ',' ) )
256+ ENV . get ( "CARGO_CFG_SANITIZE" ) . map ( |v| to_strings ( v, ',' ) )
234257 }
235258
236259 /// If CFI sanitization is generalizing pointers.
237260 #[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
238261 #[ cfg( feature = "unstable" ) ]
239262 #[ track_caller]
240263 pub fn cargo_cfg_sanitizer_cfi_generalize_pointers ( ) -> bool {
241- ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
264+ ENV . is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
242265 }
243266
244267 /// If CFI sanitization is normalizing integers.
245268 #[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
246269 #[ cfg( feature = "unstable" ) ]
247270 #[ track_caller]
248271 pub fn cargo_cfg_sanitizer_cfi_normalize_integers ( ) -> bool {
249- ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
272+ ENV . is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
250273 }
251274
252275 /// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
@@ -342,7 +365,7 @@ mod cfg {
342365 #[ cfg( feature = "unstable" ) ]
343366 #[ track_caller]
344367 pub fn cargo_cfg_target_thread_local ( ) -> bool {
345- ProcessEnv . is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
368+ ENV . is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
346369 }
347370
348371 /// The [target vendor](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_vendor).
@@ -354,27 +377,27 @@ mod cfg {
354377 #[ cfg( any( ) ) ]
355378 #[ track_caller]
356379 pub fn cargo_cfg_test ( ) -> bool {
357- ProcessEnv . is_present ( "CARGO_CFG_TEST" )
380+ ENV . is_present ( "CARGO_CFG_TEST" )
358381 }
359382
360383 /// If we are compiling with UB checks enabled.
361384 #[ doc = unstable ! ( cfg_ub_checks, 123499 ) ]
362385 #[ cfg( feature = "unstable" ) ]
363386 #[ track_caller]
364387 pub fn cargo_cfg_ub_checks ( ) -> bool {
365- ProcessEnv . is_present ( "CARGO_CFG_UB_CHECKS" )
388+ ENV . is_present ( "CARGO_CFG_UB_CHECKS" )
366389 }
367390
368391 /// Set on [unix-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
369392 #[ track_caller]
370393 pub fn cargo_cfg_unix ( ) -> bool {
371- ProcessEnv . is_present ( "CARGO_CFG_UNIX" )
394+ ENV . is_present ( "CARGO_CFG_UNIX" )
372395 }
373396
374397 /// Set on [windows-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
375398 #[ track_caller]
376399 pub fn cargo_cfg_windows ( ) -> bool {
377- ProcessEnv . is_present ( "CARGO_CFG_WINDOWS" )
400+ ENV . is_present ( "CARGO_CFG_WINDOWS" )
378401 }
379402}
380403
@@ -461,7 +484,7 @@ pub fn dep_metadata(name: &str, key: &str) -> Option<String> {
461484 let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
462485 let key = key. to_uppercase ( ) . replace ( '-' , "_" ) ;
463486 let key = format ! ( "DEP_{name}_{key}" ) ;
464- ProcessEnv . get ( & key) . map ( to_string)
487+ ENV . get ( & key) . map ( to_string)
465488}
466489
467490/// The compiler that Cargo has resolved to use.
@@ -481,7 +504,7 @@ pub fn rustdoc() -> PathBuf {
481504/// [`build.rustc-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-wrapper
482505#[ track_caller]
483506pub fn rustc_wrapper ( ) -> Option < PathBuf > {
484- ProcessEnv . get ( "RUSTC_WRAPPER" ) . map ( to_path)
507+ ENV . get ( "RUSTC_WRAPPER" ) . map ( to_path)
485508}
486509
487510/// The rustc wrapper, if any, that Cargo is using for workspace members. See
@@ -490,15 +513,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
490513/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
491514#[ track_caller]
492515pub fn rustc_workspace_wrapper ( ) -> Option < PathBuf > {
493- ProcessEnv . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
516+ ENV . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
494517}
495518
496519/// The linker that Cargo has resolved to use for the current target, if specified.
497520///
498521/// [`target.*.linker`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#targettriplelinker
499522#[ track_caller]
500523pub fn rustc_linker ( ) -> Option < PathBuf > {
501- ProcessEnv . get ( "RUSTC_LINKER" ) . map ( to_path)
524+ ENV . get ( "RUSTC_LINKER" ) . map ( to_path)
502525}
503526
504527/// Extra flags that Cargo invokes rustc with. See [`build.rustflags`].
@@ -596,8 +619,7 @@ pub fn cargo_pkg_readme() -> Option<PathBuf> {
596619
597620#[ track_caller]
598621fn var_or_panic ( key : & str ) -> std:: ffi:: OsString {
599- ProcessEnv
600- . get ( key)
622+ ENV . get ( key)
601623 . unwrap_or_else ( || panic ! ( "cargo environment variable `{key}` is missing" ) )
602624}
603625
0 commit comments