Skip to content

Commit 37df40b

Browse files
authored
Rollup merge of #77202 - ehuss:defer-apple-sdkroot, r=petrochenkov
Defer Apple SDKROOT detection to link time. This defers the detection of the SDKROOT for Apple iOS/tvOS targets to link time, instead of when the `Target` is defined. This allows commands that don't need to link to work (like `rustdoc` or `rustc --print=target-list`). This also makes `--print=target-list` a bit faster. This also removes the note in the platform support documentation about these targets being missing. When I wrote it, I misunderstood how the SDKROOT stuff worked. Notes: * This means that JSON spec targets can't explicitly override these flags. I think that is probably fine, as I believe the value is generally required, and can be set with the SDKROOT environment variable. * This changes `x86_64-apple-tvos` to use `appletvsimulator`. I think the original code was wrong (it was using `iphonesimulator`). Also, `x86_64-apple-tvos` seems broken in general, and I cannot build it locally. The `data_layout` does not appear to be correct (it is a copy of the arm64 layout instead of the x86_64 layout). I have not tried building Apple's LLVM to see if that helps, but I suspect it is just wrong (I'm uncertain since I don't know how the tvOS simulator works with its bitcode-only requirements). * I'm tempted to remove the use of `Result` for built-in target definitions, since I don't think they should be fallible. This was added in #34980, but that only relates to JSON definitions. I think the built-in targets shouldn't fail. I can do this now, or not. Fixes #36156 Fixes #76584
2 parents 8ccc063 + 7420d7a commit 37df40b

File tree

12 files changed

+114
-139
lines changed

12 files changed

+114
-139
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+86
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
15241524
// NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
15251525
add_pre_link_args(cmd, sess, flavor);
15261526

1527+
// NO-OPT-OUT, OBJECT-FILES-NO
1528+
add_apple_sdk(cmd, sess, flavor);
1529+
15271530
// NO-OPT-OUT
15281531
add_link_script(cmd, sess, tmpdir, crate_type);
15291532

@@ -2083,3 +2086,86 @@ fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
20832086
config::Lto::No | config::Lto::ThinLocal => false,
20842087
}
20852088
}
2089+
2090+
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
2091+
let arch = &sess.target.target.arch;
2092+
let os = &sess.target.target.target_os;
2093+
let llvm_target = &sess.target.target.llvm_target;
2094+
if sess.target.target.target_vendor != "apple"
2095+
|| !matches!(os.as_str(), "ios" | "tvos")
2096+
|| flavor != LinkerFlavor::Gcc
2097+
{
2098+
return;
2099+
}
2100+
let sdk_name = match (arch.as_str(), os.as_str()) {
2101+
("aarch64", "tvos") => "appletvos",
2102+
("x86_64", "tvos") => "appletvsimulator",
2103+
("arm", "ios") => "iphoneos",
2104+
("aarch64", "ios") => "iphoneos",
2105+
("x86", "ios") => "iphonesimulator",
2106+
("x86_64", "ios") if llvm_target.contains("macabi") => "macosx10.15",
2107+
("x86_64", "ios") => "iphonesimulator",
2108+
_ => {
2109+
sess.err(&format!("unsupported arch `{}` for os `{}`", arch, os));
2110+
return;
2111+
}
2112+
};
2113+
let sdk_root = match get_apple_sdk_root(sdk_name) {
2114+
Ok(s) => s,
2115+
Err(e) => {
2116+
sess.err(&e);
2117+
return;
2118+
}
2119+
};
2120+
let arch_name = llvm_target.split('-').next().expect("LLVM target must have a hyphen");
2121+
cmd.args(&["-arch", arch_name, "-isysroot", &sdk_root, "-Wl,-syslibroot", &sdk_root]);
2122+
}
2123+
2124+
fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> {
2125+
// Following what clang does
2126+
// (https://github.com/llvm/llvm-project/blob/
2127+
// 296a80102a9b72c3eda80558fb78a3ed8849b341/clang/lib/Driver/ToolChains/Darwin.cpp#L1661-L1678)
2128+
// to allow the SDK path to be set. (For clang, xcrun sets
2129+
// SDKROOT; for rustc, the user or build system can set it, or we
2130+
// can fall back to checking for xcrun on PATH.)
2131+
if let Ok(sdkroot) = env::var("SDKROOT") {
2132+
let p = Path::new(&sdkroot);
2133+
match sdk_name {
2134+
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
2135+
"appletvos"
2136+
if sdkroot.contains("TVSimulator.platform")
2137+
|| sdkroot.contains("MacOSX.platform") => {}
2138+
"appletvsimulator"
2139+
if sdkroot.contains("TVOS.platform") || sdkroot.contains("MacOSX.platform") => {}
2140+
"iphoneos"
2141+
if sdkroot.contains("iPhoneSimulator.platform")
2142+
|| sdkroot.contains("MacOSX.platform") => {}
2143+
"iphonesimulator"
2144+
if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("MacOSX.platform") => {
2145+
}
2146+
"macosx10.15"
2147+
if sdkroot.contains("iPhoneOS.platform")
2148+
|| sdkroot.contains("iPhoneSimulator.platform") => {}
2149+
// Ignore `SDKROOT` if it's not a valid path.
2150+
_ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {}
2151+
_ => return Ok(sdkroot),
2152+
}
2153+
}
2154+
let res =
2155+
Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
2156+
|output| {
2157+
if output.status.success() {
2158+
Ok(String::from_utf8(output.stdout).unwrap())
2159+
} else {
2160+
let error = String::from_utf8(output.stderr);
2161+
let error = format!("process exit with error: {}", error.unwrap());
2162+
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
2163+
}
2164+
},
2165+
);
2166+
2167+
match res {
2168+
Ok(output) => Ok(output.trim().to_string()),
2169+
Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)),
2170+
}
2171+
}

