Skip to content

Commit

Permalink
fix(build-rs): Implicitly report rerun-if-env-changed for input
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epage committed Dec 9, 2024
1 parent 04efe2e commit 0227c51
Showing 1 changed file with 53 additions and 31 deletions.
84 changes: 53 additions & 31 deletions crates/build-rs/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use std::path::PathBuf;

use crate::ident::{is_ascii_ident, is_crate_name, is_feature_name};
use crate::output::rerun_if_env_changed;

/// [`ProcessEnv`] wrapper that implicit calls [`rerun_if_env_changed`]
const ENV: RerunIfEnvChanged<ProcessEnv> = RerunIfEnvChanged::new();

/// Abstraction over environment variables
trait Env {
Expand Down Expand Up @@ -41,6 +45,28 @@ impl Env for ProcessEnv {
}
}

/// [`Env`] wrapper that implicitly calls [`rerun_if_env_changed`]
struct RerunIfEnvChanged<E: Env>(E);

impl RerunIfEnvChanged<ProcessEnv> {
const fn new() -> Self {
Self(ProcessEnv)
}
}

impl<E: Env> Env for RerunIfEnvChanged<E> {
#[track_caller]
fn get(&self, key: &str) -> Option<std::ffi::OsString> {
rerun_if_env_changed(key);
self.0.get(key)
}

#[track_caller]
fn is_present(&self, key: &str) -> bool {
self.get(key).is_some()
}
}

