]> git.lizzy.rs Git - rust.git/blob - src/libstd/build.rs
faff0d1cb47b53ed16ecf3953a6c47450e9dea36
[rust.git] / src / libstd / 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 gcc;
14 extern crate build_helper;
15
16 use std::{env, fs};
17 use std::path::PathBuf;
18 use std::process::Command;
19 use build_helper::{run, rerun_if_changed_anything_in_dir};
20
21 fn main() {
22     println!("cargo:rustc-cfg=cargobuild");
23     println!("cargo:rerun-if-changed=build.rs");
24
25     let target = env::var("TARGET").expect("TARGET was not set");
26     let host = env::var("HOST").expect("HOST was not set");
27     if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
28         !target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") {
29         build_libbacktrace(&host, &target);
30     }
31
32     if target.contains("linux") {
33         if target.contains("android") {
34             println!("cargo:rustc-link-lib=dl");
35             println!("cargo:rustc-link-lib=log");
36             println!("cargo:rustc-link-lib=gcc");
37         } else if !target.contains("musl") || target.contains("mips") {
38             println!("cargo:rustc-link-lib=dl");
39             println!("cargo:rustc-link-lib=rt");
40             println!("cargo:rustc-link-lib=pthread");
41         }
42     } else if target.contains("freebsd") {
43         println!("cargo:rustc-link-lib=execinfo");
44         println!("cargo:rustc-link-lib=pthread");
45     } else if target.contains("dragonfly") || target.contains("bitrig") ||
46               target.contains("netbsd") || target.contains("openbsd") {
47         println!("cargo:rustc-link-lib=pthread");
48     } else if target.contains("apple-darwin") {
49         println!("cargo:rustc-link-lib=System");
50     } else if target.contains("apple-ios") {
51         println!("cargo:rustc-link-lib=System");
52         println!("cargo:rustc-link-lib=objc");
53         println!("cargo:rustc-link-lib=framework=Security");
54         println!("cargo:rustc-link-lib=framework=Foundation");
55     } else if target.contains("windows") {
56         println!("cargo:rustc-link-lib=advapi32");
57         println!("cargo:rustc-link-lib=ws2_32");
58         println!("cargo:rustc-link-lib=userenv");
59         println!("cargo:rustc-link-lib=shell32");
60     } else if target.contains("fuchsia") {
61         println!("cargo:rustc-link-lib=magenta");
62         println!("cargo:rustc-link-lib=mxio");
63         println!("cargo:rustc-link-lib=launchpad"); // for std::process
64     }
65 }
66
67 fn build_libbacktrace(host: &str, target: &str) {
68     let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
69     let build_dir = PathBuf::from(build_dir).join("libbacktrace");
70     let _ = fs::create_dir_all(&build_dir);
71
72     println!("cargo:rustc-link-lib=static=backtrace");
73     println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
74     if !cfg!(stage0) {
75         return
76     }
77
78     let src_dir = env::current_dir().unwrap().join("../libbacktrace");
79     rerun_if_changed_anything_in_dir(&src_dir);
80     let compiler = gcc::Config::new().get_compiler();
81     // only msvc returns None for ar so unwrap is okay
82     let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
83     let mut cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
84                              .collect::<Vec<_>>().join(" ");
85     cflags.push_str(" -fvisibility=hidden");
86     run(Command::new("sh")
87                 .current_dir(&build_dir)
88                 .arg(src_dir.join("configure").to_str().unwrap()
89                             .replace("C:\\", "/c/")
90                             .replace("\\", "/"))
91                 .arg("--with-pic")
92                 .arg("--disable-multilib")
93                 .arg("--disable-shared")
94                 .arg("--disable-host-shared")
95                 .arg(format!("--host={}", build_helper::gnu_target(target)))
96                 .arg(format!("--build={}", build_helper::gnu_target(host)))
97                 .env("CC", compiler.path())
98                 .env("AR", &ar)
99                 .env("RANLIB", format!("{} s", ar.display()))
100                 .env("CFLAGS", cflags));
101
102     run(Command::new(build_helper::make(host))
103                 .current_dir(&build_dir)
104                 .arg(format!("INCDIR={}", src_dir.display()))
105                 .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
106 }