]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/build.rs
bootstrap: Add build scripts for crates
[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 extern crate build_helper;
12 extern crate gcc;
13
14 use std::env;
15 use std::path::PathBuf;
16 use std::process::Command;
17 use build_helper::run;
18
19 fn main() {
20     let target = env::var("TARGET").unwrap();
21     let host = env::var("HOST").unwrap();
22     let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
23     let src_dir = env::current_dir().unwrap();
24
25     if let Some(jemalloc) = env::var_os("JEMALLOC_OVERRIDE") {
26         let jemalloc = PathBuf::from(jemalloc);
27         println!("cargo:rustc-link-search=native={}",
28                  jemalloc.parent().unwrap().display());
29         let stem = jemalloc.file_stem().unwrap().to_str().unwrap();
30         let name = jemalloc.file_name().unwrap().to_str().unwrap();
31         let kind = if name.ends_with(".a") {"static"} else {"dylib"};
32         println!("cargo:rustc-link-lib={}={}", kind, &stem[3..]);
33         return
34     }
35
36     let compiler = gcc::Config::new().get_compiler();
37     let ar = build_helper::cc2ar(compiler.path(), &target);
38     let cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
39                          .collect::<Vec<_>>().join(" ");
40
41     let mut cmd = Command::new("sh");
42     cmd.arg(src_dir.join("../jemalloc/configure").to_str().unwrap()
43                    .replace("C:\\", "/c/")
44                    .replace("\\", "/"))
45        .current_dir(&build_dir)
46        .env("CC", compiler.path())
47        .env("EXTRA_CFLAGS", cflags)
48        .env("AR", &ar)
49        .env("RANLIB", format!("{} s", ar.display()));
50
51     if target.contains("windows-gnu") {
52         // A bit of history here, this used to be --enable-lazy-lock added in
53         // #14006 which was filed with jemalloc in jemalloc/jemalloc#83 which
54         // was also reported to MinGW:
55         //
56         //  http://sourceforge.net/p/mingw-w64/bugs/395/
57         //
58         // When updating jemalloc to 4.0, however, it was found that binaries
59         // would exit with the status code STATUS_RESOURCE_NOT_OWNED indicating
60         // that a thread was unlocking a mutex it never locked. Disabling this
61         // "lazy lock" option seems to fix the issue, but it was enabled by
62         // default for MinGW targets in 13473c7 for jemalloc.
63         //
64         // As a result of all that, force disabling lazy lock on Windows, and
65         // after reading some code it at least *appears* that the initialization
66         // of mutexes is otherwise ok in jemalloc, so shouldn't cause problems
67         // hopefully...
68         //
69         // tl;dr: make windows behave like other platforms by disabling lazy
70         //        locking, but requires passing an option due to a historical
71         //        default with jemalloc.
72         cmd.arg("--disable-lazy-lock");
73     } else if target.contains("ios") || target.contains("android") {
74         cmd.arg("--disable-tls");
75     }
76
77     if cfg!(feature = "debug-jemalloc") {
78         cmd.arg("--enable-debug");
79     }
80
81     // Turn off broken quarantine (see jemalloc/jemalloc#161)
82     cmd.arg("--disable-fill");
83     cmd.arg("--with-jemalloc-prefix=je_");
84     cmd.arg(format!("--host={}", build_helper::gnu_target(&target)));
85     cmd.arg(format!("--build={}", build_helper::gnu_target(&host)));
86
87     run(&mut cmd);
88     run(Command::new("make")
89                 .current_dir(&build_dir)
90                 .arg("build_lib_static")
91                 .arg("-j").arg(env::var("NUM_JOBS").unwrap()));
92
93     if target.contains("windows") {
94         println!("cargo:rustc-link-lib=static=jemalloc");
95     } else {
96         println!("cargo:rustc-link-lib=static=jemalloc_pic");
97     }
98     println!("cargo:rustc-link-search=native={}/lib", build_dir.display());
99     if target.contains("android") {
100         println!("cargo:rustc-link-lib=gcc");
101     } else if !target.contains("windows") {
102         println!("cargo:rustc-link-lib=pthread");
103     }
104 }