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("rumprun") {
26 println!("cargo:rustc-link-lib=unwind");
27 } else if target.contains("netbsd") {
28 println!("cargo:rustc-link-lib=gcc_s");
29 } else if target.contains("openbsd") {
30 if target.contains("sparc64") {
31 println!("cargo:rustc-link-lib=gcc");
33 println!("cargo:rustc-link-lib=c++abi");
35 } else if target.contains("solaris") {
36 println!("cargo:rustc-link-lib=gcc_s");
37 } else if target.contains("illumos") {
38 println!("cargo:rustc-link-lib=gcc_s");
39 } else if target.contains("dragonfly") {
40 println!("cargo:rustc-link-lib=gcc_pic");
41 } else if target.contains("pc-windows-gnu") {
42 // This is handled in the target spec with late_link_args_[static|dynamic]
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
58 /// Compile the libunwind C/C++ source code.
60 let target = env::var("TARGET").expect("TARGET was not set");
61 let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
62 let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
63 let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
64 let cfg = &mut cc::Build::new();
67 cfg.cpp_set_stdlib(None);
70 // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765
71 if target_endian_little {
72 cfg.define("__LITTLE_ENDIAN__", Some("1"));
75 if target_env == "msvc" {
76 // Don't pull in extra libraries on MSVC
79 cfg.define("_CRT_SECURE_NO_WARNINGS", None);
80 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
81 } else if target.contains("x86_64-fortanix-unknown-sgx") {
84 cfg.static_flag(true);
87 cfg.flag("-nostdinc++");
88 cfg.flag("-fno-exceptions");
89 cfg.flag("-fno-rtti");
90 cfg.flag("-fstrict-aliasing");
91 cfg.flag("-funwind-tables");
92 cfg.flag("-fvisibility=hidden");
93 cfg.flag("-fno-stack-protector");
94 cfg.flag("-ffreestanding");
95 cfg.flag("-fexceptions");
97 // easiest way to undefine since no API available in cc::Build to undefine
98 cfg.flag("-U_FORTIFY_SOURCE");
99 cfg.define("_FORTIFY_SOURCE", "0");
101 cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
103 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
104 cfg.define("RUST_SGX", "1");
105 cfg.define("__NO_STRING_INLINES", None);
106 cfg.define("__NO_MATH_INLINES", None);
107 cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
108 cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
109 cfg.define("NDEBUG", None);
111 cfg.flag("-std=c99");
112 cfg.flag("-std=c++11");
113 cfg.flag("-nostdinc++");
114 cfg.flag("-fno-exceptions");
115 cfg.flag("-fno-rtti");
116 cfg.flag("-fstrict-aliasing");
117 cfg.flag("-funwind-tables");
118 cfg.flag("-fvisibility=hidden");
119 cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
120 cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
123 let mut unwind_sources = vec![
127 "UnwindLevel1-gcc-ext.c",
129 "UnwindRegistersRestore.S",
130 "UnwindRegistersSave.S",
134 if target_vendor == "apple" {
135 unwind_sources.push("Unwind_AppleExtras.cpp");
138 if target.contains("x86_64-fortanix-unknown-sgx") {
139 unwind_sources.push("UnwindRustSgx.c");
142 let root = Path::new("../../src/llvm-project/libunwind");
143 cfg.include(root.join("include"));
144 for src in unwind_sources {
145 cfg.file(root.join("src").join(src));
148 if target_env == "musl" {
149 // use the same C compiler command to compile C++ code so we do not need to setup the
150 // C++ compiler env variables on the builders
152 // linking for musl is handled in lib.rs
153 cfg.cargo_metadata(false);
154 println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
157 cfg.compile("unwind");