]> git.lizzy.rs Git - rust.git/blob - src/libunwind/build.rs
Rollup merge of #63595 - semarie:openbsd-sparc64, r=alexcrichton
[rust.git] / src / libunwind / build.rs
1 use std::env;
2
3 fn main() {
4     println!("cargo:rerun-if-changed=build.rs");
5     let target = env::var("TARGET").expect("TARGET was not set");
6
7     if cfg!(feature = "llvm-libunwind") &&
8         ((target.contains("linux") && !target.contains("musl")) ||
9          target.contains("fuchsia")) {
10         // Build the unwinding from libunwind C/C++ source code.
11         llvm_libunwind::compile();
12     } else if target.contains("linux") {
13         if target.contains("musl") {
14             // linking for musl is handled in lib.rs
15             llvm_libunwind::compile();
16         } else if !target.contains("android") {
17             println!("cargo:rustc-link-lib=gcc_s");
18         }
19     } else if target.contains("freebsd") {
20         println!("cargo:rustc-link-lib=gcc_s");
21     } else if target.contains("rumprun") {
22         println!("cargo:rustc-link-lib=unwind");
23     } else if target.contains("netbsd") {
24         println!("cargo:rustc-link-lib=gcc_s");
25     } else if target.contains("openbsd") {
26         if target.contains("sparc64") {
27             println!("cargo:rustc-link-lib=gcc");
28         } else {
29             println!("cargo:rustc-link-lib=c++abi");
30         }
31     } else if target.contains("solaris") {
32         println!("cargo:rustc-link-lib=gcc_s");
33     } else if target.contains("dragonfly") {
34         println!("cargo:rustc-link-lib=gcc_pic");
35     } else if target.contains("pc-windows-gnu") {
36         println!("cargo:rustc-link-lib=static-nobundle=gcc_eh");
37         println!("cargo:rustc-link-lib=static-nobundle=pthread");
38     } else if target.contains("uwp-windows-gnu") {
39         println!("cargo:rustc-link-lib=unwind");
40     } else if target.contains("fuchsia") {
41         println!("cargo:rustc-link-lib=unwind");
42     } else if target.contains("haiku") {
43         println!("cargo:rustc-link-lib=gcc_s");
44     } else if target.contains("redox") {
45         // redox is handled in lib.rs
46     } else if target.contains("cloudabi") {
47         println!("cargo:rustc-link-lib=unwind");
48     }
49 }
50
51 mod llvm_libunwind {
52     use std::env;
53     use std::path::Path;
54
55     /// Compile the libunwind C/C++ source code.
56     pub fn compile() {
57         let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
58         let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
59         let cfg = &mut cc::Build::new();
60
61         cfg.cpp(true);
62         cfg.cpp_set_stdlib(None);
63         cfg.warnings(false);
64
65         if target_env == "msvc" {
66             // Don't pull in extra libraries on MSVC
67             cfg.flag("/Zl");
68             cfg.flag("/EHsc");
69             cfg.define("_CRT_SECURE_NO_WARNINGS", None);
70             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
71         } else {
72             cfg.flag("-std=c99");
73             cfg.flag("-std=c++11");
74             cfg.flag("-nostdinc++");
75             cfg.flag("-fno-exceptions");
76             cfg.flag("-fno-rtti");
77             cfg.flag("-fstrict-aliasing");
78             cfg.flag("-funwind-tables");
79         }
80
81         let mut unwind_sources = vec![
82             "Unwind-EHABI.cpp",
83             "Unwind-seh.cpp",
84             "Unwind-sjlj.c",
85             "UnwindLevel1-gcc-ext.c",
86             "UnwindLevel1.c",
87             "UnwindRegistersRestore.S",
88             "UnwindRegistersSave.S",
89             "libunwind.cpp",
90         ];
91
92         if target_vendor == "apple" {
93             unwind_sources.push("Unwind_AppleExtras.cpp");
94         }
95
96         let root = Path::new("../llvm-project/libunwind");
97         cfg.include(root.join("include"));
98         for src in unwind_sources {
99             cfg.file(root.join("src").join(src));
100         }
101
102         if target_env == "musl" {
103             // use the same C compiler command to compile C++ code so we do not need to setup the
104             // C++ compiler env variables on the builders
105             cfg.cpp(false);
106             // linking for musl is handled in lib.rs
107             cfg.cargo_metadata(false);
108             println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
109         }
110
111         cfg.compile("unwind");
112     }
113 }