4 println!("cargo:rerun-if-changed=build.rs");
5 let target = env::var("TARGET").expect("TARGET was not set");
7 if cfg!(feature = "system-llvm-libunwind") {
11 if cfg!(feature = "llvm-libunwind")
12 && ((target.contains("linux") && !target.contains("musl")) || target.contains("fuchsia"))
14 // Build the unwinding from libunwind C/C++ source code.
15 llvm_libunwind::compile();
16 } else if target.contains("x86_64-fortanix-unknown-sgx") {
17 llvm_libunwind::compile();
18 } else if target.contains("linux") {
19 // linking for Linux is handled in lib.rs
20 if target.contains("musl") {
21 llvm_libunwind::compile();
23 } else if target.contains("freebsd") {
24 println!("cargo:rustc-link-lib=gcc_s");
25 } else if target.contains("netbsd") {
26 println!("cargo:rustc-link-lib=gcc_s");
27 } else if target.contains("openbsd") {
28 if target.contains("sparc64") {
29 println!("cargo:rustc-link-lib=gcc");
31 println!("cargo:rustc-link-lib=c++abi");
33 } else if target.contains("solaris") {
34 println!("cargo:rustc-link-lib=gcc_s");
35 } else if target.contains("illumos") {
36 println!("cargo:rustc-link-lib=gcc_s");
37 } else if target.contains("dragonfly") {
38 println!("cargo:rustc-link-lib=gcc_pic");
39 } else if target.contains("pc-windows-gnu") {
40 // This is handled in the target spec with late_link_args_[static|dynamic]
41 } else if target.contains("uwp-windows-gnu") {
42 println!("cargo:rustc-link-lib=unwind");
43 } else if target.contains("fuchsia") {
44 println!("cargo:rustc-link-lib=unwind");
45 } else if target.contains("haiku") {
46 println!("cargo:rustc-link-lib=gcc_s");
47 } else if target.contains("redox") {
48 // redox is handled in lib.rs
56 /// Compile the libunwind C/C++ source code.
58 let target = env::var("TARGET").expect("TARGET was not set");
59 let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
60 let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
61 let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
62 let cfg = &mut cc::Build::new();
65 cfg.cpp_set_stdlib(None);
68 // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765
69 if target_endian_little {
70 cfg.define("__LITTLE_ENDIAN__", Some("1"));
73 if target_env == "msvc" {
74 // Don't pull in extra libraries on MSVC
77 cfg.define("_CRT_SECURE_NO_WARNINGS", None);
78 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
79 } else if target.contains("x86_64-fortanix-unknown-sgx") {
82 cfg.static_flag(true);
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 cfg.flag("-fvisibility=hidden");
91 cfg.flag("-fno-stack-protector");
92 cfg.flag("-ffreestanding");
93 cfg.flag("-fexceptions");
95 // easiest way to undefine since no API available in cc::Build to undefine
96 cfg.flag("-U_FORTIFY_SOURCE");
97 cfg.define("_FORTIFY_SOURCE", "0");
99 cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
101 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
102 cfg.define("RUST_SGX", "1");
103 cfg.define("__NO_STRING_INLINES", None);
104 cfg.define("__NO_MATH_INLINES", None);
105 cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
106 cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
107 cfg.define("NDEBUG", None);
109 cfg.flag("-std=c99");
110 cfg.flag("-std=c++11");
111 cfg.flag("-nostdinc++");
112 cfg.flag("-fno-exceptions");
113 cfg.flag("-fno-rtti");
114 cfg.flag("-fstrict-aliasing");
115 cfg.flag("-funwind-tables");
116 cfg.flag("-fvisibility=hidden");
117 cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
118 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
121 let mut unwind_sources = vec![
125 "UnwindLevel1-gcc-ext.c",
127 "UnwindRegistersRestore.S",
128 "UnwindRegistersSave.S",
132 if target_vendor == "apple" {
133 unwind_sources.push("Unwind_AppleExtras.cpp");
136 if target.contains("x86_64-fortanix-unknown-sgx") {
137 unwind_sources.push("UnwindRustSgx.c");
140 let root = Path::new("../../src/llvm-project/libunwind");
141 cfg.include(root.join("include"));
142 for src in unwind_sources {
143 cfg.file(root.join("src").join(src));
146 if target_env == "musl" {
147 // use the same C compiler command to compile C++ code so we do not need to setup the
148 // C++ compiler env variables on the builders
150 // linking for musl is handled in lib.rs
151 cfg.cargo_metadata(false);
152 println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
155 cfg.compile("unwind");