Skip to content

Commit

Permalink
Refactor Apple flags
Browse files Browse the repository at this point in the history
Non-functional change.
  • Loading branch information
madsmtm committed Nov 2, 2024
1 parent 4698f90 commit 3b8d330
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 129 deletions.
170 changes: 45 additions & 125 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2528,113 +2528,68 @@ impl Build {

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

let arch = if target.os == "macos" {
match arch_str {
"i686" => AppleArchSpec::Device("-m32"),
"x86_64" | "x86_64h" | "aarch64" | "arm64e" => AppleArchSpec::Device("-m64"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
"Unknown architecture for macOS target.",
));
}
}
} else if target.abi == "macabi" {
match arch_str {
"arm64e" => AppleArchSpec::Catalyst("arm64e"),
"arm64" | "aarch64" => AppleArchSpec::Catalyst("arm64"),
"x86_64" | "x86_64h" => AppleArchSpec::Catalyst("-m64"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
"Unknown architecture for iOS target.",
));
}
}
} else if target.abi == "sim" {
match arch_str {
"arm64" | "aarch64" => AppleArchSpec::Simulator("arm64"),
"i386" | "i686" => AppleArchSpec::Simulator("-m32"),
"x86_64" | "x86_64h" => AppleArchSpec::Simulator("-m64"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
"Unknown architecture for simulator target.",
));
}
}
} else {
match arch_str {
"arm" | "armv7" | "thumbv7" => AppleArchSpec::Device("armv7"),
"armv7k" => AppleArchSpec::Device("armv7k"),
"armv7s" | "thumbv7s" => AppleArchSpec::Device("armv7s"),
"arm64e" => AppleArchSpec::Device("arm64e"),
"arm64" | "aarch64" => AppleArchSpec::Device("arm64"),
"arm64_32" => AppleArchSpec::Device("arm64_32"),
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
format!("Unknown architecture for {:?} target.", target.os),
));
}
let arch = match &*target.full_arch {
"arm64" | "aarch64" | "arm64e" if target.os == "macos" => "-m64",
"x86_64" | "x86_64h" if target.os == "macos" => "-m64",
"arm" | "armv7" | "thumbv7" => "armv7",
"arm64_32" => "arm64_32",
"arm64" | "aarch64" => "arm64",
"arm64e" => "arm64e",
"armv7k" => "armv7k",
"armv7s" | "thumbv7s" => "armv7s",
"i386" | "i686" => "-m32",
"x86_64" | "x86_64h" => "-m64",
_ => {
return Err(Error::new(
ErrorKind::ArchitectureInvalid,
format!("unknown architecture {} for Apple target", target.full_arch),
));
}
};

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

