Skip to content

Commit d0d2f60

Browse files
Rollup merge of #100286 - Thog:rust-lld-macosx-target, r=petrochenkov
Add support for link-flavor rust-lld for macOS Also refactor iOS, watchOS and tvOS common code. The ``-arch`` argument was moved to the ``apple_base`` module instead of the target definitions for macOS. As ld64 requires ``-syslibroot`` to be passed, ``add_apple_sdk`` was modified accordingly.
2 parents 1603a70 + a725250 commit d0d2f60

File tree

6 files changed

+66
-56
lines changed

6 files changed

+66
-56
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2674,11 +2674,16 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
26742674
let os = &sess.target.os;
26752675
let llvm_target = &sess.target.llvm_target;
26762676
if sess.target.vendor != "apple"
2677-
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos")
2677+
|| !matches!(os.as_ref(), "ios" | "tvos" | "watchos" | "macos")
26782678
|| (flavor != LinkerFlavor::Gcc && flavor != LinkerFlavor::Lld(LldFlavor::Ld64))
26792679
{
26802680
return;
26812681
}
2682+
2683+
if os == "macos" && flavor != LinkerFlavor::Lld(LldFlavor::Ld64) {
2684+
return;
2685+
}
2686+
26822687
let sdk_name = match (arch.as_ref(), os.as_ref()) {
26832688
("aarch64", "tvos") => "appletvos",
26842689
("x86_64", "tvos") => "appletvsimulator",
@@ -2694,6 +2699,7 @@ fn add_apple_sdk(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
26942699
("aarch64", "watchos") if llvm_target.ends_with("-simulator") => "watchsimulator",
26952700
("aarch64", "watchos") => "watchos",
26962701
("arm", "watchos") => "watchos",
2702+
(_, "macos") => "macosx",
26972703
_ => {
26982704
sess.err(&format!("unsupported arch `{}` for os `{}`", arch, os));
26992705
return;

compiler/rustc_target/src/spec/aarch64_apple_darwin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use crate::spec::{FramePointer, LinkerFlavor, SanitizerSet, Target, TargetOptions};
1+
use crate::spec::{FramePointer, SanitizerSet, Target, TargetOptions};
22

33
pub fn target() -> Target {
4-
let mut base = super::apple_base::opts("macos");
4+
let arch = "arm64";
5+
let mut base = super::apple_base::opts("macos", arch, "");
56
base.cpu = "apple-a14".into();
67
base.max_atomic_width = Some(128);
78

89
// FIXME: The leak sanitizer currently fails the tests, see #88132.
910
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD;
1011

11-
base.add_pre_link_args(LinkerFlavor::Gcc, &["-arch", "arm64"]);
1212
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
1313

1414
// Clang automatically chooses a more specific target based on
1515
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
1616
// correctly, we do too.
17-
let llvm_target = super::apple_base::macos_llvm_target("arm64");
17+
let llvm_target = super::apple_base::macos_llvm_target(arch);
1818

1919
Target {
2020
llvm_target: llvm_target.into(),

compiler/rustc_target/src/spec/apple_base.rs

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
use std::{borrow::Cow, env};
22

3-
use crate::spec::{cvs, FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{cvs, FramePointer, SplitDebuginfo, TargetOptions};
4+
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor};
5+
6+
fn pre_link_args(os: &'static str, arch: &'static str, abi: &'static str) -> LinkArgs {
7+
let mut args = LinkArgs::new();
8+
9+
let platform_name = match abi {
10+
"sim" => format!("{}-simulator", os),
11+
"macabi" => "mac-catalyst".to_string(),
12+
_ => os.to_string(),
13+
};
14+
15+
let platform_version = match os.as_ref() {
16+
"ios" => ios_lld_platform_version(),
17+
"tvos" => tvos_lld_platform_version(),
18+
"watchos" => watchos_lld_platform_version(),
19+
"macos" => macos_lld_platform_version(arch),
20+
_ => unreachable!(),
21+
};
22+
23+
if abi != "macabi" {
24+
args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), arch.into()]);
25+
}
26+
27+
args.insert(
28+
LinkerFlavor::Lld(LldFlavor::Ld64),
29+
vec![
30+
"-arch".into(),
31+
arch.into(),
32+
"-platform_version".into(),
33+
platform_name.into(),
34+
platform_version.clone().into(),
35+
platform_version.into(),
36+
],
37+
);
38+
39+
args
40+
}
441

5-
pub fn opts(os: &'static str) -> TargetOptions {
42+
pub fn opts(os: &'static str, arch: &'static str, abi: &'static str) -> TargetOptions {
643
// ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6
744
// either the linker will complain if it is used or the binary will end up
845
// segfaulting at runtime when run on 10.6. Rust by default supports macOS
@@ -24,6 +61,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
2461
// macOS has -dead_strip, which doesn't rely on function_sections
2562
function_sections: false,
2663
dynamic_linking: true,
64+
pre_link_args: pre_link_args(os, arch, abi),
2765
linker_is_gnu: false,
2866
families: cvs!["unix"],
2967
is_like_osx: true,
@@ -73,6 +111,11 @@ fn macos_deployment_target(arch: &str) -> (u32, u32) {
73111
.unwrap_or_else(|| macos_default_deployment_target(arch))
74112
}
75113

114+
fn macos_lld_platform_version(arch: &str) -> String {
115+
let (major, minor) = macos_deployment_target(arch);
116+
format!("{}.{}", major, minor)
117+
}
118+
76119
pub fn macos_llvm_target(arch: &str) -> String {
77120
let (major, minor) = macos_deployment_target(arch);
78121
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
@@ -109,7 +152,7 @@ pub fn ios_llvm_target(arch: &str) -> String {
109152
format!("{}-apple-ios{}.{}.0", arch, major, minor)
110153
}
111154

112-
pub fn ios_lld_platform_version() -> String {
155+
fn ios_lld_platform_version() -> String {
113156
let (major, minor) = ios_deployment_target();
114157
format!("{}.{}", major, minor)
115158
}
@@ -123,7 +166,7 @@ fn tvos_deployment_target() -> (u32, u32) {
123166
deployment_target("TVOS_DEPLOYMENT_TARGET").unwrap_or((7, 0))
124167
}
125168

126-
pub fn tvos_lld_platform_version() -> String {
169+
fn tvos_lld_platform_version() -> String {
127170
let (major, minor) = tvos_deployment_target();
128171
format!("{}.{}", major, minor)
129172
}
@@ -132,7 +175,7 @@ fn watchos_deployment_target() -> (u32, u32) {
132175
deployment_target("WATCHOS_DEPLOYMENT_TARGET").unwrap_or((5, 0))
133176
}
134177

135-
pub fn watchos_lld_platform_version() -> String {
178+
fn watchos_lld_platform_version() -> String {
136179
let (major, minor) = watchos_deployment_target();
137180
format!("{}.{}", major, minor)
138181
}

compiler/rustc_target/src/spec/apple_sdk_base.rs

+2-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{cvs, LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
1+
use crate::spec::{cvs, TargetOptions};
22
use std::borrow::Cow;
33

44
use Arch::*;
@@ -61,53 +61,13 @@ fn link_env_remove(arch: Arch) -> Cow<'static, [Cow<'static, str>]> {
6161
}
6262
}
6363

64-
fn pre_link_args(os: &'static str, arch: Arch) -> LinkArgs {
65-
let mut args = LinkArgs::new();
66-
67-
let target_abi = target_abi(arch);
68-
69-
let platform_name = match target_abi {
70-
"sim" => format!("{}-simulator", os),
71-
"macabi" => "mac-catalyst".to_string(),
72-
_ => os.to_string(),
73-
};
74-
75-
let platform_version = match os.as_ref() {
76-
"ios" => super::apple_base::ios_lld_platform_version(),
77-
"tvos" => super::apple_base::tvos_lld_platform_version(),
78-
"watchos" => super::apple_base::watchos_lld_platform_version(),
79-
_ => unreachable!(),
80-
};
81-
82-
let arch_str = target_arch_name(arch);
83-
84-
if target_abi != "macabi" {
85-
args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), arch_str.into()]);
86-
}
87-
88-
args.insert(
89-
LinkerFlavor::Lld(LldFlavor::Ld64),
90-
vec![
91-
"-arch".into(),
92-
arch_str.into(),
93-
"-platform_version".into(),
94-
platform_name.into(),
95-
platform_version.clone().into(),
96-
platform_version.into(),
97-
],
98-
);
99-
100-
args
101-
}
102-
10364
pub fn opts(os: &'static str, arch: Arch) -> TargetOptions {
10465
TargetOptions {
10566
abi: target_abi(arch).into(),
10667
cpu: target_cpu(arch).into(),
10768
dynamic_linking: false,
108-
pre_link_args: pre_link_args(os, arch),
10969
link_env_remove: link_env_remove(arch),
11070
has_thread_local: false,
111-
..super::apple_base::opts(os)
71+
..super::apple_base::opts(os, target_arch_name(arch), target_abi(arch))
11272
}
11373
}

compiler/rustc_target/src/spec/i686_apple_darwin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::spec::{FramePointer, LinkerFlavor, StackProbeType, Target, TargetOptions};
22

33
pub fn target() -> Target {
4-
let mut base = super::apple_base::opts("macos");
4+
// ld64 only understand i386 and not i686
5+
let mut base = super::apple_base::opts("macos", "i386", "");
56
base.cpu = "yonah".into();
67
base.max_atomic_width = Some(64);
78
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);

compiler/rustc_target/src/spec/x86_64_apple_darwin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use crate::spec::TargetOptions;
22
use crate::spec::{FramePointer, LinkerFlavor, SanitizerSet, StackProbeType, Target};
33

44
pub fn target() -> Target {
5-
let mut base = super::apple_base::opts("macos");
5+
let arch = "x86_64";
6+
let mut base = super::apple_base::opts("macos", arch, "");
67
base.cpu = "core2".into();
78
base.max_atomic_width = Some(128); // core2 support cmpxchg16b
89
base.frame_pointer = FramePointer::Always;
9-
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m64", "-arch", "x86_64"]);
10+
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m64"]);
1011
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
1112
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
1213
base.stack_probes = StackProbeType::Call;
@@ -16,7 +17,6 @@ pub fn target() -> Target {
1617
// Clang automatically chooses a more specific target based on
1718
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
1819
// correctly, we do too.
19-
let arch = "x86_64";
2020
let llvm_target = super::apple_base::macos_llvm_target(&arch);
2121

2222
Target {

0 commit comments

Comments
 (0)