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