Skip to content

Commit f13391a

Browse files
committed
rustbuild: allow dynamically linking LLVM
The makefiles and `mklldeps.py` called `llvm-config --shared-mode` to find out if LLVM defaulted to shared or static libraries, and just went with that. But under rustbuild, `librustc_llvm/build.rs` was assuming that LLVM should be static, and even forcing `--link-static` for 3.9+. Now that build script also uses `--shared-mode` to learn the default, which should work better for pre-3.9 configured for dynamic linking, as it wasn't possible back then to choose differently via `llvm-config`. Further, the configure script now has a new `--enable-llvm-link-shared` option, which allows one to manually override `--link-shared` on 3.9+ instead of forcing static.
1 parent f22fdb0 commit f13391a

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

configure

+1
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ opt ccache 0 "invoke gcc/clang via ccache to reuse object files between builds"
624624
opt local-rust 0 "use an installed rustc rather than downloading a snapshot"
625625
opt local-rebuild 0 "assume local-rust matches the current version, for rebuilds; implies local-rust, and is implied if local-rust already matches the current version"
626626
opt llvm-static-stdcpp 0 "statically link to libstdc++ for LLVM"
627+
opt llvm-link-shared 0 "prefer shared linking to LLVM (llvm-config --link-shared)"
627628
opt rpath 1 "build rpaths into rustc itself"
628629
opt stage0-landing-pads 1 "enable landing pads during bootstrap with stage0"
629630
# This is used by the automation to produce single-target nightlies

src/bootstrap/compile.rs

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ pub fn rustc<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
212212
cargo.env("LLVM_STATIC_STDCPP",
213213
compiler_file(build.cxx(target), "libstdc++.a"));
214214
}
215+
if build.config.llvm_link_shared {
216+
cargo.env("LLVM_LINK_SHARED", "1");
217+
}
215218
if let Some(ref s) = build.config.rustc_default_linker {
216219
cargo.env("CFG_DEFAULT_LINKER", s);
217220
}

src/bootstrap/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct Config {
5353
pub llvm_release_debuginfo: bool,
5454
pub llvm_version_check: bool,
5555
pub llvm_static_stdcpp: bool,
56+
pub llvm_link_shared: bool,
5657

5758
// rust codegen options
5859
pub rust_optimize: bool,
@@ -343,6 +344,7 @@ impl Config {
343344
("OPTIMIZE_LLVM", self.llvm_optimize),
344345
("LLVM_VERSION_CHECK", self.llvm_version_check),
345346
("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
347+
("LLVM_LINK_SHARED", self.llvm_link_shared),
346348
("OPTIMIZE", self.rust_optimize),
347349
("DEBUG_ASSERTIONS", self.rust_debug_assertions),
348350
("DEBUGINFO", self.rust_debuginfo),

src/librustc_llvm/build.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,38 @@ fn main() {
123123
.cpp_link_stdlib(None) // we handle this below
124124
.compile("librustllvm.a");
125125

126+
// Find out LLVM's default linking mode.
127+
let mut cmd = Command::new(&llvm_config);
128+
cmd.arg("--shared-mode");
129+
let mut llvm_kind = if output(&mut cmd).trim() == "shared" {
130+
"dylib"
131+
} else {
132+
"static"
133+
};
134+
126135
// Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
127136
// we don't pick up system libs because unfortunately they're for the host
128137
// of llvm-config, not the target that we're attempting to link.
129138
let mut cmd = Command::new(&llvm_config);
130139
cmd.arg("--libs");
131140

132-
// Force static linking with "--link-static" if available.
141+
// Force static linking with "--link-static" if available, or
142+
// force "--link-shared" if the configuration requested it.
143+
let llvm_link_shared = env::var_os("LLVM_LINK_SHARED").is_some();
133144
let mut version_cmd = Command::new(&llvm_config);
134145
version_cmd.arg("--version");
135146
let version_output = output(&mut version_cmd);
136147
let mut parts = version_output.split('.');
137148
if let (Some(major), Some(minor)) = (parts.next().and_then(|s| s.parse::<u32>().ok()),
138149
parts.next().and_then(|s| s.parse::<u32>().ok())) {
139150
if major > 3 || (major == 3 && minor >= 9) {
140-
cmd.arg("--link-static");
151+
if llvm_link_shared {
152+
cmd.arg("--link-shared");
153+
llvm_kind = "dylib";
154+
} else {
155+
cmd.arg("--link-static");
156+
llvm_kind = "static";
157+
}
141158
}
142159
}
143160

@@ -174,7 +191,7 @@ fn main() {
174191
}
175192

176193
let kind = if name.starts_with("LLVM") {
177-
"static"
194+
llvm_kind
178195
} else {
179196
"dylib"
180197
};

0 commit comments

Comments
 (0)