compiler/rustc_target/src/spec/aarch64_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::Arm64, AppleOS::iOS)?;
5+
let base = opts(Arch::Arm64);
66
Ok(Target {
77
llvm_target: "arm64-apple-ios".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/aarch64_apple_tvos.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::Arm64, AppleOS::tvOS)?;
5+
let base = opts(Arch::Arm64);
66
Ok(Target {
77
llvm_target: "arm64-apple-tvos".to_string(),
88
target_endian: "little".to_string(),
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
2-
use std::env;
3-
use std::io;
4-
use std::path::Path;
5-
use std::process::Command;
1+
use crate::spec::TargetOptions;
62

73
use Arch::*;
84
#[allow(non_camel_case_types)]
@@ -16,108 +12,6 @@ pub enum Arch {
1612
X86_64_macabi,
1713
}
1814

19-
#[allow(non_camel_case_types)]
20-
#[derive(Copy, Clone)]
21-
pub enum AppleOS {
22-
tvOS,
23-
iOS,
24-
}
25-
26-
impl Arch {
27-
pub fn to_string(self) -> &'static str {
28-
match self {
29-
Armv7 => "armv7",
30-
Armv7s => "armv7s",
31-
Arm64 => "arm64",
32-
I386 => "i386",
33-
X86_64 => "x86_64",
34-
X86_64_macabi => "x86_64",
35-
}
36-
}
37-
}
38-
39-
pub fn get_sdk_root(sdk_name: &str) -> Result<String, String> {
40-
// Following what clang does
41-
// (https://github.com/llvm/llvm-project/blob/
42-
// 296a80102a9b72c3eda80558fb78a3ed8849b341/clang/lib/Driver/ToolChains/Darwin.cpp#L1661-L1678)
43-
// to allow the SDK path to be set. (For clang, xcrun sets
44-
// SDKROOT; for rustc, the user or build system can set it, or we
45-
// can fall back to checking for xcrun on PATH.)
46-
if let Ok(sdkroot) = env::var("SDKROOT") {
47-
let p = Path::new(&sdkroot);
48-
match sdk_name {
49-
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
50-
"appletvos"
51-
if sdkroot.contains("TVSimulator.platform")
52-
|| sdkroot.contains("MacOSX.platform") => {}
53-
"appletvsimulator"
54-
if sdkroot.contains("TVOS.platform") || sdkroot.contains("MacOSX.platform") => {}
55-
"iphoneos"
56-
if sdkroot.contains("iPhoneSimulator.platform")
57-
|| sdkroot.contains("MacOSX.platform") => {}
58-
"iphonesimulator"
59-
if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("MacOSX.platform") => {
60-
}
61-
"macosx10.15"
62-
if sdkroot.contains("iPhoneOS.platform")
63-
|| sdkroot.contains("iPhoneSimulator.platform") => {}
64-
// Ignore `SDKROOT` if it's not a valid path.
65-
_ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {}
66-
_ => return Ok(sdkroot),
67-
}
68-
}
69-
let res =
70-
Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
71-
|output| {
72-
if output.status.success() {
73-
Ok(String::from_utf8(output.stdout).unwrap())
74-
} else {
75-
let error = String::from_utf8(output.stderr);
76-
let error = format!("process exit with error: {}", error.unwrap());
77-
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
78-
}
79-
},
80-
);
81-
82-
match res {
83-
Ok(output) => Ok(output.trim().to_string()),
84-
Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)),
85-
}
86-
}
87-
88-
fn build_pre_link_args(arch: Arch, os: AppleOS) -> Result<LinkArgs, String> {
89-
let sdk_name = match (arch, os) {
90-
(Arm64, AppleOS::tvOS) => "appletvos",
91-
(X86_64, AppleOS::tvOS) => "appletvsimulator",
92-
(Armv7, AppleOS::iOS) => "iphoneos",
93-
(Armv7s, AppleOS::iOS) => "iphoneos",
94-
(Arm64, AppleOS::iOS) => "iphoneos",
95-
(I386, AppleOS::iOS) => "iphonesimulator",
96-
(X86_64, AppleOS::iOS) => "iphonesimulator",
97-
(X86_64_macabi, AppleOS::iOS) => "macosx10.15",
98-
_ => unreachable!(),
99-
};
100-
101-
let arch_name = arch.to_string();
102-
103-
let sdk_root = get_sdk_root(sdk_name)?;
104-
105-
let mut args = LinkArgs::new();
106-
args.insert(
107-
LinkerFlavor::Gcc,
108-
vec![
109-
"-arch".to_string(),
110-
arch_name.to_string(),
111-
"-isysroot".to_string(),
112-
sdk_root.clone(),
113-
"-Wl,-syslibroot".to_string(),
114-
sdk_root,
115-
],
116-
);
117-
118-
Ok(args)
119-
}
120-
12115
fn target_cpu(arch: Arch) -> String {
12216
match arch {
12317
Armv7 => "cortex-a8", // iOS7 is supported on iPhone 4 and higher
@@ -137,15 +31,13 @@ fn link_env_remove(arch: Arch) -> Vec<String> {
13731
}
13832
}
13933

140-
pub fn opts(arch: Arch, os: AppleOS) -> Result<TargetOptions, String> {
141-
let pre_link_args = build_pre_link_args(arch, os)?;
142-
Ok(TargetOptions {
34+
pub fn opts(arch: Arch) -> TargetOptions {
35+
TargetOptions {
14336
cpu: target_cpu(arch),
14437
executables: true,
145-
pre_link_args,
14638
link_env_remove: link_env_remove(arch),
14739
has_elf_tls: false,
14840
eliminate_frame_pointer: false,
14941
..super::apple_base::opts()
150-
})
42+
}
15143
}

compiler/rustc_target/src/spec/armv7_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::Armv7, AppleOS::iOS)?;
5+
let base = opts(Arch::Armv7);
66
Ok(Target {
77
llvm_target: "armv7-apple-ios".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/armv7s_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::Armv7s, AppleOS::iOS)?;
5+
let base = opts(Arch::Armv7s);
66
Ok(Target {
77
llvm_target: "armv7s-apple-ios".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/i386_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::I386, AppleOS::iOS)?;
5+
let base = opts(Arch::I386);
66
Ok(Target {
77
llvm_target: "i386-apple-ios".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/x86_64_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::X86_64, AppleOS::iOS)?;
5+
let base = opts(Arch::X86_64);
66
Ok(Target {
77
llvm_target: "x86_64-apple-ios".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/x86_64_apple_ios_macabi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::X86_64_macabi, AppleOS::iOS)?;
5+
let base = opts(Arch::X86_64_macabi);
66
Ok(Target {
77
llvm_target: "x86_64-apple-ios13.0-macabi".to_string(),
88
target_endian: "little".to_string(),

compiler/rustc_target/src/spec/x86_64_apple_tvos.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::apple_sdk_base::{opts, AppleOS, Arch};
1+
use super::apple_sdk_base::{opts, Arch};
22
use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult};
33

44
pub fn target() -> TargetResult {
5-
let base = opts(Arch::X86_64, AppleOS::iOS)?;
5+
let base = opts(Arch::X86_64);
66
Ok(Target {
77
llvm_target: "x86_64-apple-tvos".to_string(),
88
target_endian: "little".to_string(),

src/doc/rustc/src/platform-support.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Specifically, these platforms are required to have each of the following:
5757

5858
target | std | host | notes
5959
-------|-----|------|-------
60-
`aarch64-apple-ios` | ✓[^apple] | | ARM64 iOS
60+
`aarch64-apple-ios` | ✓ | | ARM64 iOS
6161
`aarch64-fuchsia` | ✓ | | ARM64 Fuchsia
6262
`aarch64-linux-android` | ✓ | | ARM64 Android
6363
`aarch64-pc-windows-msvc` | ✓ | | ARM64 Windows MSVC
@@ -122,7 +122,7 @@ target | std | host | notes
122122
`wasm32-unknown-emscripten` | ✓ | | WebAssembly via Emscripten
123123
`wasm32-unknown-unknown` | ✓ | | WebAssembly
124124
`wasm32-wasi` | ✓ | | WebAssembly with WASI
125-
`x86_64-apple-ios` | ✓[^apple] | | 64-bit x86 iOS
125+
`x86_64-apple-ios` | ✓ | | 64-bit x86 iOS
126126
`x86_64-fortanix-unknown-sgx` | ✓ | | [Fortanix ABI] for 64-bit Intel SGX
127127
`x86_64-fuchsia` | ✓ | | 64-bit Fuchsia
128128
`x86_64-linux-android` | ✓ | | 64-bit x86 Android
@@ -146,7 +146,7 @@ not available.
146146
target | std | host | notes
147147
-------|-----|------|-------
148148
`aarch64-apple-darwin` | ? | | ARM64 macOS
149-
`aarch64-apple-tvos` | *[^apple] | | ARM64 tvOS
149+
`aarch64-apple-tvos` | * | | ARM64 tvOS
150150
`aarch64-unknown-cloudabi` | ✓ | | ARM64 CloudABI
151151
`aarch64-unknown-freebsd` | ✓ | ✓ | ARM64 FreeBSD
152152
`aarch64-unknown-hermit` | ? | |
@@ -158,16 +158,16 @@ target | std | host | notes
158158
`armv4t-unknown-linux-gnueabi` | ? | |
159159
`armv6-unknown-freebsd` | ✓ | ✓ | ARMv6 FreeBSD
160160
`armv6-unknown-netbsd-eabihf` | ? | |
161-
`armv7-apple-ios` | ✓[^apple] | | ARMv7 iOS, Cortex-a8
161+
`armv7-apple-ios` | ✓ | | ARMv7 iOS, Cortex-a8
162162
`armv7-unknown-cloudabi-eabihf` | ✓ | | ARMv7 CloudABI, hardfloat
163163
`armv7-unknown-freebsd` | ✓ | ✓ | ARMv7 FreeBSD
164164
`armv7-unknown-netbsd-eabihf` | ? | |
165165
`armv7-wrs-vxworks-eabihf` | ? | |
166166
`armv7a-none-eabihf` | * | | ARM Cortex-A, hardfloat
167-
`armv7s-apple-ios` | ✓[^apple] | |
167+
`armv7s-apple-ios` | ✓ | |
168168
`avr-unknown-gnu-atmega328` | ✗ | | AVR. Requires `-Z build-std=core`
169169
`hexagon-unknown-linux-musl` | ? | |
170-
`i386-apple-ios` | ✓[^apple] | | 32-bit x86 iOS
170+
`i386-apple-ios` | ✓ | | 32-bit x86 iOS
171171
`i686-apple-darwin` | ✓ | ✓ | 32-bit OSX (10.7+, Lion+)
172172
`i686-pc-windows-msvc` | ✓ | | 32-bit Windows XP support
173173
`i686-unknown-cloudabi` | ✓ | | 32-bit CloudABI
@@ -203,8 +203,8 @@ target | std | host | notes
203203
`thumbv7a-uwp-windows-msvc` | ✓ | |
204204
`thumbv7neon-unknown-linux-musleabihf` | ? | | Thumb2-mode ARMv7a Linux with NEON, MUSL
205205
`thumbv4t-none-eabi` | * | | ARMv4T T32
206-
`x86_64-apple-ios-macabi` | ✓[^apple] | | Apple Catalyst
207-
`x86_64-apple-tvos` | *[^apple] | | x86 64-bit tvOS
206+
`x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst
207+
`x86_64-apple-tvos` | * | | x86 64-bit tvOS
208208
`x86_64-linux-kernel` | * | | Linux kernel modules
209209
`x86_64-pc-solaris` | ? | |
210210
`x86_64-pc-windows-msvc` | ✓ | | 64-bit Windows XP support
@@ -221,4 +221,3 @@ target | std | host | notes
221221
`x86_64-wrs-vxworks` | ? | |
222222

223223
[runs on NVIDIA GPUs]: https://github.com/japaric-archived/nvptx#targets
224-
[^apple]: These targets are only available on macOS.

src/tools/tier-check/src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ fn main() {
2525
let doc_targets: HashSet<_> = doc_targets_md
2626
.lines()
2727
.filter(|line| line.starts_with('`') && line.contains('|'))
28-
// These platforms only exist on macos.
29-
.filter(|line| !line.contains("[^apple]") || cfg!(target_os = "macos"))
3028
.map(|line| line.split('`').skip(1).next().expect("expected target code span"))
3129
.collect();
3230

0 commit comments

Comments
 (0)