]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/build.rs
fbda425a70bf5d6388455a4a0014e58a0a13d1d0
[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 cc;
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("bitrig") || target.contains("emscripten") || target.contains("fuchsia") ||
33        target.contains("msvc") || target.contains("openbsd") || target.contains("redox") ||
34        target.contains("rumprun") || target.contains("wasm32") {
35         println!("cargo:rustc-cfg=dummy_jemalloc");
36         return;
37     }
38
39     // CloudABI ships with a copy of jemalloc that has been patched to
40     // work well with sandboxing. Don't attempt to build our own copy,
41     // as it won't build.
42     if target.contains("cloudabi") {
43         return;
44     }
45
46     if target.contains("android") {
47         println!("cargo:rustc-link-lib=gcc");
48     } else if !target.contains("windows") && !target.contains("musl") {
49         println!("cargo:rustc-link-lib=pthread");
50     }
51
52     if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
53         let jemalloc = PathBuf::from(jemalloc);
54         println!("cargo:rustc-link-search=native={}",
55                  jemalloc.parent().unwrap().display());
56         let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
57         let name = jemalloc.file_name().unwrap().to_str().unwrap();
58         let kind = if name.ends_with(".a") {
59             "static"
60         } else {
61             "dylib"
62         };
63         println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
64         return;
65     }
66
67     let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
68     let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") {
69         Ok(native) => native,
70         _ => return,
71     };
72
73     let mut cmd = Command::new("sh");
74     cmd.arg(native.src_dir.join("configure")
75                           .to_str()
76                           .unwrap()
77                           .replace("C:\\", "/c/")
78                           .replace("\\", "/"))
79        .current_dir(&native.out_dir)
80        // jemalloc generates Makefile deps using GCC's "-MM" flag. This means
81        // that GCC will run the preprocessor, and only the preprocessor, over
82        // jemalloc's source files. If we don't specify CPPFLAGS, then at least
83        // on ARM that step fails with a "Missing implementation for 32-bit
84        // atomic operations" error. This is because no "-march" flag will be
85        // passed to GCC, and then GCC won't define the
86        // "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" macro that jemalloc needs to
87        // select an atomic operation implementation.
88        .env("CPPFLAGS", env::var_os("CFLAGS").unwrap_or_default());
89
90     if target.contains("ios") {
91         cmd.arg("--disable-tls");
92     } else if target.contains("android") {
93         // We force android to have prefixed symbols because apparently
94         // replacement of the libc allocator doesn't quite work. When this was
95         // tested (unprefixed symbols), it was found that the `realpath`
96         // function in libc would allocate with libc malloc (not jemalloc
97         // malloc), and then the standard library would free with jemalloc free,
98         // causing a segfault.
99         //
100         // If the test suite passes, however, without symbol prefixes then we
101         // should be good to go!
102         cmd.arg("--with-jemalloc-prefix=je_");
103         cmd.arg("--disable-tls");
104     } else if target.contains("dragonfly") || target.contains("musl") {
105         cmd.arg("--with-jemalloc-prefix=je_");
106     }
107
108     if cfg!(feature = "debug") {
109         // Enable jemalloc assertions.
110         cmd.arg("--enable-debug");
111     }
112
113     cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
114     cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
115
116     // for some reason, jemalloc configure doesn't detect this value
117     // automatically for this target
118     if target == "sparc64-unknown-linux-gnu" {
119         cmd.arg("--with-lg-quantum=4");
120     }
121
122     run(&mut cmd);
123
124     let mut make = Command::new(build_helper::make(&host));
125     make.current_dir(&native.out_dir)
126         .arg("build_lib_static");
127
128     // These are intended for mingw32-make which we don't use
129     if cfg!(windows) {
130         make.env_remove("MAKEFLAGS").env_remove("MFLAGS");
131     }
132
133     // mingw make seems... buggy? unclear...
134     if !host.contains("windows") {
135         make.arg("-j")
136             .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
137     }
138
139     run(&mut make);
140
141     // The pthread_atfork symbols is used by jemalloc on android but the really
142     // old android we're building on doesn't have them defined, so just make
143     // sure the symbols are available.
144     if target.contains("androideabi") {
145         println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
146         cc::Build::new()
147             .flag("-fvisibility=hidden")
148             .file("pthread_atfork_dummy.c")
149             .compile("pthread_atfork_dummy");
150     }
151 }