|
| 1 | +From fbc700f92bdb008a9fd76e2a02230cea6c23d2c4 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Trevor Gross < [email protected]> |
| 3 | +Date: Tue, 5 Aug 2025 20:56:27 +0000 |
| 4 | +Subject: [PATCH] configure: Use `CARGO_CFG_*_{F16,F128}` rather than invoking |
| 5 | + rustc |
| 6 | + |
| 7 | +Currently we run the `rustc` from the `RUSTC` environment variable to |
| 8 | +figure out whether or not to enable `f16` and `f128`, based on the |
| 9 | +`target_has_reliable_{f16,f128}` config. However, this does not know |
| 10 | +about the codegen backend used, and the backend isn't trivial to check |
| 11 | +in a build script (usually it gets set via `RUSTFLAGS`). |
| 12 | + |
| 13 | +It turns out we don't actually need to run `rustc` here: Cargo |
| 14 | +unconditionally emits all config from the relevant compiler as |
| 15 | +`CARGO_CFG_*` variables, regardless of whether or not they are known |
| 16 | +options. Switch to checking these for setting config rather than |
| 17 | +invoking `rustc`. |
| 18 | + |
| 19 | +As an added advantage, this will work with target.json files without any |
| 20 | +special handling. |
| 21 | + |
| 22 | +Fixes: ed17b95715dd ("Use the compiler to determine whether or not to enable `f16` and `f128`") |
| 23 | +--- |
| 24 | + .../compiler-builtins/configure.rs | 27 +++-------------- |
| 25 | + library/compiler-builtins/libm/configure.rs | 30 ++++--------------- |
| 26 | + 2 files changed, 10 insertions(+), 47 deletions(-) |
| 27 | + |
| 28 | +diff --git a/library/compiler-builtins/compiler-builtins/configure.rs b/library/compiler-builtins/compiler-builtins/configure.rs |
| 29 | +index caedc034da6..79e238abc0f 100644 |
| 30 | +--- a/library/compiler-builtins/compiler-builtins/configure.rs |
| 31 | ++++ b/library/compiler-builtins/compiler-builtins/configure.rs |
| 32 | +@@ -1,6 +1,5 @@ |
| 33 | + // Configuration that is shared between `compiler_builtins` and `builtins_test`. |
| 34 | + |
| 35 | +-use std::process::{Command, Stdio}; |
| 36 | + use std::{env, str}; |
| 37 | + |
| 38 | + #[derive(Debug)] |
| 39 | +@@ -35,26 +34,6 @@ pub fn from_env() -> Self { |
| 40 | + .map(|s| s.to_lowercase().replace("_", "-")) |
| 41 | + .collect(); |
| 42 | + |
| 43 | +- // Query rustc for options that Cargo does not provide env for. The bootstrap hack is used |
| 44 | +- // to get consistent output regardless of channel (`f16`/`f128` config options are hidden |
| 45 | +- // on stable otherwise). |
| 46 | +- let mut cmd = Command::new(env::var("RUSTC").unwrap()); |
| 47 | +- cmd.args(["--print=cfg", "--target", &triple]) |
| 48 | +- .env("RUSTC_BOOTSTRAP", "1") |
| 49 | +- .stderr(Stdio::inherit()); |
| 50 | +- let out = cmd |
| 51 | +- .output() |
| 52 | +- .unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}")); |
| 53 | +- let rustc_cfg = str::from_utf8(&out.stdout).unwrap(); |
| 54 | +- |
| 55 | +- // If we couldn't query `rustc` (e.g. a custom JSON target was used), make the safe |
| 56 | +- // choice and leave `f16` and `f128` disabled. |
| 57 | +- let rustc_output_ok = out.status.success(); |
| 58 | +- let reliable_f128 = |
| 59 | +- rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f128"); |
| 60 | +- let reliable_f16 = |
| 61 | +- rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f16"); |
| 62 | +- |
| 63 | + Self { |
| 64 | + triple, |
| 65 | + triple_split, |
| 66 | +@@ -74,8 +53,10 @@ pub fn from_env() -> Self { |
| 67 | + .split(",") |
| 68 | + .map(ToOwned::to_owned) |
| 69 | + .collect(), |
| 70 | +- reliable_f128, |
| 71 | +- reliable_f16, |
| 72 | ++ // Note that these are unstable options, so only show up with the nightly compiler or |
| 73 | ++ // with `RUSTC_BOOTSTRAP=1` (which is required to use the types anyway). |
| 74 | ++ reliable_f128: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F128").is_some(), |
| 75 | ++ reliable_f16: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F16").is_some(), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +diff --git a/library/compiler-builtins/libm/configure.rs b/library/compiler-builtins/libm/configure.rs |
| 80 | +index f9100d2d58b..76186e63652 100644 |
| 81 | +--- a/library/compiler-builtins/libm/configure.rs |
| 82 | ++++ b/library/compiler-builtins/libm/configure.rs |
| 83 | +@@ -1,9 +1,9 @@ |
| 84 | + // Configuration shared with both libm and libm-test |
| 85 | + |
| 86 | ++use std::env; |
| 87 | + use std::path::PathBuf; |
| 88 | +-use std::process::{Command, Stdio}; |
| 89 | +-use std::{env, str}; |
| 90 | + |
| 91 | ++#[derive(Debug)] |
| 92 | + #[allow(dead_code)] |
| 93 | + pub struct Config { |
| 94 | + pub manifest_dir: PathBuf, |
| 95 | +@@ -33,26 +33,6 @@ pub fn from_env() -> Self { |
| 96 | + .map(|s| s.to_lowercase().replace("_", "-")) |
| 97 | + .collect(); |
| 98 | + |
| 99 | +- // Query rustc for options that Cargo does not provide env for. The bootstrap hack is used |
| 100 | +- // to get consistent output regardless of channel (`f16`/`f128` config options are hidden |
| 101 | +- // on stable otherwise). |
| 102 | +- let mut cmd = Command::new(env::var("RUSTC").unwrap()); |
| 103 | +- cmd.args(["--print=cfg", "--target", &target_triple]) |
| 104 | +- .env("RUSTC_BOOTSTRAP", "1") |
| 105 | +- .stderr(Stdio::inherit()); |
| 106 | +- let out = cmd |
| 107 | +- .output() |
| 108 | +- .unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}")); |
| 109 | +- let rustc_cfg = str::from_utf8(&out.stdout).unwrap(); |
| 110 | +- |
| 111 | +- // If we couldn't query `rustc` (e.g. a custom JSON target was used), make the safe |
| 112 | +- // choice and leave `f16` and `f128` disabled. |
| 113 | +- let rustc_output_ok = out.status.success(); |
| 114 | +- let reliable_f128 = |
| 115 | +- rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f128"); |
| 116 | +- let reliable_f16 = |
| 117 | +- rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f16"); |
| 118 | +- |
| 119 | + Self { |
| 120 | + target_triple, |
| 121 | + manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()), |
| 122 | +@@ -66,8 +46,10 @@ pub fn from_env() -> Self { |
| 123 | + target_string: env::var("TARGET").unwrap(), |
| 124 | + target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(), |
| 125 | + target_features, |
| 126 | +- reliable_f128, |
| 127 | +- reliable_f16, |
| 128 | ++ // Note that these are unstable options, so only show up with the nightly compiler or |
| 129 | ++ // with `RUSTC_BOOTSTRAP=1` (which is required to use the types anyway). |
| 130 | ++ reliable_f128: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F128").is_some(), |
| 131 | ++ reliable_f16: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F16").is_some(), |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +-- |
| 136 | +2.48.1 |
| 137 | + |
0 commit comments