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;
22 println!("cargo:rustc-cfg=cargobuild");
23 println!("cargo:rerun-if-changed=build.rs");
25 let target = env::var("TARGET").unwrap();
26 let host = env::var("HOST").unwrap();
27 let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
28 let src_dir = env::current_dir().unwrap();
30 if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
31 let jemalloc = PathBuf::from(jemalloc);
32 println!("cargo:rustc-link-search=native={}",
33 jemalloc.parent().unwrap().display());
34 let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
35 let name = jemalloc.file_name().unwrap().to_str().unwrap();
36 let kind = if name.ends_with(".a") {
41 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
45 let compiler = gcc::Config::new().get_compiler();
46 let ar = build_helper::cc2ar(compiler.path(), &target);
47 let cflags = compiler.args()
49 .map(|s| s.to_str().unwrap())
53 let mut stack = src_dir.join("../jemalloc")
58 while let Some(entry) = stack.pop() {
59 let path = entry.path();
60 if entry.file_type().unwrap().is_dir() {
61 stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
63 println!("cargo:rerun-if-changed={}", path.display());
67 let mut cmd = Command::new("sh");
68 cmd.arg(src_dir.join("../jemalloc/configure")
71 .replace("C:\\", "/c/")
73 .current_dir(&build_dir)
74 .env("CC", compiler.path())
75 .env("EXTRA_CFLAGS", cflags)
77 .env("RANLIB", format!("{} s", ar.display()));
79 if target.contains("windows") {
80 // A bit of history here, this used to be --enable-lazy-lock added in
81 // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
82 // was also reported to MinGW:
84 // http://sourceforge.net/p/mingw-w64/bugs/395/
86 // When updating jemalloc to 4.0, however, it was found that binaries
87 // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
88 // that a thread was unlocking a mutex it never locked. Disabling this
89 // "lazy lock" option seems to fix the issue, but it was enabled by
90 // default for MinGW targets in 13473c7 for jemalloc.
92 // As a result of all that, force disabling lazy lock on Windows, and
93 // after reading some code it at least *appears* that the initialization
94 // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
97 // tl;dr: make windows behave like other platforms by disabling lazy
98 // locking, but requires passing an option due to a historical
99 // default with jemalloc.
100 cmd.arg("--disable-lazy-lock");
101 } else if target.contains("ios") {
102 cmd.arg("--disable-tls");
103 } else if target.contains("android") {
104 // We force android to have prefixed symbols because apparently
105 // replacement of the libc allocator doesn't quite work. When this was
106 // tested (unprefixed symbols), it was found that the `realpath`
107 // function in libc would allocate with libc malloc (not jemalloc
108 // malloc), and then the standard library would free with jemalloc free,
109 // causing a segfault.
111 // If the test suite passes, however, without symbol prefixes then we
112 // should be good to go!
113 cmd.arg("--with-jemalloc-prefix=je_");
114 cmd.arg("--disable-tls");
115 } else if target.contains("dragonfly") {
116 cmd.arg("--with-jemalloc-prefix=je_");
119 if cfg!(feature = "debug-jemalloc") {
120 cmd.arg("--enable-debug");
123 // Turn off broken quarantine (see jemalloc/jemalloc#161)
124 cmd.arg("--disable-fill");
125 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
126 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
129 run(Command::new("make")
130 .current_dir(&build_dir)
131 .arg("build_lib_static")
133 .arg(env::var("NUM_JOBS").unwrap()));
135 if target.contains("windows") {
136 println!("cargo:rustc-link-lib=static=jemalloc");
138 println!("cargo:rustc-link-lib=static=jemalloc_pic");
140 println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
141 if target.contains("android") {
142 println!("cargo:rustc-link-lib=gcc");
143 } else if !target.contains("windows") && !target.contains("musl") {
144 println!("cargo:rustc-link-lib=pthread");