]> git.lizzy.rs Git - rust.git/blob - src/libunwind/build.rs
Rollup merge of #71756 - carstenandrich:master, r=dtolnay
[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("illumos") {
34         println!("cargo:rustc-link-lib=gcc_s");
35     } else if target.contains("dragonfly") {
36         println!("cargo:rustc-link-lib=gcc_pic");
37     } else if target.contains("pc-windows-gnu") {
38         // This is handled in the target spec with late_link_args_[static|dynamic]
39     } else if target.contains("uwp-windows-gnu") {
40         println!("cargo:rustc-link-lib=unwind");
41     } else if target.contains("fuchsia") {
42         println!("cargo:rustc-link-lib=unwind");
43     } else if target.contains("haiku") {
44         println!("cargo:rustc-link-lib=gcc_s");
45     } else if target.contains("redox") {
46         // redox is handled in lib.rs
47     } else if target.contains("cloudabi") {
48         println!("cargo:rustc-link-lib=unwind");
49     }
50 }
51
52 mod llvm_libunwind {
53     use std::env;
54     use std::path::Path;
55
56     /// Compile the libunwind C/C++ source code.
57     pub fn compile() {
58         let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
59         let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
60         let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
61         let cfg = &mut cc::Build::new();
62
63         cfg.cpp(true);
64         cfg.cpp_set_stdlib(None);
65         cfg.warnings(false);
66
67         // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765
68         if target_endian_little {
69             cfg.define("__LITTLE_ENDIAN__", Some("1"));
70         }
71
72         if target_env == "msvc" {
73             // Don't pull in extra libraries on MSVC
74             cfg.flag("/Zl");
75             cfg.flag("/EHsc");
76             cfg.define("_CRT_SECURE_NO_WARNINGS", None);
77             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
78         } else {
79             cfg.flag("-std=c99");
80             cfg.flag("-std=c++11");
81             cfg.flag("-nostdinc++");
82             cfg.flag("-fno-exceptions");
83             cfg.flag("-fno-rtti");
84             cfg.flag("-fstrict-aliasing");
85             cfg.flag("-funwind-tables");
86             cfg.flag("-fvisibility=hidden");
87             cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
88             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
89         }
90
91         let mut unwind_sources = vec![
92             "Unwind-EHABI.cpp",
93             "Unwind-seh.cpp",
94             "Unwind-sjlj.c",
95             "UnwindLevel1-gcc-ext.c",
96             "UnwindLevel1.c",
97             "UnwindRegistersRestore.S",
98             "UnwindRegistersSave.S",
99             "libunwind.cpp",
100         ];
101
102         if target_vendor == "apple" {
103             unwind_sources.push("Unwind_AppleExtras.cpp");
104         }
105
106         let root = Path::new("../llvm-project/libunwind");
107         cfg.include(root.join("include"));
108         for src in unwind_sources {
109             cfg.file(root.join("src").join(src));
110         }
111
112         if target_env == "musl" {
113             // use the same C compiler command to compile C++ code so we do not need to setup the
114             // C++ compiler env variables on the builders
115             cfg.cpp(false);
116             // linking for musl is handled in lib.rs
117             cfg.cargo_metadata(false);
118             println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
119         }
120
121         cfg.compile("unwind");
122     }
123 }