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