2016-01-21 01:14:01 +00: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.
|
|
|
|
|
2017-09-30 07:47:14 +00:00
|
|
|
extern crate cc;
|
2018-05-04 14:46:30 +00:00
|
|
|
extern crate fs_extra;
|
2016-01-21 01:03:24 +00:00
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2018-05-04 14:46:30 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::{Path, PathBuf};
|
2016-01-21 01:03:24 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
2017-11-27 13:53:07 +00:00
|
|
|
fn gnu_target(target: &str) -> String {
|
|
|
|
match target {
|
|
|
|
"i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
|
|
|
|
"x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
|
|
|
|
"i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
|
|
|
|
"x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
|
|
|
|
s => s.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:03:24 +00:00
|
|
|
fn main() {
|
2017-11-27 18:07:57 +00:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
|
|
|
let host = env::var("HOST").expect("HOST was not set");
|
2018-05-02 08:04:09 +00:00
|
|
|
let num_jobs = env::var("NUM_JOBS").expect("NUM_JOBS was not set");
|
|
|
|
let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR was not set"));
|
|
|
|
println!("TARGET={}", target.clone());
|
|
|
|
println!("HOST={}", host.clone());
|
|
|
|
println!("NUM_JOBS={}", num_jobs.clone());
|
|
|
|
println!("OUT_DIR={:?}", out_dir);
|
|
|
|
let build_dir = out_dir.join("build");
|
|
|
|
println!("BUILD_DIR={:?}", build_dir);
|
|
|
|
let src_dir = env::current_dir().expect("failed to get current directory");
|
|
|
|
println!("SRC_DIR={:?}", src_dir);
|
|
|
|
|
2018-06-23 18:02:52 +00:00
|
|
|
let disable_bg_thread = !env::var("CARGO_FEATURE_BG_THREAD").is_ok();
|
2018-05-22 14:56:01 +00:00
|
|
|
|
2017-11-27 18:07:57 +00:00
|
|
|
let unsupported_targets = [
|
2018-05-03 15:00:07 +00:00
|
|
|
"rumprun",
|
|
|
|
"bitrig",
|
|
|
|
"openbsd",
|
|
|
|
"msvc",
|
|
|
|
"emscripten",
|
|
|
|
"fuchsia",
|
|
|
|
"redox",
|
|
|
|
"wasm32",
|
2017-11-27 18:07:57 +00:00
|
|
|
];
|
|
|
|
for i in &unsupported_targets {
|
|
|
|
if target.contains(i) {
|
|
|
|
panic!("jemalloc does not support target: {}", target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:03:24 +00:00
|
|
|
if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
|
2018-05-02 08:04:09 +00:00
|
|
|
println!("jemalloc override set");
|
2016-01-21 01:03:24 +00:00
|
|
|
let jemalloc = PathBuf::from(jemalloc);
|
2018-05-03 15:00:07 +00:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
jemalloc.parent().unwrap().display()
|
|
|
|
);
|
2016-01-21 01:03:24 +00:00
|
|
|
let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
|
|
|
|
let name = jemalloc.file_name().unwrap().to_str().unwrap();
|
2018-05-03 15:00:07 +00:00
|
|
|
let kind = if name.ends_with(".a") {
|
|
|
|
"static"
|
|
|
|
} else {
|
|
|
|
"dylib"
|
|
|
|
};
|
2016-01-21 01:03:24 +00:00
|
|
|
println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
|
2018-05-03 15:00:07 +00:00
|
|
|
return;
|
2016-01-21 01:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs::create_dir_all(&build_dir).unwrap();
|
2018-05-02 08:04:09 +00:00
|
|
|
// Disable -Wextra warnings - jemalloc doesn't compile free of warnings with
|
|
|
|
// it enabled: https://github.com/jemalloc/jemalloc/issues/1196
|
|
|
|
let compiler = cc::Build::new().extra_warnings(false).get_compiler();
|
2018-05-03 15:00:07 +00:00
|
|
|
let cflags = compiler
|
|
|
|
.args()
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.to_str().unwrap())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(" ");
|
2018-05-02 08:04:09 +00:00
|
|
|
println!("CC={:?}", compiler.path());
|
|
|
|
println!("CFLAGS={:?}", cflags);
|
|
|
|
|
2018-05-02 22:23:40 +00:00
|
|
|
let jemalloc_src_dir = out_dir.join("jemalloc");
|
2018-05-02 08:04:09 +00:00
|
|
|
println!("JEMALLOC_SRC_DIR={:?}", jemalloc_src_dir);
|
|
|
|
|
2018-05-02 22:23:40 +00:00
|
|
|
if jemalloc_src_dir.exists() {
|
2018-05-04 14:46:30 +00:00
|
|
|
fs::remove_dir_all(jemalloc_src_dir.clone()).unwrap();
|
2018-05-02 22:23:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:41:15 +00:00
|
|
|
// Copy jemalloc submodule to the OUT_DIR
|
2018-05-03 14:07:07 +00:00
|
|
|
assert!(out_dir.exists(), "OUT_DIR does not exist");
|
2018-05-04 14:46:30 +00:00
|
|
|
let mut copy_options = fs_extra::dir::CopyOptions::new();
|
|
|
|
copy_options.overwrite = true;
|
|
|
|
copy_options.copy_inside = true;
|
|
|
|
fs_extra::dir::copy(
|
|
|
|
Path::new("jemalloc"),
|
|
|
|
jemalloc_src_dir.clone(),
|
|
|
|
©_options,
|
|
|
|
).expect("failed to copy jemalloc source code to OUT_DIR");
|
2018-05-02 22:23:40 +00:00
|
|
|
|
2018-05-02 15:18:59 +00:00
|
|
|
// Configuration files
|
|
|
|
let config_files = ["configure", "VERSION"];
|
|
|
|
|
|
|
|
// Verify that the configuration files are up-to-date
|
|
|
|
if env::var_os("JEMALLOC_SYS_VERIFY_CONFIGURE").is_some() {
|
|
|
|
assert!(!jemalloc_src_dir.join("configure").exists(),
|
|
|
|
"the jemalloc source directory cannot contain configuration files like 'configure' and 'VERSION'");
|
2018-05-03 14:07:07 +00:00
|
|
|
// Run autoconf:
|
|
|
|
let mut cmd = Command::new("autoconf");
|
|
|
|
cmd.current_dir(jemalloc_src_dir.clone());
|
2018-05-02 09:53:10 +00:00
|
|
|
run(&mut cmd);
|
2017-11-27 18:07:57 +00:00
|
|
|
|
2018-05-02 15:18:59 +00:00
|
|
|
for f in &config_files {
|
2018-05-04 14:46:30 +00:00
|
|
|
use std::io::Read;
|
|
|
|
let mut file = File::open(jemalloc_src_dir.join(f)).expect("file not found");
|
|
|
|
let mut source_contents = String::new();
|
|
|
|
file.read_to_string(&mut source_contents)
|
|
|
|
.expect("failed to read file");
|
|
|
|
let mut file =
|
|
|
|
File::open(Path::new(&format!("configure/{}", f))).expect("file not found");
|
|
|
|
let mut reference_contents = String::new();
|
|
|
|
file.read_to_string(&mut reference_contents)
|
|
|
|
.expect("failed to read file");
|
|
|
|
if source_contents != reference_contents {
|
|
|
|
panic!("the file \"{}\" differs from the jemalloc source and the reference in \"jemalloc-sys/configure/{}\"", jemalloc_src_dir.join(f).display(), f);
|
|
|
|
}
|
2018-05-02 15:18:59 +00:00
|
|
|
}
|
2018-05-02 14:06:16 +00:00
|
|
|
} else {
|
2018-05-02 15:18:59 +00:00
|
|
|
// Copy the configuration files to jemalloc's source directory
|
|
|
|
for f in &config_files {
|
2018-05-04 14:46:30 +00:00
|
|
|
fs::copy(
|
|
|
|
Path::new(&format!("configure/{}", f)),
|
|
|
|
jemalloc_src_dir.join(f),
|
|
|
|
).expect("failed to copy config file to OUT_DIR");
|
2018-05-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-02 14:06:16 +00:00
|
|
|
|
2018-05-02 08:04:09 +00:00
|
|
|
// Run configure:
|
|
|
|
let configure = jemalloc_src_dir.join("configure");
|
2016-01-21 01:03:24 +00:00
|
|
|
let mut cmd = Command::new("sh");
|
2018-05-03 15:00:07 +00:00
|
|
|
cmd.arg(
|
|
|
|
configure
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.replace("C:\\", "/c/")
|
|
|
|
.replace("\\", "/"),
|
|
|
|
).current_dir(&build_dir)
|
2018-05-03 16:15:57 +00:00
|
|
|
.env("CC", compiler.path())
|
2018-05-03 15:00:07 +00:00
|
|
|
.env("CFLAGS", cflags.clone())
|
2018-05-03 16:15:57 +00:00
|
|
|
.env("LDFLAGS", cflags.clone())
|
2018-05-03 15:00:07 +00:00
|
|
|
.env("CPPFLAGS", cflags.clone())
|
|
|
|
.arg("--disable-cxx");
|
2016-01-21 01:03:24 +00:00
|
|
|
|
2017-11-27 13:53:07 +00:00
|
|
|
if target == "sparc64-unknown-linux-gnu" {
|
2018-05-03 17:19:01 +00:00
|
|
|
// jemalloc's configure doesn't detect this value
|
|
|
|
// automatically for this target:
|
2017-11-27 13:53:07 +00:00
|
|
|
cmd.arg("--with-lg-quantum=4");
|
2018-05-03 17:19:01 +00:00
|
|
|
// See: https://github.com/jemalloc/jemalloc/issues/999
|
|
|
|
cmd.arg("--disable-thp");
|
2017-11-27 13:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 14:56:01 +00:00
|
|
|
if disable_bg_thread {
|
|
|
|
cmd.arg("--with-malloc-conf=background_thread:false");
|
|
|
|
}
|
|
|
|
|
2018-06-24 05:11:34 +00:00
|
|
|
let mut use_prefix = !env::var_os("CARGO_FEATURE_NO_PREFIX").is_some();
|
|
|
|
if !use_prefix &&
|
|
|
|
(target.contains("android")
|
|
|
|
|| target.contains("dragonfly")
|
|
|
|
|| target.contains("musl")
|
|
|
|
|| target.contains("darwin"))
|
|
|
|
{
|
|
|
|
println!("ignoring no_prefix feature on unsupported platform");
|
|
|
|
use_prefix = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if use_prefix {
|
|
|
|
cmd.arg("--with-jemalloc-prefix=_rjem_");
|
|
|
|
println!("cargo:rustc-cfg=prefixed");
|
|
|
|
}
|
2017-07-07 18:07:16 +00:00
|
|
|
|
2018-06-24 20:14:48 +00:00
|
|
|
cmd.arg("--with-private-namespace=_rjem_");
|
|
|
|
|
2017-09-26 21:06:56 +00:00
|
|
|
if env::var_os("CARGO_FEATURE_DEBUG").is_some() {
|
2018-05-02 08:04:09 +00:00
|
|
|
println!("CARGO_FEATURE_DEBUG set");
|
2016-01-21 01:03:24 +00:00
|
|
|
cmd.arg("--enable-debug");
|
|
|
|
}
|
|
|
|
|
2016-08-07 07:55:49 +00:00
|
|
|
if env::var_os("CARGO_FEATURE_PROFILING").is_some() {
|
2018-06-24 05:11:34 +00:00
|
|
|
println!("CARGO_FEATURE_PROFILING set");
|
2016-08-07 07:55:49 +00:00
|
|
|
cmd.arg("--enable-prof");
|
|
|
|
}
|
2017-11-27 13:53:07 +00:00
|
|
|
cmd.arg(format!("--host={}", gnu_target(&target)));
|
|
|
|
cmd.arg(format!("--build={}", gnu_target(&host)));
|
2016-01-21 01:03:24 +00:00
|
|
|
cmd.arg(format!("--prefix={}", out_dir.display()));
|
|
|
|
|
|
|
|
run(&mut cmd);
|
2017-11-06 11:34:30 +00:00
|
|
|
|
2018-05-03 15:00:07 +00:00
|
|
|
let make = if host.contains("bitrig") || host.contains("dragonfly") || host.contains("freebsd")
|
|
|
|
|| host.contains("netbsd") || host.contains("openbsd")
|
|
|
|
{
|
2017-11-06 11:34:30 +00:00
|
|
|
"gmake"
|
|
|
|
} else {
|
|
|
|
"make"
|
|
|
|
};
|
|
|
|
|
2018-05-02 08:04:09 +00:00
|
|
|
// Make:
|
2017-11-06 11:34:30 +00:00
|
|
|
run(Command::new(make)
|
2018-05-02 08:04:09 +00:00
|
|
|
.current_dir(&build_dir)
|
2018-05-03 15:00:07 +00:00
|
|
|
.arg("-j")
|
|
|
|
.arg(num_jobs.clone()));
|
2018-05-02 08:04:09 +00:00
|
|
|
|
|
|
|
if env::var_os("JEMALLOC_SYS_RUN_TESTS").is_some() {
|
|
|
|
println!("JEMALLOC_SYS_RUN_TESTS set: building and running jemalloc tests...");
|
|
|
|
// Make tests:
|
|
|
|
run(Command::new(make)
|
|
|
|
.current_dir(&build_dir)
|
2018-05-03 15:00:07 +00:00
|
|
|
.arg("-j")
|
|
|
|
.arg(num_jobs.clone())
|
2018-05-02 08:04:09 +00:00
|
|
|
.arg("tests"));
|
|
|
|
|
|
|
|
// Run tests:
|
2018-05-03 15:00:07 +00:00
|
|
|
run(Command::new(make).current_dir(&build_dir).arg("check"));
|
2018-05-02 08:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make install:
|
|
|
|
run(Command::new(make)
|
|
|
|
.current_dir(&build_dir)
|
|
|
|
.arg("install_lib_static")
|
|
|
|
.arg("install_include")
|
2018-05-03 15:00:07 +00:00
|
|
|
.arg("-j")
|
|
|
|
.arg(num_jobs.clone()));
|
2016-01-21 01:03:24 +00:00
|
|
|
|
|
|
|
println!("cargo:root={}", out_dir.display());
|
|
|
|
|
|
|
|
// Linkage directives to pull in jemalloc and its dependencies.
|
|
|
|
//
|
|
|
|
// On some platforms we need to be sure to link in `pthread` which jemalloc
|
|
|
|
// depends on, and specifically on android we need to also link to libgcc.
|
|
|
|
// Currently jemalloc is compiled with gcc which will generate calls to
|
|
|
|
// intrinsics that are libgcc specific (e.g. those intrinsics aren't present in
|
|
|
|
// libcompiler-rt), so link that in to get that support.
|
|
|
|
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") {
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(cmd: &mut Command) {
|
|
|
|
println!("running: {:?}", cmd);
|
|
|
|
let status = match cmd.status() {
|
|
|
|
Ok(status) => status,
|
|
|
|
Err(e) => panic!("failed to execute command: {}", e),
|
|
|
|
};
|
|
|
|
if !status.success() {
|
2018-05-03 15:00:07 +00:00
|
|
|
panic!(
|
|
|
|
"command did not execute successfully: {:?}\n\
|
|
|
|
expected success, got: {}",
|
|
|
|
cmd, status
|
|
|
|
);
|
2016-01-21 01:03:24 +00:00
|
|
|
}
|
|
|
|
}
|