Skip to content

Commit 75f3ea1

Browse files
committed
Refactor Apple flags
Non-functional change.
1 parent c3ad44e commit 75f3ea1

File tree

2 files changed

+49
-129
lines changed

2 files changed

+49
-129
lines changed

src/lib.rs

+45-125
Original file line numberDiff line numberDiff line change
@@ -2528,113 +2528,68 @@ impl Build {
25282528

25292529
fn apple_flags(&self, cmd: &mut Tool) -> Result<(), Error> {
25302530
let target = self.get_target()?;
2531-
let arch_str = &*target.full_arch;
25322531

2533-
let arch = if target.os == "macos" {
2534-
match arch_str {
2535-
"i686" => AppleArchSpec::Device("-m32"),
2536-
"x86_64" | "x86_64h" | "aarch64" | "arm64e" => AppleArchSpec::Device("-m64"),
2537-
_ => {
2538-
return Err(Error::new(
2539-
ErrorKind::ArchitectureInvalid,
2540-
"Unknown architecture for macOS target.",
2541-
));
2542-
}
2543-
}
2544-
} else if target.abi == "macabi" {
2545-
match arch_str {
2546-
"arm64e" => AppleArchSpec::Catalyst("arm64e"),
2547-
"arm64" | "aarch64" => AppleArchSpec::Catalyst("arm64"),
2548-
"x86_64" | "x86_64h" => AppleArchSpec::Catalyst("-m64"),
2549-
_ => {
2550-
return Err(Error::new(
2551-
ErrorKind::ArchitectureInvalid,
2552-
"Unknown architecture for iOS target.",
2553-
));
2554-
}
2555-
}
2556-
} else if target.abi == "sim" {
2557-
match arch_str {
2558-
"arm64" | "aarch64" => AppleArchSpec::Simulator("arm64"),
2559-
"i386" | "i686" => AppleArchSpec::Simulator("-m32"),
2560-
"x86_64" | "x86_64h" => AppleArchSpec::Simulator("-m64"),
2561-
_ => {
2562-
return Err(Error::new(
2563-
ErrorKind::ArchitectureInvalid,
2564-
"Unknown architecture for simulator target.",
2565-
));
2566-
}
2567-
}
2568-
} else {
2569-
match arch_str {
2570-
"arm" | "armv7" | "thumbv7" => AppleArchSpec::Device("armv7"),
2571-
"armv7k" => AppleArchSpec::Device("armv7k"),
2572-
"armv7s" | "thumbv7s" => AppleArchSpec::Device("armv7s"),
2573-
"arm64e" => AppleArchSpec::Device("arm64e"),
2574-
"arm64" | "aarch64" => AppleArchSpec::Device("arm64"),
2575-
"arm64_32" => AppleArchSpec::Device("arm64_32"),
2576-
_ => {
2577-
return Err(Error::new(
2578-
ErrorKind::ArchitectureInvalid,
2579-
format!("Unknown architecture for {:?} target.", target.os),
2580-
));
2581-
}
2532+
let arch = match &*target.full_arch {
2533+
"arm64" | "aarch64" | "arm64e" if target.os == "macos" => "-m64",
2534+
"x86_64" | "x86_64h" if target.os == "macos" => "-m64",
2535+
"arm" | "armv7" | "thumbv7" => "armv7",
2536+
"arm64_32" => "arm64_32",
2537+
"arm64" | "aarch64" => "arm64",
2538+
"arm64e" => "arm64e",
2539+
"armv7k" => "armv7k",
2540+
"armv7s" | "thumbv7s" => "armv7s",
2541+
"i386" | "i686" => "-m32",
2542+
"x86_64" | "x86_64h" => "-m64",
2543+
_ => {
2544+
return Err(Error::new(
2545+
ErrorKind::ArchitectureInvalid,
2546+
format!("unknown architecture {} for Apple target", target.full_arch),
2547+
));
25822548
}
25832549
};
25842550

2585-
let sdk_details = apple_os_sdk_parts(&target.os, &arch);
25862551
let min_version = self.apple_deployment_target(&target);
25872552

2588-
match arch {
2589-
AppleArchSpec::Device(_) if target.os == "macos" => {
2590-
cmd.args
2591-
.push(format!("-mmacosx-version-min={}", min_version).into());
2592-
}
2593-
AppleArchSpec::Device(arch) => {
2553+
if target.os != "macos" && target.abi != "macabi" {
2554+
if arch.starts_with('-') {
2555+
// -m32 or -m64
2556+
cmd.args.push(arch.into());
2557+
} else {
25942558
cmd.args.push("-arch".into());
25952559
cmd.args.push(arch.into());
2596-
// `-mxros-version-min` does not exist
2597-
// https://github.com/llvm/llvm-project/issues/88271
2598-
if target.os != "visionos" {
2599-
cmd.args.push(
2600-
format!("-m{}os-version-min={}", sdk_details.sdk_prefix, min_version)
2601-
.into(),
2602-
);
2603-
}
26042560
}
2605-
AppleArchSpec::Simulator(arch) => {
2606-
if arch.starts_with('-') {
2607-
// -m32 or -m64
2608-
cmd.args.push(arch.into());
2609-
} else {
2610-
cmd.args.push("-arch".into());
2611-
cmd.args.push(arch.into());
2612-
}
2613-
if target.os != "visionos" {
2614-
cmd.args.push(
2615-
format!(
2616-
"-m{}simulator-version-min={}",
2617-
sdk_details.sim_prefix, min_version
2618-
)
2619-
.into(),
2620-
);
2621-
}
2622-
}
2623-
AppleArchSpec::Catalyst(_) => {}
2561+
}
2562+
2563+
let version_flag = match (&*target.os, &*target.abi) {
2564+
("macos", "") => Some("-mmacosx-version-min"),
2565+
("ios", "") => Some("-miphoneos-version-min"),
2566+
("ios", "sim") => Some("-mios-simulator-version-min"),
2567+
("tvos", "") => Some("-mappletvos-version-min"),
2568+
("tvos", "sim") => Some("-mappletvsimulator-version-min"),
2569+
("watchos", "") => Some("-mwatchos-version-min"),
2570+
("watchos", "sim") => Some("-mwatchsimulator-version-min"),
2571+
// `-mxros-version-min` does not exist
2572+
// https://github.com/llvm/llvm-project/issues/88271
2573+
_ => None,
26242574
};
2575+
if let Some(version_flag) = version_flag {
2576+
cmd.args
2577+
.push(format!("{}={}", version_flag, min_version).into());
2578+
}
26252579

26262580
// AppleClang sometimes requires sysroot even on macOS
26272581
if cmd.is_xctoolchain_clang() || target.os != "macos" {
26282582
self.cargo_output.print_metadata(&format_args!(
26292583
"Detecting {:?} SDK path for {}",
2630-
target.os, sdk_details.sdk
2584+
target.os,
2585+
target.apple_sdk_name(),
26312586
));
2632-
let sdk_path = self.apple_sdk_root(&sdk_details.sdk)?;
2587+
let sdk_path = self.apple_sdk_root(&target)?;
26332588

26342589
cmd.args.push("-isysroot".into());
26352590
cmd.args.push(OsStr::new(&sdk_path).to_owned());
26362591

2637-
if let AppleArchSpec::Catalyst(_) = arch {
2592+
if target.abi == "macabi" {
26382593
// Mac Catalyst uses the macOS SDK, but to compile against and
26392594
// link to iOS-specific frameworks, we should have the support
26402595
// library stubs in the include and library search path.
@@ -3708,7 +3663,9 @@ impl Build {
37083663
Ok(Arc::from(OsStr::new(sdk_path.trim())))
37093664
}
37103665

3711-
fn apple_sdk_root(&self, sdk: &str) -> Result<Arc<OsStr>, Error> {
3666+
fn apple_sdk_root(&self, target: &TargetInfo) -> Result<Arc<OsStr>, Error> {
3667+
let sdk = target.apple_sdk_name();
3668+
37123669
if let Some(ret) = self
37133670
.apple_sdk_root_cache
37143671
.read()
@@ -3959,43 +3916,6 @@ fn fail(s: &str) -> ! {
39593916
std::process::exit(1);
39603917
}
39613918

3962-
struct AppleSdkTargetParts {
3963-
sdk_prefix: &'static str,
3964-
sim_prefix: &'static str,
3965-
sdk: Cow<'static, str>,
3966-
}
3967-
3968-
fn apple_os_sdk_parts(os: &str, arch: &AppleArchSpec) -> AppleSdkTargetParts {
3969-
let (sdk_prefix, sim_prefix) = match os {
3970-
"macos" => ("macosx", ""),
3971-
"ios" => ("iphone", "ios-"),
3972-
"watchos" => ("watch", "watch"),
3973-
"tvos" => ("appletv", "appletv"),
3974-
"visionos" => ("xr", "xr"),
3975-
os => unreachable!("unknown Apple OS {}", os),
3976-
};
3977-
let sdk = match arch {
3978-
AppleArchSpec::Device(_) if os == "macos" => Cow::Borrowed("macosx"),
3979-
AppleArchSpec::Device(_) => format!("{}os", sdk_prefix).into(),
3980-
AppleArchSpec::Simulator(_) => format!("{}simulator", sdk_prefix).into(),
3981-
AppleArchSpec::Catalyst(_) => Cow::Borrowed("macosx"),
3982-
};
3983-
3984-
AppleSdkTargetParts {
3985-
sdk_prefix,
3986-
sim_prefix,
3987-
sdk,
3988-
}
3989-
}
3990-
3991-
#[allow(dead_code)]
3992-
enum AppleArchSpec {
3993-
Device(&'static str),
3994-
Simulator(&'static str),
3995-
#[allow(dead_code)]
3996-
Catalyst(&'static str),
3997-
}
3998-
39993919
// Use by default minimum available API level
40003920
// See note about naming here
40013921
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang

tests/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ fn gnu_aarch64_none_no_pic() {
274274
for target in &["aarch64-unknown-none-softfloat", "aarch64-unknown-none"] {
275275
let test = Test::gnu();
276276
test.gcc()
277-
.target(&target)
278-
.host(&target)
277+
.target(target)
278+
.host(target)
279279
.file("foo.c")
280280
.compile("foo");
281281

@@ -629,8 +629,8 @@ fn clang_apple_mac_catalyst() {
629629
"-iframework",
630630
&format!("{sdkroot}/System/iOSSupport/System/Library/Frameworks"),
631631
);
632-
execution.must_have(&format!("-L{sdkroot}/System/iOSSupport/usr/lib"));
633-
execution.must_have(&format!(
632+
execution.must_have(format!("-L{sdkroot}/System/iOSSupport/usr/lib"));
633+
execution.must_have(format!(
634634
"-F{sdkroot}/System/iOSSupport/System/Library/Frameworks"
635635
));
636636
}

0 commit comments

Comments
 (0)