From bf836390d6ab83c5f950fab80b152da208af2253 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 22 Nov 2024 01:05:56 +0100 Subject: [PATCH 1/2] Document what `Build::target` does --- src/lib.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fb78b7624..08bb53635 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1088,10 +1088,16 @@ impl Build { self } - /// Configures the target this configuration will be compiling for. + /// Configures the `rustc` target this configuration will be compiling + /// for. /// - /// This option is automatically scraped from the `TARGET` environment - /// variable by build scripts, so it's not required to call this function. + /// This will fail if using a target not in a pre-compiled list taken from + /// `rustc +nightly --print target-list`. The list will be updated + /// periodically. + /// + /// You should avoid setting this in build scripts, as that allows `cc` + /// to instead retrieve the desired target information from the + /// environment variables that Cargo sets. /// /// # Example /// From c66521144f49f81e1d2711d99b9ef4e9c2ccf147 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 22 Nov 2024 01:14:18 +0100 Subject: [PATCH 2/2] Fetch target info from Cargo even if target is set --- src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 08bb53635..c3b10c2a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1095,9 +1095,9 @@ impl Build { /// `rustc +nightly --print target-list`. The list will be updated /// periodically. /// - /// You should avoid setting this in build scripts, as that allows `cc` - /// to instead retrieve the desired target information from the - /// environment variables that Cargo sets. + /// You should avoid setting this in build scripts, target information + /// will instead be retrieved from the environment variables `TARGET` and + /// `CARGO_CFG_TARGET_*` that Cargo sets. /// /// # Example /// @@ -3417,8 +3417,11 @@ impl Build { fn get_target(&self) -> Result, Error> { match &self.target { - Some(t) => t.parse(), - None => self + Some(t) if Some(&**t) != self.getenv_unwrap_str("TARGET").ok().as_deref() => t.parse(), + // Fetch target information from environment if not set, or if the + // target was the same as the TARGET environment variable, in + // case the user did `build.target(&env::var("TARGET").unwrap())`. + _ => self .build_cache .target_info_parser .parse_from_cargo_environment_variables(),