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")) ||
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");
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 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");
55 /// Compile the libunwind C/C++ source code.
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();
63 cfg.cpp_set_stdlib(None);
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"));
71 if target_env == "msvc" {
72 // Don't pull in extra libraries on MSVC
75 cfg.define("_CRT_SECURE_NO_WARNINGS", None);
76 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
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");
87 let mut unwind_sources = vec![
91 "UnwindLevel1-gcc-ext.c",
93 "UnwindRegistersRestore.S",
94 "UnwindRegistersSave.S",
98 if target_vendor == "apple" {
99 unwind_sources.push("Unwind_AppleExtras.cpp");
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));
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
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());
117 cfg.compile("unwind");