match arch {
AppleArchSpec::Device(_) if target.os == "macos" => {
cmd.args
.push(format!("-mmacosx-version-min={}", min_version).into());
}
AppleArchSpec::Device(arch) => {
if target.os != "macos" && target.abi != "macabi" {
if arch.starts_with('-') {
// -m32 or -m64
cmd.args.push(arch.into());
} else {
cmd.args.push("-arch".into());
cmd.args.push(arch.into());
// `-mxros-version-min` does not exist
// https://github.com/llvm/llvm-project/issues/88271
if target.os != "visionos" {
cmd.args.push(
format!("-m{}os-version-min={}", sdk_details.sdk_prefix, min_version)
.into(),
);
}
}
AppleArchSpec::Simulator(arch) => {
if arch.starts_with('-') {
// -m32 or -m64
cmd.args.push(arch.into());
} else {
cmd.args.push("-arch".into());
cmd.args.push(arch.into());
}
if target.os != "visionos" {
cmd.args.push(
format!(
"-m{}simulator-version-min={}",
sdk_details.sim_prefix, min_version
)
.into(),
);
}
}
AppleArchSpec::Catalyst(_) => {}
}

let version_flag = match (&*target.os, &*target.abi) {
("macos", "") => Some("-mmacosx-version-min"),
("ios", "") => Some("-miphoneos-version-min"),
("ios", "sim") => Some("-mios-simulator-version-min"),
("tvos", "") => Some("-mappletvos-version-min"),
("tvos", "sim") => Some("-mappletvsimulator-version-min"),
("watchos", "") => Some("-mwatchos-version-min"),
("watchos", "sim") => Some("-mwatchsimulator-version-min"),
// `-mxros-version-min` does not exist
// https://github.com/llvm/llvm-project/issues/88271
_ => None,
};
if let Some(version_flag) = version_flag {
cmd.args
.push(format!("{}={}", version_flag, min_version).into());
}

// AppleClang sometimes requires sysroot even on macOS
if cmd.is_xctoolchain_clang() || target.os != "macos" {
self.cargo_output.print_metadata(&format_args!(
"Detecting {:?} SDK path for {}",
target.os, sdk_details.sdk
target.os,
target.apple_sdk_name(),
));
let sdk_path = self.apple_sdk_root(&sdk_details.sdk)?;
let sdk_path = self.apple_sdk_root(&target)?;

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

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

fn apple_sdk_root(&self, sdk: &str) -> Result<Arc<OsStr>, Error> {
fn apple_sdk_root(&self, target: &TargetInfo) -> Result<Arc<OsStr>, Error> {
let sdk = target.apple_sdk_name();

if let Some(ret) = self
.apple_sdk_root_cache
.read()
Expand Down Expand Up @@ -3959,43 +3916,6 @@ fn fail(s: &str) -> ! {
std::process::exit(1);
}

struct AppleSdkTargetParts {
sdk_prefix: &'static str,
sim_prefix: &'static str,
sdk: Cow<'static, str>,
}

fn apple_os_sdk_parts(os: &str, arch: &AppleArchSpec) -> AppleSdkTargetParts {
let (sdk_prefix, sim_prefix) = match os {
"macos" => ("macosx", ""),
"ios" => ("iphone", "ios-"),
"watchos" => ("watch", "watch"),
"tvos" => ("appletv", "appletv"),
"visionos" => ("xr", "xr"),
os => unreachable!("unknown Apple OS {}", os),
};
let sdk = match arch {
AppleArchSpec::Device(_) if os == "macos" => Cow::Borrowed("macosx"),
AppleArchSpec::Device(_) => format!("{}os", sdk_prefix).into(),
AppleArchSpec::Simulator(_) => format!("{}simulator", sdk_prefix).into(),
AppleArchSpec::Catalyst(_) => Cow::Borrowed("macosx"),
};

AppleSdkTargetParts {
sdk_prefix,
sim_prefix,
sdk,
}
}

#[allow(dead_code)]
enum AppleArchSpec {
Device(&'static str),
Simulator(&'static str),
#[allow(dead_code)]
Catalyst(&'static str),
}

// Use by default minimum available API level
// See note about naming here
// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang
Expand Down
8 changes: 4 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ fn gnu_aarch64_none_no_pic() {
for target in &["aarch64-unknown-none-softfloat", "aarch64-unknown-none"] {
let test = Test::gnu();
test.gcc()
.target(&target)
.host(&target)
.target(target)
.host(target)
.file("foo.c")
.compile("foo");

Expand Down Expand Up @@ -629,8 +629,8 @@ fn clang_apple_mac_catalyst() {
"-iframework",
&format!("{sdkroot}/System/iOSSupport/System/Library/Frameworks"),
);
execution.must_have(&format!("-L{sdkroot}/System/iOSSupport/usr/lib"));
execution.must_have(&format!(
execution.must_have(format!("-L{sdkroot}/System/iOSSupport/usr/lib"));
execution.must_have(format!(
"-F{sdkroot}/System/iOSSupport/System/Library/Frameworks"
));
}
Expand Down

0 comments on commit 3b8d330

Please sign in to comment.