Skip to content

Commit 11b7ca3

Browse files
authored
Unrolled build for rust-lang#130435
Rollup merge of rust-lang#130435 - madsmtm:move-apple-link-args, r=petrochenkov Move Apple linker args from `rustc_target` to `rustc_codegen_ssa` They are dependent on the deployment target and SDK version, but having these in `rustc_target` makes it hard to introduce that dependency. Part of the work needed to do rust-lang#118204, see rust-lang#129342 for some discussion. Tested using: ```console ./x test tests/run-make/apple-deployment-target --target="aarch64-apple-darwin,aarch64-apple-ios,aarch64-apple-ios-macabi,aarch64-apple-ios-sim,aarch64-apple-tvos,aarch64-apple-tvos-sim,aarch64-apple-visionos,aarch64-apple-visionos-sim,aarch64-apple-watchos,aarch64-apple-watchos-sim,arm64_32-apple-watchos,armv7k-apple-watchos,armv7s-apple-ios,x86_64-apple-darwin,x86_64-apple-ios,x86_64-apple-ios-macabi,x86_64-apple-tvos,x86_64-apple-watchos-sim,x86_64h-apple-darwin" IPHONEOS_DEPLOYMENT_TARGET=10.0 ./x test tests/run-make/apple-deployment-target --target=i386-apple-ios ``` `arm64e-apple-darwin` and `arm64e-apple-ios` have not been tested, see rust-lang#130085, neither is `i686-apple-darwin`, since that requires using an x86_64 macbook, and I currently can't get mine to work, see rust-lang#130434. CC `@petrochenkov`
2 parents a3f76a2 + fb10eeb commit 11b7ca3

File tree

2 files changed

+134
-121
lines changed
  • compiler
    • rustc_codegen_ssa/src/back
    • rustc_target/src/spec/base/apple

2 files changed

+134
-121
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+132-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_target::spec::crt_objects::CrtObjects;
4040
use rustc_target::spec::{
4141
Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault, LinkerFeatures,
4242
LinkerFlavor, LinkerFlavorCli, Lld, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
43-
SplitDebuginfo,
43+
SplitDebuginfo, current_apple_deployment_target,
4444
};
4545
use tempfile::Builder as TempFileBuilder;
4646
use tracing::{debug, info, warn};
@@ -2405,6 +2405,8 @@ fn add_order_independent_options(
24052405
// Take care of the flavors and CLI options requesting the `lld` linker.
24062406
add_lld_args(cmd, sess, flavor, self_contained_components);
24072407

2408+
add_apple_link_args(cmd, sess, flavor);
2409+
24082410
let apple_sdk_root = add_apple_sdk(cmd, sess, flavor);
24092411

24102412
add_link_script(cmd, sess, tmpdir, crate_type);
@@ -2957,6 +2959,135 @@ pub(crate) fn are_upstream_rust_objects_already_included(sess: &Session) -> bool
29572959
}
29582960
}
29592961

