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.
14 extern crate build_helper;
18 use std::fs::{self, File};
19 use std::path::{Path, PathBuf};
20 use std::process::Command;
21 use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date};
24 println!("cargo:rustc-cfg=cargobuild");
25 println!("cargo:rerun-if-changed=build.rs");
27 // FIXME: This is a hack to support building targets that don't
28 // support jemalloc alongside hosts that do. The jemalloc build is
29 // controlled by a feature of the std crate, and if that feature
30 // changes between targets, it invalidates the fingerprint of
31 // std's build script (this is a cargo bug); so we must ensure
32 // that the feature set used by std is the same across all
33 // targets, which means we have to build the alloc_jemalloc crate
34 // for targets like emscripten, even if we don't use it.
35 let target = env::var("TARGET").expect("TARGET was not set");
36 let host = env::var("HOST").expect("HOST was not set");
37 if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
38 target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
39 target.contains("redox") {
40 println!("cargo:rustc-cfg=dummy_jemalloc");
44 if target.contains("android") {
45 println!("cargo:rustc-link-lib=gcc");
46 } else if !target.contains("windows") && !target.contains("musl") {
47 println!("cargo:rustc-link-lib=pthread");
50 if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
51 let jemalloc = PathBuf::from(jemalloc);
52 println!("cargo:rustc-link-search=native={}",
53 jemalloc.parent().unwrap().display());
54 let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
55 let name = jemalloc.file_name().unwrap().to_str().unwrap();
56 let kind = if name.ends_with(".a") {
61 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
65 let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
66 let build_dir = PathBuf::from(build_dir).join("jemalloc");
67 let _ = fs::create_dir_all(&build_dir);
69 if target.contains("windows") {
70 println!("cargo:rustc-link-lib=static=jemalloc");
72 println!("cargo:rustc-link-lib=static=jemalloc_pic");
74 println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
75 let src_dir = env::current_dir().unwrap().join("../jemalloc");
76 rerun_if_changed_anything_in_dir(&src_dir);
77 let timestamp = build_dir.join("rustbuild.timestamp");
78 if up_to_date(&Path::new("build.rs"), ×tamp) && up_to_date(&src_dir, ×tamp) {
82 let compiler = gcc::Config::new().get_compiler();
83 // only msvc returns None for ar so unwrap is okay
84 let ar = build_helper::cc2ar(compiler.path(), &target).unwrap();
85 let cflags = compiler.args()
87 .map(|s| s.to_str().unwrap())
91 let mut cmd = Command::new("sh");
92 cmd.arg(src_dir.join("configure")
95 .replace("C:\\", "/c/")
97 .current_dir(&build_dir)
98 .env("CC", compiler.path())
99 .env("EXTRA_CFLAGS", cflags.clone())
100 // jemalloc generates Makefile deps using GCC's "-MM" flag. This means
101 // that GCC will run the preprocessor, and only the preprocessor, over
102 // jemalloc's source files. If we don't specify CPPFLAGS, then at least
103 // on ARM that step fails with a "Missing implementation for 32-bit
104 // atomic operations" error. This is because no "-march" flag will be
105 // passed to GCC, and then GCC won't define the
106 // "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" macro that jemalloc needs to
107 // select an atomic operation implementation.
108 .env("CPPFLAGS", cflags.clone())
110 .env("RANLIB", format!("{} s", ar.display()));
112 if target.contains("windows") {
113 // A bit of history here, this used to be --enable-lazy-lock added in
114 // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
115 // was also reported to MinGW:
117 // http://sourceforge.net/p/mingw-w64/bugs/395/
119 // When updating jemalloc to 4.0, however, it was found that binaries
120 // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
121 // that a thread was unlocking a mutex it never locked. Disabling this
122 // "lazy lock" option seems to fix the issue, but it was enabled by
123 // default for MinGW targets in 13473c7 for jemalloc.
125 // As a result of all that, force disabling lazy lock on Windows, and
126 // after reading some code it at least *appears* that the initialization
127 // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
130 // tl;dr: make windows behave like other platforms by disabling lazy
131 // locking, but requires passing an option due to a historical
132 // default with jemalloc.
133 cmd.arg("--disable-lazy-lock");
134 } else if target.contains("ios") {
135 cmd.arg("--disable-tls");
136 } else if target.contains("android") {
137 // We force android to have prefixed symbols because apparently
138 // replacement of the libc allocator doesn't quite work. When this was
139 // tested (unprefixed symbols), it was found that the `realpath`
140 // function in libc would allocate with libc malloc (not jemalloc
141 // malloc), and then the standard library would free with jemalloc free,
142 // causing a segfault.
144 // If the test suite passes, however, without symbol prefixes then we
145 // should be good to go!
146 cmd.arg("--with-jemalloc-prefix=je_");
147 cmd.arg("--disable-tls");
148 } else if target.contains("dragonfly") {
149 cmd.arg("--with-jemalloc-prefix=je_");
152 if cfg!(feature = "debug-jemalloc") {
153 cmd.arg("--enable-debug");
156 // Turn off broken quarantine (see jemalloc/jemalloc#161)
157 cmd.arg("--disable-fill");
158 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
159 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
161 // for some reason, jemalloc configure doesn't detect this value
162 // automatically for this target
163 if target == "sparc64-unknown-linux-gnu" {
164 cmd.arg("--with-lg-quantum=4");
169 let mut make = Command::new(build_helper::make(&host));
170 make.current_dir(&build_dir)
171 .arg("build_lib_static");
173 // mingw make seems... buggy? unclear...
174 if !host.contains("windows") {
176 .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
181 // The pthread_atfork symbols is used by jemalloc on android but the really
182 // old android we're building on doesn't have them defined, so just make
183 // sure the symbols are available.
184 if target.contains("androideabi") {
185 println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
187 .flag("-fvisibility=hidden")
188 .file("pthread_atfork_dummy.c")
189 .compile("libpthread_atfork_dummy.a");
192 t!(File::create(×tamp));