]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/build.rs
Update jemalloc to 4.5.0
[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, 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("ios") {
97         cmd.arg("--disable-tls");
98     } else if target.contains("android") {
99         // We force android to have prefixed symbols because apparently
100         // replacement of the libc allocator doesn't quite work. When this was
101         // tested (unprefixed symbols), it was found that the `realpath`
102         // function in libc would allocate with libc malloc (not jemalloc
103         // malloc), and then the standard library would free with jemalloc free,
104         // causing a segfault.
105         //
106         // If the test suite passes, however, without symbol prefixes then we
107         // should be good to go!
108         cmd.arg("--with-jemalloc-prefix=je_");
109         cmd.arg("--disable-tls");
110     } else if target.contains("dragonfly") || target.contains("musl") {
111         cmd.arg("--with-jemalloc-prefix=je_");
112     }
113
114     if cfg!(feature = "debug-jemalloc") {
115         cmd.arg("--enable-debug");
116     }
117
118     // Turn off broken quarantine (see jemalloc/jemalloc#161)
119     cmd.arg("--disable-fill");
120     cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
121     cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
122
123     // for some reason, jemalloc configure doesn't detect this value
124     // automatically for this target
125     if target == "sparc64-unknown-linux-gnu" {
126         cmd.arg("--with-lg-quantum=4");
127     }
128
129     run(&mut cmd);
130
131     let mut make = Command::new(build_helper::make(&host));
132     make.current_dir(&native.out_dir)
133         .arg("build_lib_static");
134
135     // mingw make seems... buggy? unclear...
136     if !host.contains("windows") {
137         make.arg("-j")
138             .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
139     }
140
141     run(&mut make);
142
143     // The pthread_atfork symbols is used by jemalloc on android but the really
144     // old android we're building on doesn't have them defined, so just make
145     // sure the symbols are available.
146     if target.contains("androideabi") {
147         println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
148         gcc::Config::new()
149             .flag("-fvisibility=hidden")
150             .file("pthread_atfork_dummy.c")
151             .compile("libpthread_atfork_dummy.a");
152     }
153 }