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("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");
56 /// Compile the libunwind C/C++ source code.
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();
64 cfg.cpp_set_stdlib(None);
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"));
72 if target_env == "msvc" {
73 // Don't pull in extra libraries on MSVC
76 cfg.define("_CRT_SECURE_NO_WARNINGS", None);
77 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
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);
91 let mut unwind_sources = vec![
95 "UnwindLevel1-gcc-ext.c",
97 "UnwindRegistersRestore.S",
98 "UnwindRegistersSave.S",
102 if target_vendor == "apple" {
103 unwind_sources.push("Unwind_AppleExtras.cpp");
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));
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
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());
121 cfg.compile("unwind");