Skip to content

Commit 5ccd53c

Browse files
committed
Test building custom targets and resolve an issue probing rustc
The `rustc` probe done in our build scripts needs to pass `--target` to get the correct configuration, which usually comes from the `TARGET` environment variable. However, for targets specified via a `target.json` file, `TARGET` gets set to the file name without an extension or path. `rustc` will check a search path to attempt to locate the file, but this is likely to fail since the directory where Cargo invokes build scripts (and hence where those scripts invoke `rustc`) might not have any relation to the JSON spec file. Resolve this for now by leaving `f16` and `f128` disabled if the `rustc` command fails. Result of the discussion at CARGO-14208 may eventually provide a better solution. A CI test is also added since custom JSON files are an edge case that could fail in other ways. I verified this fails without the fix here. The JSON file is the output for `thumbv7em-none-eabi`, just renamed so `rustc` doesn't identify it.
1 parent df2e48e commit 5ccd53c

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

.github/workflows/main.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ jobs:
195195
run: ./ci/update-musl.sh
196196
- run: cargo clippy --workspace --all-targets
197197

198+
build-custom:
199+
name: Build custom target
200+
runs-on: ubuntu-24.04
201+
timeout-minutes: 10
202+
steps:
203+
- uses: actions/checkout@v4
204+
- name: Install Rust
205+
run: |
206+
rustup update nightly --no-self-update
207+
rustup default nightly
208+
rustup component add rust-src
209+
- uses: Swatinem/rust-cache@v2
210+
- run: |
211+
# Ensure we can build with custom target.json files (these can interact
212+
# poorly with build scripts)
213+
cargo build -p compiler_builtins -p libm \
214+
--target etc/thumbv7em-none-eabi-renamed.json \
215+
-Zbuild-std=core
216+
198217
benchmarks:
199218
name: Benchmarks
200219
timeout-minutes: 20
@@ -331,6 +350,7 @@ jobs:
331350
success:
332351
needs:
333352
- benchmarks
353+
- build-custom
334354
- clippy
335355
- extensive
336356
- miri

compiler-builtins/configure.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ impl Target {
4545
let out = cmd
4646
.output()
4747
.unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}"));
48-
assert!(out.status.success(), "failed to run `{cmd:?}`");
4948
let rustc_cfg = str::from_utf8(&out.stdout).unwrap();
5049

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+
5158
Self {
5259
triple,
5360
triple_split,
@@ -67,8 +74,8 @@ impl Target {
6774
.split(",")
6875
.map(ToOwned::to_owned)
6976
.collect(),
70-
reliable_f128: rustc_cfg.lines().any(|l| l == "target_has_reliable_f128"),
71-
reliable_f16: rustc_cfg.lines().any(|l| l == "target_has_reliable_f16"),
77+
reliable_f128,
78+
reliable_f16,
7279
}
7380
}
7481

etc/thumbv7em-none-eabi-renamed.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"abi": "eabi",
3+
"arch": "arm",
4+
"c-enum-min-bits": 8,
5+
"crt-objects-fallback": "false",
6+
"data-layout": "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
7+
"emit-debug-gdb-scripts": false,
8+
"frame-pointer": "always",
9+
"linker": "rust-lld",
10+
"linker-flavor": "gnu-lld",
11+
"llvm-floatabi": "soft",
12+
"llvm-target": "thumbv7em-none-eabi",
13+
"max-atomic-width": 32,
14+
"metadata": {
15+
"description": "Bare ARMv7E-M",
16+
"host_tools": false,
17+
"std": false,
18+
"tier": 2
19+
},
20+
"panic-strategy": "abort",
21+
"relocation-model": "static",
22+
"target-pointer-width": "32"
23+
}

libm/configure.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ impl Config {
4343
let out = cmd
4444
.output()
4545
.unwrap_or_else(|e| panic!("failed to run `{cmd:?}`: {e}"));
46-
assert!(out.status.success(), "failed to run `{cmd:?}`");
4746
let rustc_cfg = str::from_utf8(&out.stdout).unwrap();
4847

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+
4956
Self {
5057
target_triple,
5158
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
@@ -59,8 +66,8 @@ impl Config {
5966
target_string: env::var("TARGET").unwrap(),
6067
target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
6168
target_features,
62-
reliable_f128: rustc_cfg.lines().any(|l| l == "target_has_reliable_f128"),
63-
reliable_f16: rustc_cfg.lines().any(|l| l == "target_has_reliable_f16"),
69+
reliable_f128,
70+
reliable_f16,
6471
}
6572
}
6673
}

0 commit comments

Comments
 (0)