/// Path to the `cargo` binary performing the build.
#[track_caller]
pub fn cargo() -> PathBuf {
Expand All @@ -60,8 +86,7 @@ pub fn cargo_manifest_dir() -> PathBuf {
/// The path to the manifest of your package.
#[track_caller]
pub fn cargo_manifest_path() -> PathBuf {
ProcessEnv
.get("CARGO_MANIFEST_PATH")
ENV.get("CARGO_MANIFEST_PATH")
.map(to_path)
.unwrap_or_else(|| {
let mut path = cargo_manifest_dir();
Expand All @@ -73,7 +98,7 @@ pub fn cargo_manifest_path() -> PathBuf {
/// The manifest `links` value.
#[track_caller]
pub fn cargo_manifest_links() -> Option<String> {
ProcessEnv.get("CARGO_MANIFEST_LINKS").map(to_string)
ENV.get("CARGO_MANIFEST_LINKS").map(to_string)
}

/// Contains parameters needed for Cargo’s [jobserver] implementation to parallelize
Expand All @@ -88,7 +113,7 @@ pub fn cargo_manifest_links() -> Option<String> {
/// [jobserver]: https://www.gnu.org/software/make/manual/html_node/Job-Slots.html
#[track_caller]
pub fn cargo_makeflags() -> Option<String> {
ProcessEnv.get("CARGO_MAKEFLAGS").map(to_string)
ENV.get("CARGO_MAKEFLAGS").map(to_string)
}

/// For each activated feature of the package being built, this will be `true`.
Expand All @@ -99,7 +124,7 @@ pub fn cargo_feature(name: &str) -> bool {
}
let name = name.to_uppercase().replace('-', "_");
let key = format!("CARGO_FEATURE_{name}");
ProcessEnv.is_present(&key)
ENV.is_present(&key)
}

/// For each [configuration option] of the package being built, this will contain
Expand All @@ -113,7 +138,7 @@ pub fn cargo_feature(name: &str) -> bool {
#[track_caller]
pub fn cargo_cfg(cfg: &str) -> Option<Vec<String>> {
let var = cargo_cfg_var(cfg);
ProcessEnv.get(&var).map(|v| to_strings(v, ','))
ENV.get(&var).map(|v| to_strings(v, ','))
}

#[track_caller]
Expand All @@ -136,7 +161,7 @@ mod cfg {
#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_clippy() -> bool {
ProcessEnv.is_present("CARGO_CFG_CLIPPY")
ENV.is_present("CARGO_CFG_CLIPPY")
}

/// If we are compiling with debug assertions enabled.
Expand All @@ -147,25 +172,25 @@ mod cfg {
#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_debug_assertions() -> bool {
ProcessEnv.is_present("CARGO_CFG_DEBUG_ASSERTIONS")
ENV.is_present("CARGO_CFG_DEBUG_ASSERTIONS")
}

#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_doc() -> bool {
ProcessEnv.is_present("CARGO_CFG_DOC")
ENV.is_present("CARGO_CFG_DOC")
}

#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_docsrs() -> bool {
ProcessEnv.is_present("CARGO_CFG_DOCSRS")
ENV.is_present("CARGO_CFG_DOCSRS")
}

#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_doctest() -> bool {
ProcessEnv.is_present("CARGO_CFG_DOCTEST")
ENV.is_present("CARGO_CFG_DOCTEST")
}

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

/// If we are compiling with overflow checks enabled.
#[doc = unstable!(cfg_overflow_checks, 111466)]
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_overflow_checks() -> bool {
ProcessEnv.is_present("CARGO_CFG_OVERFLOW_CHECKS")
ENV.is_present("CARGO_CFG_OVERFLOW_CHECKS")
}

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

/// The target relocation model.
Expand All @@ -213,33 +238,31 @@ mod cfg {
#[cfg(any())]
#[track_caller]
pub fn cargo_cfg_rustfmt() -> bool {
ProcessEnv.is_present("CARGO_CFG_RUSTFMT")
ENV.is_present("CARGO_CFG_RUSTFMT")
}

/// Sanitizers enabled for the crate being compiled.
#[doc = unstable!(cfg_sanitize, 39699)]
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_sanitize() -> Option<Vec<String>> {
ProcessEnv
.get("CARGO_CFG_SANITIZE")
.map(|v| to_strings(v, ','))
ENV.get("CARGO_CFG_SANITIZE").map(|v| to_strings(v, ','))
}

/// If CFI sanitization is generalizing pointers.
#[doc = unstable!(cfg_sanitizer_cfi, 89653)]
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_sanitizer_cfi_generalize_pointers() -> bool {
ProcessEnv.is_present("CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS")
ENV.is_present("CARGO_CFG_SANITIZER_CFI_GENERALIZE_POINTERS")
}

/// If CFI sanitization is normalizing integers.
#[doc = unstable!(cfg_sanitizer_cfi, 89653)]
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_sanitizer_cfi_normalize_integers() -> bool {
ProcessEnv.is_present("CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS")
ENV.is_present("CARGO_CFG_SANITIZER_CFI_NORMALIZE_INTEGERS")
}

/// Disambiguation of the [target ABI](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#target_abi)
Expand Down Expand Up @@ -335,7 +358,7 @@ mod cfg {
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_target_thread_local() -> bool {
ProcessEnv.is_present("CARGO_CFG_TARGET_THREAD_LOCAL")
ENV.is_present("CARGO_CFG_TARGET_THREAD_LOCAL")
}

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

/// If we are compiling with UB checks enabled.
#[doc = unstable!(cfg_ub_checks, 123499)]
#[cfg(feature = "unstable")]
#[track_caller]
pub fn cargo_cfg_ub_checks() -> bool {
ProcessEnv.is_present("CARGO_CFG_UB_CHECKS")
ENV.is_present("CARGO_CFG_UB_CHECKS")
}

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

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

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

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

/// The rustc wrapper, if any, that Cargo is using for workspace members. See
Expand All @@ -483,15 +506,15 @@ pub fn rustc_wrapper() -> Option<PathBuf> {
/// [`build.rustc-workspace-wrapper`]: https://doc.rust-lang.org/stable/cargo/reference/config.html#buildrustc-workspace-wrapper
#[track_caller]
pub fn rustc_workspace_wrapper() -> Option<PathBuf> {
ProcessEnv.get("RUSTC_WORKSPACE_WRAPPER").map(to_path)
ENV.get("RUSTC_WORKSPACE_WRAPPER").map(to_path)
}

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

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

#[track_caller]
fn var_or_panic(key: &str) -> std::ffi::OsString {
ProcessEnv
.get(key)
ENV.get(key)
.unwrap_or_else(|| panic!("cargo environment variable `{key}` is missing"))
}

Expand Down

0 comments on commit 0227c51

Please sign in to comment.