6
6
//!
7
7
//! Reference: <https://doc.rust-lang.org/stable/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
8
8
9
- use std:: env:: var_os;
10
9
use std:: path:: PathBuf ;
11
10
12
11
use crate :: ident:: { is_ascii_ident, is_crate_name, is_feature_name} ;
13
12
13
+ /// Abstraction over environment variables
14
+ trait Env {
15
+ /// Fetches the environment variable `key`, returning `None` if the variable isn’t set or if
16
+ /// there is another error.
17
+ ///
18
+ /// It may return `None` if the environment variable’s name contains the equal sign character
19
+ /// (`=`) or the NUL character.
20
+ ///
21
+ /// Note that this function will not check if the environment variable is valid Unicode.
22
+ fn get ( & self , key : & str ) -> Option < std:: ffi:: OsString > ;
23
+
24
+ /// Checks the environment variable `key` is present
25
+ ///
26
+ /// It may not be considered present if the environment variable’s name contains the equal sign character
27
+ /// (`=`) or the NUL character.
28
+ fn is_present ( & self , key : & str ) -> bool ;
29
+ }
30
+
31
+ /// Fetches environment variables from the current process
32
+ struct ProcessEnv ;
33
+
34
+ impl Env for ProcessEnv {
35
+ fn get ( & self , key : & str ) -> Option < std:: ffi:: OsString > {
36
+ std:: env:: var_os ( key)
37
+ }
38
+
39
+ fn is_present ( & self , key : & str ) -> bool {
40
+ self . get ( key) . is_some ( )
41
+ }
42
+ }
43
+
14
44
/// Path to the `cargo` binary performing the build.
15
45
#[ track_caller]
16
46
pub fn cargo ( ) -> PathBuf {
@@ -30,7 +60,8 @@ pub fn cargo_manifest_dir() -> PathBuf {
30
60
/// The path to the manifest of your package.
31
61
#[ track_caller]
32
62
pub fn cargo_manifest_path ( ) -> PathBuf {
33
- var_os ( "CARGO_MANIFEST_PATH" )
63
+ ProcessEnv
64
+ . get ( "CARGO_MANIFEST_PATH" )
34
65
. map ( to_path)
35
66
. unwrap_or_else ( || {
36
67
let mut path = cargo_manifest_dir ( ) ;
@@ -42,7 +73,7 @@ pub fn cargo_manifest_path() -> PathBuf {
42
73
/// The manifest `links` value.
43
74
#[ track_caller]
44
75
pub fn cargo_manifest_links ( ) -> Option < String > {
45
- var_os ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
76
+ ProcessEnv . get ( "CARGO_MANIFEST_LINKS" ) . map ( to_string)
46
77
}
47
78
48
79
/// Contains parameters needed for Cargo’s [jobserver] implementation to parallelize
@@ -57,7 +88,7 @@ pub fn cargo_manifest_links() -> Option<String> {
57
88
/// [jobserver]: https://www.gnu.org/software/make/manual/html_node/Job-Slots.html
58
89
#[ track_caller]
59
90
pub fn cargo_makeflags ( ) -> Option < String > {
60
- var_os ( "CARGO_MAKEFLAGS" ) . map ( to_string)
91
+ ProcessEnv . get ( "CARGO_MAKEFLAGS" ) . map ( to_string)
61
92
}
62
93
63
94
/// For each activated feature of the package being built, this will be `true`.
@@ -68,7 +99,7 @@ pub fn cargo_feature(name: &str) -> bool {
68
99
}
69
100
let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
70
101
let key = format ! ( "CARGO_FEATURE_{name}" ) ;
71
- is_present ( & key)
102
+ ProcessEnv . is_present ( & key)
72
103
}
73
104
74
105
/// For each [configuration option] of the package being built, this will contain
@@ -82,7 +113,7 @@ pub fn cargo_feature(name: &str) -> bool {
82
113
#[ track_caller]
83
114
pub fn cargo_cfg ( cfg : & str ) -> Option < Vec < String > > {
84
115
let var = cargo_cfg_var ( cfg) ;
85
- var_os ( & var) . map ( |v| to_strings ( v, ',' ) )
116
+ ProcessEnv . get ( & var) . map ( |v| to_strings ( v, ',' ) )
86
117
}
87
118
88
119
#[ track_caller]
@@ -112,7 +143,7 @@ mod cfg {
112
143
#[ cfg( any( ) ) ]
113
144
#[ track_caller]
114
145
pub fn cargo_cfg_clippy ( ) -> bool {
115
- is_present ( "CARGO_CFG_CLIPPY" )
146
+ ProcessEnv . is_present ( "CARGO_CFG_CLIPPY" )
116
147
}
117
148
118
149
/// If we are compiling with debug assertions enabled.
@@ -123,25 +154,25 @@ mod cfg {
123
154
#[ cfg( any( ) ) ]
124
155
#[ track_caller]
125
156
pub fn cargo_cfg_debug_assertions ( ) -> bool {
126
- is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
157
+ ProcessEnv . is_present ( "CARGO_CFG_DEBUG_ASSERTIONS" )
127
158
}
128
159
129
160
#[ cfg( any( ) ) ]
130
161
#[ track_caller]
131
162
pub fn cargo_cfg_doc ( ) -> bool {
132
- is_present ( "CARGO_CFG_DOC" )
163
+ ProcessEnv . is_present ( "CARGO_CFG_DOC" )
133
164
}
134
165
135
166
#[ cfg( any( ) ) ]
136
167
#[ track_caller]
137
168
pub fn cargo_cfg_docsrs ( ) -> bool {
138
- is_present ( "CARGO_CFG_DOCSRS" )
169
+ ProcessEnv . is_present ( "CARGO_CFG_DOCSRS" )
139
170
}
140
171
141
172
#[ cfg( any( ) ) ]
142
173
#[ track_caller]
143
174
pub fn cargo_cfg_doctest ( ) -> bool {
144
- is_present ( "CARGO_CFG_DOCTEST" )
175
+ ProcessEnv . is_present ( "CARGO_CFG_DOCTEST" )
145
176
}
146
177
147
178
/// The level of detail provided by derived [`Debug`] implementations.
@@ -155,15 +186,15 @@ mod cfg {
155
186
#[ cfg( any( ) ) ]
156
187
#[ track_caller]
157
188
pub fn cargo_cfg_miri ( ) -> bool {
158
- is_present ( "CARGO_CFG_MIRI" )
189
+ ProcessEnv . is_present ( "CARGO_CFG_MIRI" )
159
190
}
160
191
161
192
/// If we are compiling with overflow checks enabled.
162
193
#[ doc = unstable ! ( cfg_overflow_checks, 111466 ) ]
163
194
#[ cfg( feature = "unstable" ) ]
164
195
#[ track_caller]
165
196
pub fn cargo_cfg_overflow_checks ( ) -> bool {
166
- is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
197
+ ProcessEnv . is_present ( "CARGO_CFG_OVERFLOW_CHECKS" )
167
198
}
168
199
169
200
/// The [panic strategy](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#panic).
@@ -175,7 +206,7 @@ mod cfg {
175
206
/// If the crate is being compiled as a procedural macro.
176
207
#[ track_caller]
177
208
pub fn cargo_cfg_proc_macro ( ) -> bool {
178
- is_present ( "CARGO_CFG_PROC_MACRO" )
209
+ ProcessEnv . is_present ( "CARGO_CFG_PROC_MACRO" )
179
210
}
180
211
181
212
/// The target relocation model.
@@ -189,31 +220,33 @@ mod cfg {
189
220
#[ cfg( any( ) ) ]
190
221
#[ track_caller]
191
222
pub fn cargo_cfg_rustfmt ( ) -> bool {
192
- is_present ( "CARGO_CFG_RUSTFMT" )
223
+ ProcessEnv . is_present ( "CARGO_CFG_RUSTFMT" )
193
224
}
194
225
195
226
/// Sanitizers enabled for the crate being compiled.
196
227
#[ doc = unstable ! ( cfg_sanitize, 39699 ) ]
197
228
#[ cfg( feature = "unstable" ) ]
198
229
#[ track_caller]
199
230
pub fn cargo_cfg_sanitize ( ) -> Option < Vec < String > > {
200
- var_os ( "CARGO_CFG_SANITIZE" ) . map ( |v| to_strings ( v, ',' ) )
231
+ ProcessEnv
232
+ . get ( "CARGO_CFG_SANITIZE" )
233
+ . map ( |v| to_strings ( v, ',' ) )
201
234
}
202
235
203
236
/// If CFI sanitization is generalizing pointers.
204
237
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
205
238
#[ cfg( feature = "unstable" ) ]
206
239
#[ track_caller]
207
240
pub fn cargo_cfg_sanitizer_cfi_generalize_pointers ( ) -> bool {
208
- is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
241
+ ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS" )
209
242
}
210
243
211
244
/// If CFI sanitization is normalizing integers.
212
245
#[ doc = unstable ! ( cfg_sanitizer_cfi, 89653 ) ]
213
246
#[ cfg( feature = "unstable" ) ]
214
247
#[ track_caller]
215
248
pub fn cargo_cfg_sanitizer_cfi_normalize_integers ( ) -> bool {
216
- is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
249
+ ProcessEnv . is_present ( "CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS" )
217
250
}
218
251
219
252
/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
@@ -309,7 +342,7 @@ mod cfg {
309
342
#[ cfg( feature = "unstable" ) ]
310
343
#[ track_caller]
311
344
pub fn cargo_cfg_target_thread_local ( ) -> bool {
312
- is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
345
+ ProcessEnv . is_present ( "CARGO_CFG_TARGET_THREAD_LOCAL" )
313
346
}
314
347
315
348
/// The [target vendor](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_vendor).
@@ -321,27 +354,27 @@ mod cfg {
321
354
#[ cfg( any( ) ) ]
322
355
#[ track_caller]
323
356
pub fn cargo_cfg_test ( ) -> bool {
324
- is_present ( "CARGO_CFG_TEST" )
357
+ ProcessEnv . is_present ( "CARGO_CFG_TEST" )
325
358
}
326
359
327
360
/// If we are compiling with UB checks enabled.
328
361
#[ doc = unstable ! ( cfg_ub_checks, 123499 ) ]
329
362
#[ cfg( feature = "unstable" ) ]
330
363
#[ track_caller]
331
364
pub fn cargo_cfg_ub_checks ( ) -> bool {
332
- is_present ( "CARGO_CFG_UB_CHECKS" )
365
+ ProcessEnv . is_present ( "CARGO_CFG_UB_CHECKS" )
333
366
}
334
367
335
368
/// Set on [unix-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
336
369
#[ track_caller]
337
370
pub fn cargo_cfg_unix ( ) -> bool {
338
- is_present ( "CARGO_CFG_UNIX" )
371
+ ProcessEnv . is_present ( "CARGO_CFG_UNIX" )
339
372
}
340
373
341
374
/// Set on [windows-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
342
375
#[ track_caller]
343
376
pub fn cargo_cfg_windows ( ) -> bool {
344
- is_present ( "CARGO_CFG_WINDOWS" )
377
+ ProcessEnv . is_present ( "CARGO_CFG_WINDOWS" )
345
378
}
346
379
}
347
380
@@ -428,7 +461,7 @@ pub fn dep_metadata(name: &str, key: &str) -> Option<String> {
428
461
let name = name. to_uppercase ( ) . replace ( '-' , "_" ) ;
429
462
let key = key. to_uppercase ( ) . replace ( '-' , "_" ) ;
430
463
let key = format ! ( "DEP_{name}_{key}" ) ;
431
- var_os ( & key) . map ( to_string)
464
+ ProcessEnv . get ( & key) . map ( to_string)
432
465
}
433
466
434
467
/// The compiler that Cargo has resolved to use.
@@ -448,7 +481,7 @@ pub fn rustdoc() -> PathBuf {
448
481
/// [`build.rustc-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-wrapper
449
482
#[ track_caller]
450
483
pub fn rustc_wrapper ( ) -> Option < PathBuf > {
451
- var_os ( "RUSTC_WRAPPER" ) . map ( to_path)
484
+ ProcessEnv . get ( "RUSTC_WRAPPER" ) . map ( to_path)
452
485
}
453
486
454
487
/// The rustc wrapper, if any, that Cargo is using for workspace members. See
@@ -457,15 +490,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
457
490
/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
458
491
#[ track_caller]
459
492
pub fn rustc_workspace_wrapper ( ) -> Option < PathBuf > {
460
- var_os ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
493
+ ProcessEnv . get ( "RUSTC_WORKSPACE_WRAPPER" ) . map ( to_path)
461
494
}
462
495
463
496
/// The linker that Cargo has resolved to use for the current target, if specified.
464
497
///
465
498
/// [`target.*.linker`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#targettriplelinker
466
499
#[ track_caller]
467
500
pub fn rustc_linker ( ) -> Option < PathBuf > {
468
- var_os ( "RUSTC_LINKER" ) . map ( to_path)
501
+ ProcessEnv . get ( "RUSTC_LINKER" ) . map ( to_path)
469
502
}
470
503
471
504
/// Extra flags that Cargo invokes rustc with. See [`build.rustflags`].
@@ -561,13 +594,11 @@ pub fn cargo_pkg_readme() -> Option<PathBuf> {
561
594
to_opt ( var_or_panic ( "CARGO_PKG_README" ) ) . map ( to_path)
562
595
}
563
596
564
- fn is_present ( key : & str ) -> bool {
565
- var_os ( key) . is_some ( )
566
- }
567
-
568
597
#[ track_caller]
569
598
fn var_or_panic ( key : & str ) -> std:: ffi:: OsString {
570
- var_os ( key) . unwrap_or_else ( || panic ! ( "cargo environment variable `{key}` is missing" ) )
599
+ ProcessEnv
600
+ . get ( key)
601
+ . unwrap_or_else ( || panic ! ( "cargo environment variable `{key}` is missing" ) )
571
602
}
572
603
573
604
fn to_path ( value : std:: ffi:: OsString ) -> PathBuf {
0 commit comments