1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 extern crate build_helper;
17 use std::path::PathBuf;
18 use std::process::Command;
19 use build_helper::{run, native_lib_boilerplate};
22 // FIXME: This is a hack to support building targets that don't
23 // support jemalloc alongside hosts that do. The jemalloc build is
24 // controlled by a feature of the std crate, and if that feature
25 // changes between targets, it invalidates the fingerprint of
26 // std's build script (this is a cargo bug); so we must ensure
27 // that the feature set used by std is the same across all
28 // targets, which means we have to build the alloc_jemalloc crate
29 // for targets like emscripten, even if we don't use it.
30 let target = env::var("TARGET").expect("TARGET was not set");
31 let host = env::var("HOST").expect("HOST was not set");
32 if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
33 target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
34 target.contains("redox") {
35 println!("cargo:rustc-cfg=dummy_jemalloc");
39 if target.contains("android") {
40 println!("cargo:rustc-link-lib=gcc");
41 } else if !target.contains("windows") && !target.contains("musl") {
42 println!("cargo:rustc-link-lib=pthread");
45 if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
46 let jemalloc = PathBuf::from(jemalloc);
47 println!("cargo:rustc-link-search=native={}",
48 jemalloc.parent().unwrap().display());
49 let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
50 let name = jemalloc.file_name().unwrap().to_str().unwrap();
51 let kind = if name.ends_with(".a") {
56 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
60 let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
61 let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") {
66 let compiler = gcc::Config::new().get_compiler();
67 // only msvc returns None for ar so unwrap is okay
68 let ar = build_helper::cc2ar(compiler.path(), &target).unwrap();
69 let cflags = compiler.args()
71 .map(|s| s.to_str().unwrap())
75 let mut cmd = Command::new("sh");
76 cmd.arg(native.src_dir.join("configure")
79 .replace("C:\\", "/c/")
81 .current_dir(&native.out_dir)
82 .env("CC", compiler.path())
83 .env("EXTRA_CFLAGS", cflags.clone())
84 // jemalloc generates Makefile deps using GCC's "-MM" flag. This means
85 // that GCC will run the preprocessor, and only the preprocessor, over
86 // jemalloc's source files. If we don't specify CPPFLAGS, then at least
87 // on ARM that step fails with a "Missing implementation for 32-bit
88 // atomic operations" error. This is because no "-march" flag will be
89 // passed to GCC, and then GCC won't define the
90 // "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" macro that jemalloc needs to
91 // select an atomic operation implementation.
92 .env("CPPFLAGS", cflags.clone())
94 .env("RANLIB", format!("{} s", ar.display()));
96 if target.contains("ios") {
97 cmd.arg("--disable-tls");
98 } else if target.contains("android") {
99 // We force android to have prefixed symbols because apparently
100 // replacement of the libc allocator doesn't quite work. When this was
101 // tested (unprefixed symbols), it was found that the `realpath`
102 // function in libc would allocate with libc malloc (not jemalloc
103 // malloc), and then the standard library would free with jemalloc free,
104 // causing a segfault.
106 // If the test suite passes, however, without symbol prefixes then we
107 // should be good to go!
108 cmd.arg("--with-jemalloc-prefix=je_");
109 cmd.arg("--disable-tls");
110 } else if target.contains("dragonfly") || target.contains("musl") {
111 cmd.arg("--with-jemalloc-prefix=je_");
114 if cfg!(feature = "debug-jemalloc") {
115 cmd.arg("--enable-debug");
118 // Turn off broken quarantine (see jemalloc/jemalloc#161)
119 cmd.arg("--disable-fill");
120 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
121 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
123 // for some reason, jemalloc configure doesn't detect this value
124 // automatically for this target
125 if target == "sparc64-unknown-linux-gnu" {
126 cmd.arg("--with-lg-quantum=4");
131 let mut make = Command::new(build_helper::make(&host));
132 make.current_dir(&native.out_dir)
133 .arg("build_lib_static");
135 // mingw make seems... buggy? unclear...
136 if !host.contains("windows") {
138 .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
143 // The pthread_atfork symbols is used by jemalloc on android but the really
144 // old android we're building on doesn't have them defined, so just make
145 // sure the symbols are available.
146 if target.contains("androideabi") {
147 println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
149 .flag("-fvisibility=hidden")
150 .file("pthread_atfork_dummy.c")
151 .compile("libpthread_atfork_dummy.a");