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:rustc-cfg=cargobuild");
25 println!("cargo:rerun-if-changed=build.rs");
27 let target = env::var("TARGET").expect("TARGET was not set");
28 let host = env::var("HOST").expect("HOST was not set");
29 if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
30 !target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") {
31 build_libbacktrace(&host, &target);
34 if target.contains("linux") {
35 if target.contains("android") {
36 println!("cargo:rustc-link-lib=dl");
37 println!("cargo:rustc-link-lib=log");
38 println!("cargo:rustc-link-lib=gcc");
39 } else if !target.contains("musl") || target.contains("mips") {
40 println!("cargo:rustc-link-lib=dl");
41 println!("cargo:rustc-link-lib=rt");
42 println!("cargo:rustc-link-lib=pthread");
44 } else if target.contains("freebsd") {
45 println!("cargo:rustc-link-lib=execinfo");
46 println!("cargo:rustc-link-lib=pthread");
47 } else if target.contains("dragonfly") || target.contains("bitrig") ||
48 target.contains("netbsd") || target.contains("openbsd") {
49 println!("cargo:rustc-link-lib=pthread");
50 } else if target.contains("apple-darwin") {
51 println!("cargo:rustc-link-lib=System");
52 } else if target.contains("apple-ios") {
53 println!("cargo:rustc-link-lib=System");
54 println!("cargo:rustc-link-lib=objc");
55 println!("cargo:rustc-link-lib=framework=Security");
56 println!("cargo:rustc-link-lib=framework=Foundation");
57 } else if target.contains("windows") {
58 println!("cargo:rustc-link-lib=advapi32");
59 println!("cargo:rustc-link-lib=ws2_32");
60 println!("cargo:rustc-link-lib=userenv");
61 println!("cargo:rustc-link-lib=shell32");
62 } else if target.contains("fuchsia") {
63 println!("cargo:rustc-link-lib=magenta");
64 println!("cargo:rustc-link-lib=mxio");
65 println!("cargo:rustc-link-lib=launchpad"); // for std::process
69 fn build_libbacktrace(host: &str, target: &str) {
70 let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
71 let build_dir = PathBuf::from(build_dir).join("libbacktrace");
72 let _ = fs::create_dir_all(&build_dir);
74 println!("cargo:rustc-link-lib=static=backtrace");
75 println!("cargo:rustc-link-search=native={}/.libs", build_dir.display());
76 let src_dir = env::current_dir().unwrap().join("../libbacktrace");
77 rerun_if_changed_anything_in_dir(&src_dir);
78 let timestamp = build_dir.join("rustbuild.timestamp");
79 if up_to_date(&Path::new("build.rs"), ×tamp) && up_to_date(&src_dir, ×tamp) {
83 let compiler = gcc::Config::new().get_compiler();
84 // only msvc returns None for ar so unwrap is okay
85 let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
86 let mut cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
87 .collect::<Vec<_>>().join(" ");
88 cflags.push_str(" -fvisibility=hidden");
89 run(Command::new("sh")
90 .current_dir(&build_dir)
91 .arg(src_dir.join("configure").to_str().unwrap()
92 .replace("C:\\", "/c/")
95 .arg("--disable-multilib")
96 .arg("--disable-shared")
97 .arg("--disable-host-shared")
98 .arg(format!("--host={}", build_helper::gnu_target(target)))
99 .arg(format!("--build={}", build_helper::gnu_target(host)))
100 .env("CC", compiler.path())
102 .env("RANLIB", format!("{} s", ar.display()))
103 .env("CFLAGS", cflags));
105 run(Command::new(build_helper::make(host))
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")));
110 t!(File::create(×tamp));