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.
11 extern crate build_helper;
15 use std::path::PathBuf;
16 use std::process::Command;
17 use build_helper::run;
20 println!("cargo:rustc-cfg=cargobuild");
22 let target = env::var("TARGET").unwrap();
23 let host = env::var("HOST").unwrap();
24 let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
25 let src_dir = env::current_dir().unwrap();
27 if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
28 let jemalloc = PathBuf::from(jemalloc);
29 println!("cargo:rustc-link-search=native={}",
30 jemalloc.parent().unwrap().display());
31 let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
32 let name = jemalloc.file_name().unwrap().to_str().unwrap();
33 let kind = if name.ends_with(".a") {"static"} else {"dylib"};
34 println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
38 let compiler = gcc::Config::new().get_compiler();
39 let ar = build_helper::cc2ar(compiler.path(), &target);
40 let cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
41 .collect::<Vec<_>>().join(" ");
43 let mut cmd = Command::new("sh");
44 cmd.arg(src_dir.join("../jemalloc/configure").to_str().unwrap()
45 .replace("C:\\", "/c/")
47 .current_dir(&build_dir)
48 .env("CC", compiler.path())
49 .env("EXTRA_CFLAGS", cflags)
51 .env("RANLIB", format!("{} s", ar.display()));
53 if target.contains("windows") {
54 // A bit of history here, this used to be --enable-lazy-lock added in
55 // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
56 // was also reported to MinGW:
58 // http://sourceforge.net/p/mingw-w64/bugs/395/
60 // When updating jemalloc to 4.0, however, it was found that binaries
61 // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
62 // that a thread was unlocking a mutex it never locked. Disabling this
63 // "lazy lock" option seems to fix the issue, but it was enabled by
64 // default for MinGW targets in 13473c7 for jemalloc.
66 // As a result of all that, force disabling lazy lock on Windows, and
67 // after reading some code it at least *appears* that the initialization
68 // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
71 // tl;dr: make windows behave like other platforms by disabling lazy
72 // locking, but requires passing an option due to a historical
73 // default with jemalloc.
74 cmd.arg("--disable-lazy-lock");
75 } else if target.contains("ios") {
76 cmd.arg("--disable-tls");
77 } else if target.contains("android") {
78 // We force android to have prefixed symbols because apparently
79 // replacement of the libc allocator doesn't quite work. When this was
80 // tested (unprefixed symbols), it was found that the `realpath`
81 // function in libc would allocate with libc malloc (not jemalloc
82 // malloc), and then the standard library would free with jemalloc free,
83 // causing a segfault.
85 // If the test suite passes, however, without symbol prefixes then we
86 // should be good to go!
87 cmd.arg("--with-jemalloc-prefix=je_");
88 cmd.arg("--disable-tls");
91 if cfg!(feature = "debug-jemalloc") {
92 cmd.arg("--enable-debug");
95 // Turn off broken quarantine (see jemalloc/jemalloc#161)
96 cmd.arg("--disable-fill");
97 cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
98 cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
101 run(Command::new("make")
102 .current_dir(&build_dir)
103 .arg("build_lib_static")
104 .arg("-j").arg(env::var("NUM_JOBS").unwrap()));
106 if target.contains("windows") {
107 println!("cargo:rustc-link-lib=static=jemalloc");
109 println!("cargo:rustc-link-lib=static=jemalloc_pic");
111 println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
112 if target.contains("android") {
113 println!("cargo:rustc-link-lib=gcc");
114 } else if !target.contains("windows") {
115 println!("cargo:rustc-link-lib=pthread");