2016-01-21 17:21:13 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-04-29 12:39:28 -05:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2017-01-31 15:27:51 -06:00
|
|
|
#[macro_use]
|
2016-01-21 17:21:13 -06:00
|
|
|
extern crate build_helper;
|
|
|
|
extern crate gcc;
|
|
|
|
|
|
|
|
use std::env;
|
2017-03-02 17:15:56 -06:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::PathBuf;
|
2016-01-21 17:21:13 -06:00
|
|
|
use std::process::Command;
|
2017-03-02 17:15:56 -06:00
|
|
|
use build_helper::{run, native_lib_boilerplate};
|
2016-01-21 17:21:13 -06:00
|
|
|
|
|
|
|
fn main() {
|
2016-09-27 16:27:22 -05:00
|
|
|
// 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
|
|
|
|
// changes between targets, it invalidates the fingerprint of
|
|
|
|
// std's build script (this is a cargo bug); so we must ensure
|
|
|
|
// 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.
|
2017-01-26 16:57:30 -06:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
2017-01-28 07:05:13 -06:00
|
|
|
let host = env::var("HOST").expect("HOST was not set");
|
2016-10-16 05:11:01 -05:00
|
|
|
if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
|
2016-12-15 09:23:33 -06:00
|
|
|
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
|
|
|
|
target.contains("redox") {
|
2016-09-27 16:27:22 -05:00
|
|
|
println!("cargo:rustc-cfg=dummy_jemalloc");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 01:15:34 -06:00
|
|
|
if target.contains("android") {
|
|
|
|
println!("cargo:rustc-link-lib=gcc");
|
|
|
|
} else if !target.contains("windows") && !target.contains("musl") {
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
|
|
|
|
let jemalloc = PathBuf::from(jemalloc);
|
|
|
|
println!("cargo:rustc-link-search=native={}",
|
|
|
|
jemalloc.parent().unwrap().display());
|
|
|
|
let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
|
|
|
|
let name = jemalloc.file_name().unwrap().to_str().unwrap();
|
2016-06-05 01:09:17 -05:00
|
|
|
let kind = if name.ends_with(".a") {
|
|
|
|
"static"
|
|
|
|
} else {
|
|
|
|
"dylib"
|
|
|
|
};
|
2016-01-21 17:21:13 -06:00
|
|
|
println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
|
2016-06-05 01:09:17 -05:00
|
|
|
return;
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|
|
|
|
|
2017-03-02 17:15:56 -06:00
|
|
|
let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
|
|
|
|
let native = native_lib_boilerplate("jemalloc", "jemalloc", link_name,
|
|
|
|
"rustbuild.timestamp", "lib");
|
|
|
|
if native.skip_build {
|
2017-01-26 16:57:30 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
let compiler = gcc::Config::new().get_compiler();
|
2016-06-16 07:38:06 -05:00
|
|
|
// only msvc returns None for ar so unwrap is okay
|
|
|
|
let ar = build_helper::cc2ar(compiler.path(), &target).unwrap();
|
2016-06-05 01:09:17 -05:00
|
|
|
let cflags = compiler.args()
|
2016-10-16 05:11:01 -05:00
|
|
|
.iter()
|
|
|
|
.map(|s| s.to_str().unwrap())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(" ");
|
2016-01-21 17:21:13 -06:00
|
|
|
|
|
|
|
let mut cmd = Command::new("sh");
|
2017-03-02 17:15:56 -06:00
|
|
|
cmd.arg(native.src_dir.join("configure")
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.replace("C:\\", "/c/")
|
|
|
|
.replace("\\", "/"))
|
|
|
|
.current_dir(&native.out_dir)
|
2016-01-21 17:21:13 -06:00
|
|
|
.env("CC", compiler.path())
|
2016-04-27 20:02:31 -05:00
|
|
|
.env("EXTRA_CFLAGS", cflags.clone())
|
|
|
|
// jemalloc generates Makefile deps using GCC's "-MM" flag. This means
|
|
|
|
// that GCC will run the preprocessor, and only the preprocessor, over
|
|
|
|
// jemalloc's source files. If we don't specify CPPFLAGS, then at least
|
|
|
|
// on ARM that step fails with a "Missing implementation for 32-bit
|
|
|
|
// atomic operations" error. This is because no "-march" flag will be
|
|
|
|
// passed to GCC, and then GCC won't define the
|
|
|
|
// "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" macro that jemalloc needs to
|
|
|
|
// select an atomic operation implementation.
|
|
|
|
.env("CPPFLAGS", cflags.clone())
|
2016-01-21 17:21:13 -06:00
|
|
|
.env("AR", &ar)
|
|
|
|
.env("RANLIB", format!("{} s", ar.display()));
|
|
|
|
|
2016-02-07 00:54:35 -06:00
|
|
|
if target.contains("windows") {
|
2016-01-21 17:21:13 -06:00
|
|
|
// A bit of history here, this used to be --enable-lazy-lock added in
|
|
|
|
// #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
|
|
|
|
// was also reported to MinGW:
|
|
|
|
//
|
|
|
|
// http://sourceforge.net/p/mingw-w64/bugs/395/
|
|
|
|
//
|
|
|
|
// When updating jemalloc to 4.0, however, it was found that binaries
|
|
|
|
// would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
|
|
|
|
// that a thread was unlocking a mutex it never locked. Disabling this
|
|
|
|
// "lazy lock" option seems to fix the issue, but it was enabled by
|
|
|
|
// default for MinGW targets in 13473c7 for jemalloc.
|
|
|
|
//
|
|
|
|
// As a result of all that, force disabling lazy lock on Windows, and
|
|
|
|
// after reading some code it at least *appears* that the initialization
|
|
|
|
// of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
|
|
|
|
// hopefully...
|
|
|
|
//
|
|
|
|
// tl;dr: make windows behave like other platforms by disabling lazy
|
|
|
|
// locking, but requires passing an option due to a historical
|
|
|
|
// default with jemalloc.
|
|
|
|
cmd.arg("--disable-lazy-lock");
|
2016-02-07 00:54:35 -06:00
|
|
|
} else if target.contains("ios") {
|
|
|
|
cmd.arg("--disable-tls");
|
|
|
|
} else if target.contains("android") {
|
|
|
|
// We force android to have prefixed symbols because apparently
|
|
|
|
// replacement of the libc allocator doesn't quite work. When this was
|
|
|
|
// tested (unprefixed symbols), it was found that the `realpath`
|
|
|
|
// function in libc would allocate with libc malloc (not jemalloc
|
|
|
|
// malloc), and then the standard library would free with jemalloc free,
|
|
|
|
// causing a segfault.
|
|
|
|
//
|
|
|
|
// If the test suite passes, however, without symbol prefixes then we
|
|
|
|
// should be good to go!
|
|
|
|
cmd.arg("--with-jemalloc-prefix=je_");
|
2016-01-21 17:21:13 -06:00
|
|
|
cmd.arg("--disable-tls");
|
2016-04-02 11:40:59 -05:00
|
|
|
} else if target.contains("dragonfly") {
|
|
|
|
cmd.arg("--with-jemalloc-prefix=je_");
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg!(feature = "debug-jemalloc") {
|
|
|
|
cmd.arg("--enable-debug");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn off broken quarantine (see jemalloc/jemalloc#161)
|
|
|
|
cmd.arg("--disable-fill");
|
|
|
|
cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
|
|
|
|
cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
|
|
|
|
|
2016-12-30 00:34:06 -06:00
|
|
|
// for some reason, jemalloc configure doesn't detect this value
|
|
|
|
// automatically for this target
|
|
|
|
if target == "sparc64-unknown-linux-gnu" {
|
|
|
|
cmd.arg("--with-lg-quantum=4");
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:13 -06:00
|
|
|
run(&mut cmd);
|
2017-01-26 16:57:30 -06:00
|
|
|
|
2016-12-17 13:09:23 -06:00
|
|
|
let mut make = Command::new(build_helper::make(&host));
|
2017-03-02 17:15:56 -06:00
|
|
|
make.current_dir(&native.out_dir)
|
2016-11-16 14:31:19 -06:00
|
|
|
.arg("build_lib_static");
|
|
|
|
|
|
|
|
// mingw make seems... buggy? unclear...
|
|
|
|
if !host.contains("windows") {
|
|
|
|
make.arg("-j")
|
|
|
|
.arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
|
|
|
|
}
|
|
|
|
|
|
|
|
run(&mut make);
|
2016-01-21 17:21:13 -06:00
|
|
|
|
2017-01-22 14:53:35 -06:00
|
|
|
// 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.
|
|
|
|
if target.contains("androideabi") {
|
|
|
|
println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
|
|
|
|
gcc::Config::new()
|
|
|
|
.flag("-fvisibility=hidden")
|
|
|
|
.file("pthread_atfork_dummy.c")
|
|
|
|
.compile("libpthread_atfork_dummy.a");
|
|
|
|
}
|
2017-01-31 15:27:51 -06:00
|
|
|
|
2017-03-02 17:15:56 -06:00
|
|
|
t!(File::create(&native.timestamp));
|
2016-01-21 17:21:13 -06:00
|
|
|
}
|