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