2017-09-22 23:34:27 -05:00
|
|
|
extern crate cc;
|
2016-01-21 17:21:13 -06:00
|
|
|
extern crate build_helper;
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
use std::env;
|
2016-07-25 17:37:12 -05:00
|
|
|
use std::path::{PathBuf, Path};
|
2016-01-21 17:21:13 -06:00
|
|
|
|
|
|
|
use build_helper::output;
|
|
|
|
|
2017-11-30 19:54:02 -06:00
|
|
|
fn detect_llvm_link() -> (&'static str, &'static str) {
|
|
|
|
// 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() {
|
|
|
|
("dylib", "--link-shared")
|
|
|
|
} else {
|
|
|
|
("static", "--link-static")
|
2016-11-17 23:50:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
fn main() {
|
2018-04-17 18:50:41 -05:00
|
|
|
if env::var_os("RUST_CHECK").is_some() {
|
|
|
|
// If we're just running `check`, there's no need for LLVM to be built.
|
2018-04-18 18:09:41 -05:00
|
|
|
println!("cargo:rerun-if-env-changed=RUST_CHECK");
|
2018-04-17 18:50:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-25 10:59:12 -05:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
2016-05-29 04:27:34 -05:00
|
|
|
let llvm_config = env::var_os("LLVM_CONFIG")
|
2016-10-22 08:07:35 -05:00
|
|
|
.map(PathBuf::from)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
|
|
|
|
let to_test = dir.parent()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(&target)
|
|
|
|
.join("llvm/bin/llvm-config");
|
|
|
|
if Command::new(&to_test).output().is_ok() {
|
|
|
|
return to_test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathBuf::from("llvm-config")
|
|
|
|
});
|
2016-01-21 17:21:13 -06:00
|
|
|
|
|
|
|
println!("cargo:rerun-if-changed={}", llvm_config.display());
|
2017-06-30 11:55:32 -05:00
|
|
|
println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
|
2017-06-04 13:35:57 -05:00
|
|
|
|
2016-02-24 19:09:17 -06:00
|
|
|
// Test whether we're cross-compiling LLVM. This is a pretty rare case
|
|
|
|
// currently where we're producing an LLVM for a different platform than
|
|
|
|
// what this build script is currently running on.
|
|
|
|
//
|
|
|
|
// In that case, there's no guarantee that we can actually run the target,
|
|
|
|
// so the build system works around this by giving us the LLVM_CONFIG for
|
|
|
|
// the host platform. This only really works if the host LLVM and target
|
|
|
|
// LLVM are compiled the same way, but for us that's typically the case.
|
|
|
|
//
|
2016-03-11 18:03:28 -06:00
|
|
|
// We *want* detect this cross compiling situation by asking llvm-config
|
|
|
|
// what it's host-target is. If that's not the TARGET, then we're cross
|
|
|
|
// compiling. Unfortunately `llvm-config` seems either be buggy, or we're
|
|
|
|
// misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
|
|
|
|
// report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
|
|
|
|
// tricks us into thinking we're doing a cross build when we aren't, so
|
|
|
|
// havoc ensues.
|
|
|
|
//
|
|
|
|
// In any case, if we're cross compiling, this generally just means that we
|
|
|
|
// can't trust all the output of llvm-config becaues it might be targeted
|
|
|
|
// for the host rather than the target. As a result a bunch of blocks below
|
|
|
|
// are gated on `if !is_crossed`
|
2016-09-25 10:59:12 -05:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
|
|
|
let host = env::var("HOST").expect("HOST was not set");
|
2016-02-24 19:09:17 -06:00
|
|
|
let is_crossed = target != host;
|
|
|
|
|
2017-04-25 17:08:13 -05:00
|
|
|
let mut optional_components =
|
2018-07-18 22:03:39 -05:00
|
|
|
vec!["x86", "arm", "aarch64", "amdgpu", "mips", "powerpc",
|
2017-06-16 17:43:43 -05:00
|
|
|
"systemz", "jsbackend", "webassembly", "msp430", "sparc", "nvptx"];
|
2017-04-25 17:08:13 -05:00
|
|
|
|
|
|
|
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());
|
2017-11-30 19:54:02 -06:00
|
|
|
let (major, _minor) =
|
2017-04-25 17:08:13 -05:00
|
|
|
if let (Some(major), Some(minor)) = (parts.next(), parts.next()) {
|
|
|
|
(major, minor)
|
|
|
|
} else {
|
2017-11-30 19:54:02 -06:00
|
|
|
(3, 9)
|
2017-04-25 17:08:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if major > 3 {
|
|
|
|
optional_components.push("hexagon");
|
|
|
|
}
|
2016-01-21 17:21:13 -06:00
|
|
|
|
2018-07-24 05:03:28 -05:00
|
|
|
if major > 6 {
|
|
|
|
optional_components.push("riscv");
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
// FIXME: surely we don't need all these components, right? Stuff like mcjit
|
|
|
|
// or interpreter the compiler itself never uses.
|
2016-05-29 04:27:34 -05:00
|
|
|
let required_components = &["ipo",
|
|
|
|
"bitreader",
|
|
|
|
"bitwriter",
|
|
|
|
"linker",
|
|
|
|
"asmparser",
|
|
|
|
"mcjit",
|
rustc: Implement ThinLTO
This commit is an implementation of LLVM's ThinLTO for consumption in rustc
itself. Currently today LTO works by merging all relevant LLVM modules into one
and then running optimization passes. "Thin" LTO operates differently by having
more sharded work and allowing parallelism opportunities between optimizing
codegen units. Further down the road Thin LTO also allows *incremental* LTO
which should enable even faster release builds without compromising on the
performance we have today.
This commit uses a `-Z thinlto` flag to gate whether ThinLTO is enabled. It then
also implements two forms of ThinLTO:
* In one mode we'll *only* perform ThinLTO over the codegen units produced in a
single compilation. That is, we won't load upstream rlibs, but we'll instead
just perform ThinLTO amongst all codegen units produced by the compiler for
the local crate. This is intended to emulate a desired end point where we have
codegen units turned on by default for all crates and ThinLTO allows us to do
this without performance loss.
* In anther mode, like full LTO today, we'll optimize all upstream dependencies
in "thin" mode. Unlike today, however, this LTO step is fully parallelized so
should finish much more quickly.
There's a good bit of comments about what the implementation is doing and where
it came from, but the tl;dr; is that currently most of the support here is
copied from upstream LLVM. This code duplication is done for a number of
reasons:
* Controlling parallelism means we can use the existing jobserver support to
avoid overloading machines.
* We will likely want a slightly different form of incremental caching which
integrates with our own incremental strategy, but this is yet to be
determined.
* This buys us some flexibility about when/where we run ThinLTO, as well as
having it tailored to fit our needs for the time being.
* Finally this allows us to reuse some artifacts such as our `TargetMachine`
creation, where all our options we used today aren't necessarily supported by
upstream LLVM yet.
My hope is that we can get some experience with this copy/paste in tree and then
eventually upstream some work to LLVM itself to avoid the duplication while
still ensuring our needs are met. Otherwise I fear that maintaining these
bindings may be quite costly over the years with LLVM updates!
2017-07-23 10:14:38 -05:00
|
|
|
"lto",
|
2016-05-29 04:27:34 -05:00
|
|
|
"interpreter",
|
2016-01-21 17:21:13 -06:00
|
|
|
"instrumentation"];
|
|
|
|
|
|
|
|
let components = output(Command::new(&llvm_config).arg("--components"));
|
|
|
|
let mut components = components.split_whitespace().collect::<Vec<_>>();
|
2016-05-29 04:27:34 -05:00
|
|
|
components.retain(|c| optional_components.contains(c) || required_components.contains(c));
|
2016-01-21 17:21:13 -06:00
|
|
|
|
|
|
|
for component in required_components {
|
|
|
|
if !components.contains(component) {
|
|
|
|
panic!("require llvm component {} but wasn't found", component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for component in components.iter() {
|
|
|
|
println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link in our own LLVM shims, compiled with the same flags as LLVM
|
|
|
|
let mut cmd = Command::new(&llvm_config);
|
|
|
|
cmd.arg("--cxxflags");
|
|
|
|
let cxxflags = output(&mut cmd);
|
2017-09-22 23:34:27 -05:00
|
|
|
let mut cfg = cc::Build::new();
|
2017-09-05 10:48:47 -05:00
|
|
|
cfg.warnings(false);
|
2016-01-21 17:21:13 -06:00
|
|
|
for flag in cxxflags.split_whitespace() {
|
2016-02-24 19:09:17 -06:00
|
|
|
// Ignore flags like `-m64` when we're doing a cross build
|
|
|
|
if is_crossed && flag.starts_with("-m") {
|
2016-05-29 04:27:34 -05:00
|
|
|
continue;
|
2016-02-24 19:09:17 -06:00
|
|
|
}
|
2017-03-17 14:52:44 -05:00
|
|
|
|
|
|
|
// -Wdate-time is not supported by the netbsd cross compiler
|
|
|
|
if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
cfg.flag(flag);
|
|
|
|
}
|
2016-02-16 10:07:30 -06:00
|
|
|
|
2017-03-24 03:31:26 -05:00
|
|
|
for component in &components {
|
2017-11-26 15:25:49 -06:00
|
|
|
let mut flag = String::from("LLVM_COMPONENT_");
|
2016-02-16 10:07:30 -06:00
|
|
|
flag.push_str(&component.to_uppercase());
|
2017-11-26 15:25:49 -06:00
|
|
|
cfg.define(&flag, None);
|
2016-02-16 10:07:30 -06:00
|
|
|
}
|
|
|
|
|
2018-01-22 09:29:24 -06:00
|
|
|
println!("cargo:rerun-if-changed-env=LLVM_RUSTLLVM");
|
2016-07-29 08:09:32 -05:00
|
|
|
if env::var_os("LLVM_RUSTLLVM").is_some() {
|
2017-11-26 15:25:49 -06:00
|
|
|
cfg.define("LLVM_RUSTLLVM", None);
|
2016-07-29 08:09:32 -05:00
|
|
|
}
|
|
|
|
|
2017-02-23 09:49:54 -06:00
|
|
|
build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
|
2018-10-23 02:59:14 -05:00
|
|
|
cfg.file("../rustllvm/PassWrapper.cpp")
|
2016-01-21 17:21:13 -06:00
|
|
|
.file("../rustllvm/RustWrapper.cpp")
|
|
|
|
.file("../rustllvm/ArchiveWrapper.cpp")
|
2018-02-12 10:38:46 -06:00
|
|
|
.file("../rustllvm/Linker.cpp")
|
2016-01-21 17:21:13 -06:00
|
|
|
.cpp(true)
|
|
|
|
.cpp_link_stdlib(None) // we handle this below
|
2017-11-26 15:43:24 -06:00
|
|
|
.compile("rustllvm");
|
2016-01-21 17:21:13 -06:00
|
|
|
|
2017-11-30 19:54:02 -06:00
|
|
|
let (llvm_kind, llvm_link_arg) = detect_llvm_link();
|
2016-11-17 01:28:14 -06:00
|
|
|
|
2016-02-24 19:09:17 -06:00
|
|
|
// 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.
|
2016-01-21 17:21:13 -06:00
|
|
|
let mut cmd = Command::new(&llvm_config);
|
2017-11-30 19:54:02 -06:00
|
|
|
cmd.arg(llvm_link_arg).arg("--libs");
|
2016-11-06 04:45:21 -06:00
|
|
|
|
2016-02-24 19:09:17 -06:00
|
|
|
if !is_crossed {
|
|
|
|
cmd.arg("--system-libs");
|
|
|
|
}
|
2017-03-24 03:31:26 -05:00
|
|
|
cmd.args(&components);
|
2016-02-24 19:09:17 -06:00
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
for lib in output(&mut cmd).split_whitespace() {
|
|
|
|
let name = if lib.starts_with("-l") {
|
|
|
|
&lib[2..]
|
|
|
|
} else if lib.starts_with("-") {
|
|
|
|
&lib[1..]
|
2016-07-25 17:37:12 -05:00
|
|
|
} else if Path::new(lib).exists() {
|
|
|
|
// On MSVC llvm-config will print the full name to libraries, but
|
|
|
|
// we're only interested in the name part
|
|
|
|
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
|
2018-12-05 08:42:56 -06:00
|
|
|
name.trim_end_matches(".lib")
|
2016-07-25 17:37:12 -05:00
|
|
|
} else if lib.ends_with(".lib") {
|
|
|
|
// Some MSVC libraries just come up with `.lib` tacked on, so chop
|
|
|
|
// that off
|
2018-12-05 08:42:56 -06:00
|
|
|
lib.trim_end_matches(".lib")
|
2016-01-21 17:21:13 -06:00
|
|
|
} else {
|
2016-10-22 08:07:35 -05:00
|
|
|
continue;
|
2016-01-21 17:21:13 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Don't need or want this library, but LLVM's CMake build system
|
|
|
|
// doesn't provide a way to disable it, so filter it here even though we
|
|
|
|
// may or may not have built it. We don't reference anything from this
|
|
|
|
// library and it otherwise may just pull in extra dependencies on
|
|
|
|
// libedit which we don't want
|
|
|
|
if name == "LLVMLineEditor" {
|
2016-10-22 08:07:35 -05:00
|
|
|
continue;
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|
|
|
|
|
2016-05-29 04:27:34 -05:00
|
|
|
let kind = if name.starts_with("LLVM") {
|
2016-11-17 01:28:14 -06:00
|
|
|
llvm_kind
|
2016-05-29 04:27:34 -05:00
|
|
|
} else {
|
|
|
|
"dylib"
|
|
|
|
};
|
2016-01-21 17:21:13 -06:00
|
|
|
println!("cargo:rustc-link-lib={}={}", kind, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// LLVM ldflags
|
2016-02-24 19:09:17 -06:00
|
|
|
//
|
|
|
|
// If we're a cross-compile of LLVM then unfortunately we can't trust these
|
|
|
|
// ldflags (largely where all the LLVM libs are located). Currently just
|
|
|
|
// hack around this by replacing the host triple with the target and pray
|
|
|
|
// that those -L directories are the same!
|
2016-01-21 17:21:13 -06:00
|
|
|
let mut cmd = Command::new(&llvm_config);
|
2017-11-30 19:54:02 -06:00
|
|
|
cmd.arg(llvm_link_arg).arg("--ldflags");
|
2016-01-21 17:21:13 -06:00
|
|
|
for lib in output(&mut cmd).split_whitespace() {
|
2016-07-25 17:37:12 -05:00
|
|
|
if lib.starts_with("-LIBPATH:") {
|
2016-10-22 08:07:35 -05:00
|
|
|
println!("cargo:rustc-link-search=native={}", &lib[9..]);
|
2016-07-25 17:37:12 -05:00
|
|
|
} else if is_crossed {
|
2016-02-24 19:09:17 -06:00
|
|
|
if lib.starts_with("-L") {
|
|
|
|
println!("cargo:rustc-link-search=native={}",
|
|
|
|
lib[2..].replace(&host, &target));
|
|
|
|
}
|
|
|
|
} else if lib.starts_with("-l") {
|
2016-01-21 17:21:13 -06:00
|
|
|
println!("cargo:rustc-link-lib={}", &lib[2..]);
|
|
|
|
} else if lib.starts_with("-L") {
|
|
|
|
println!("cargo:rustc-link-search=native={}", &lib[2..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 13:31:49 -05:00
|
|
|
let llvm_static_stdcpp = env::var_os("LLVM_STATIC_STDCPP");
|
|
|
|
|
2016-12-18 11:31:42 -06:00
|
|
|
let stdcppname = if target.contains("openbsd") {
|
2017-11-26 03:08:25 -06:00
|
|
|
// llvm-config on OpenBSD doesn't mention stdlib=libc++
|
|
|
|
"c++"
|
2017-09-15 22:14:34 -05:00
|
|
|
} else if target.contains("freebsd") {
|
|
|
|
"c++"
|
2017-03-19 13:31:49 -05:00
|
|
|
} else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
|
|
|
|
// NetBSD uses a separate library when relocation is required
|
|
|
|
"stdc++_pic"
|
2016-12-18 11:31:42 -06:00
|
|
|
} else {
|
|
|
|
"stdc++"
|
|
|
|
};
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
// C++ runtime library
|
|
|
|
if !target.contains("msvc") {
|
2017-03-19 13:31:49 -05:00
|
|
|
if let Some(s) = llvm_static_stdcpp {
|
2016-01-21 17:21:13 -06:00
|
|
|
assert!(!cxxflags.contains("stdlib=libc++"));
|
|
|
|
let path = PathBuf::from(s);
|
|
|
|
println!("cargo:rustc-link-search=native={}",
|
|
|
|
path.parent().unwrap().display());
|
2016-12-18 11:31:42 -06:00
|
|
|
println!("cargo:rustc-link-lib=static={}", stdcppname);
|
2016-01-21 17:21:13 -06:00
|
|
|
} else if cxxflags.contains("stdlib=libc++") {
|
|
|
|
println!("cargo:rustc-link-lib=c++");
|
|
|
|
} else {
|
2016-12-18 11:31:42 -06:00
|
|
|
println!("cargo:rustc-link-lib={}", stdcppname);
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|
|
|
|
}
|
2017-03-24 12:59:27 -05:00
|
|
|
|
2017-05-20 05:57:41 -05:00
|
|
|
// LLVM requires symbols from this library, but apparently they're not printed
|
2017-03-24 12:59:27 -05:00
|
|
|
// during llvm-config?
|
2017-04-13 15:27:53 -05:00
|
|
|
if target.contains("windows-gnu") {
|
|
|
|
println!("cargo:rustc-link-lib=static-nobundle=gcc_s");
|
|
|
|
println!("cargo:rustc-link-lib=static-nobundle=pthread");
|
2018-07-31 07:36:38 -05:00
|
|
|
println!("cargo:rustc-link-lib=dylib=uuid");
|
2017-04-13 15:27:53 -05:00
|
|
|
}
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|