Skip to content

Commit e889875

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 20c909f commit e889875

File tree

5 files changed

+38
-48
lines changed

5 files changed

+38
-48
lines changed

compiler/rustc_codegen_ssa/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ codegen_ssa_unknown_atomic_ordering = unknown ordering in atomic intrinsic
342342
343343
codegen_ssa_unknown_reuse_kind = unknown cgu-reuse-kind `{$kind}` specified
344344
345-
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
346-
347345
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
348346
349347
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use rustc_target::spec::Target;
2+
3+
pub fn sdk_name(target: &Target) -> &'static str {
4+
match (&*target.os, &*target.abi) {
5+
("ios", "") => "iPhoneOS",
6+
("ios", "sim") => "iPhoneSimulator",
7+
// Mac Catalyst uses the macOS SDK
8+
("ios", "macabi") => "MacOSX",
9+
("macos", "") => "MacOSX",
10+
("tvos", "") => "AppleTVOS",
11+
("tvos", "sim") => "AppleTVSimulator",
12+
("visionos", "") => "XROS",
13+
("visionos", "sim") => "XRSimulator",
14+
("watchos", "") => "WatchOS",
15+
("watchos", "sim") => "WatchSimulator",
16+
(os, abi) => unreachable!("invalid os '{os}' / abi '{abi}' combination for Apple target"),
17+
}
18+
}

compiler/rustc_codegen_ssa/src/back/link.rs

+19-39
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use rustc_target::spec::{
4545
use tempfile::Builder as TempFileBuilder;
4646
use tracing::{debug, info, warn};
4747

48+
use super::apple;
4849
use super::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
4950
use super::command::Command;
5051
use super::linker::{self, Linker};
@@ -3125,9 +3126,7 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
31253126
}
31263127

31273128
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) -> Option<PathBuf> {
3128-
let arch = &sess.target.arch;
31293129
let os = &sess.target.os;
3130-
let llvm_target = &sess.target.llvm_target;
31313130
if sess.target.vendor != "apple"
31323131
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "visionos" | "macos")
31333132
|| !matches!(flavor, LinkerFlavor::Darwin(..))
@@ -3139,30 +3138,8 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) ->
31393138
return None;
31403139
}
31413140

3142-
let sdk_name = match (arch.as_ref(), os.as_ref()) {
3143-
("aarch64", "tvos") if llvm_target.ends_with("-simulator") => "appletvsimulator",
3144-
("aarch64", "tvos") => "appletvos",
3145-
("x86_64", "tvos") => "appletvsimulator",
3146-
("arm", "ios") => "iphoneos",
3147-
("aarch64", "ios") if llvm_target.contains("macabi") => "macosx",
3148-
("aarch64", "ios") if llvm_target.ends_with("-simulator") => "iphonesimulator",
3149-
("aarch64", "ios") => "iphoneos",
3150-
("x86", "ios") => "iphonesimulator",
3151-
("x86_64", "ios") if llvm_target.contains("macabi") => "macosx",
3152-
("x86_64", "ios") => "iphonesimulator",
3153-
("x86_64", "watchos") => "watchsimulator",
3154-
("arm64_32", "watchos") => "watchos",
3155-
("aarch64", "watchos") if llvm_target.ends_with("-simulator") => "watchsimulator",
3156-
("aarch64", "watchos") => "watchos",
3157-
("aarch64", "visionos") if llvm_target.ends_with("-simulator") => "xrsimulator",
3158-
("aarch64", "visionos") => "xros",
3159-
("arm", "watchos") => "watchos",
3160-
(_, "macos") => "macosx",
3161-
_ => {
3162-
sess.dcx().emit_err(errors::UnsupportedArch { arch, os });
3163-
return None;
3164-
}
3165-
};
3141+
let sdk_name = apple::sdk_name(&sess.target);
3142+
31663143
let sdk_root = match get_apple_sdk_root(sdk_name) {
31673144
Ok(s) => s,
31683145
Err(e) => {
@@ -3199,7 +3176,7 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, errors::AppleSdkRootErro
31993176
// can fall back to checking for xcrun on PATH.)
32003177
if let Ok(sdkroot) = env::var("SDKROOT") {
32013178
let p = Path::new(&sdkroot);
3202-
match sdk_name {
3179+
match &*sdk_name.to_lowercase() {
32033180
// Ignore `SDKROOT` if it's clearly set for the wrong platform.
32043181
"appletvos"
32053182
if sdkroot.contains("TVSimulator.platform")
@@ -3230,18 +3207,21 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, errors::AppleSdkRootErro
32303207
_ => return Ok(sdkroot),
32313208
}
32323209
}
3233-
let res =
3234-
Command::new("xcrun").arg("--show-sdk-path").arg("-sdk").arg(sdk_name).output().and_then(
3235-
|output| {
3236-
if output.status.success() {
3237-
Ok(String::from_utf8(output.stdout).unwrap())
3238-
} else {
3239-
let error = String::from_utf8(output.stderr);
3240-
let error = format!("process exit with error: {}", error.unwrap());
3241-
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
3242-
}
3243-
},
3244-
);
3210+
3211+
let res = Command::new("xcrun")
3212+
.arg("--show-sdk-path")
3213+
.arg("-sdk")
3214+
.arg(sdk_name.to_lowercase())
3215+
.output()
3216+
.and_then(|output| {
3217+
if output.status.success() {
3218+
Ok(String::from_utf8(output.stdout).unwrap())
3219+
} else {
3220+
let error = String::from_utf8(output.stderr);
3221+
let error = format!("process exit with error: {}", error.unwrap());
3222+
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
3223+
}
3224+
});
32453225

32463226
match res {
32473227
Ok(output) => Ok(output.trim().to_string()),

compiler/rustc_codegen_ssa/src/back/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod apple;
12
pub mod archive;
23
pub(crate) mod command;
34
pub mod link;

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)]

0 commit comments

Comments
 (0)