Skip to content

Commit 5b0e086

Browse files
committed
Apple: Improve comments for -arch linker argument
1 parent 45fbf41 commit 5b0e086

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

compiler/rustc_target/src/spec/base/apple/mod.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,40 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
116116
};
117117
let sdk_version = min_version.clone();
118118

119-
let mut args = TargetOptions::link_args(
120-
LinkerFlavor::Darwin(Cc::No, Lld::No),
121-
&["-arch", arch.target_name(), "-platform_version"],
122-
);
119+
let mut args = LinkArgs::new();
120+
// From the man page for ld64 (`man ld`):
121+
// > The linker accepts universal (multiple-architecture) input files,
122+
// > but always creates a "thin" (single-architecture), standard Mach-O
123+
// > output file. The architecture for the output file is specified using
124+
// > the -arch option.
125+
//
126+
// The linker has heuristics to determine the desired architecture, but to
127+
// be safe, and to avoid a warning, we set the architecture explicitly.
128+
//
129+
// Supported architecture names can be found in the source:
130+
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
131+
let ld_arch = match arch {
132+
Armv7k => "armv7k",
133+
Armv7s => "armv7s",
134+
Arm64 => "arm64",
135+
Arm64e => "arm64e",
136+
Arm64_32 => "arm64_32",
137+
// ld64 doesn't understand i686, so fall back to i386 instead
138+
//
139+
// Same story when linking with cc, since that ends up invoking ld64.
140+
I386 | I686 => "i386",
141+
X86_64 => "x86_64",
142+
X86_64h => "x86_64h",
143+
};
144+
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::No, Lld::No), &["-arch", ld_arch]);
145+
123146
add_link_args_iter(
124147
&mut args,
125148
LinkerFlavor::Darwin(Cc::No, Lld::No),
126-
[platform_name, min_version, sdk_version].into_iter(),
149+
["-platform_version".into(), platform_name, min_version, sdk_version].into_iter(),
127150
);
128151
if abi != TargetAbi::MacCatalyst {
129-
add_link_args(
130-
&mut args,
131-
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
132-
&["-arch", arch.target_name()],
133-
);
152+
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-arch", ld_arch]);
134153
} else {
135154
add_link_args_iter(
136155
&mut args,

compiler/rustc_target/src/spec/targets/i686_apple_darwin.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::spec::base::apple::{macos_llvm_target, opts, Arch, TargetAbi};
22
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, Target, TargetOptions};
33

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

0 commit comments

Comments
 (0)