Skip to content

Commit

Permalink
Apple: Improve comments for -arch linker argument
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 3, 2024
1 parent d6c8169 commit 010cf51
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
39 changes: 29 additions & 10 deletions compiler/rustc_target/src/spec/base/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,40 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
};
let sdk_version = min_version.clone();

let mut args = TargetOptions::link_args(
LinkerFlavor::Darwin(Cc::No, Lld::No),
&["-arch", arch.target_name(), "-platform_version"],
);
let mut args = LinkArgs::new();
// From the man page for ld64 (`man ld`):
// > The linker accepts universal (multiple-architecture) input files,
// > but always creates a "thin" (single-architecture), standard Mach-O
// > output file. The architecture for the output file is specified using
// > the -arch option.
//
// The linker has heuristics to determine the desired architecture, but to
// be safe, and to avoid a warning, we set the architecture explicitly.
//
// Supported architecture names can be found in the source:
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
let ld_arch = match arch {
Armv7k => "armv7k",
Armv7s => "armv7s",
Arm64 => "arm64",
Arm64e => "arm64e",
Arm64_32 => "arm64_32",
// ld64 doesn't understand i686, so fall back to i386 instead
//
// Same story when linking with cc, since that ends up invoking ld64.
I386 | I686 => "i386",
X86_64 => "x86_64",
X86_64h => "x86_64h",
};
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::No, Lld::No), &["-arch", ld_arch]);

add_link_args_iter(
&mut args,
LinkerFlavor::Darwin(Cc::No, Lld::No),
[platform_name, min_version, sdk_version].into_iter(),
["-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", arch.target_name()],
);
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-arch", ld_arch]);
} else {
add_link_args_iter(
&mut args,
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_target/src/spec/targets/i686_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::spec::base::apple::{macos_llvm_target, opts, Arch, TargetAbi};
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, Target, TargetOptions};

pub(crate) fn target() -> Target {
// ld64 only understands i386 and not i686
let arch = Arch::I386;
let arch = Arch::I686;
let mut base = opts("macos", arch, TargetAbi::Normal);
base.max_atomic_width = Some(64);
base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-m32"]);
Expand All @@ -13,9 +12,7 @@ pub(crate) fn target() -> Target {
// Clang automatically chooses a more specific target based on
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
// correctly, we do too.
//
// While ld64 doesn't understand i686, LLVM does.
llvm_target: macos_llvm_target(Arch::I686).into(),
llvm_target: macos_llvm_target(arch).into(),
metadata: crate::spec::TargetMetadata {
description: Some("32-bit macOS (10.12+, Sierra+)".into()),
tier: Some(3),
Expand Down

0 comments on commit 010cf51

Please sign in to comment.