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, native_lib_boilerplate};
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");
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,
42 if target.contains("cloudabi") {
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");
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") {
63 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
67 let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" };
68 let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") {
73 let mut cmd = Command::new("sh");
74 cmd.arg(native.src_dir.join("configure")
77 .replace("C:\\", "/c/")
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());
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.
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_");
108 if cfg!(feature = "debug") {
109 // Enable jemalloc assertions.
110 cmd.arg("--enable-debug");
113 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
114 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
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");
124 let mut make = Command::new(build_helper::make(&host));
125 make.current_dir(&native.out_dir)
126 .arg("build_lib_static");
128 // These are intended for mingw32-make which we don't use
130 make.env_remove("MAKEFLAGS").env_remove("MFLAGS");
133 // mingw make seems... buggy? unclear...
134 if !host.contains("windows") {
136 .arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"));
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");
147 .flag("-fvisibility=hidden")
148 .file("pthread_atfork_dummy.c")
149 .compile("pthread_atfork_dummy");