Skip to content

Commit ee56ca2

Browse files
committed
Link LLVM dynamically on aarch64-apple-darwin
1 parent e8e909c commit ee56ca2

5 files changed

Lines changed: 41 additions & 4 deletions

File tree

compiler/rustc_llvm/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ fn main() {
411411
continue;
412412
}
413413

414+
// On apple-darwin, llvm-config reports the versioned shared library name such as LLVM-22-..., but
415+
// the distributed toolchain ships libLLVM.dylib. Normalize the link name here.
416+
let name =
417+
if target.contains("apple-darwin") && llvm_kind == "dylib" && name.starts_with("LLVM-")
418+
{
419+
"LLVM"
420+
} else {
421+
name
422+
};
423+
414424
let kind = if name.starts_with("LLVM") {
415425
llvm_kind
416426
} else if is_static {

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ pub fn walk_native_lib_search_dirs<R>(
6262
f(&sess.target_tlib_path.dir.join("self-contained"), false)?;
6363
}
6464

65+
let has_shared_llvm_apple_darwin =
66+
sess.target.is_like_darwin && sess.target_tlib_path.dir.join("libLLVM.dylib").exists();
67+
6568
// Toolchains for some targets may ship `libunwind.a`, but place it into the main sysroot
6669
// library directory instead of the self-contained directories.
6770
// Sanitizer libraries have the same issue and are also linked by name on Apple targets.
6871
// The targets here should be in sync with `copy_third_party_objects` in bootstrap.
69-
// Finally there is shared LLVM library, which unlike compiler libraries, is linked by the name,
70-
// therefore requiring the search path for the linker.
72+
// On Apple targets, shared LLVM is linked by name, so when `libLLVM.dylib` is
73+
// present in the target libdir, add that directory to the linker search path.
7174
// FIXME: implement `-Clink-self-contained=+/-unwind,+/-sanitizers`, move the shipped libunwind
7275
// and sanitizers to self-contained directory, and stop adding this search path.
7376
// FIXME: On AIX this also has the side-effect of making the list of library search paths
@@ -77,7 +80,8 @@ pub fn walk_native_lib_search_dirs<R>(
7780
|| sess.target.os == Os::Linux
7881
|| sess.target.os == Os::Fuchsia
7982
|| sess.target.is_like_aix
80-
|| sess.target.is_like_darwin && !sess.sanitizers().is_empty()
83+
|| sess.target.is_like_darwin
84+
&& (!sess.sanitizers().is_empty() || has_shared_llvm_apple_darwin)
8185
|| sess.target.os == Os::Windows
8286
&& sess.target.env == Env::Gnu
8387
&& sess.target.cfg_abi == CfgAbi::Llvm
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Change this file to make users of the `download-ci-llvm` configuration download
22
a new version of LLVM from CI, even if the LLVM submodule hasn’t changed.
33

4-
Last change is for: https://github.com/rust-lang/rust/pull/154485
4+
Last change is for: https://github.com/rust-lang/rust/pull/154839

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,15 @@ fn maybe_install_llvm(
25132513
let llvm_dylib_path = src_libdir.join("libLLVM.dylib");
25142514
if llvm_dylib_path.exists() {
25152515
builder.install(&llvm_dylib_path, dst_libdir, FileType::NativeLibrary);
2516+
2517+
if install_symlink && let Some(llvm_config_path) = &builder.llvm_config(target) {
2518+
let major = llvm::get_llvm_version_major(builder, llvm_config_path);
2519+
let versioned_name = match &builder.config.llvm_version_suffix {
2520+
Some(version_suffix) => format!("libLLVM-{major}{version_suffix}.dylib"),
2521+
None => format!("libLLVM-{major}.dylib"),
2522+
};
2523+
t!(builder.symlink_file("libLLVM.dylib", dst_libdir.join(versioned_name)));
2524+
}
25162525
}
25172526
!builder.config.dry_run()
25182527
} else if let llvm::LlvmBuildStatus::AlreadyBuilt(llvm::LlvmResult {

src/ci/github-actions/jobs.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ optional:
164164
IMAGE: pr-check-1
165165
<<: *job-linux-4c
166166

167+
- name: test-aarch64-apple-darwin-link-llvm-shared
168+
env:
169+
SCRIPT: >-
170+
./x.py build --stage 1 compiler/rustc
171+
--host=aarch64-apple-darwin
172+
--target=aarch64-apple-darwin
173+
RUST_CONFIGURE_ARGS: >-
174+
--set llvm.link-shared=true
175+
MACOSX_DEPLOYMENT_TARGET: 11.0
176+
MACOSX_STD_DEPLOYMENT_TARGET: 11.0
177+
DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer
178+
<<: *job-macos
179+
167180
# Main CI jobs that have to be green to merge a commit into the default branch.
168181
#
169182
# These jobs automatically inherit envs.auto, to avoid repeating it in each job
@@ -523,6 +536,7 @@ auto:
523536
--enable-sanitizers
524537
--enable-profiler
525538
--set rust.jemalloc
539+
--set llvm.link-shared=true
526540
--set rust.lto=thin
527541
--set rust.codegen-units=1
528542
# Aarch64 tooling only needs to support macOS 11.0 and up as nothing else

0 commit comments

Comments
 (0)