From 7fc91797d5deae23a93072ed7d92dfbe6dcd666c Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 21 Aug 2024 15:35:28 +0200 Subject: [PATCH] Apple: Add comments for `-platform_version` linker argument --- .../rustc_target/src/spec/base/apple/mod.rs | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_target/src/spec/base/apple/mod.rs b/compiler/rustc_target/src/spec/base/apple/mod.rs index aac2dc41da7bb..915b2b5356f1c 100644 --- a/compiler/rustc_target/src/spec/base/apple/mod.rs +++ b/compiler/rustc_target/src/spec/base/apple/mod.rs @@ -98,18 +98,6 @@ impl TargetAbi { } fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs { - let platform_name: StaticCow = match abi { - TargetAbi::Normal => os.into(), - TargetAbi::Simulator => format!("{os}-simulator").into(), - TargetAbi::MacCatalyst => "mac-catalyst".into(), - }; - - let min_version: StaticCow = { - let (major, minor, patch) = deployment_target(os, arch, abi); - format!("{major}.{minor}.{patch}").into() - }; - let sdk_version = min_version.clone(); - let mut args = LinkArgs::new(); // From the man page for ld64 (`man ld`): // > The linker accepts universal (multiple-architecture) input files, @@ -137,11 +125,46 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs { }; add_link_args(&mut args, LinkerFlavor::Darwin(Cc::No, Lld::No), &["-arch", ld_arch]); + // From the man page for ld64 (`man ld`): + // > This is set to indicate the platform, oldest supported version of + // > that platform that output is to be used on, and the SDK that the + // > output was built against. platform [...] may be one of the following + // > strings: + // > - macos + // > - ios + // > - tvos + // > - watchos + // > - bridgeos + // > - visionos + // > - xros + // > - mac-catalyst + // > - ios-simulator + // > - tvos-simulator + // > - watchos-simulator + // > - visionos-simulator + // > - xros-simulator + // > - driverkit + // + // Like with `-arch`, the linker can figure out the platform versions + // itself from the binaries being linked, but to be safe, we specify the + // desired versions here explicitly. + let platform_name: StaticCow = match abi { + TargetAbi::Normal => os.into(), + TargetAbi::Simulator => format!("{os}-simulator").into(), + TargetAbi::MacCatalyst => "mac-catalyst".into(), + }; + let min_version: StaticCow = { + let (major, minor, patch) = deployment_target(os, arch, abi); + format!("{major}.{minor}.{patch}").into() + }; + // Lie about the SDK version, we don't know it here + let sdk_version = min_version.clone(); add_link_args_iter( &mut args, LinkerFlavor::Darwin(Cc::No, Lld::No), ["-platform_version".into(), platform_name, min_version, sdk_version].into_iter(), ); + if abi != TargetAbi::MacCatalyst { add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-arch", ld_arch]); } else {