]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/build.rs
Rollup merge of #34175 - rwz:patch-2, r=alexcrichton
[rust.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;
20
21 fn main() {
22     println!("cargo:rustc-cfg=cargobuild");
23     println!("cargo:rerun-if-changed=build.rs");
24
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();
29
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") {
37             "static"
38         } else {
39             "dylib"
40         };
41         println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
42         return;
43     }
44
45     let compiler = gcc::Config::new().get_compiler();
46     let ar = build_helper::cc2ar(compiler.path(), &target);
47     let cflags = compiler.args()
48                          .iter()
49                          .map(|s| s.to_str().unwrap())
50                          .collect::<Vec<_>>()
51                          .join(" ");
52
53     let mut stack = src_dir.join("../jemalloc")
54                            .read_dir()
55                            .unwrap()
56                            .map(|e| e.unwrap())
57                            .collect::<Vec<_>>();
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()));
62         } else {
63             println!("cargo:rerun-if-changed={}", path.display());
64         }
65     }
66
67     let mut cmd = Command::new("sh");
68     cmd.arg(src_dir.join("../jemalloc/configure")
69                    .to_str()
70                    .unwrap()
71                    .replace("C:\\", "/c/")
72                    .replace("\\", "/"))
73        .current_dir(&build_dir)
74        .env("CC", compiler.path())
75        .env("EXTRA_CFLAGS", cflags)
76        .env("AR", &ar)
77        .env("RANLIB", format!("{} s", ar.display()));
78
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:
83         //
84         //  http://sourceforge.net/p/mingw-w64/bugs/395/
85         //
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.
91         //
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
95         // hopefully...
96         //
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.
110         //
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_");
117     }
118
119     if cfg!(feature = "debug-jemalloc") {
120         cmd.arg("--enable-debug");
121     }
122
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)));
127
128     run(&mut cmd);
129     run(Command::new("make")
130             .current_dir(&build_dir)
131             .arg("build_lib_static")
132             .arg("-j")
133             .arg(env::var("NUM_JOBS").unwrap()));
134
135     if target.contains("windows") {
136         println!("cargo:rustc-link-lib=static=jemalloc");
137     } else {
138         println!("cargo:rustc-link-lib=static=jemalloc_pic");
139     }
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");
145     }
146 }