Skip to content

Commit 01aa10c

Browse files
Rollup merge of #98418 - topjohnwu:macos-dylib, r=jyn514
Allow macOS to build LLVM as shared library Inspired by how [homebrew](https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/llvm.rb) builds and distributes llvm, here we manually create a symlink with a versioned dylib path to make `llvm-config` work properly. Note, the resulting `rustc` executable and `librustc_driver-<hash>.dylib` still links to the un-versioned `libLLVM.dylib` as expected when distributed in the final output. I have confirmed this by checking `otool -L` on both binaries. After the change, enabling `llvm.link-shared` and `llvm.thin-lto` will be possible on macOS.
2 parents 194764f + b6e28b5 commit 01aa10c

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/bootstrap/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,11 @@ use std::cell::{Cell, RefCell};
107107
use std::collections::{HashMap, HashSet};
108108
use std::env;
109109
use std::fs::{self, File};
110+
use std::io;
110111
use std::path::{Path, PathBuf};
111112
use std::process::{self, Command};
112113
use std::str;
113114

114-
#[cfg(unix)]
115-
use std::os::unix::fs::symlink as symlink_file;
116-
#[cfg(windows)]
117-
use std::os::windows::fs::symlink_file;
118-
119115
use filetime::FileTime;
120116
use once_cell::sync::OnceCell;
121117

@@ -1460,7 +1456,7 @@ impl Build {
14601456
src = t!(fs::canonicalize(src));
14611457
} else {
14621458
let link = t!(fs::read_link(src));
1463-
t!(symlink_file(link, dst));
1459+
t!(self.symlink_file(link, dst));
14641460
return;
14651461
}
14661462
}
@@ -1585,6 +1581,14 @@ impl Build {
15851581
iter.map(|e| t!(e)).collect::<Vec<_>>().into_iter()
15861582
}
15871583

1584+
fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(&self, src: P, link: Q) -> io::Result<()> {
1585+
#[cfg(unix)]
1586+
use std::os::unix::fs::symlink as symlink_file;
1587+
#[cfg(windows)]
1588+
use std::os::windows::fs::symlink_file;
1589+
if !self.config.dry_run { symlink_file(src.as_ref(), link.as_ref()) } else { Ok(()) }
1590+
}
1591+
15881592
fn remove(&self, f: &Path) {
15891593
if self.config.dry_run {
15901594
return;

src/bootstrap/native.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ impl Step for Llvm {
251251
};
252252

253253
builder.update_submodule(&Path::new("src").join("llvm-project"));
254-
if builder.llvm_link_shared()
255-
&& (target.contains("windows") || target.contains("apple-darwin"))
256-
{
254+
if builder.llvm_link_shared() && target.contains("windows") {
257255
panic!("shared linking to LLVM is not currently supported on {}", target.triple);
258256
}
259257

@@ -359,7 +357,9 @@ impl Step for Llvm {
359357
//
360358
// If we're not linking rustc to a dynamic LLVM, though, then don't link
361359
// tools to it.
362-
if builder.llvm_link_tools_dynamically(target) && builder.llvm_link_shared() {
360+
let llvm_link_shared =
361+
builder.llvm_link_tools_dynamically(target) && builder.llvm_link_shared();
362+
if llvm_link_shared {
363363
cfg.define("LLVM_LINK_LLVM_DYLIB", "ON");
364364
}
365365

@@ -438,18 +438,18 @@ impl Step for Llvm {
438438
);
439439
}
440440

441-
if let Some(ref suffix) = builder.config.llvm_version_suffix {
441+
let llvm_version_suffix = if let Some(ref suffix) = builder.config.llvm_version_suffix {
442442
// Allow version-suffix="" to not define a version suffix at all.
443-
if !suffix.is_empty() {
444-
cfg.define("LLVM_VERSION_SUFFIX", suffix);
445-
}
443+
if !suffix.is_empty() { Some(suffix.to_string()) } else { None }
446444
} else if builder.config.channel == "dev" {
447445
// Changes to a version suffix require a complete rebuild of the LLVM.
448446
// To avoid rebuilds during a time of version bump, don't include rustc
449447
// release number on the dev channel.
450-
cfg.define("LLVM_VERSION_SUFFIX", "-rust-dev");
448+
Some("-rust-dev".to_string())
451449
} else {
452-
let suffix = format!("-rust-{}-{}", builder.version, builder.config.channel);
450+
Some(format!("-rust-{}-{}", builder.version, builder.config.channel))
451+
};
452+
if let Some(ref suffix) = llvm_version_suffix {
453453
cfg.define("LLVM_VERSION_SUFFIX", suffix);
454454
}
455455

@@ -478,6 +478,27 @@ impl Step for Llvm {
478478

479479
cfg.build();
480480

481+
// When building LLVM with LLVM_LINK_LLVM_DYLIB for macOS, an unversioned
482+
// libLLVM.dylib will be built. However, llvm-config will still look
483+
// for a versioned path like libLLVM-14.dylib. Manually create a symbolic
484+
// link to make llvm-config happy.
485+
if llvm_link_shared && target.contains("apple-darwin") {
486+
let mut cmd = Command::new(&build_llvm_config);
487+
let version = output(cmd.arg("--version"));
488+
let major = version.split('.').next().unwrap();
489+
let lib_name = match llvm_version_suffix {
490+
Some(s) => format!("lib/libLLVM-{}{}.dylib", major, s),
491+
None => format!("lib/libLLVM-{}.dylib", major),
492+
};
493+
494+
// The reason why we build the library path from llvm-config is because
495+
// the output of llvm-config depends on its location in the file system.
496+
// Make sure we create the symlink exactly where it's needed.
497+
let llvm_base = build_llvm_config.parent().unwrap().parent().unwrap();
498+
let lib_llvm = llvm_base.join(lib_name);
499+
t!(builder.symlink_file("libLLVM.dylib", &lib_llvm));
500+
}
501+
481502
t!(stamp.write());
482503

483504
build_llvm_config

0 commit comments

Comments
 (0)