]> git.proxmox.com Git - rustc.git/blame - src/liballoc_jemalloc/build.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / liballoc_jemalloc / build.rs
CommitLineData
7453a54e
SL
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
a7813a04
XL
11#![deny(warnings)]
12
7453a54e
SL
13extern crate build_helper;
14extern crate gcc;
15
16use std::env;
17use std::path::PathBuf;
18use std::process::Command;
19use build_helper::run;
20
21fn main() {
22 println!("cargo:rustc-cfg=cargobuild");
a7813a04 23 println!("cargo:rerun-if-changed=build.rs");
7453a54e 24
9e0c209e
SL
25 let target = env::var("TARGET").expect("TARGET was not set");
26 let host = env::var("HOST").expect("HOST was not set");
7453a54e
SL
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();
3157f602
XL
36 let kind = if name.ends_with(".a") {
37 "static"
38 } else {
39 "dylib"
40 };
7453a54e 41 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
3157f602 42 return;
7453a54e
SL
43 }
44
45 let compiler = gcc::Config::new().get_compiler();
3157f602
XL
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()
49 .iter()
50 .map(|s| s.to_str().unwrap())
51 .collect::<Vec<_>>()
52 .join(" ");
7453a54e 53
a7813a04 54 let mut stack = src_dir.join("../jemalloc")
3157f602
XL
55 .read_dir()
56 .unwrap()
a7813a04
XL
57 .map(|e| e.unwrap())
58 .collect::<Vec<_>>();
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()));
63 } else {
64 println!("cargo:rerun-if-changed={}", path.display());
65 }
66 }
67
7453a54e 68 let mut cmd = Command::new("sh");
3157f602
XL
69 cmd.arg(src_dir.join("../jemalloc/configure")
70 .to_str()
71 .unwrap()
7453a54e
SL
72 .replace("C:\\", "/c/")
73 .replace("\\", "/"))
74 .current_dir(&build_dir)
75 .env("CC", compiler.path())
5bcae85e
SL
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())
7453a54e
SL
86 .env("AR", &ar)
87 .env("RANLIB", format!("{} s", ar.display()));
88
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:
93 //
94 // http://sourceforge.net/p/mingw-w64/bugs/395/
95 //
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.
101 //
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
105 // hopefully...
106 //
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.
120 //
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");
54a0048b
SL
125 } else if target.contains("dragonfly") {
126 cmd.arg("--with-jemalloc-prefix=je_");
7453a54e
SL
127 }
128
129 if cfg!(feature = "debug-jemalloc") {
130 cmd.arg("--enable-debug");
131 }
132
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)));
137
138 run(&mut cmd);
139 run(Command::new("make")
3157f602
XL
140 .current_dir(&build_dir)
141 .arg("build_lib_static")
142 .arg("-j")
9e0c209e 143 .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
7453a54e
SL
144
145 if target.contains("windows") {
146 println!("cargo:rustc-link-lib=static=jemalloc");
147 } else {
148 println!("cargo:rustc-link-lib=static=jemalloc_pic");
149 }
150 println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
151 if target.contains("android") {
152 println!("cargo:rustc-link-lib=gcc");
54a0048b 153 } else if !target.contains("windows") && !target.contains("musl") {
7453a54e
SL
154 println!("cargo:rustc-link-lib=pthread");
155 }
156}