Skip to content

Commit

Permalink
rustbuild: update the llvm link logic further
Browse files Browse the repository at this point in the history
There are now four static/shared scenarios that can happen for the
supported LLVM versions:

- 3.9+: By default use `llvm-config --link-static`
- 3.9+ and `--enable-llvm-link-shared`: Use `--link-shared` instead.
- 3.8: Use `llvm-config --shared-mode` and go with its answer.
- 3.7: Just assume static, maintaining the status quo.
  • Loading branch information
cuviper committed Nov 18, 2016
1 parent f13391a commit f324037
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/librustc_llvm/build.rs
Expand Up @@ -17,6 +17,35 @@ use std::path::{PathBuf, Path};

use build_helper::output;

fn detect_llvm_link(llvm_config: &Path) -> (&'static str, Option<&'static str>) {
let mut version_cmd = Command::new(llvm_config);
version_cmd.arg("--version");
let version_output = output(&mut version_cmd);
let mut parts = version_output.split('.').take(2)
.filter_map(|s| s.parse::<u32>().ok());
if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
if major > 3 || (major == 3 && minor >= 9) {
// Force the link mode we want, preferring static by default, but
// possibly overridden by `configure --enable-llvm-link-shared`.
if env::var_os("LLVM_LINK_SHARED").is_some() {
return ("dylib", Some("--link-shared"));
} else {
return ("static", Some("--link-static"));
}
} else if major == 3 && minor == 8 {
// Find out LLVM's default linking mode.
let mut mode_cmd = Command::new(llvm_config);
mode_cmd.arg("--shared-mode");
if output(&mut mode_cmd).trim() == "shared" {
return ("dylib", None);
} else {
return ("static", None);
}
}
}
("static", None)
}

fn main() {
println!("cargo:rustc-cfg=cargobuild");

Expand Down Expand Up @@ -123,39 +152,16 @@ fn main() {
.cpp_link_stdlib(None) // we handle this below
.compile("librustllvm.a");

// Find out LLVM's default linking mode.
let mut cmd = Command::new(&llvm_config);
cmd.arg("--shared-mode");
let mut llvm_kind = if output(&mut cmd).trim() == "shared" {
"dylib"
} else {
"static"
};
let (llvm_kind, llvm_link_arg) = detect_llvm_link(&llvm_config);

// Link in all LLVM libraries, if we're uwring the "wrong" llvm-config then
// we don't pick up system libs because unfortunately they're for the host
// of llvm-config, not the target that we're attempting to link.
let mut cmd = Command::new(&llvm_config);
cmd.arg("--libs");

// Force static linking with "--link-static" if available, or
// force "--link-shared" if the configuration requested it.
let llvm_link_shared = env::var_os("LLVM_LINK_SHARED").is_some();
let mut version_cmd = Command::new(&llvm_config);
version_cmd.arg("--version");
let version_output = output(&mut version_cmd);
let mut parts = version_output.split('.');
if let (Some(major), Some(minor)) = (parts.next().and_then(|s| s.parse::<u32>().ok()),
parts.next().and_then(|s| s.parse::<u32>().ok())) {
if major > 3 || (major == 3 && minor >= 9) {
if llvm_link_shared {
cmd.arg("--link-shared");
llvm_kind = "dylib";
} else {
cmd.arg("--link-static");
llvm_kind = "static";
}
}
if let Some(link_arg) = llvm_link_arg {
cmd.arg(link_arg);
}

if !is_crossed {
Expand Down

0 comments on commit f324037

Please sign in to comment.