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.
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.
14 extern crate build_helper;
17 use std::path::PathBuf;
18 use std::process::Command;
20 use build_helper::run;
23 println!("cargo:rustc-cfg=cargobuild");
24 println!("cargo:rerun-if-changed=build.rs");
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") && !target.contains("redox") {
30 build_libbacktrace(&host, &target);
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");
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
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());
72 println!("cargo:rustc-link-lib=static=backtrace");
73 println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
75 let mut stack = src_dir.read_dir().unwrap()
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()));
83 println!("cargo:rerun-if-changed={}", path.display());
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 mut cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
91 .collect::<Vec<_>>().join(" ");
92 cflags.push_str(" -fvisibility=hidden");
93 run(Command::new("sh")
94 .current_dir(&build_dir)
95 .arg(src_dir.join("configure").to_str().unwrap()
96 .replace("C:\\", "/c/")
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())
106 .env("RANLIB", format!("{} s", ar.display()))
107 .env("CFLAGS", cflags));
108 run(Command::new(build_helper::make(host))
109 .current_dir(&build_dir)
110 .arg(format!("INCDIR={}", src_dir.display()))
111 .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));