@@ -39,6 +39,25 @@ impl Arch {
39
39
}
40
40
}
41
41
42
+ /// The architecture name to forward to the linker.
43
+ fn ld_arch ( self ) -> & ' static str {
44
+ // Supported architecture names can be found in the source:
45
+ // https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
46
+ match arch {
47
+ Armv7k => "armv7k" ,
48
+ Armv7s => "armv7s" ,
49
+ Arm64 => "arm64" ,
50
+ Arm64e => "arm64e" ,
51
+ Arm64_32 => "arm64_32" ,
52
+ // ld64 doesn't understand i686, so fall back to i386 instead
53
+ //
54
+ // Same story when linking with cc, since that ends up invoking ld64.
55
+ I386 | I686 => "i386" ,
56
+ X86_64 => "x86_64" ,
57
+ X86_64h => "x86_64h" ,
58
+ }
59
+ }
60
+
42
61
pub ( crate ) fn target_arch ( self ) -> Cow < ' static , str > {
43
62
Cow :: Borrowed ( match self {
44
63
Armv7k | Armv7s => "arm" ,
@@ -116,21 +135,28 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
116
135
} ;
117
136
let sdk_version = min_version. clone ( ) ;
118
137
138
+ // From the man page for ld64 (`man ld`):
139
+ // > The linker accepts universal (multiple-architecture) input files,
140
+ // > but always creates a "thin" (single-architecture), standard Mach-O
141
+ // > output file. The architecture for the output file is specified using
142
+ // > the -arch option.
143
+ //
144
+ // The linker has heuristics to determine the desired architecture, but to
145
+ // be safe, and to avoid a warning, we set the architecture explicitly.
119
146
let mut args = TargetOptions :: link_args (
120
147
LinkerFlavor :: Darwin ( Cc :: No , Lld :: No ) ,
121
- & [ "-arch" , arch. target_name ( ) , "-platform_version" ] ,
148
+ & [ "-arch" , arch. ld_arch ( ) ] ,
122
149
) ;
150
+
123
151
add_link_args_iter (
124
152
& mut args,
125
153
LinkerFlavor :: Darwin ( Cc :: No , Lld :: No ) ,
126
- [ platform_name, min_version, sdk_version] . into_iter ( ) ,
154
+ [ "-platform_version" . into ( ) , platform_name, min_version, sdk_version] . into_iter ( ) ,
127
155
) ;
128
156
if abi != TargetAbi :: MacCatalyst {
129
- add_link_args (
130
- & mut args,
131
- LinkerFlavor :: Darwin ( Cc :: Yes , Lld :: No ) ,
132
- & [ "-arch" , arch. target_name ( ) ] ,
133
- ) ;
157
+ // CC forwards the `-arch` to the linker, so we use the same value
158
+ // here intentionally.
159
+ add_link_args ( & mut args, LinkerFlavor :: Darwin ( Cc :: Yes , Lld :: No ) , & [ "-arch" , arch. ld_arch ( ) ] ) ;
134
160
} else {
135
161
add_link_args_iter (
136
162
& mut args,
0 commit comments