1
Fork 0

rustbuild: Build jemalloc and libbacktrace only once (take 2)

This commit is contained in:
Vadim Petrochenkov 2017-01-27 01:57:30 +03:00
parent 1a2428fc88
commit 6c2ef5201a
4 changed files with 60 additions and 53 deletions

View file

@ -482,7 +482,8 @@ impl Build {
//
// These variables are primarily all read by
// src/bootstrap/bin/{rustc.rs,rustdoc.rs}
cargo.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
cargo.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
.env("RUSTC_REAL", self.compiler_path(compiler))
.env("RUSTC_STAGE", stage.to_string())
.env("RUSTC_DEBUGINFO", self.config.rust_debuginfo.to_string())
@ -746,10 +747,15 @@ impl Build {
}
}
/// Directory for libraries built from C/C++ code and shared between stages.
fn native_dir(&self, target: &str) -> PathBuf {
self.out.join(target).join("native")
}
/// Root output directory for rust_test_helpers library compiled for
/// `target`
fn test_helpers_out(&self, target: &str) -> PathBuf {
self.out.join(target).join("rust-test-helpers")
self.native_dir(target).join("rust-test-helpers")
}
/// Adds the compiler's directory of dynamic libraries to `cmd`'s dynamic

View file

@ -88,6 +88,21 @@ pub fn output(cmd: &mut Command) -> String {
String::from_utf8(output.stdout).unwrap()
}
pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
let mut stack = dir.read_dir().unwrap()
.map(|e| e.unwrap())
.filter(|e| &*e.file_name() != ".git")
.collect::<Vec<_>>();
while let Some(entry) = stack.pop() {
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
} else {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
fn fail(s: &str) -> ! {
println!("\n\n{}\n\n", s);
std::process::exit(1);

View file

@ -13,20 +13,15 @@
extern crate build_helper;
extern crate gcc;
use std::env;
use std::{env, fs};
use std::path::PathBuf;
use std::process::Command;
use build_helper::run;
use build_helper::{run, rerun_if_changed_anything_in_dir};
fn main() {
println!("cargo:rustc-cfg=cargobuild");
println!("cargo:rerun-if-changed=build.rs");
let target = env::var("TARGET").expect("TARGET was not set");
let host = env::var("HOST").expect("HOST was not set");
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let src_dir = env::current_dir().unwrap();
// FIXME: This is a hack to support building targets that don't
// support jemalloc alongside hosts that do. The jemalloc build is
// controlled by a feature of the std crate, and if that feature
@ -35,6 +30,7 @@ fn main() {
// that the feature set used by std is the same across all
// targets, which means we have to build the alloc_jemalloc crate
// for targets like emscripten, even if we don't use it.
let target = env::var("TARGET").expect("TARGET was not set");
if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
target.contains("redox") {
@ -57,6 +53,28 @@ fn main() {
return;
}
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
let build_dir = PathBuf::from(build_dir).join("jemalloc");
let _ = fs::create_dir_all(&build_dir);
if target.contains("windows") {
println!("cargo:rustc-link-lib=static=jemalloc");
} else {
println!("cargo:rustc-link-lib=static=jemalloc_pic");
}
println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
if target.contains("android") {
println!("cargo:rustc-link-lib=gcc");
} else if !target.contains("windows") && !target.contains("musl") {
println!("cargo:rustc-link-lib=pthread");
}
if !cfg!(stage0) {
return
}
let host = env::var("HOST").expect("HOST was not set");
let src_dir = env::current_dir().unwrap().join("../jemalloc");
rerun_if_changed_anything_in_dir(&src_dir);
let compiler = gcc::Config::new().get_compiler();
// only msvc returns None for ar so unwrap is okay
let ar = build_helper::cc2ar(compiler.path(), &target).unwrap();
@ -66,23 +84,8 @@ fn main() {
.collect::<Vec<_>>()
.join(" ");
let mut stack = src_dir.join("../jemalloc")
.read_dir()
.unwrap()
.map(|e| e.unwrap())
.filter(|e| &*e.file_name() != ".git")
.collect::<Vec<_>>();
while let Some(entry) = stack.pop() {
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
} else {
println!("cargo:rerun-if-changed={}", path.display());
}
}
let mut cmd = Command::new("sh");
cmd.arg(src_dir.join("../jemalloc/configure")
cmd.arg(src_dir.join("configure")
.to_str()
.unwrap()
.replace("C:\\", "/c/")
@ -158,6 +161,7 @@ fn main() {
}
run(&mut cmd);
let mut make = Command::new(build_helper::make(&host));
make.current_dir(&build_dir)
.arg("build_lib_static");
@ -170,18 +174,6 @@ fn main() {
run(&mut make);
if target.contains("windows") {
println!("cargo:rustc-link-lib=static=jemalloc");
} else {
println!("cargo:rustc-link-lib=static=jemalloc_pic");
}
println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
if target.contains("android") {
println!("cargo:rustc-link-lib=gcc");
} else if !target.contains("windows") && !target.contains("musl") {
println!("cargo:rustc-link-lib=pthread");
}
// The pthread_atfork symbols is used by jemalloc on android but the really
// old android we're building on doesn't have them defined, so just make
// sure the symbols are available.

View file

@ -13,11 +13,10 @@
extern crate gcc;
extern crate build_helper;
use std::env;
use std::{env, fs};
use std::path::PathBuf;
use std::process::Command;
use build_helper::run;
use build_helper::{run, rerun_if_changed_anything_in_dir};
fn main() {
println!("cargo:rustc-cfg=cargobuild");
@ -66,24 +65,18 @@ fn main() {
}
fn build_libbacktrace(host: &str, target: &str) {
let src_dir = env::current_dir().unwrap().join("../libbacktrace");
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
let build_dir = PathBuf::from(build_dir).join("libbacktrace");
let _ = fs::create_dir_all(&build_dir);
println!("cargo:rustc-link-lib=static=backtrace");
println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
let mut stack = src_dir.read_dir().unwrap()
.map(|e| e.unwrap())
.collect::<Vec<_>>();
while let Some(entry) = stack.pop() {
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
} else {
println!("cargo:rerun-if-changed={}", path.display());
}
if !cfg!(stage0) {
return
}
let src_dir = env::current_dir().unwrap().join("../libbacktrace");
rerun_if_changed_anything_in_dir(&src_dir);
let compiler = gcc::Config::new().get_compiler();
// only msvc returns None for ar so unwrap is okay
let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
@ -105,6 +98,7 @@ fn build_libbacktrace(host: &str, target: &str) {
.env("AR", &ar)
.env("RANLIB", format!("{} s", ar.display()))
.env("CFLAGS", cflags));
run(Command::new(build_helper::make(host))
.current_dir(&build_dir)
.arg(format!("INCDIR={}", src_dir.display()))