]> git.lizzy.rs Git - rust.git/blob - src/libstd/build.rs
Rollup merge of #56841 - phansch:add_various_compiletest_unittests, r=oli-obk
[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 build_helper;
14 extern crate cc;
15
16 use build_helper::native_lib_boilerplate;
17 use std::env;
18 use std::fs::File;
19
20 fn main() {
21     let target = env::var("TARGET").expect("TARGET was not set");
22     if cfg!(feature = "backtrace") &&
23         !target.contains("cloudabi") &&
24         !target.contains("emscripten") &&
25         !target.contains("msvc") &&
26         !target.contains("wasm32")
27     {
28         let _ = build_libbacktrace(&target);
29     }
30
31     if target.contains("linux") {
32         if target.contains("android") {
33             println!("cargo:rustc-link-lib=dl");
34             println!("cargo:rustc-link-lib=log");
35             println!("cargo:rustc-link-lib=gcc");
36         } else if !target.contains("musl") {
37             println!("cargo:rustc-link-lib=dl");
38             println!("cargo:rustc-link-lib=rt");
39             println!("cargo:rustc-link-lib=pthread");
40         }
41     } else if target.contains("freebsd") {
42         println!("cargo:rustc-link-lib=execinfo");
43         println!("cargo:rustc-link-lib=pthread");
44     } else if target.contains("netbsd") {
45         println!("cargo:rustc-link-lib=pthread");
46         println!("cargo:rustc-link-lib=rt");
47     } else if target.contains("dragonfly") || target.contains("bitrig") ||
48               target.contains("openbsd") {
49         println!("cargo:rustc-link-lib=pthread");
50     } else if target.contains("solaris") {
51         println!("cargo:rustc-link-lib=socket");
52         println!("cargo:rustc-link-lib=posix4");
53         println!("cargo:rustc-link-lib=pthread");
54         println!("cargo:rustc-link-lib=resolv");
55     } else if target.contains("apple-darwin") {
56         println!("cargo:rustc-link-lib=System");
57
58         // res_init and friends require -lresolv on macOS/iOS.
59         // See #41582 and http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
60         println!("cargo:rustc-link-lib=resolv");
61     } else if target.contains("apple-ios") {
62         println!("cargo:rustc-link-lib=System");
63         println!("cargo:rustc-link-lib=objc");
64         println!("cargo:rustc-link-lib=framework=Security");
65         println!("cargo:rustc-link-lib=framework=Foundation");
66         println!("cargo:rustc-link-lib=resolv");
67     } else if target.contains("windows") {
68         println!("cargo:rustc-link-lib=advapi32");
69         println!("cargo:rustc-link-lib=ws2_32");
70         println!("cargo:rustc-link-lib=userenv");
71     } else if target.contains("fuchsia") {
72         println!("cargo:rustc-link-lib=zircon");
73         println!("cargo:rustc-link-lib=fdio");
74     } else if target.contains("cloudabi") {
75         if cfg!(feature = "backtrace") {
76             println!("cargo:rustc-link-lib=unwind");
77         }
78         println!("cargo:rustc-link-lib=c");
79         println!("cargo:rustc-link-lib=compiler_rt");
80     }
81 }
82
83 fn build_libbacktrace(target: &str) -> Result<(), ()> {
84     let native = native_lib_boilerplate(
85         "../libbacktrace".as_ref(),
86         "libbacktrace",
87         "backtrace",
88         "",
89     )?;
90
91     let mut build = cc::Build::new();
92     build
93         .flag("-fvisibility=hidden")
94         .include("../libbacktrace")
95         .include(&native.out_dir)
96         .out_dir(&native.out_dir)
97         .warnings(false)
98         .file("../libbacktrace/alloc.c")
99         .file("../libbacktrace/backtrace.c")
100         .file("../libbacktrace/dwarf.c")
101         .file("../libbacktrace/fileline.c")
102         .file("../libbacktrace/posix.c")
103         .file("../libbacktrace/read.c")
104         .file("../libbacktrace/sort.c")
105         .file("../libbacktrace/state.c");
106
107     let any_debug = env::var("RUSTC_DEBUGINFO").unwrap_or_default() == "true" ||
108         env::var("RUSTC_DEBUGINFO_LINES").unwrap_or_default() == "true";
109     build.debug(any_debug);
110
111     if target.contains("darwin") {
112         build.file("../libbacktrace/macho.c");
113     } else if target.contains("windows") {
114         build.file("../libbacktrace/pecoff.c");
115     } else {
116         build.file("../libbacktrace/elf.c");
117
118         let pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
119         if pointer_width == "64" {
120             build.define("BACKTRACE_ELF_SIZE", "64");
121         } else {
122             build.define("BACKTRACE_ELF_SIZE", "32");
123         }
124     }
125
126     File::create(native.out_dir.join("backtrace-supported.h")).unwrap();
127     build.define("BACKTRACE_SUPPORTED", "1");
128     build.define("BACKTRACE_USES_MALLOC", "1");
129     build.define("BACKTRACE_SUPPORTS_THREADS", "0");
130     build.define("BACKTRACE_SUPPORTS_DATA", "0");
131
132     File::create(native.out_dir.join("config.h")).unwrap();
133     if !target.contains("apple-ios") &&
134        !target.contains("solaris") &&
135        !target.contains("redox") &&
136        !target.contains("android") &&
137        !target.contains("haiku") {
138         build.define("HAVE_DL_ITERATE_PHDR", "1");
139     }
140     build.define("_GNU_SOURCE", "1");
141     build.define("_LARGE_FILES", "1");
142
143     build.compile("backtrace");
144     Ok(())
145 }