Skip to content

Commit 2a6a33d

Browse files
committed
fix(build-rs): Implicitly report rerun-if-env-changed for input
As we abstract aware the env variables, users can't do a good job of reporting these, so we'll do it ourselves. We could make the traits public and take and explicit `env` parameter. I figured we can wait until there is a motivating use case. `build_env` does have a fancier `Env` impl where you pass it HOST and TARGET and it automatically looks up values for those with a fallback scheme.
1 parent bf51650 commit 2a6a33d

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

crates/build-rs/src/input.rs

+53-31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use std::path::PathBuf;
1010

1111
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();
1216

1317
/// Abstraction over environment variables
1418
trait 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]
4672
pub 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]
6288
pub 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]
75100
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)
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]
90115
pub 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]
114139
pub 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]
@@ -136,7 +161,7 @@ mod cfg {
136161
#[cfg(any())]
137162
#[track_caller]
138163
pub fn cargo_cfg_clippy() -> bool {
139-
ProcessEnv.is_present("CARGO_CFG_CLIPPY")
164+
ENV.is_present("CARGO_CFG_CLIPPY")
140165
}
141166

142167
/// If we are compiling with debug assertions enabled.
@@ -147,25 +172,25 @@ mod cfg {
147172
#[cfg(any())]
148173
#[track_caller]
149174
pub fn cargo_cfg_debug_assertions() -> bool {
150-
ProcessEnv.is_present("CARGO_CFG_DEBUG_ASSERTIONS")
175+
ENV.is_present("CARGO_CFG_DEBUG_ASSERTIONS")
151176
}
152177

153178
#[cfg(any())]
154179
#[track_caller]
155180
pub fn cargo_cfg_doc() -> bool {
156-
ProcessEnv.is_present("CARGO_CFG_DOC")
181+
ENV.is_present("CARGO_CFG_DOC")
157182
}
158183

159184
#[cfg(any())]
160185
#[track_caller]
161186
pub fn cargo_cfg_docsrs() -> bool {
162-
ProcessEnv.is_present("CARGO_CFG_DOCSRS")
187+
ENV.is_present("CARGO_CFG_DOCSRS")
163188
}
164189

165190
#[cfg(any())]
166191
#[track_caller]
167192
pub fn cargo_cfg_doctest() -> bool {
168-
ProcessEnv.is_present("CARGO_CFG_DOCTEST")
193+
ENV.is_present("CARGO_CFG_DOCTEST")
169194
}
170195

171196
/// The level of detail provided by derived [`Debug`] implementations.
@@ -179,15 +204,15 @@ mod cfg {
179204
#[cfg(any())]
180205
#[track_caller]
181206
pub fn cargo_cfg_miri() -> bool {
182-
ProcessEnv.is_present("CARGO_CFG_MIRI")
207+
ENV.is_present("CARGO_CFG_MIRI")
183208
}
184209

185210
/// If we are compiling with overflow checks enabled.
186211
#[doc = unstable!(cfg_overflow_checks, 111466)]
187212
#[cfg(feature = "unstable")]
188213
#[track_caller]
189214
pub fn cargo_cfg_overflow_checks() -> bool {
190-
ProcessEnv.is_present("CARGO_CFG_OVERFLOW_CHECKS")
215+
ENV.is_present("CARGO_CFG_OVERFLOW_CHECKS")
191216
}
192217

193218
/// The [panic strategy](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#panic).
@@ -199,7 +224,7 @@ mod cfg {
199224
/// If the crate is being compiled as a procedural macro.
200225
#[track_caller]
201226
pub fn cargo_cfg_proc_macro() -> bool {
202-
ProcessEnv.is_present("CARGO_CFG_PROC_MACRO")
227+
ENV.is_present("CARGO_CFG_PROC_MACRO")
203228
}
204229

205230
/// The target relocation model.
@@ -213,33 +238,31 @@ mod cfg {
213238
#[cfg(any())]
214239
#[track_caller]
215240
pub fn cargo_cfg_rustfmt() -> bool {
216-
ProcessEnv.is_present("CARGO_CFG_RUSTFMT")
241+
ENV.is_present("CARGO_CFG_RUSTFMT")
217242
}
218243

219244
/// Sanitizers enabled for the crate being compiled.
220245
#[doc = unstable!(cfg_sanitize, 39699)]
221246
#[cfg(feature = "unstable")]
222247
#[track_caller]
223248
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, ','))
227250
}
228251

229252
/// If CFI sanitization is generalizing pointers.
230253
#[doc = unstable!(cfg_sanitizer_cfi, 89653)]
231254
#[cfg(feature = "unstable")]
232255
#[track_caller]
233256
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")
235258
}
236259

237260
/// If CFI sanitization is normalizing integers.
238261
#[doc = unstable!(cfg_sanitizer_cfi, 89653)]
239262
#[cfg(feature = "unstable")]
240263
#[track_caller]
241264
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")
243266
}
244267

