]>
git.proxmox.com Git - rustc.git/blob - 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.
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 // only msvc returns None for ar so unwrap is okay
47 let ar
= build_helper
::cc2ar(compiler
.path(), &target
).unwrap();
48 let cflags
= compiler
.args()
50 .map(|s
| s
.to_str().unwrap())
54 let mut stack
= src_dir
.join("../jemalloc")
59 while let Some(entry
) = stack
.pop() {
60 let path
= entry
.path();
61 if entry
.file_type().unwrap().is_dir() {
62 stack
.extend(path
.read_dir().unwrap().map(|e
| e
.unwrap()));
64 println
!("cargo:rerun-if-changed={}", path
.display());
68 let mut cmd
= Command
::new("sh");
69 cmd
.arg(src_dir
.join("../jemalloc/configure")
72 .replace("C:\\", "/c/")
74 .current_dir(&build_dir
)
75 .env("CC", compiler
.path())
76 .env("EXTRA_CFLAGS", cflags
.clone())
77 // jemalloc generates Makefile deps using GCC's "-MM" flag. This means
78 // that GCC will run the preprocessor, and only the preprocessor, over
79 // jemalloc's source files. If we don't specify CPPFLAGS, then at least
80 // on ARM that step fails with a "Missing implementation for 32-bit
81 // atomic operations" error. This is because no "-march" flag will be
82 // passed to GCC, and then GCC won't define the
83 // "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" macro that jemalloc needs to
84 // select an atomic operation implementation.
85 .env("CPPFLAGS", cflags
.clone())
87 .env("RANLIB", format
!("{} s", ar
.display()));
89 if target
.contains("windows") {
90 // A bit of history here, this used to be --enable-lazy-lock added in
91 // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
92 // was also reported to MinGW:
94 // http://sourceforge.net/p/mingw-w64/bugs/395/
96 // When updating jemalloc to 4.0, however, it was found that binaries
97 // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
98 // that a thread was unlocking a mutex it never locked. Disabling this
99 // "lazy lock" option seems to fix the issue, but it was enabled by
100 // default for MinGW targets in 13473c7 for jemalloc.
102 // As a result of all that, force disabling lazy lock on Windows, and
103 // after reading some code it at least *appears* that the initialization
104 // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
107 // tl;dr: make windows behave like other platforms by disabling lazy
108 // locking, but requires passing an option due to a historical
109 // default with jemalloc.
110 cmd
.arg("--disable-lazy-lock");
111 } else if target
.contains("ios") {
112 cmd
.arg("--disable-tls");
113 } else if target
.contains("android") {
114 // We force android to have prefixed symbols because apparently
115 // replacement of the libc allocator doesn't quite work. When this was
116 // tested (unprefixed symbols), it was found that the `realpath`
117 // function in libc would allocate with libc malloc (not jemalloc
118 // malloc), and then the standard library would free with jemalloc free,
119 // causing a segfault.
121 // If the test suite passes, however, without symbol prefixes then we
122 // should be good to go!
123 cmd
.arg("--with-jemalloc-prefix=je_");
124 cmd
.arg("--disable-tls");
125 } else if target
.contains("dragonfly") {
126 cmd
.arg("--with-jemalloc-prefix=je_");
129 if cfg
!(feature
= "debug-jemalloc") {
130 cmd
.arg("--enable-debug");
133 // Turn off broken quarantine (see jemalloc/jemalloc#161)
134 cmd
.arg("--disable-fill");
135 cmd
.arg(format
!("--host={}", build_helper
::gnu_target(&target
)));
136 cmd
.arg(format
!("--build={}", build_helper
::gnu_target(&host
)));
139 run(Command
::new("make")
140 .current_dir(&build_dir
)
141 .arg("build_lib_static")
143 .arg(env
::var("NUM_JOBS").unwrap()));
145 if target
.contains("windows") {
146 println
!("cargo:rustc-link-lib=static=jemalloc");
148 println
!("cargo:rustc-link-lib=static=jemalloc_pic");
150 println
!("cargo:rustc-link-search=native={}/lib", build_dir
.display());
151 if target
.contains("android") {
152 println
!("cargo:rustc-link-lib=gcc");
153 } else if !target
.contains("windows") && !target
.contains("musl") {
154 println
!("cargo:rustc-link-lib=pthread");