9
9
use std:: path:: PathBuf ;
10
10
11
11
use 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 ( ) ;
12
16
13
17
/// Abstraction over environment variables
14
18
trait Env {
@@ -41,6 +45,28 @@ impl Env for ProcessEnv {
41
45
}
42
46
}
43
47
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
+
44
70
/// Path to the `cargo` binary performing the build.
45
71
#[ track_caller]
46
72
pub fn cargo ( ) -> PathBuf {
@@ -60,8 +86,7 @@ pub fn cargo_manifest_dir() -> PathBuf {
60
86
/// The path to the manifest of your package.
61
87
#[ track_caller]
62
88
pub fn cargo_manifest_path ( ) -> PathBuf {
63
- ProcessEnv
64
- . get ( "CARGO_MANIFEST_PATH" )
89
+ ENV . get ( "CARGO_MANIFEST_PATH" )
65
90
. map ( to_path)
66
91
. unwrap_or_else ( || {
67
92
let mut path = cargo_manifest_dir ( ) ;
@@ -73,7 +98,7 @@ pub fn cargo_manifest_path() -> PathBuf {
73
98
/// The manifest `links` value.
74
99
#[ track_caller]
75
100
pub fn cargo_manifest_links ( ) -> Option < String > {
76
- ProcessEnv . get ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
101
+ ENV . get ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
77
102
}
78
103
79
104
/// Contains parameters needed for Cargo’s [jobserver] implementation to parallelize
@@ -88,7 +113,7 @@ pub fn cargo_manifest_links() -> Option<String> {
88
113
/// [jobserver]: https://www.gnu.org/software/make/manual/html_node/Job-Slots.html
89
114
#[ track_caller]
90
115
pub fn cargo_makeflags ( ) -> Option < String > {
91
- ProcessEnv . get ( "CARGO_MAKEFLAGS" ) . map ( to_string)
116
+ ENV . get ( "CARGO_MAKEFLAGS" ) . map ( to_string)
92
117
}
93
118
94
119
/// For each activated feature of the package being built, this will be `true`.
@@ -99,7 +124,7 @@ pub fn cargo_feature(name: &str) -> bool {
99
124
}
100
125
let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
101
126
let key = format ! ( "CARGO_FEATURE_{name}" ) ;
102
- ProcessEnv . is_present ( & key)
127
+ ENV . is_present ( & key)
103
128
}
104
129
105
130
/// For each [configuration option] of the package being built, this will contain
@@ -113,7 +138,7 @@ pub fn cargo_feature(name: &str) -> bool {
113
138
#[ track_caller]
114
139
pub fn cargo_cfg ( cfg : & str ) -> Option < Vec < String > > {
115
140
let var = cargo_cfg_var ( cfg) ;
116
- ProcessEnv . get ( & var) . map ( |v| to_strings ( v, ',' ) )
141
+ ENV . get ( & var) . map ( |v| to_strings ( v, ',' ) )
117
142
}
118
143
119
144
#[ track_caller]
@@ -143,7 +168,7 @@ mod cfg {
143
168
#[ cfg( any( ) ) ]
144
169
#[ track_caller]
145
170
pub fn cargo_cfg_clippy ( ) -> bool {
146
- ProcessEnv . is_present ( "CARGO_CFG_CLIPPY" )
171
+ ENV . is_present ( "CARGO_CFG_CLIPPY" )
147
172
}
148
173
149
174
/// If we are compiling with debug assertions enabled.
@@ -154,25 +179,25 @@ mod cfg {
154
179
#[ cfg( any( ) ) ]
155
180
#[ track_caller]
156
181
pub fn cargo_cfg_debug_assertions ( ) -> bool {
157
- ProcessEnv . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
182
+ ENV . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
158
183
}
159
184
160
185
#[ cfg( any( ) ) ]
161
186
#[ track_caller]
162
187
pub fn cargo_cfg_doc ( ) -> bool {
163
- ProcessEnv . is_present ( "CARGO_CFG_DOC" )
188
+ ENV . is_present ( "CARGO_CFG_DOC" )
164
189
}
165
190
166
191
#[ cfg( any( ) ) ]
167
192
#[ track_caller]
168
193
pub fn cargo_cfg_docsrs ( ) -> bool {
169
- ProcessEnv . is_present ( "CARGO_CFG_DOCSRS" )
194
+ ENV . is_present ( "CARGO_CFG_DOCSRS" )
170
195
}
171
196
172
197
#[ cfg( any( ) ) ]
173
198
#[ track_caller]
174
199
pub fn cargo_cfg_doctest ( ) -> bool {
175
- ProcessEnv . is_present ( "CARGO_CFG_DOCTEST" )
200
+ ENV . is_present ( "CARGO_CFG_DOCTEST" )
176
201
}
177
202
178
203
/// The level of detail provided by derived [`Debug`] implementations.
@@ -186,15 +211,15 @@ mod cfg {
186
211
#[ cfg( any( ) ) ]
187
212
#[ track_caller]
188
213
pub fn cargo_cfg_miri ( ) -> bool {
189
- ProcessEnv . is_present ( "CARGO_CFG_MIRI" )
214
+ ENV . is_present ( "CARGO_CFG_MIRI" )
190
215
}
191
216
192
217
/// If we are compiling with overflow checks enabled.
193
218
#[ doc = unstable ! ( cfg_overflow_checks, 111466 ) ]
194
219
#[ cfg( feature = "unstable" ) ]
195
220
#[ track_caller]
196
221
pub fn cargo_cfg_overflow_checks ( ) -> bool {
197
- ProcessEnv . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
222
+ ENV . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
198
223
}
199
224
200
225
/// The [panic strategy](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#panic).
@@ -206,7 +231,7 @@ mod cfg {
206
231
/// If the crate is being compiled as a procedural macro.
207
232
#[ track_caller]
208
233
pub fn cargo_cfg_proc_macro ( ) -> bool {
209
- ProcessEnv . is_present ( "CARGO_CFG_PROC_MACRO" )
234
+ ENV . is_present ( "CARGO_CFG_PROC_MACRO" )
210
235
}
211
236
212
237
/// The target relocation model.
@@ -220,33 +245,31 @@ mod cfg {
220
245
#[ cfg( any( ) ) ]
221
246
#[ track_caller]
222
247
pub fn cargo_cfg_rustfmt ( ) -> bool {
223
- ProcessEnv . is_present ( "CARGO_CFG_RUSTFMT" )
248
+ ENV . is_present ( "CARGO_CFG_RUSTFMT" )
224
249
}
225
250
226
251
/// Sanitizers enabled for the crate being compiled.
227
252
#[ doc = unstable ! ( cfg_sanitize, 39699 ) ]
228
253
#[ cfg( feature = "unstable" ) ]
229
254
#[ track_caller]
230
255
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, ',' ) )
234
257
}
235
258
236
259
/// If CFI sanitization is generalizing pointers.
237
260
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
238
261
#[ cfg( feature = "unstable" ) ]
239
262
#[ track_caller]
240
263
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" )
242
265
}
243
266
244
267
/// If CFI sanitization is normalizing integers.
245
268
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
246
269
#[ cfg( feature = "unstable" ) ]
247
270
#[ track_caller]
248
271
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" )
250
273
}
251
274
252
275
/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
@@ -342,7 +365,7 @@ mod cfg {
342
365
#[ cfg( feature = "unstable" ) ]
343
366
#[ track_caller]
344
367
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" )
346
369
}
347
370
348
371
/// The [target vendor](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_vendor).
@@ -354,27 +377,27 @@ mod cfg {
354
377
#[ cfg( any( ) ) ]
355
378
#[ track_caller]
356
379
pub fn cargo_cfg_test ( ) -> bool {
357
- ProcessEnv . is_present ( "CARGO_CFG_TEST" )
380
+ ENV . is_present ( "CARGO_CFG_TEST" )
358
381
}
359
382
360
383
/// If we are compiling with UB checks enabled.
361
384
#[ doc = unstable ! ( cfg_ub_checks, 123499 ) ]
362
385
#[ cfg( feature = "unstable" ) ]
363
386
#[ track_caller]
364
387
pub fn cargo_cfg_ub_checks ( ) -> bool {
365
- ProcessEnv . is_present ( "CARGO_CFG_UB_CHECKS" )
388
+ ENV . is_present ( "CARGO_CFG_UB_CHECKS" )
366
389
}
367
390
368
391
/// Set on [unix-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
369
392
#[ track_caller]
370
393
pub fn cargo_cfg_unix ( ) -> bool {
371
- ProcessEnv . is_present ( "CARGO_CFG_UNIX" )
394
+ ENV . is_present ( "CARGO_CFG_UNIX" )
372
395
}
373
396
374
397
/// Set on [windows-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
375
398
#[ track_caller]
376
399
pub fn cargo_cfg_windows ( ) -> bool {
377
- ProcessEnv . is_present ( "CARGO_CFG_WINDOWS" )
400
+ ENV . is_present ( "CARGO_CFG_WINDOWS" )
378
401
}
379
402
}
380
403
@@ -461,7 +484,7 @@ pub fn dep_metadata(name: &str, key: &str) -> Option<String> {
461
484
let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
462
485
let key = key. to_uppercase ( ) . replace ( '-' , "_" ) ;
463
486
let key = format ! ( "DEP_{name}_{key}" ) ;
464
- ProcessEnv . get ( & key) . map ( to_string)
487
+ ENV . get ( & key) . map ( to_string)
465
488
}
466
489
467
490
/// The compiler that Cargo has resolved to use.
@@ -481,7 +504,7 @@ pub fn rustdoc() -> PathBuf {
481
504
/// [`build.rustc-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-wrapper
482
505
#[ track_caller]
483
506
pub fn rustc_wrapper ( ) -> Option < PathBuf > {
484
- ProcessEnv . get ( "RUSTC_WRAPPER" ) . map ( to_path)
507
+ ENV . get ( "RUSTC_WRAPPER" ) . map ( to_path)
485
508
}
486
509
487
510
/// The rustc wrapper, if any, that Cargo is using for workspace members. See
@@ -490,15 +513,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
490
513
/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
491
514
#[ track_caller]
492
515
pub fn rustc_workspace_wrapper ( ) -> Option < PathBuf > {
493
- ProcessEnv . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
516
+ ENV . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
494
517
}
495
518
496
519
/// The linker that Cargo has resolved to use for the current target, if specified.
497
520
///
498
521
/// [`target.*.linker`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#targettriplelinker
499
522
#[ track_caller]
500
523
pub fn rustc_linker ( ) -> Option < PathBuf > {
501
- ProcessEnv . get ( "RUSTC_LINKER" ) . map ( to_path)
524
+ ENV . get ( "RUSTC_LINKER" ) . map ( to_path)
502
525
}
503
526
504
527
/// Extra flags that Cargo invokes rustc with. See [`build.rustflags`].
@@ -596,8 +619,7 @@ pub fn cargo_pkg_readme() -> Option<PathBuf> {
596
619
597
620
#[ track_caller]
598
621
fn var_or_panic ( key : & str ) -> std:: ffi:: OsString {
599
- ProcessEnv
600
- . get ( key)
622
+ ENV . get ( key)
601
623
. unwrap_or_else ( || panic ! ( "cargo environment variable `{key}` is missing" ) )
602
624
}
603
625
0 commit comments