2962+
/// We need to communicate four things to the linker on Apple/Darwin targets:
2963+
/// - The architecture.
2964+
/// - The operating system (and that it's an Apple platform).
2965+
/// - The deployment target.
2966+
/// - The environment / ABI.
2967+
fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
2968+
if !sess.target.is_like_osx {
2969+
return;
2970+
}
2971+
let LinkerFlavor::Darwin(cc, _) = flavor else {
2972+
return;
2973+
};
2974+
2975+
// `sess.target.arch` (`target_arch`) is not detailed enough.
2976+
let llvm_arch = sess.target.llvm_target.split_once('-').expect("LLVM target must have arch").0;
2977+
let target_os = &*sess.target.os;
2978+
let target_abi = &*sess.target.abi;
2979+
2980+
// The architecture name to forward to the linker.
2981+
//
2982+
// Supported architecture names can be found in the source:
2983+
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
2984+
//
2985+
// Intentially verbose to ensure that the list always matches correctly
2986+
// with the list in the source above.
2987+
let ld64_arch = match llvm_arch {
2988+
"armv7k" => "armv7k",
2989+
"armv7s" => "armv7s",
2990+
"arm64" => "arm64",
2991+
"arm64e" => "arm64e",
2992+
"arm64_32" => "arm64_32",
2993+
// ld64 doesn't understand i686, so fall back to i386 instead.
2994+
//
2995+
// Same story when linking with cc, since that ends up invoking ld64.
2996+
"i386" | "i686" => "i386",
2997+
"x86_64" => "x86_64",
2998+
"x86_64h" => "x86_64h",
2999+
_ => bug!("unsupported architecture in Apple target: {}", sess.target.llvm_target),
3000+
};
3001+
3002+
if cc == Cc::No {
3003+
// From the man page for ld64 (`man ld`):
3004+
// > The linker accepts universal (multiple-architecture) input files,
3005+
// > but always creates a "thin" (single-architecture), standard
3006+
// > Mach-O output file. The architecture for the output file is
3007+
// > specified using the -arch option.
3008+
//
3009+
// The linker has heuristics to determine the desired architecture,
3010+
// but to be safe, and to avoid a warning, we set the architecture
3011+
// explicitly.
3012+
cmd.link_args(&["-arch", ld64_arch]);
3013+
3014+
// Man page says that ld64 supports the following platform names:
3015+
// > - macos
3016+
// > - ios
3017+
// > - tvos
3018+
// > - watchos
3019+
// > - bridgeos
3020+
// > - visionos
3021+
// > - xros
3022+
// > - mac-catalyst
3023+
// > - ios-simulator
3024+
// > - tvos-simulator
3025+
// > - watchos-simulator
3026+
// > - visionos-simulator
3027+
// > - xros-simulator
3028+
// > - driverkit
3029+
let platform_name = match (target_os, target_abi) {
3030+
(os, "") => os,
3031+
("ios", "macabi") => "mac-catalyst",
3032+
("ios", "sim") => "ios-simulator",
3033+
("tvos", "sim") => "tvos-simulator",
3034+
("watchos", "sim") => "watchos-simulator",
3035+
("visionos", "sim") => "visionos-simulator",
3036+
_ => bug!("invalid OS/ABI combination for Apple target: {target_os}, {target_abi}"),
3037+
};
3038+
3039+
let (major, minor, patch) = current_apple_deployment_target(&sess.target);
3040+
let min_version = format!("{major}.{minor}.{patch}");
3041+
3042+
// Lie about the SDK version, we don't know it here
3043+
let sdk_version = &*min_version;
3044+
3045+
// From the man page for ld64 (`man ld`):
3046+
// > This is set to indicate the platform, oldest supported version of
3047+
// > that platform that output is to be used on, and the SDK that the
3048+
// > output was built against.
3049+
//
3050+
// Like with `-arch`, the linker can figure out the platform versions
3051+
// itself from the binaries being linked, but to be safe, we specify
3052+
// the desired versions here explicitly.
3053+
cmd.link_args(&["-platform_version", platform_name, &*min_version, sdk_version]);
3054+
} else {
3055+
// cc == Cc::Yes
3056+
// We'd _like_ to use `-target` everywhere, since that can uniquely
3057+
// communicate all the required details, but that doesn't work on GCC,
3058+
// and since we don't know whether the `cc` compiler is Clang, GCC, or
3059+
// something else, we fall back to other options that also work on GCC
3060+
// when compiling for macOS.
3061+
//
3062+
// Targets other than macOS are ill-supported by GCC (it doesn't even
3063+
// support e.g. `-miphoneos-version-min`), so in those cases we can
3064+
// fairly safely use `-target`. See also the following, where it is
3065+
// made explicit that the recommendation by LLVM developers is to use
3066+
// `-target`: <https://github.com/llvm/llvm-project/issues/88271>
3067+
if target_os == "macos" {
3068+
// `-arch` communicates the architecture.
3069+
//
3070+
// CC forwards the `-arch` to the linker, so we use the same value
3071+
// here intentionally.
3072+
cmd.cc_args(&["-arch", ld64_arch]);
3073+
3074+
// The presence of `-mmacosx-version-min` makes CC default to
3075+
// macOS, and it sets the deployment target.
3076+
let (major, minor, patch) = current_apple_deployment_target(&sess.target);
3077+
// Intentionally pass this as a single argument, Clang doesn't
3078+
// seem to like it otherwise.
3079+
cmd.cc_arg(&format!("-mmacosx-version-min={major}.{minor}.{patch}"));
3080+
3081+
// macOS has no environment, so with these two, we've told CC the
3082+
// four desired parameters.
3083+
//
3084+
// We avoid `-m32`/`-m64`, as this is already encoded by `-arch`.
3085+
} else {
3086+
cmd.cc_args(&["-target", &sess.target.llvm_target]);
3087+
}
3088+
}
3089+
}
3090+
29603091
fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) -> Option<PathBuf> {
29613092
let arch = &sess.target.arch;
29623093
let os = &sess.target.os;

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

+2-120
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::env;
33
use std::num::ParseIntError;
44

55
use crate::spec::{
6-
Cc, DebuginfoKind, FramePointer, LinkArgs, LinkerFlavor, Lld, SplitDebuginfo, StackProbeType,
7-
StaticCow, Target, TargetOptions, add_link_args, add_link_args_iter, cvs,
6+
Cc, DebuginfoKind, FramePointer, LinkerFlavor, Lld, SplitDebuginfo, StackProbeType, StaticCow,
7+
Target, TargetOptions, cvs,
88
};
99

1010
#[cfg(test)]
@@ -40,25 +40,6 @@ impl Arch {
4040
}
4141
}
4242

