Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08cab75

Browse files
committedFeb 28, 2025·
Parse target triples
But keep using a generated LLVM/Clang triple mapping for now.
1 parent 9131f33 commit 08cab75

File tree

9 files changed

+1062
-3804
lines changed

9 files changed

+1062
-3804
lines changed
 
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Test nightly `rustc` targets and add issue comment if changed
2+
3+
on:
4+
workflow_dispatch: # Allow running on-demand
5+
schedule:
6+
- cron: '0 3 * * 4'
7+
push:
8+
branches:
9+
- 'main'
10+
paths:
11+
- 'dev-tools/gen-target-info/**'
12+
13+
jobs:
14+
regenerate:
15+
if: github.repository_owner == 'rust-lang'
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
persist-credentials: true
21+
22+
- name: Install current nightly Rust
23+
run: |
24+
rustup toolchain install nightly --no-self-update --profile minimal
25+
rustup default nightly
26+
- run: cargo update
27+
- uses: Swatinem/rust-cache@v2
28+
29+
- name: Test with `RUSTFLAGS=--cfg=rustc_target_test cargo test --lib`
30+
id: test
31+
continue-on-error: true # We want to open an issue if it fails
32+
run: RUSTFLAGS=--cfg=rustc_target_test cargo test --lib 2>&1 | tee test_output.txt
33+
34+
# Added to https://github.com/rust-lang/cc-rs/issues/1426
35+
- name: Add issue comment if test failed
36+
if: steps.test.outcome == 'failure'
37+
env:
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
run: |
40+
gh issue comment 1426 --body "
41+
Failed parsing `rustc` target on `$(rustc --version)`.
42+
43+
<details><summary>Test output</summary>
44+
<p>
45+
\`\`\`
46+
$(cat test_output.txt)
47+
\`\`\`
48+
</p>
49+
</details>
50+
" --head $(git branch --show-current)

‎dev-tools/gen-target-info/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ pub use target_specs::*;
33

44
mod read;
55
pub use read::*;
6-
7-
mod write;
8-
pub use write::*;

‎dev-tools/gen-target-info/src/main.rs

+6-64
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,18 @@ use gen_target_info::{
55
get_target_spec_from_msrv, get_target_specs_from_json, get_targets_msrv, RustcTargetSpecs,
66
};
77

8-
const PRELUDE: &str = r#"//! This file is generated code. Please edit the generator
9-
//! in dev-tools/gen-target-info if you need to make changes.
8+
const PRELUDE: &str = r#"//! This file is generated code. Please edit the generator in
9+
//! dev-tools/gen-target-info if you need to make changes, or see
10+
//! src/target/llvm.rs if you need to configure a specific LLVM triple.
1011
1112
"#;
1213

1314
fn generate_target_mapping(f: &mut File, target_specs: &RustcTargetSpecs) -> std::io::Result<()> {
14-
writeln!(f, "use super::TargetInfo;")?;
15-
writeln!(f)?;
16-
writeln!(
17-
f,
18-
"pub(crate) const LIST: &[(&str, TargetInfo<'static>)] = &["
19-
)?;
15+
writeln!(f, "#[rustfmt::skip]")?;
16+
writeln!(f, "pub(crate) const LLVM_TARGETS: &[(&str, &str)] = &[")?;
2017

2118
for (triple, spec) in &target_specs.0 {
22-
let full_arch = triple.split_once('-').unwrap().0;
23-
let arch = &spec.arch;
24-
let vendor = spec.vendor.as_deref().unwrap_or("unknown");
25-
let os = spec.os.as_deref().unwrap_or("none");
26-
let env = spec.env.as_deref().unwrap_or("");
27-
let abi = spec.abi.as_deref().unwrap_or("");
28-
29-
// FIXME(madsmtm): Unnecessary once we bump MSRV to Rust 1.74
30-
let llvm_target = if spec.llvm_target == "armv7-apple-ios7.0.0" {
31-
"armv7-apple-ios".to_string()
32-
} else if os == "uefi" {
33-
// Override the UEFI LLVM targets.
34-
//
35-
// The rustc mappings (as of 1.82) for the UEFI targets are:
36-
// * i686-unknown-uefi -> i686-unknown-windows-gnu
37-
// * x86_64-unknown-uefi -> x86_64-unknown-windows
38-
// * aarch64-unknown-uefi -> aarch64-unknown-windows
39-
//
40-
// However, in cc-rs all the UEFI targets use
41-
// -windows-gnu. This has been the case since 2021 [1].
42-
// * i686-unknown-uefi -> i686-unknown-windows-gnu
43-
// * x86_64-unknown-uefi -> x86_64-unknown-windows-gnu
44-
// * aarch64-unknown-uefi -> aarch64-unknown-windows-gnu
45-
//
46-
// For now, override the UEFI mapping to keep the behavior
47-
// of cc-rs unchanged.
48-
//
49-
// TODO: as discussed in [2], it may be possible to switch
50-
// to new UEFI targets added to clang, and regardless it
51-
// would be good to have consistency between rustc and
52-
// cc-rs.
53-
//
54-
// [1]: https://github.com/rust-lang/cc-rs/pull/623
55-
// [2]: https://github.com/rust-lang/cc-rs/pull/1264
56-
let arch = if spec.arch == "x86" {
57-
"i686"
58-
} else {
59-
&spec.arch
60-
};
61-
format!("{}-unknown-windows-gnu", arch)
62-
} else {
63-
spec.llvm_target.clone()
64-
};
65-
66-
writeln!(f, " (")?;
67-
writeln!(f, " {triple:?},")?;
68-
writeln!(f, " TargetInfo {{")?;
69-
writeln!(f, " full_arch: {full_arch:?},")?;
70-
writeln!(f, " arch: {arch:?},")?;
71-
writeln!(f, " vendor: {vendor:?},")?;
72-
writeln!(f, " os: {os:?},")?;
73-
writeln!(f, " env: {env:?},")?;
74-
writeln!(f, " abi: {abi:?},")?;
75-
writeln!(f, " llvm_target: {llvm_target:?},")?;
76-
writeln!(f, " }},")?;
77-
writeln!(f, " ),")?;
19+
writeln!(f, " ({triple:?}, {:?}),", spec.llvm_target)?;
7820
}
7921

8022
writeln!(f, "];")?;

‎dev-tools/gen-target-info/src/write.rs

-18
This file was deleted.

‎src/lib.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -2202,14 +2202,14 @@ impl Build {
22022202
// So instead, we pass the deployment target with `-m*-version-min=`, and only
22032203
// pass it here on visionOS and Mac Catalyst where that option does not exist:
22042204
// https://github.com/rust-lang/cc-rs/issues/1383
2205-
let clang_target = if target.os == "visionos" || target.abi == "macabi" {
2206-
Cow::Owned(
2207-
target.versioned_llvm_target(&self.apple_deployment_target(target)),
2208-
)
2205+
let version = if target.os == "visionos" || target.abi == "macabi" {
2206+
Some(self.apple_deployment_target(target))
22092207
} else {
2210-
Cow::Borrowed(target.llvm_target)
2208+
None
22112209
};
22122210

2211+
let clang_target =
2212+
target.llvm_target(&self.get_raw_target()?, version.as_deref());
22132213
cmd.push_cc_arg(format!("--target={clang_target}").into());
22142214
}
22152215
}
@@ -2235,7 +2235,13 @@ impl Build {
22352235
// <https://github.com/microsoft/STL/pull/4741>.
22362236
cmd.push_cc_arg("-arch:SSE2".into());
22372237
} else {
2238-
cmd.push_cc_arg(format!("--target={}", target.llvm_target).into());
2238+
cmd.push_cc_arg(
2239+
format!(
2240+
"--target={}",
2241+
target.llvm_target(&self.get_raw_target()?, None)
2242+
)
2243+
.into(),
2244+
);
22392245
}
22402246
} else if target.full_arch == "i586" {
22412247
cmd.push_cc_arg("-arch:IA32".into());
@@ -3520,7 +3526,9 @@ impl Build {
35203526

35213527
fn get_target(&self) -> Result<TargetInfo<'_>, Error> {
35223528
match &self.target {
3523-
Some(t) if Some(&**t) != self.getenv_unwrap_str("TARGET").ok().as_deref() => t.parse(),
3529+
Some(t) if Some(&**t) != self.getenv_unwrap_str("TARGET").ok().as_deref() => {
3530+
TargetInfo::from_rustc_target(t)
3531+
}
35243532
// Fetch target information from environment if not set, or if the
35253533
// target was the same as the TARGET environment variable, in
35263534
// case the user did `build.target(&env::var("TARGET").unwrap())`.

‎src/target.rs

-90
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
//!
33
//! See the `target-lexicon` crate for a more principled approach to this.
44
5-
use std::str::FromStr;
6-
7-
use crate::{Error, ErrorKind};
8-
95
mod apple;
106
mod generated;
117
mod llvm;
@@ -43,90 +39,4 @@ pub(crate) struct TargetInfo<'a> {
4339
///
4440
/// This is the same as the value of `cfg!(target_abi)`.
4541
pub abi: &'a str,
46-
/// The unversioned LLVM/Clang target triple.
47-
///
48-
/// NOTE: You should never need to match on this explicitly, use the other
49-
/// fields on [`TargetInfo`] instead.
50-
pub llvm_target: &'a str,
51-
}
52-
53-
impl FromStr for TargetInfo<'_> {
54-
type Err = Error;
55-
56-
/// This will fail when using a custom target triple unknown to `rustc`.
57-
fn from_str(target_triple: &str) -> Result<Self, Error> {
58-
if let Ok(index) =
59-
generated::LIST.binary_search_by_key(&target_triple, |(target_triple, _)| target_triple)
60-
{
61-
let (_, info) = &generated::LIST[index];
62-
Ok(info.clone())
63-
} else {
64-
Err(Error::new(
65-
ErrorKind::UnknownTarget,
66-
format!(
67-
"unknown target `{target_triple}`.
68-
69-
NOTE: `cc-rs` only supports a fixed set of targets when not in a build script.
70-
- If adding a new target, you will need to fork of `cc-rs` until the target
71-
has landed on nightly and the auto-generated list has been updated. See also
72-
the `rustc` dev guide on adding a new target:
73-
https://rustc-dev-guide.rust-lang.org/building/new-target.html
74-
- If using a custom target, prefer to upstream it to `rustc` if possible,
75-
otherwise open an issue with `cc-rs`:
76-
https://github.com/rust-lang/cc-rs/issues/new
77-
"
78-
),
79-
))
80-
}
81-
}
82-
}
83-
84-
#[cfg(test)]
85-
mod tests {
86-
use std::str::FromStr;
87-
88-
use super::TargetInfo;
89-
90-
// Test tier 1 targets
91-
#[test]
92-
fn tier1() {
93-
let targets = [
94-
"aarch64-unknown-linux-gnu",
95-
"aarch64-apple-darwin",
96-
"i686-pc-windows-gnu",
97-
"i686-pc-windows-msvc",
98-
"i686-unknown-linux-gnu",
99-
"x86_64-apple-darwin",
100-
"x86_64-pc-windows-gnu",
101-
"x86_64-pc-windows-msvc",
102-
"x86_64-unknown-linux-gnu",
103-
];
104-
105-
for target in targets {
106-
// Check that it parses
107-
let _ = TargetInfo::from_str(target).unwrap();
108-
}
109-
}
110-
111-
// Various custom target triples not (or no longer) known by `rustc`
112-
#[test]
113-
fn cannot_parse_extra() {
114-
let targets = [
115-
"aarch64-unknown-none-gnu",
116-
"aarch64-uwp-windows-gnu",
117-
"arm-frc-linux-gnueabi",
118-
"arm-unknown-netbsd-eabi",
119-
"armv7neon-unknown-linux-gnueabihf",
120-
"armv7neon-unknown-linux-musleabihf",
121-
"thumbv7-unknown-linux-gnueabihf",
122-
"thumbv7-unknown-linux-musleabihf",
123-
"x86_64-rumprun-netbsd",
124-
"x86_64-unknown-linux",
125-
];
126-
127-
for target in targets {
128-
// Check that it does not parse
129-
let _ = TargetInfo::from_str(target).unwrap_err();
130-
}
131-
}
13242
}

0 commit comments

Comments
 (0)
Please sign in to comment.