]> git.lizzy.rs Git - rust.git/blob - src/libstd/build.rs
Auto merge of #37789 - arielb1:length-limit, r=nikomatsakis
[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     }
64 }
65
66 fn build_libbacktrace(host: &str, target: &str) {
67     let src_dir = env::current_dir().unwrap().join("../libbacktrace");
68     let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
69
70     println!("cargo:rustc-link-lib=static=backtrace");
71     println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
72
73     let mut stack = src_dir.read_dir().unwrap()
74                            .map(|e| e.unwrap())
75                            .collect::<Vec<_>>();
76     while let Some(entry) = stack.pop() {
77         let path = entry.path();
78         if entry.file_type().unwrap().is_dir() {
79             stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
80         } else {
81             println!("cargo:rerun-if-changed={}", path.display());
82         }
83     }
84
85     let compiler = gcc::Config::new().get_compiler();
86     // only msvc returns None for ar so unwrap is okay
87     let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
88     let cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
89                          .collect::<Vec<_>>().join(" ");
90     run(Command::new("sh")
91                 .current_dir(&build_dir)
92                 .arg(src_dir.join("configure").to_str().unwrap()
93                             .replace("C:\\", "/c/")
94                             .replace("\\", "/"))
95                 .arg("--with-pic")
96                 .arg("--disable-multilib")
97                 .arg("--disable-shared")
98                 .arg("--disable-host-shared")
99                 .arg(format!("--host={}", build_helper::gnu_target(target)))
100                 .arg(format!("--build={}", build_helper::gnu_target(host)))
101                 .env("CC", compiler.path())
102                 .env("AR", &ar)
103                 .env("RANLIB", format!("{} s", ar.display()))
104                 .env("CFLAGS", cflags));
105     run(Command::new("make")
106                 .current_dir(&build_dir)
107                 .arg(format!("INCDIR={}", src_dir.display()))
108                 .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
109 }