Skip to content

Commit 87a66ec

Browse files
committed
configure: Use CARGO_CFG_*_{F16,F128} rather than invoking rustc
Currently we run the `rustc` from the `RUSTC` environment variable to figure out whether or not to enable `f16` and `f128`, based on the `target_has_reliable_{f16,f128}` config. However, this does not know about the codegen backend used, and the backend isn't trivial to check in a build script (usually it gets set via `RUSTFLAGS`). It turns out we don't actually need to run `rustc` here: Cargo unconditionally emits all config from the relevant compiler as `CARGO_CFG_*` variables, regardless of whether or not they are known options. Switch to checking these for setting config rather than invoking `rustc`. As an added advantage, this will work with target.json files without any special handling. Fixes: ed17b95 ("Use the compiler to determine whether or not to enable `f16` and `f128`")
1 parent 13c5374 commit 87a66ec

File tree

2 files changed

+10
-47
lines changed

2 files changed

+10
-47
lines changed

compiler-builtins/configure.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Configuration that is shared between `compiler_builtins` and `builtins_test`.
22

3-
use std::process::{Command, Stdio};
43
use std::{env, str};
54

65
#[derive(Debug)]
@@ -35,26 +34,6 @@ impl Target {
3534
.map(|s| s.to_lowercase().replace("_", "-"))
3635
.collect();
3736

38-
// Query rustc for options that Cargo does not provide env for. The bootstrap hack is used
39-
// to get consistent output regardless of channel (`f16`/`f128` config options are hidden
40-
// on stable otherwise).
41-
let mut cmd = Command::new(env::var("RUSTC").unwrap());
42-
cmd.args(["--print=cfg", "--target", &triple])
43-
.env("RUSTC_BOOTSTRAP", "1")
44-
.stderr(Stdio::inherit());
45-
let out = cmd
46-
.output()
47-
.unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}"));
48-
let rustc_cfg = str::from_utf8(&out.stdout).unwrap();
49-
50-
// If we couldn't query `rustc` (e.g. a custom JSON target was used), make the safe
51-
// choice and leave `f16` and `f128` disabled.
52-
let rustc_output_ok = out.status.success();
53-
let reliable_f128 =
54-
rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f128");
55-
let reliable_f16 =
56-
rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f16");
57-
5837
Self {
5938
triple,
6039
triple_split,
@@ -74,8 +53,10 @@ impl Target {
7453
.split(",")
7554
.map(ToOwned::to_owned)
7655
.collect(),
77-
reliable_f128,
78-
reliable_f16,
56+
// Note that these are unstable options, so only show up with the nightly compiler or
57+
// with `RUSTC_BOOTSTRAP=1` (which is required to use the types anyway).
58+
reliable_f128: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F128").is_some(),
59+
reliable_f16: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F16").is_some(),
7960
}
8061
}
8162

libm/configure.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Configuration shared with both libm and libm-test
22

3+
use std::env;
34
use std::path::PathBuf;
4-
use std::process::{Command, Stdio};
5-
use std::{env, str};
65

6+
#[derive(Debug)]
77
#[allow(dead_code)]
88
pub struct Config {
99
pub manifest_dir: PathBuf,
@@ -33,26 +33,6 @@ impl Config {
3333
.map(|s| s.to_lowercase().replace("_", "-"))
3434
.collect();
3535

36-
// Query rustc for options that Cargo does not provide env for. The bootstrap hack is used
37-
// to get consistent output regardless of channel (`f16`/`f128` config options are hidden
38-
// on stable otherwise).
39-
let mut cmd = Command::new(env::var("RUSTC").unwrap());
40-
cmd.args(["--print=cfg", "--target", &target_triple])
41-
.env("RUSTC_BOOTSTRAP", "1")
42-
.stderr(Stdio::inherit());
43-
let out = cmd
44-
.output()
45-
.unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}"));
46-
let rustc_cfg = str::from_utf8(&out.stdout).unwrap();
47-
48-
// If we couldn't query `rustc` (e.g. a custom JSON target was used), make the safe
49-
// choice and leave `f16` and `f128` disabled.
50-
let rustc_output_ok = out.status.success();
51-
let reliable_f128 =
52-
rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f128");
53-
let reliable_f16 =
54-
rustc_output_ok && rustc_cfg.lines().any(|l| l == "target_has_reliable_f16");
55-
5636
Self {
5737
target_triple,
5838
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
@@ -66,8 +46,10 @@ impl Config {
6646
target_string: env::var("TARGET").unwrap(),
6747
target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
6848
target_features,
69-
reliable_f128,
70-
reliable_f16,
49+
// Note that these are unstable options, so only show up with the nightly compiler or
50+
// with `RUSTC_BOOTSTRAP=1` (which is required to use the types anyway).
51+
reliable_f128: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F128").is_some(),
52+
reliable_f16: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F16").is_some(),
7153
}
7254
}
7355
}

0 commit comments

Comments
 (0)