-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve linker detection for ld64.lld for Apple devices #86064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1357ec4
820e8d2
4caeb6a
88d7494
31f4ec7
dae13ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1205,7 +1205,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { | |
LinkerFlavor::Ld | ||
} else if stem == "link" || stem == "lld-link" { | ||
LinkerFlavor::Msvc | ||
} else if stem == "lld" || stem == "rust-lld" { | ||
} else if stem == "lld" || stem == "ld64" || stem == "rust-lld" { | ||
LinkerFlavor::Lld(sess.target.lld_flavor) | ||
} else { | ||
// fall back to the value in the target spec | ||
|
@@ -1905,6 +1905,10 @@ fn add_order_independent_options( | |
) { | ||
add_apple_sdk(cmd, sess, flavor); | ||
|
||
// NO-OPT-OUT, OBJECT-FILES-NO | ||
add_apple_platform_version(cmd, sess, flavor); | ||
|
||
// NO-OPT-OUT | ||
add_link_script(cmd, sess, tmpdir, crate_type); | ||
|
||
if sess.target.is_like_fuchsia && crate_type == CrateType::Executable { | ||
|
@@ -2183,7 +2187,11 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( | |
|
||
// Converts a library file-stem into a cc -l argument | ||
fn unlib<'a>(target: &Target, stem: &'a str) -> &'a str { | ||
if stem.starts_with("lib") && !target.is_like_windows { &stem[3..] } else { stem } | ||
if stem.starts_with("lib") && !target.is_like_windows { | ||
&stem[3..] | ||
} else { | ||
stem | ||
} | ||
} | ||
|
||
// Adds the static "rlib" versions of all crates to the command line. | ||
|
@@ -2504,3 +2512,59 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> { | |
Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)), | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The LLVM linker requires the platform_version option. The Apple linker also supports it. |
||
fn add_apple_platform_version(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) { | ||
let arch = &sess.target.arch; | ||
let os = &sess.target.os; | ||
let llvm_target = &sess.target.llvm_target; | ||
if sess.target.vendor != "apple" || flavor != LinkerFlavor::Lld(LldFlavor::Ld64) { | ||
return; | ||
} | ||
let sdk_name = match (arch.as_str(), os.as_str()) { | ||
("aarch64", "tvos") => "appletvos", | ||
("x86_64", "tvos") => "appletvsimulator", | ||
("arm", "ios") => "iphoneos", | ||
("aarch64", "ios") if llvm_target.contains("macabi") => "macosx", | ||
("aarch64", "ios") if llvm_target.contains("sim") => "iphonesimulator", | ||
("aarch64", "ios") => "iphoneos", | ||
("x86", "ios") => "iphonesimulator", | ||
("x86_64", "ios") if llvm_target.contains("macabi") => "macosx", | ||
("x86_64", "ios") => "iphonesimulator", | ||
(_, "macos") => "macosx", | ||
_ => { | ||
sess.err(&format!("unsupported arch `{}` for os `{}`", arch, os)); | ||
return; | ||
} | ||
}; | ||
let platform_version = match get_apple_platform_version(sdk_name) { | ||
Ok(s) => s, | ||
Err(e) => { | ||
sess.err(&e); | ||
return; | ||
} | ||
}; | ||
|
||
cmd.args(&["-platform_version", os, "10.7", &platform_version]); | ||
} | ||
|
||
fn get_apple_platform_version(sdk_name: &str) -> Result<String, String> { | ||
let res = Command::new("xcrun") | ||
.arg("--show-sdk-platform-version") | ||
.arg("-sdk") | ||
.arg(sdk_name) | ||
.output() | ||
.and_then(|output| { | ||
if output.status.success() { | ||
Ok(String::from_utf8(output.stdout).unwrap()) | ||
} else { | ||
let error = String::from_utf8(output.stderr); | ||
let error = format!("process exit with error: {}", error.unwrap()); | ||
Err(io::Error::new(io::ErrorKind::Other, &error[..])) | ||
} | ||
}); | ||
|
||
match res { | ||
Ok(output) => Ok(output.trim().to_string()), | ||
Err(e) => Err(format!("failed to get {} SDK platform version: {}", sdk_name, e)), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::spec::{LinkerFlavor, SanitizerSet, StackProbeType, Target, TargetOptions}; | ||
use crate::spec::{LinkerFlavor, LldFlavor, SanitizerSet, StackProbeType, Target, TargetOptions}; | ||
|
||
pub fn target() -> Target { | ||
let mut base = super::apple_base::opts("macos"); | ||
|
@@ -9,6 +9,10 @@ pub fn target() -> Target { | |
LinkerFlavor::Gcc, | ||
vec!["-m64".to_string(), "-arch".to_string(), "x86_64".to_string()], | ||
); | ||
base.pre_link_args.insert( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be needed for other Apple devices, but I have no hardware. |
||
LinkerFlavor::Lld(LldFlavor::Ld64), | ||
vec!["-arch".to_string(), "x86_64".to_string()], | ||
); | ||
base.link_env_remove.extend(super::apple_base::macos_link_env_remove()); | ||
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved | ||
base.stack_probes = StackProbeType::Call; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was by rustfmt.