Skip to content

Commit 168951c

Browse files
committed
Move knowledge of SDK names to rustc_codegen_ssa::back::apple
Also make the SDK name be the same casing as used in the file system.
1 parent e3a918e commit 168951c

File tree

4 files changed

+35
-48
lines changed

4 files changed

+35
-48
lines changed

compiler/rustc_codegen_ssa/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ codegen_ssa_unknown_atomic_ordering = unknown ordering in atomic intrinsic
348348
349349
codegen_ssa_unknown_reuse_kind = unknown cgu-reuse-kind `{$kind}` specified
350350
351-
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
352-
353351
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
354352
355353
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)

compiler/rustc_codegen_ssa/src/back/apple.rs

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ use crate::errors::AppleDeploymentTarget;
1010
#[cfg(test)]
1111
mod tests;
1212

13+
pub(super) fn sdk_name(target: &Target) -> &'static str {
14+
match (&*target.os, &*target.abi) {
15+
("ios", "") => "iPhoneOS",
16+
("ios", "sim") => "iPhoneSimulator",
17+
// Mac Catalyst uses the macOS SDK
18+
("ios", "macabi") => "MacOSX",
19+
("macos", "") => "MacOSX",
20+
("tvos", "") => "AppleTVOS",
21+
("tvos", "sim") => "AppleTVSimulator",
22+
("visionos", "") => "XROS",
23+
("visionos", "sim") => "XRSimulator",
24+
("watchos", "") => "WatchOS",
25+
("watchos", "sim") => "WatchSimulator",
26+
(os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"),
27+
}
28+
}
29+
1330
pub(super) fn macho_platform(target: &Target) -> u32 {
1431
match (&*target.os, &*target.abi) {
1532
("macos", _) => object::macho::PLATFORM_MACOS,

compiler/rustc_codegen_ssa/src/back/link.rs

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

31263126
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) -> Option<PathBuf> {
3127-
let arch = &sess.target.arch;
31283127
let os = &sess.target.os;
3129-
let llvm_target = &sess.target.llvm_target;
31303128
if sess.target.vendor != "apple"
31313129
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "visionos" | "macos")
31323130
|| !matches!(flavor, LinkerFlavor::Darwin(..))
@@ -3138,30 +3136,8 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) ->
31383136
return None;
31393137
}
31403138

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

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

compiler/rustc_codegen_ssa/src/errors.rs

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

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

0 commit comments

Comments
 (0)