]> git.proxmox.com Git - rustc.git/blob - src/liballoc_jemalloc/build.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / liballoc_jemalloc / build.rs
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.
4 //
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.
10
11 #![deny(warnings)]
12
13 extern crate build_helper;
14 extern crate gcc;
15
16 use std::env;
17 use std::path::PathBuf;
18 use std::process::Command;
19 use build_helper::{run, native_lib_boilerplate};
20
21 fn main() {
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");
36 return;
37 }
38
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");
43 }
44
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") {
52 "static"
53 } else {
54 "dylib"
55 };
56 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
57 return;
58 }
59
60 let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
61 let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") {
62 Ok(native) => native,
63 _ => return,
64 };
65
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()
70 .iter()
71 .map(|s| s.to_str().unwrap())
72 .collect::<Vec<_>>()
73 .join(" ");
74
75 let mut cmd = Command::new("sh");
76 cmd.arg(native.src_dir.join("configure")
77 .to_str()
78 .unwrap()
79 .replace("C:\\", "/c/")
80 .replace("\\", "/"))
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())
93 .env("AR", &ar)
94 .env("RANLIB", format!("{} s", ar.display()));
95
96 if target.contains("windows") {
97 // A bit of history here, this used to be --enable-lazy-lock added in
98 // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
99 // was also reported to MinGW:
100 //
101 // http://sourceforge.net/p/mingw-w64/bugs/395/
102 //
103 // When updating jemalloc to 4.0, however, it was found that binaries
104 // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
105 // that a thread was unlocking a mutex it never locked. Disabling this
106 // "lazy lock" option seems to fix the issue, but it was enabled by
107 // default for MinGW targets in 13473c7 for jemalloc.
108 //
109 // As a result of all that, force disabling lazy lock on Windows, and
110 // after reading some code it at least *appears* that the initialization
111 // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
112 // hopefully...
113 //
114 // tl;dr: make windows behave like other platforms by disabling lazy
115 // locking, but requires passing an option due to a historical
116 // default with jemalloc.
117 cmd.arg("--disable-lazy-lock");
118 } else if target.contains("ios") {
119 cmd.arg("--disable-tls");
120 } else if target.contains("android") {
121 // We force android to have prefixed symbols because apparently
122 // replacement of the libc allocator doesn't quite work. When this was
123 // tested (unprefixed symbols), it was found that the `realpath`
124 // function in libc would allocate with libc malloc (not jemalloc
125 // malloc), and then the standard library would free with jemalloc free,
126 // causing a segfault.
127 //
128 // If the test suite passes, however, without symbol prefixes then we
129 // should be good to go!
130 cmd.arg("--with-jemalloc-prefix=je_");
131 cmd.arg("--disable-tls");
132 } else if target.contains("dragonfly") {
133 cmd.arg("--with-jemalloc-prefix=je_");
134 }
135
136 if cfg!(feature = "debug-jemalloc") {
137 cmd.arg("--enable-debug");
138 }
139
140 // Turn off broken quarantine (see jemalloc/jemalloc#161)
141 cmd.arg("--disable-fill");
142 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
143 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
144
145 // for some reason, jemalloc configure doesn't detect this value
146 // automatically for this target
147 if target == "sparc64-unknown-linux-gnu" {
148 cmd.arg("--with-lg-quantum=4");
149 }
150
151 run(&mut cmd);
152
153 let mut make = Command::new(build_helper::make(&host));
154 make.current_dir(&native.out_dir)
155 .arg("build_lib_static");
156
157 // mingw make seems... buggy? unclear...
158 if !host.contains("windows") {
159 make.arg("-j")
160 .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
161 }
162
163 run(&mut make);
164
165 // The pthread_atfork symbols is used by jemalloc on android but the really
166 // old android we're building on doesn't have them defined, so just make
167 // sure the symbols are available.
168 if target.contains("androideabi") {
169 println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
170 gcc::Config::new()
171 .flag("-fvisibility=hidden")
172 .file("pthread_atfork_dummy.c")
173 .compile("libpthread_atfork_dummy.a");
174 }
175 }