245268
/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
@@ -335,7 +358,7 @@ mod cfg {
335358
#[cfg(feature = "unstable")]
336359
#[track_caller]
337360
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")
339362
}
340363

341364
/// The [target vendor](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_vendor).
@@ -347,27 +370,27 @@ mod cfg {
347370
#[cfg(any())]
348371
#[track_caller]
349372
pub fn cargo_cfg_test() -> bool {
350-
ProcessEnv.is_present("CARGO_CFG_TEST")
373+
ENV.is_present("CARGO_CFG_TEST")
351374
}
352375

353376
/// If we are compiling with UB checks enabled.
354377
#[doc = unstable!(cfg_ub_checks, 123499)]
355378
#[cfg(feature = "unstable")]
356379
#[track_caller]
357380
pub fn cargo_cfg_ub_checks() -> bool {
358-
ProcessEnv.is_present("CARGO_CFG_UB_CHECKS")
381+
ENV.is_present("CARGO_CFG_UB_CHECKS")
359382
}
360383

361384
/// Set on [unix-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
362385
#[track_caller]
363386
pub fn cargo_cfg_unix() -> bool {
364-
ProcessEnv.is_present("CARGO_CFG_UNIX")
387+
ENV.is_present("CARGO_CFG_UNIX")
365388
}
366389

367390
/// Set on [windows-like platforms](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#unix-and-windows).
368391
#[track_caller]
369392
pub fn cargo_cfg_windows() -> bool {
370-
ProcessEnv.is_present("CARGO_CFG_WINDOWS")
393+
ENV.is_present("CARGO_CFG_WINDOWS")
371394
}
372395
}
373396

@@ -454,7 +477,7 @@ pub fn dep_metadata(name: &str, key: &str) -> Option<String> {
454477
let name = name.to_uppercase().replace('-', "_");
455478
let key = key.to_uppercase().replace('-', "_");
456479
let key = format!("DEP_{name}_{key}");
457-
ProcessEnv.get(&key).map(to_string)
480+
ENV.get(&key).map(to_string)
458481
}
459482

460483
/// The compiler that Cargo has resolved to use.
@@ -474,7 +497,7 @@ pub fn rustdoc() -> PathBuf {
474497
/// [`build.rustc-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-wrapper
475498
#[track_caller]
476499
pub fn rustc_wrapper() -> Option<PathBuf> {
477-
ProcessEnv.get("RUSTC_WRAPPER").map(to_path)
500+
ENV.get("RUSTC_WRAPPER").map(to_path)
478501
}
479502

480503
/// The rustc wrapper, if any, that Cargo is using for workspace members. See
@@ -483,15 +506,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
483506
/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
484507
#[track_caller]
485508
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)
487510
}
488511

489512
/// The linker that Cargo has resolved to use for the current target, if specified.
490513
///
491514
/// [`target.*.linker`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#targettriplelinker
492515
#[track_caller]
493516
pub fn rustc_linker() -> Option<PathBuf> {
494-
ProcessEnv.get("RUSTC_LINKER").map(to_path)
517+
ENV.get("RUSTC_LINKER").map(to_path)
495518
}
496519

497520
/// Extra flags that Cargo invokes rustc with. See [`build.rustflags`].
@@ -589,8 +612,7 @@ pub fn cargo_pkg_readme() -> Option<PathBuf> {
589612

590613
#[track_caller]
591614
fn var_or_panic(key: &str) -> std::ffi::OsString {
592-
ProcessEnv
593-
.get(key)
615+
ENV.get(key)
594616
.unwrap_or_else(|| panic!("cargo environment variable `{key}` is missing"))
595617
}
596618

0 commit comments

Comments
 (0)