Skip to content

Commit 8055191

Browse files
committed
Move knowledge of SDK names to rustc_target
Also make the SDK name be the same casing as used in the file system.
1 parent 6f4ae0f commit 8055191

File tree

5 files changed

+36
-49
lines changed

5 files changed

+36
-49
lines changed

Diff for: compiler/rustc_codegen_ssa/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,6 @@ codegen_ssa_unknown_atomic_ordering = unknown ordering in atomic intrinsic
337337
338338
codegen_ssa_unknown_reuse_kind = unknown cgu-reuse-kind `{$kind}` specified
339339
340-
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
341-
342340
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
343341
344342
codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+18-39
Original file line numberDiff line numberDiff line change
@@ -3123,9 +3123,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31233123
}
31243124

31253125
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) -> Option<PathBuf> {
3126-
let arch = &sess.target.arch;
31273126
let os = &sess.target.os;
3128-
let llvm_target = &sess.target.llvm_target;
31293127
if sess.target.vendor != "apple"
31303128
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "visionos" | "macos")
31313129
|| !matches!(flavor, LinkerFlavor::Darwin(..))
@@ -3137,30 +3135,8 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) ->
31373135
return None;
31383136
}
31393137

3140-
let sdk_name = match (arch.as_ref(), os.as_ref()) {
3141-
("aarch64", "tvos") if llvm_target.ends_with("-simulator") => "appletvsimulator",
3142-
("aarch64", "tvos") => "appletvos",
3143-
("x86_64", "tvos") => "appletvsimulator",
3144-
("arm", "ios") => "iphoneos",
3145-
("aarch64", "ios") if llvm_target.contains("macabi") => "macosx",
3146-
("aarch64", "ios") if llvm_target.ends_with("-simulator") => "iphonesimulator",
3147-
("aarch64", "ios") => "iphoneos",
3148-
("x86", "ios") => "iphonesimulator",
3149-
("x86_64", "ios") if llvm_target.contains("macabi") => "macosx",
3150-
("x86_64", "ios") => "iphonesimulator",
3151-
("x86_64", "watchos") => "watchsimulator",
3152-
("arm64_32", "watchos") => "watchos",
3153-
("aarch64", "watchos") if llvm_target.ends_with("-simulator") => "watchsimulator",
3154-
("aarch64", "watchos") => "watchos",
3155-
("aarch64", "visionos") if llvm_target.ends_with("-simulator") => "xrsimulator",
3156-
("aarch64", "visionos") => "xros",
3157-
("arm", "watchos") => "watchos",
3158-
(_, "macos") => "macosx",
3159-
_ => {
3160-
sess.dcx().emit_err(errors::UnsupportedArch { arch, os });
3161-
return None;
3162-
}
3163-
};
3138+
let sdk_name = rustc_target::spec::apple_sdk_name(&sess.target);
3139+
31643140
let sdk_root = match get_apple_sdk_root(sdk_name) {
31653141
Ok(s) => s,
31663142
Err(e) => {
@@ -3197,7 +3173,7 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, errors::AppleSdkRootErro
31973173
// can fall back to checking for xcrun on PATH.)
31983174
if let Ok(sdkroot) = env::var("SDKROOT") {
31993175
let p = Path::new(&sdkroot);
3200-
match sdk_name {
3176+
match &*sdk_name.to_lowercase() {
32013177
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
32023178
"appletvos"
32033179
if sdkroot.contains("TVSimulator.platform")
@@ -3228,18 +3204,21 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, errors::AppleSdkRootErro
32283204
_ => return Ok(sdkroot),
32293205
}
32303206
}
3231-
let res =
3232-
Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
3233-
|output| {
3234-
if output.status.success() {
3235-
Ok(String::from_utf8(output.stdout).unwrap())
3236-
} else {
3237-
let error = String::from_utf8(output.stderr);
3238-
let error = format!("process exit with error: {}", error.unwrap());
3239-
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
3240-
}
3241-
},
3242-
);
3207+
3208+
let res = Command::new("xcrun")
3209+
.arg("--show-sdk-path")
3210+
.arg("-sdk")
3211+
.arg(sdk_name.to_lowercase())
3212+
.output()
3213+
.and_then(|output| {
3214+
if output.status.success() {
3215+
Ok(String::from_utf8(output.stdout).unwrap())
3216+
} else {
3217+
let error = String::from_utf8(output.stderr);
3218+
let error = format!("process exit with error: {}", error.unwrap());
3219+
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
3220+
}
3221+
});
32433222

32443223
match res {
32453224
Ok(output) => Ok(output.trim().to_string()),

Diff for: compiler/rustc_codegen_ssa/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,6 @@ pub enum ExtractBundledLibsError<'a> {
532532
ExtractSection { rlib: &'a Path, error: Box<dyn std::error::Error> },
533533
}
534534

535-
#[derive(Diagnostic)]
536-
#[diag(codegen_ssa_unsupported_arch)]
537-
pub(crate) struct UnsupportedArch<'a> {
538-
pub arch: &'a str,
539-
pub os: &'a str,
540-
}
541-
542535
#[derive(Diagnostic)]
543536
pub(crate) enum AppleSdkRootError<'a> {
544537
#[diag(codegen_ssa_apple_sdk_error_sdk_path)]

Diff for: compiler/rustc_target/src/spec/base/apple/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ pub(crate) fn base(
158158
(opts, llvm_target(os, arch, abi), arch.target_arch())
159159
}
160160

161+
pub fn sdk_name(target: &Target) -> &'static str {
162+
match (&*target.os, &*target.abi) {
163+
("ios", "") => "iPhoneOS",
164+
("ios", "sim") => "iPhoneSimulator",
165+
// Mac Catalyst uses the macOS SDK
166+
("ios", "macabi") => "MacOSX",
167+
("macos", "") => "MacOSX",
168+
("tvos", "") => "AppleTVOS",
169+
("tvos", "sim") => "AppleTVSimulator",
170+
("visionos", "") => "XROS",
171+
("visionos", "sim") => "XRSimulator",
172+
("watchos", "") => "WatchOS",
173+
("watchos", "sim") => "WatchSimulator",
174+
(os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"),
175+
}
176+
}
177+
161178
pub fn platform(target: &Target) -> Option<u32> {
162179
Some(match (&*target.os, &*target.abi) {
163180
("macos", _) => object::macho::PLATFORM_MACOS,

Diff for: compiler/rustc_target/src/spec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub mod crt_objects;
6161
mod base;
6262
pub use base::apple::{
6363
deployment_target_for_target as current_apple_deployment_target,
64-
platform as current_apple_platform,
64+
platform as current_apple_platform, sdk_name as apple_sdk_name,
6565
};
6666
pub use base::avr_gnu::ef_avr_arch;
6767

0 commit comments

Comments
 (0)