43-
/// The architecture name to forward to the linker.
44-
fn ld_arch(self) -> &'static str {
45-
// Supported architecture names can be found in the source:
46-
// https://github.com/apple-oss-distributions/ld64/blob/ld64-951.9/src/abstraction/MachOFileAbstraction.hpp#L578-L648
47-
match self {
48-
Armv7k => "armv7k",
49-
Armv7s => "armv7s",
50-
Arm64 => "arm64",
51-
Arm64e => "arm64e",
52-
Arm64_32 => "arm64_32",
53-
// ld64 doesn't understand i686, so fall back to i386 instead
54-
//
55-
// Same story when linking with cc, since that ends up invoking ld64.
56-
I386 | I686 => "i386",
57-
X86_64 => "x86_64",
58-
X86_64h => "x86_64h",
59-
}
60-
}
61-
6243
pub(crate) fn target_arch(self) -> Cow<'static, str> {
6344
Cow::Borrowed(match self {
6445
Armv7k | Armv7s => "arm",
@@ -116,104 +97,6 @@ impl TargetAbi {
11697
}
11798
}
11899

119-
fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
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-
let mut args =
129-
TargetOptions::link_args(LinkerFlavor::Darwin(Cc::No, Lld::No), &["-arch", arch.ld_arch()]);
130-
131-
// From the man page for ld64 (`man ld`):
132-
// > This is set to indicate the platform, oldest supported version of
133-
// > that platform that output is to be used on, and the SDK that the
134-
// > output was built against. platform [...] may be one of the following
135-
// > strings:
136-
// > - macos
137-
// > - ios
138-
// > - tvos
139-
// > - watchos
140-
// > - bridgeos
141-
// > - visionos
142-
// > - xros
143-
// > - mac-catalyst
144-
// > - ios-simulator
145-
// > - tvos-simulator
146-
// > - watchos-simulator
147-
// > - visionos-simulator
148-
// > - xros-simulator
149-
// > - driverkit
150-
//
151-
// Like with `-arch`, the linker can figure out the platform versions
152-
// itself from the binaries being linked, but to be safe, we specify the
153-
// desired versions here explicitly.
154-
let platform_name: StaticCow<str> = match abi {
155-
TargetAbi::Normal => os.into(),
156-
TargetAbi::Simulator => format!("{os}-simulator").into(),
157-
TargetAbi::MacCatalyst => "mac-catalyst".into(),
158-
};
159-
let min_version: StaticCow<str> = {
160-
let (major, minor, patch) = deployment_target(os, arch, abi);
161-
format!("{major}.{minor}.{patch}").into()
162-
};
163-
// Lie about the SDK version, we don't know it here
164-
let sdk_version = min_version.clone();
165-
add_link_args_iter(
166-
&mut args,
167-
LinkerFlavor::Darwin(Cc::No, Lld::No),
168-
["-platform_version".into(), platform_name, min_version, sdk_version].into_iter(),
169-
);
170-
171-
// We need to communicate four things to the C compiler to be able to link:
172-
// - The architecture.
173-
// - The operating system (and that it's an Apple platform).
174-
// - The deployment target.
175-
// - The environment / ABI.
176-
//
177-
// We'd like to use `-target` everywhere, since that can uniquely
178-
// communicate all of these, but that doesn't work on GCC, and since we
179-
// don't know whether the `cc` compiler is Clang, GCC, or something else,
180-
// we fall back to other options that also work on GCC when compiling for
181-
// macOS.
182-
//
183-
// Targets other than macOS are ill-supported by GCC (it doesn't even
184-
// support e.g. `-miphoneos-version-min`), so in those cases we can fairly
185-
// safely use `-target`. See also the following, where it is made explicit
186-
// that the recommendation by LLVM developers is to use `-target`:
187-
// <https://github.com/llvm/llvm-project/issues/88271>
188-
if os == "macos" {
189-
// `-arch` communicates the architecture.
190-
//
191-
// CC forwards the `-arch` to the linker, so we use the same value
192-
// here intentionally.
193-
add_link_args(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), &[
194-
"-arch",
195-
arch.ld_arch(),
196-
]);
197-
// The presence of `-mmacosx-version-min` makes CC default to macOS,
198-
// and it sets the deployment target.
199-
let (major, minor, patch) = deployment_target(os, arch, abi);
200-
let opt = format!("-mmacosx-version-min={major}.{minor}.{patch}").into();
201-
add_link_args_iter(&mut args, LinkerFlavor::Darwin(Cc::Yes, Lld::No), [opt].into_iter());
202-
// macOS has no environment, so with these two, we've told CC all the
203-
// desired parameters.
204-
//
205-
// We avoid `-m32`/`-m64`, as this is already encoded by `-arch`.
206-
} else {
207-
add_link_args_iter(
208-
&mut args,
209-
LinkerFlavor::Darwin(Cc::Yes, Lld::No),
210-
["-target".into(), llvm_target(os, arch, abi)].into_iter(),
211-
);
212-
}
213-
214-
args
215-
}
216-
217100
/// Get the base target options, LLVM target and `target_arch` from the three
218101
/// things that uniquely identify Rust's Apple targets: The OS, the
219102
/// architecture, and the ABI.
@@ -232,7 +115,6 @@ pub(crate) fn base(
232115
// macOS has -dead_strip, which doesn't rely on function_sections
233116
function_sections: false,
234117
dynamic_linking: true,
235-
pre_link_args: pre_link_args(os, arch, abi),
236118
families: cvs!["unix"],
237119
is_like_osx: true,
238120
// LLVM notes that macOS 10.11+ and iOS 9+ default

0 commit comments

Comments
 (0)