]> git.lizzy.rs Git - rust.git/blob - src/libunwind/build.rs
Auto merge of #68943 - ecstatic-morse:no-useless-drop-on-enum-variants, r=matthewjasper
[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")) || target.contains("fuchsia"))
9     {
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 target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
60         let cfg = &mut cc::Build::new();
61
62         cfg.cpp(true);
63         cfg.cpp_set_stdlib(None);
64         cfg.warnings(false);
65
66         // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765
67         if target_endian_little {
68             cfg.define("__LITTLE_ENDIAN__", Some("1"));
69         }
70
71         if target_env == "msvc" {
72             // Don't pull in extra libraries on MSVC
73             cfg.flag("/Zl");
74             cfg.flag("/EHsc");
75             cfg.define("_CRT_SECURE_NO_WARNINGS", None);
76             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
77         } else {
78             cfg.flag("-std=c99");
79             cfg.flag("-std=c++11");
80             cfg.flag("-nostdinc++");
81             cfg.flag("-fno-exceptions");
82             cfg.flag("-fno-rtti");
83             cfg.flag("-fstrict-aliasing");
84             cfg.flag("-funwind-tables");
85         }
86
87         let mut unwind_sources = vec![
88             "Unwind-EHABI.cpp",
89             "Unwind-seh.cpp",
90             "Unwind-sjlj.c",
91             "UnwindLevel1-gcc-ext.c",
92             "UnwindLevel1.c",
93             "UnwindRegistersRestore.S",
94             "UnwindRegistersSave.S",
95             "libunwind.cpp",
96         ];
97
98         if target_vendor == "apple" {
99             unwind_sources.push("Unwind_AppleExtras.cpp");
100         }
101
102         let root = Path::new("../llvm-project/libunwind");
103         cfg.include(root.join("include"));
104         for src in unwind_sources {
105             cfg.file(root.join("src").join(src));
106         }
107
108         if target_env == "musl" {
109             // use the same C compiler command to compile C++ code so we do not need to setup the
110             // C++ compiler env variables on the builders
111             cfg.cpp(false);
112             // linking for musl is handled in lib.rs
113             cfg.cargo_metadata(false);
114             println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
115         }
116
117         cfg.compile("unwind");
118     }
119 }