]> git.lizzy.rs Git - rust.git/blob - library/unwind/build.rs
Merge commit 'dbee13661efa269cb4cd57bb4c6b99a19732b484' into sync_cg_clif-2020-12-27
[rust.git] / library / unwind / build.rs
1 use std::env;
2
3 fn 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         // linking for Linux is handled in lib.rs
16         if target.contains("musl") {
17             llvm_libunwind::compile();
18         }
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");
28         } else {
29             println!("cargo:rustc-link-lib=c++abi");
30         }
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     }
48 }
49
50 mod llvm_libunwind {
51     use std::env;
52     use std::path::Path;
53
54     /// Compile the libunwind C/C++ source code.
55     pub fn compile() {
56         let target = env::var("TARGET").expect("TARGET was not set");
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();
61
62         cfg.cpp(true);
63         cfg.cpp_set_stdlib(None);
64         cfg.warnings(false);
65
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"));
69         }
70
71         if target_env == "msvc" {
72             // Don't pull in extra libraries on MSVC
73             cfg.flag("/Zl");
74             cfg.flag("/EHsc");
75             cfg.define("_CRT_SECURE_NO_WARNINGS", None);
76             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
77         } else if target.contains("x86_64-fortanix-unknown-sgx") {
78             cfg.cpp(false);
79
80             cfg.static_flag(true);
81             cfg.opt_level(3);
82
83             cfg.flag("-nostdinc++");
84             cfg.flag("-fno-exceptions");
85             cfg.flag("-fno-rtti");
86             cfg.flag("-fstrict-aliasing");
87             cfg.flag("-funwind-tables");
88             cfg.flag("-fvisibility=hidden");
89             cfg.flag("-fno-stack-protector");
90             cfg.flag("-ffreestanding");
91             cfg.flag("-fexceptions");
92
93             // easiest way to undefine since no API available in cc::Build to undefine
94             cfg.flag("-U_FORTIFY_SOURCE");
95             cfg.define("_FORTIFY_SOURCE", "0");
96
97             cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
98
99             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
100             cfg.define("RUST_SGX", "1");
101             cfg.define("__NO_STRING_INLINES", None);
102             cfg.define("__NO_MATH_INLINES", None);
103             cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
104             cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
105             cfg.define("NDEBUG", None);
106         } else {
107             cfg.flag("-std=c99");
108             cfg.flag("-std=c++11");
109             cfg.flag("-nostdinc++");
110             cfg.flag("-fno-exceptions");
111             cfg.flag("-fno-rtti");
112             cfg.flag("-fstrict-aliasing");
113             cfg.flag("-funwind-tables");
114             cfg.flag("-fvisibility=hidden");
115             cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
116             cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
117         }
118
119         let mut unwind_sources = vec![
120             "Unwind-EHABI.cpp",
121             "Unwind-seh.cpp",
122             "Unwind-sjlj.c",
123             "UnwindLevel1-gcc-ext.c",
124             "UnwindLevel1.c",
125             "UnwindRegistersRestore.S",
126             "UnwindRegistersSave.S",
127             "libunwind.cpp",
128         ];
129
130         if target_vendor == "apple" {
131             unwind_sources.push("Unwind_AppleExtras.cpp");
132         }
133
134         if target.contains("x86_64-fortanix-unknown-sgx") {
135             unwind_sources.push("UnwindRustSgx.c");
136         }
137
138         let root = Path::new("../../src/llvm-project/libunwind");
139         cfg.include(root.join("include"));
140         for src in unwind_sources {
141             cfg.file(root.join("src").join(src));
142         }
143
144         if target_env == "musl" {
145             // use the same C compiler command to compile C++ code so we do not need to setup the
146             // C++ compiler env variables on the builders
147             cfg.cpp(false);
148             // linking for musl is handled in lib.rs
149             cfg.cargo_metadata(false);
150             println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
151         }
152
153         cfg.compile("unwind");
154     }
155 }