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]
@@ -136,7 +161,7 @@ mod cfg {
136
161
#[ cfg( any( ) ) ]
137
162
#[ track_caller]
138
163
pub fn cargo_cfg_clippy ( ) -> bool {
139
- ProcessEnv . is_present ( "CARGO_CFG_CLIPPY" )
164
+ ENV . is_present ( "CARGO_CFG_CLIPPY" )
140
165
}
141
166
142
167
/// If we are compiling with debug assertions enabled.
@@ -147,25 +172,25 @@ mod cfg {
147
172
#[ cfg( any( ) ) ]
148
173
#[ track_caller]
149
174
pub fn cargo_cfg_debug_assertions ( ) -> bool {
150
- ProcessEnv . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
175
+ ENV . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
151
176
}
152
177
153
178
#[ cfg( any( ) ) ]
154
179
#[ track_caller]
155
180
pub fn cargo_cfg_doc ( ) -> bool {
156
- ProcessEnv . is_present ( "CARGO_CFG_DOC" )
181
+ ENV . is_present ( "CARGO_CFG_DOC" )
157
182
}
158
183
159
184
#[ cfg( any( ) ) ]
160
185
#[ track_caller]
161
186
pub fn cargo_cfg_docsrs ( ) -> bool {
162
- ProcessEnv . is_present ( "CARGO_CFG_DOCSRS" )
187
+ ENV . is_present ( "CARGO_CFG_DOCSRS" )
163
188
}
164
189
165
190
#[ cfg( any( ) ) ]
166
191
#[ track_caller]
167
192
pub fn cargo_cfg_doctest ( ) -> bool {
168
- ProcessEnv . is_present ( "CARGO_CFG_DOCTEST" )
193
+ ENV . is_present ( "CARGO_CFG_DOCTEST" )
169
194
}
170
195
171
196
/// The level of detail provided by derived [`Debug`] implementations.
@@ -179,15 +204,15 @@ mod cfg {
179
204
#[ cfg( any( ) ) ]
180
205
#[ track_caller]
181
206
pub fn cargo_cfg_miri ( ) -> bool {
182
- ProcessEnv . is_present ( "CARGO_CFG_MIRI" )
207
+ ENV . is_present ( "CARGO_CFG_MIRI" )
183
208
}
184
209
185
210
/// If we are compiling with overflow checks enabled.
186
211
#[ doc = unstable ! ( cfg_overflow_checks, 111466 ) ]
187
212
#[ cfg( feature = "unstable" ) ]
188
213
#[ track_caller]
189
214
pub fn cargo_cfg_overflow_checks ( ) -> bool {
190
- ProcessEnv . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
215
+ ENV . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
191
216
}
192
217
193
218
/// The [panic strategy](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#panic).
@@ -199,7 +224,7 @@ mod cfg {
199
224
/// If the crate is being compiled as a procedural macro.
200
225
#[ track_caller]
201
226
pub fn cargo_cfg_proc_macro ( ) -> bool {
202
- ProcessEnv . is_present ( "CARGO_CFG_PROC_MACRO" )
227
+ ENV . is_present ( "CARGO_CFG_PROC_MACRO" )
203
228
}
204
229
205
230
/// The target relocation model.
@@ -213,33 +238,31 @@ mod cfg {
213
238
#[ cfg( any( ) ) ]
214
239
#[ track_caller]
215
240
pub fn cargo_cfg_rustfmt ( ) -> bool {
216
- ProcessEnv . is_present ( "CARGO_CFG_RUSTFMT" )
241
+ ENV . is_present ( "CARGO_CFG_RUSTFMT" )
217
242
}
218
243
219
244
/// Sanitizers enabled for the crate being compiled.
220
245
#[ doc = unstable ! ( cfg_sanitize, 39699 ) ]
221
246
#[ cfg( feature = "unstable" ) ]
222
247
#[ track_caller]
223
248
pub fn cargo_cfg_sanitize ( ) -> Option < Vec < String > > {
224
- ProcessEnv
225
- . get ( "CARGO_CFG_SANITIZE" )
226
- . map ( |v| to_strings ( v, ',' ) )
249
+ ENV . get ( "CARGO_CFG_SANITIZE" ) . map ( |v| to_strings ( v, ',' ) )
227
250
}
228
251
229
252
/// If CFI sanitization is generalizing pointers.
230
253
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
231
254
#[ cfg( feature = "unstable" ) ]
232
255
#[ track_caller]
233
256
pub fn cargo_cfg_sanitizer_cfi_generalize_pointers ( ) -> bool {
234
- ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
257
+ ENV . is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
235
258
}
236
259
237
260
/// If CFI sanitization is normalizing integers.
238
261
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
239
262
#[ cfg( feature = "unstable" ) ]
240
263
#[ track_caller]
241
264
pub fn cargo_cfg_sanitizer_cfi_normalize_integers ( ) -> bool {
242
- ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
265
+ ENV . is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
243
266
}
244
267
245
268
/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
@@ -335,7 +358,7 @@ mod cfg {
335
358
#[ cfg( feature = "unstable" ) ]
336
359
#[ track_caller]
337
360
pub fn cargo_cfg_target_thread_local ( ) -> bool {
338
- ProcessEnv . is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
361
+ ENV . is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
339
362
}
340
363
341
364
/// The [target vendor](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_vendor).
@@ -347,27 +370,27 @@ mod cfg {
347
370
#[ cfg( any( ) ) ]
348
371
#[ track_caller]
349
372
pub fn cargo_cfg_test ( ) -> bool {
350
- ProcessEnv . is_present ( "CARGO_CFG_TEST" )
373
+ ENV . is_present ( "CARGO_CFG_TEST" )
351
374
}
352
375
353
376
/// If we are compiling with UB checks enabled.
354
377
#[ doc = unstable ! ( cfg_ub_checks, 123499 ) ]
355
378
#[ cfg( feature = "unstable" ) ]
356
379
#[ track_caller]
357
380
pub fn cargo_cfg_ub_checks ( ) -> bool {
358
- ProcessEnv . is_present ( "CARGO_CFG_UB_CHECKS" )
381
+ ENV . is_present ( "CARGO_CFG_UB_CHECKS" )
359
382
}
360
383
361
384
/// Set on [unix-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
362
385
#[ track_caller]
363
386
pub fn cargo_cfg_unix ( ) -> bool {
364
- ProcessEnv . is_present ( "CARGO_CFG_UNIX" )
387
+ ENV . is_present ( "CARGO_CFG_UNIX" )
365
388
}
366
389
367
390
/// Set on [windows-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
368
391
#[ track_caller]
369
392
pub fn cargo_cfg_windows ( ) -> bool {
370
- ProcessEnv . is_present ( "CARGO_CFG_WINDOWS" )
393
+ ENV . is_present ( "CARGO_CFG_WINDOWS" )
371
394
}
372
395
}
373
396
@@ -454,7 +477,7 @@ pub fn dep_metadata(name: &str, key: &str) -> Option<String> {
454
477
let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
455
478
let key = key. to_uppercase ( ) . replace ( '-' , "_" ) ;
456
479
let key = format ! ( "DEP_{name}_{key}" ) ;
457
- ProcessEnv . get ( & key) . map ( to_string)
480
+ ENV . get ( & key) . map ( to_string)
458
481
}
459
482
460
483
/// The compiler that Cargo has resolved to use.
@@ -474,7 +497,7 @@ pub fn rustdoc() -> PathBuf {
474
497
/// [`build.rustc-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-wrapper
475
498
#[ track_caller]
476
499
pub fn rustc_wrapper ( ) -> Option < PathBuf > {
477
- ProcessEnv . get ( "RUSTC_WRAPPER" ) . map ( to_path)
500
+ ENV . get ( "RUSTC_WRAPPER" ) . map ( to_path)
478
501
}
479
502
480
503
/// The rustc wrapper, if any, that Cargo is using for workspace members. See
@@ -483,15 +506,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
483
506
/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
484
507
#[ track_caller]
485
508
pub fn rustc_workspace_wrapper ( ) -> Option < PathBuf > {
486
- ProcessEnv . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
509
+ ENV . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
487
510
}
488
511
489
512
/// The linker that Cargo has resolved to use for the current target, if specified.
490
513
///
491
514
/// [`target.*.linker`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#targettriplelinker
492
515
#[ track_caller]
493
516
pub fn rustc_linker ( ) -> Option < PathBuf > {
494
- ProcessEnv . get ( "RUSTC_LINKER" ) . map ( to_path)
517
+ ENV . get ( "RUSTC_LINKER" ) . map ( to_path)
495
518
}
496
519
497
520
/// Extra flags that Cargo invokes rustc with. See [`build.rustflags`].
@@ -589,8 +612,7 @@ pub fn cargo_pkg_readme() -> Option<PathBuf> {
589
612
590
613
#[ track_caller]
591
614
fn var_or_panic ( key : & str ) -> std:: ffi:: OsString {
592
- ProcessEnv
593
- . get ( key)
615
+ ENV . get ( key)
594
616
. unwrap_or_else ( || panic ! ( "cargo environment variable `{key}` is missing" ) )
595
617
}
596
618
0 commit comments