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;
18 use std::fs::{self, File};
19 use std::path::{Path, PathBuf};
20 use std::process::Command;
21 use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date};
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 build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
70 let build_dir = PathBuf::from(build_dir).join("libbacktrace");
71 let _ = fs::create_dir_all(&build_dir);
73 println!("cargo:rustc-link-lib=static=backtrace");
74 println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
75 let src_dir = env::current_dir().unwrap().join("../libbacktrace");
76 rerun_if_changed_anything_in_dir(&src_dir);
77 let timestamp = build_dir.join("rustbuild.timestamp");
78 if up_to_date(&Path::new("build.rs"), ×tamp) && up_to_date(&src_dir, ×tamp) {
82 let compiler = gcc::Config::new().get_compiler();
83 // only msvc returns None for ar so unwrap is okay
84 let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
85 let mut cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
86 .collect::<Vec<_>>().join(" ");
87 cflags.push_str(" -fvisibility=hidden");
88 run(Command::new("sh")
89 .current_dir(&build_dir)
90 .arg(src_dir.join("configure").to_str().unwrap()
91 .replace("C:\\", "/c/")
94 .arg("--disable-multilib")
95 .arg("--disable-shared")
96 .arg("--disable-host-shared")
97 .arg(format!("--host={}", build_helper::gnu_target(target)))
98 .arg(format!("--build={}", build_helper::gnu_target(host)))
99 .env("CC", compiler.path())
101 .env("RANLIB", format!("{} s", ar.display()))
102 .env("CFLAGS", cflags));
104 run(Command::new(build_helper::make(host))
105 .current_dir(&build_dir)
106 .arg(format!("INCDIR={}", src_dir.display()))
107 .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set")));
109 t!(File::create(×tamp));