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