4 println!("cargo:rerun-if-changed=build.rs");
5 let target = env::var("TARGET").expect("TARGET was not set");
7 if cfg!(feature = "llvm-libunwind")
8 && ((target.contains("linux") && !target.contains("musl")) || 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");
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");
29 println!("cargo:rustc-link-lib=c++abi");
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]
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");
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");
60 /// Compile the libunwind C/C++ source code.
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();
68 cfg.cpp_set_stdlib(None);
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"));
76 if target_env == "msvc" {
77 // Don't pull in extra libraries on MSVC
80 cfg.define("_CRT_SECURE_NO_WARNINGS", None);
81 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
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");
92 let mut unwind_sources = vec![
96 "UnwindLevel1-gcc-ext.c",
98 "UnwindRegistersRestore.S",
99 "UnwindRegistersSave.S",
103 if target_vendor == "apple" {
104 unwind_sources.push("Unwind_AppleExtras.cpp");
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));
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
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());
122 cfg.compile("unwind");