blob: ab09a6e324d8b21dac9ff4d03c99e9bfe14c49c9 [file] [log] [blame]
ThiƩbaud Weksteend4f2c3c2020-11-03 11:08:00 +01001use std::env;
2
3fn main() {
4 println!("cargo:rerun-if-changed=build.rs");
5 let target = env::var("TARGET").expect("TARGET was not set");
6
7 if cfg!(feature = "llvm-libunwind")
8 && ((target.contains("linux") && !target.contains("musl")) || target.contains("fuchsia"))
9 {
10 // Build the unwinding from libunwind C/C++ source code.
11 llvm_libunwind::compile();
12 } else if target.contains("x86_64-fortanix-unknown-sgx") {
13 llvm_libunwind::compile();
14 } else if target.contains("linux") {
15 if target.contains("musl") {
16 // linking for musl is handled in lib.rs
17 llvm_libunwind::compile();
18 } else if !target.contains("android") {
19 println!("cargo:rustc-link-lib=gcc_s");
20 }
21 } else if target.contains("freebsd") {
22 println!("cargo:rustc-link-lib=gcc_s");
23 } else if target.contains("rumprun") {
24 println!("cargo:rustc-link-lib=unwind");
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");
30 } else {
31 println!("cargo:rustc-link-lib=c++abi");
32 }
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
49 } else if target.contains("cloudabi") {
50 println!("cargo:rustc-link-lib=unwind");
51 }
52}
53
54mod llvm_libunwind {
55 use std::env;
56 use std::path::Path;
57
58 /// Compile the libunwind C/C++ source code.
59 pub fn compile() {
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();
65
66 cfg.cpp(true);
67 cfg.cpp_set_stdlib(None);
68 cfg.warnings(false);
69
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"));
73 }
74
75 if target_env == "msvc" {
76 // Don't pull in extra libraries on MSVC
77 cfg.flag("/Zl");
78 cfg.flag("/EHsc");
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") {
82 cfg.cpp(false);
83
84 cfg.static_flag(true);
85 cfg.opt_level(3);
86
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");
96
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");
100
101 cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
102
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);
110 } else {
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);
121 }
122
123 let mut unwind_sources = vec![
124 "Unwind-EHABI.cpp",
125 "Unwind-seh.cpp",
126 "Unwind-sjlj.c",
127 "UnwindLevel1-gcc-ext.c",
128 "UnwindLevel1.c",
129 "UnwindRegistersRestore.S",
130 "UnwindRegistersSave.S",
131 "libunwind.cpp",
132 ];
133
134 if target_vendor == "apple" {
135 unwind_sources.push("Unwind_AppleExtras.cpp");
136 }
137
138 if target.contains("x86_64-fortanix-unknown-sgx") {
139 unwind_sources.push("UnwindRustSgx.c");
140 }
141
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));
146 }
147
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
151 cfg.cpp(false);
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());
155 }
156
157 cfg.compile("unwind");
158 }
159}