Skip to content
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

Improve linker detection for ld64.lld for Apple devices #86064

Closed
wants to merge 6 commits into from
Closed

Improve linker detection for ld64.lld for Apple devices #86064

wants to merge 6 commits into from

Conversation

tschuett
Copy link

@tschuett tschuett commented Jun 6, 2021

Other Apple devices will need a similar solution, but I have no hardware.

see #85938

I can now build a large workspace with 300+ crates.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @davidtwco (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 6, 2021
@tschuett tschuett changed the title Improve linker detection for ld64 for Apple devices Improve linker detection for ld64.lld for Apple devices Jun 6, 2021
@bors
Copy link
Contributor

bors commented Jun 6, 2021

☔ The latest upstream changes (presumably #85086) made this pull request unmergeable. Please resolve the merge conflicts.

@@ -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)),
}
}

Copy link
Author

Choose a reason for hiding this comment

The 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.

@@ -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(
Copy link
Author

@tschuett tschuett Jun 7, 2021

Choose a reason for hiding this comment

The 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.

@@ -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 {
Copy link
Author

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.

@@ -231,7 +231,9 @@ impl<'a> GccLinker<'a> {
fn build_dylib(&mut self, out_filename: &Path) {
// On mac we need to tell the linker to let this library be rpathed
if self.sess.target.is_like_osx {
self.cmd.arg("-dynamiclib");
if self.sess.target.lld_flavor != LldFlavor::Ld64 {
self.cmd.arg("-dynamiclib");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems be only needed for gcc. -dynamiclib is an gcc option.

};

let path = format!("{}/usr/lib", &sdk_path);
cmd.args(&["-syslibroot", &sdk_path]);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an implementation of -isysroot for ld64.

@petrochenkov petrochenkov self-assigned this Jun 7, 2021
@petrochenkov
Copy link
Contributor

@tschuett
From #85938:

First issue, rust appends -Wl, which makes the linker unhappy.

rustc doesn't generally support linking with a "naked" linker (with exception of bare-metal platforms, cc #11937), and lld is supposed to be used indirectly through the -fuse-ld=lld option to C compiler.

This PR attempts to add support for linking directly with ld64 for Apple targets, which I'm not sure is something desirable.

@tschuett
Copy link
Author

tschuett commented Jun 7, 2021

How do I get cargo to use ld64.lld?

@petrochenkov
Copy link
Contributor

In other words, -Clinker ld64.lld may be an intuitive way to switch linker to LLD, but it's not correct, the working way is -Clink-arg -fuse-ld=lld (although I didn't personally try it on Apple).

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
configure: rust.channel         := nightly
configure: rust.debug-assertions := True
configure: llvm.assertions      := True
configure: dist.missing-tools   := True
configure: build.configure-args := ['--enable-sccache', '--disable-manage-submodu ...
configure: writing `config.toml` in current directory
configure: 
configure: run `python /checkout/x.py --help`
configure: 
---
skip untracked path cpu-usage.csv during rustfmt invocations
skip untracked path src/doc/book/ during rustfmt invocations
skip untracked path src/doc/rust-by-example/ during rustfmt invocations
skip untracked path src/llvm-project/ during rustfmt invocations
Diff in /checkout/compiler/rustc_codegen_ssa/src/back/link.rs at line 2187:
 
     // 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..]
-            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.
Running `"/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/rustfmt" "--config-path" "/checkout" "--edition" "2018" "--unstable-features" "--skip-children" "--check" "/checkout/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs" "/checkout/compiler/rustc_codegen_ssa/src/back/metadata.rs" "/checkout/compiler/rustc_codegen_ssa/src/back/link.rs" "/checkout/compiler/rustc_codegen_ssa/src/common.rs" "/checkout/compiler/rustc_lint/src/methods.rs" "/checkout/compiler/rustc_codegen_ssa/src/back/archive.rs" "/checkout/compiler/rustc_lint/src/early.rs" "/checkout/compiler/rustc_codegen_ssa/src/back/command.rs"` failed.
If you're running `tidy`, try again with `--bless`. Or, if you just want to format code, run `./x.py fmt` instead.
Build completed unsuccessfully in 0:00:18

@tschuett
Copy link
Author

tschuett commented Jun 7, 2021

Indeed

RUSTFLAGS="-C link-arg=-fuse-ld=/..../ld64.lld" cargo build

succeeds.

@tschuett tschuett closed this Jun 7, 2021
@petrochenkov petrochenkov mentioned this pull request Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants