]> git.proxmox.com Git - rustc.git/blame - vendor/compiler_builtins/build.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / vendor / compiler_builtins / build.rs
CommitLineData
0531ce1d 1use std::env;
041b39d2 2
0531ce1d
XL
3fn main() {
4 println!("cargo:rerun-if-changed=build.rs");
041b39d2 5
0531ce1d 6 let target = env::var("TARGET").unwrap();
0731742a
XL
7 let cwd = env::current_dir().unwrap();
8
9 println!("cargo:compiler-rt={}", cwd.join("compiler-rt").display());
041b39d2 10
60c5eb7d
XL
11 // Activate libm's unstable features to make full use of Nightly.
12 println!("cargo:rustc-cfg=feature=\"unstable\"");
13
0531ce1d
XL
14 // Emscripten's runtime includes all the builtins
15 if target.contains("emscripten") {
16 return;
041b39d2
XL
17 }
18
94b46f34
XL
19 // OpenBSD provides compiler_rt by default, use it instead of rebuilding it from source
20 if target.contains("openbsd") {
21 println!("cargo:rustc-link-search=native=/usr/lib");
8faf50e0 22 println!("cargo:rustc-link-lib=compiler_rt");
94b46f34
XL
23 return;
24 }
25
0731742a 26 // Forcibly enable memory intrinsics on wasm32 & SGX as we don't have a libc to
0531ce1d 27 // provide them.
48663c56
XL
28 if (target.contains("wasm32") && !target.contains("wasi"))
29 || (target.contains("sgx") && target.contains("fortanix"))
30 {
0531ce1d 31 println!("cargo:rustc-cfg=feature=\"mem\"");
041b39d2
XL
32 }
33
0531ce1d
XL
34 // NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
35 // target triple. This is usually correct for our built-in targets but can break in presence of
36 // custom targets, which can have arbitrary names.
37 let llvm_target = target.split('-').collect::<Vec<_>>();
abe05a73 38
0531ce1d
XL
39 // Build missing intrinsics from compiler-rt C source code. If we're
40 // mangling names though we assume that we're also in test mode so we don't
41 // build anything and we rely on the upstream implementation of compiler-rt
42 // functions
43 if !cfg!(feature = "mangled-names") && cfg!(feature = "c") {
48663c56
XL
44 // Don't use a C compiler for these targets:
45 //
46 // * wasm32 - clang 8 for wasm is somewhat hard to come by and it's
47 // unlikely that the C is really that much better than our own Rust.
48 // * nvptx - everything is bitcode, not compatible with mixed C/Rust
49 // * riscv - the rust-lang/rust distribution container doesn't have a C
50 // compiler nor is cc-rs ready for compilation to riscv (at this
51 // time). This can probably be removed in the future
52 if !target.contains("wasm32") && !target.contains("nvptx") && !target.starts_with("riscv") {
0531ce1d 53 #[cfg(feature = "c")]
ba9703b0 54 c::compile(&llvm_target, &target);
abe05a73
XL
55 }
56 }
57
0531ce1d
XL
58 // To compile intrinsics.rs for thumb targets, where there is no libc
59 if llvm_target[0].starts_with("thumb") {
60 println!("cargo:rustc-cfg=thumb")
041b39d2
XL
61 }
62
532ac7d7
XL
63 // compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
64 // these targets do not have full Thumb-2 support but only original Thumb-1.
65 // We have to cfg our code accordingly.
66 if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
67 println!("cargo:rustc-cfg=thumb_1")
041b39d2
XL
68 }
69
0531ce1d
XL
70 // Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures.
71 if llvm_target[0] == "armv4t" || llvm_target[0] == "armv5te" {
72 println!("cargo:rustc-cfg=kernel_user_helpers")
041b39d2
XL
73 }
74}
75
76#[cfg(feature = "c")]
77mod c {
ea8adc8c 78 extern crate cc;
041b39d2
XL
79
80 use std::collections::BTreeMap;
81 use std::env;
dc9dc135 82 use std::path::PathBuf;
041b39d2
XL
83
84 struct Sources {
85 // SYMBOL -> PATH TO SOURCE
86 map: BTreeMap<&'static str, &'static str>,
87 }
88
89 impl Sources {
90 fn new() -> Sources {
48663c56
XL
91 Sources {
92 map: BTreeMap::new(),
93 }
041b39d2
XL
94 }
95
48663c56 96 fn extend(&mut self, sources: &[(&'static str, &'static str)]) {
041b39d2
XL
97 // NOTE Some intrinsics have both a generic implementation (e.g.
98 // `floatdidf.c`) and an arch optimized implementation
99 // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
100 // implementation and discard the generic implementation. If we don't
101 // and keep both implementations, the linker will yell at us about
102 // duplicate symbols!
48663c56 103 for (symbol, src) in sources {
041b39d2
XL
104 if src.contains("/") {
105 // Arch-optimized implementation (preferred)
106 self.map.insert(symbol, src);
107 } else {
108 // Generic implementation
109 if !self.map.contains_key(symbol) {
110 self.map.insert(symbol, src);
111 }
112 }
113 }
114 }
115
116 fn remove(&mut self, symbols: &[&str]) {
117 for symbol in symbols {
118 self.map.remove(*symbol).unwrap();
119 }
120 }
121 }
8bb4bdeb 122
041b39d2 123 /// Compile intrinsics from the compiler-rt C source code
ba9703b0 124 pub fn compile(llvm_target: &[&str], target: &String) {
041b39d2
XL
125 let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
126 let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
127 let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
128 let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
ba9703b0 129 let mut consider_float_intrinsics = true;
ea8adc8c
XL
130 let cfg = &mut cc::Build::new();
131
ba9703b0
XL
132 // AArch64 GCCs exit with an error condition when they encounter any kind of floating point
133 // code if the `nofp` and/or `nosimd` compiler flags have been set.
134 //
135 // Therefore, evaluate if those flags are present and set a boolean that causes any
136 // compiler-rt intrinsics that contain floating point source to be excluded for this target.
137 if target_arch == "aarch64" {
138 let cflags_key = String::from("CFLAGS_") + &(target.to_owned().replace("-", "_"));
139 if let Ok(cflags_value) = env::var(cflags_key) {
140 if cflags_value.contains("+nofp") || cflags_value.contains("+nosimd") {
141 consider_float_intrinsics = false;
142 }
143 }
144 }
145
ea8adc8c 146 cfg.warnings(false);
041b39d2
XL
147
148 if target_env == "msvc" {
149 // Don't pull in extra libraries on MSVC
150 cfg.flag("/Zl");
151
152 // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
153 cfg.define("__func__", Some("__FUNCTION__"));
154 } else {
155 // Turn off various features of gcc and such, mostly copying
156 // compiler-rt's build system already
157 cfg.flag("-fno-builtin");
158 cfg.flag("-fvisibility=hidden");
041b39d2 159 cfg.flag("-ffreestanding");
ff7c6d11
XL
160 // Avoid the following warning appearing once **per file**:
161 // clang: warning: optimization flag '-fomit-frame-pointer' is not supported for target 'armv7' [-Wignored-optimization-argument]
162 //
163 // Note that compiler-rt's build system also checks
164 //
165 // `check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)`
166 //
167 // in https://github.com/rust-lang/compiler-rt/blob/c8fbcb3/cmake/config-ix.cmake#L19.
168 cfg.flag_if_supported("-fomit-frame-pointer");
041b39d2
XL
169 cfg.define("VISIBILITY_HIDDEN", None);
170 }
171
041b39d2 172 let mut sources = Sources::new();
48663c56
XL
173 sources.extend(&[
174 ("__absvdi2", "absvdi2.c"),
175 ("__absvsi2", "absvsi2.c"),
176 ("__addvdi3", "addvdi3.c"),
177 ("__addvsi3", "addvsi3.c"),
178 ("apple_versioning", "apple_versioning.c"),
179 ("__clzdi2", "clzdi2.c"),
180 ("__clzsi2", "clzsi2.c"),
181 ("__cmpdi2", "cmpdi2.c"),
182 ("__ctzdi2", "ctzdi2.c"),
183 ("__ctzsi2", "ctzsi2.c"),
48663c56 184 ("__int_util", "int_util.c"),
48663c56
XL
185 ("__mulvdi3", "mulvdi3.c"),
186 ("__mulvsi3", "mulvsi3.c"),
48663c56 187 ("__negdi2", "negdi2.c"),
48663c56
XL
188 ("__negvdi2", "negvdi2.c"),
189 ("__negvsi2", "negvsi2.c"),
190 ("__paritydi2", "paritydi2.c"),
191 ("__paritysi2", "paritysi2.c"),
192 ("__popcountdi2", "popcountdi2.c"),
193 ("__popcountsi2", "popcountsi2.c"),
48663c56
XL
194 ("__subvdi3", "subvdi3.c"),
195 ("__subvsi3", "subvsi3.c"),
48663c56
XL
196 ("__ucmpdi2", "ucmpdi2.c"),
197 ]);
041b39d2 198
ba9703b0
XL
199 if consider_float_intrinsics {
200 sources.extend(&[
201 ("__divdc3", "divdc3.c"),
202 ("__divsc3", "divsc3.c"),
203 ("__divxc3", "divxc3.c"),
204 ("__extendhfsf2", "extendhfsf2.c"),
205 ("__muldc3", "muldc3.c"),
206 ("__mulsc3", "mulsc3.c"),
207 ("__mulxc3", "mulxc3.c"),
208 ("__negdf2", "negdf2.c"),
209 ("__negsf2", "negsf2.c"),
210 ("__powixf2", "powixf2.c"),
211 ("__truncdfhf2", "truncdfhf2.c"),
212 ("__truncdfsf2", "truncdfsf2.c"),
213 ("__truncsfhf2", "truncsfhf2.c"),
214 ]);
215 }
216
041b39d2
XL
217 // When compiling in rustbuild (the rust-lang/rust repo) this library
218 // also needs to satisfy intrinsics that jemalloc or C in general may
219 // need, so include a few more that aren't typically needed by
220 // LLVM/Rust.
ea8adc8c 221 if cfg!(feature = "rustbuild") {
48663c56 222 sources.extend(&[("__ffsdi2", "ffsdi2.c")]);
ea8adc8c 223 }
041b39d2 224
ea8adc8c
XL
225 // On iOS and 32-bit OSX these are all just empty intrinsics, no need to
226 // include them.
227 if target_os != "ios" && (target_vendor != "apple" || target_arch != "x86") {
48663c56
XL
228 sources.extend(&[
229 ("__absvti2", "absvti2.c"),
230 ("__addvti3", "addvti3.c"),
231 ("__clzti2", "clzti2.c"),
232 ("__cmpti2", "cmpti2.c"),
233 ("__ctzti2", "ctzti2.c"),
234 ("__ffsti2", "ffsti2.c"),
235 ("__mulvti3", "mulvti3.c"),
236 ("__negti2", "negti2.c"),
48663c56
XL
237 ("__parityti2", "parityti2.c"),
238 ("__popcountti2", "popcountti2.c"),
239 ("__subvti3", "subvti3.c"),
240 ("__ucmpti2", "ucmpti2.c"),
241 ]);
ba9703b0
XL
242
243 if consider_float_intrinsics {
244 sources.extend(&[("__negvti2", "negvti2.c")]);
245 }
041b39d2
XL
246 }
247
248 if target_vendor == "apple" {
48663c56
XL
249 sources.extend(&[
250 ("atomic_flag_clear", "atomic_flag_clear.c"),
251 ("atomic_flag_clear_explicit", "atomic_flag_clear_explicit.c"),
252 ("atomic_flag_test_and_set", "atomic_flag_test_and_set.c"),
253 (
254 "atomic_flag_test_and_set_explicit",
041b39d2 255 "atomic_flag_test_and_set_explicit.c",
48663c56
XL
256 ),
257 ("atomic_signal_fence", "atomic_signal_fence.c"),
258 ("atomic_thread_fence", "atomic_thread_fence.c"),
259 ]);
041b39d2
XL
260 }
261
262 if target_env == "msvc" {
263 if target_arch == "x86_64" {
48663c56
XL
264 sources.extend(&[
265 ("__floatdisf", "x86_64/floatdisf.c"),
266 ("__floatdixf", "x86_64/floatdixf.c"),
267 ]);
041b39d2
XL
268 }
269 } else {
270 // None of these seem to be used on x86_64 windows, and they've all
271 // got the wrong ABI anyway, so we want to avoid them.
272 if target_os != "windows" {
273 if target_arch == "x86_64" {
48663c56
XL
274 sources.extend(&[
275 ("__floatdisf", "x86_64/floatdisf.c"),
276 ("__floatdixf", "x86_64/floatdixf.c"),
277 ("__floatundidf", "x86_64/floatundidf.S"),
278 ("__floatundisf", "x86_64/floatundisf.S"),
279 ("__floatundixf", "x86_64/floatundixf.S"),
280 ]);
041b39d2
XL
281 }
282 }
283
284 if target_arch == "x86" {
48663c56
XL
285 sources.extend(&[
286 ("__ashldi3", "i386/ashldi3.S"),
287 ("__ashrdi3", "i386/ashrdi3.S"),
288 ("__divdi3", "i386/divdi3.S"),
289 ("__floatdidf", "i386/floatdidf.S"),
290 ("__floatdisf", "i386/floatdisf.S"),
291 ("__floatdixf", "i386/floatdixf.S"),
292 ("__floatundidf", "i386/floatundidf.S"),
293 ("__floatundisf", "i386/floatundisf.S"),
294 ("__floatundixf", "i386/floatundixf.S"),
295 ("__lshrdi3", "i386/lshrdi3.S"),
296 ("__moddi3", "i386/moddi3.S"),
297 ("__muldi3", "i386/muldi3.S"),
298 ("__udivdi3", "i386/udivdi3.S"),
299 ("__umoddi3", "i386/umoddi3.S"),
300 ]);
041b39d2
XL
301 }
302 }
303
a1dfa0c6 304 if target_arch == "arm" && target_os != "ios" && target_env != "msvc" {
48663c56
XL
305 sources.extend(&[
306 ("__aeabi_div0", "arm/aeabi_div0.c"),
307 ("__aeabi_drsub", "arm/aeabi_drsub.c"),
308 ("__aeabi_frsub", "arm/aeabi_frsub.c"),
309 ("__bswapdi2", "arm/bswapdi2.S"),
310 ("__bswapsi2", "arm/bswapsi2.S"),
311 ("__clzdi2", "arm/clzdi2.S"),
312 ("__clzsi2", "arm/clzsi2.S"),
313 ("__divmodsi4", "arm/divmodsi4.S"),
314 ("__divsi3", "arm/divsi3.S"),
315 ("__modsi3", "arm/modsi3.S"),
316 ("__switch16", "arm/switch16.S"),
317 ("__switch32", "arm/switch32.S"),
318 ("__switch8", "arm/switch8.S"),
319 ("__switchu8", "arm/switchu8.S"),
320 ("__sync_synchronize", "arm/sync_synchronize.S"),
321 ("__udivmodsi4", "arm/udivmodsi4.S"),
322 ("__udivsi3", "arm/udivsi3.S"),
323 ("__umodsi3", "arm/umodsi3.S"),
324 ]);
2c00a5a8 325
532ac7d7 326 if target_os == "freebsd" {
48663c56 327 sources.extend(&[("__clear_cache", "clear_cache.c")]);
532ac7d7
XL
328 }
329
2c00a5a8
XL
330 // First of all aeabi_cdcmp and aeabi_cfcmp are never called by LLVM.
331 // Second are little-endian only, so build fail on big-endian targets.
332 // Temporally workaround: exclude these files for big-endian targets.
48663c56
XL
333 if !llvm_target[0].starts_with("thumbeb") && !llvm_target[0].starts_with("armeb") {
334 sources.extend(&[
335 ("__aeabi_cdcmp", "arm/aeabi_cdcmp.S"),
336 ("__aeabi_cdcmpeq_check_nan", "arm/aeabi_cdcmpeq_check_nan.c"),
337 ("__aeabi_cfcmp", "arm/aeabi_cfcmp.S"),
338 ("__aeabi_cfcmpeq_check_nan", "arm/aeabi_cfcmpeq_check_nan.c"),
339 ]);
2c00a5a8 340 }
041b39d2
XL
341 }
342
343 if llvm_target[0] == "armv7" {
48663c56
XL
344 sources.extend(&[
345 ("__sync_fetch_and_add_4", "arm/sync_fetch_and_add_4.S"),
346 ("__sync_fetch_and_add_8", "arm/sync_fetch_and_add_8.S"),
347 ("__sync_fetch_and_and_4", "arm/sync_fetch_and_and_4.S"),
348 ("__sync_fetch_and_and_8", "arm/sync_fetch_and_and_8.S"),
349 ("__sync_fetch_and_max_4", "arm/sync_fetch_and_max_4.S"),
350 ("__sync_fetch_and_max_8", "arm/sync_fetch_and_max_8.S"),
351 ("__sync_fetch_and_min_4", "arm/sync_fetch_and_min_4.S"),
352 ("__sync_fetch_and_min_8", "arm/sync_fetch_and_min_8.S"),
353 ("__sync_fetch_and_nand_4", "arm/sync_fetch_and_nand_4.S"),
354 ("__sync_fetch_and_nand_8", "arm/sync_fetch_and_nand_8.S"),
355 ("__sync_fetch_and_or_4", "arm/sync_fetch_and_or_4.S"),
356 ("__sync_fetch_and_or_8", "arm/sync_fetch_and_or_8.S"),
357 ("__sync_fetch_and_sub_4", "arm/sync_fetch_and_sub_4.S"),
358 ("__sync_fetch_and_sub_8", "arm/sync_fetch_and_sub_8.S"),
359 ("__sync_fetch_and_umax_4", "arm/sync_fetch_and_umax_4.S"),
360 ("__sync_fetch_and_umax_8", "arm/sync_fetch_and_umax_8.S"),
361 ("__sync_fetch_and_umin_4", "arm/sync_fetch_and_umin_4.S"),
362 ("__sync_fetch_and_umin_8", "arm/sync_fetch_and_umin_8.S"),
363 ("__sync_fetch_and_xor_4", "arm/sync_fetch_and_xor_4.S"),
364 ("__sync_fetch_and_xor_8", "arm/sync_fetch_and_xor_8.S"),
365 ]);
041b39d2
XL
366 }
367
368 if llvm_target.last().unwrap().ends_with("eabihf") {
48663c56
XL
369 if !llvm_target[0].starts_with("thumbv7em")
370 && !llvm_target[0].starts_with("thumbv8m.main")
371 {
532ac7d7
XL
372 // The FPU option chosen for these architectures in cc-rs, ie:
373 // -mfpu=fpv4-sp-d16 for thumbv7em
374 // -mfpu=fpv5-sp-d16 for thumbv8m.main
375 // do not support double precision floating points conversions so the files
376 // that include such instructions are not included for these targets.
48663c56
XL
377 sources.extend(&[
378 ("__fixdfsivfp", "arm/fixdfsivfp.S"),
379 ("__fixunsdfsivfp", "arm/fixunsdfsivfp.S"),
380 ("__floatsidfvfp", "arm/floatsidfvfp.S"),
381 ("__floatunssidfvfp", "arm/floatunssidfvfp.S"),
382 ]);
041b39d2
XL
383 }
384
48663c56
XL
385 sources.extend(&[
386 ("__fixsfsivfp", "arm/fixsfsivfp.S"),
387 ("__fixunssfsivfp", "arm/fixunssfsivfp.S"),
388 ("__floatsisfvfp", "arm/floatsisfvfp.S"),
389 ("__floatunssisfvfp", "arm/floatunssisfvfp.S"),
390 ("__floatunssisfvfp", "arm/floatunssisfvfp.S"),
391 ("__restore_vfp_d8_d15_regs", "arm/restore_vfp_d8_d15_regs.S"),
392 ("__save_vfp_d8_d15_regs", "arm/save_vfp_d8_d15_regs.S"),
393 ("__negdf2vfp", "arm/negdf2vfp.S"),
394 ("__negsf2vfp", "arm/negsf2vfp.S"),
395 ]);
041b39d2
XL
396 }
397
ba9703b0 398 if target_arch == "aarch64" && consider_float_intrinsics {
48663c56
XL
399 sources.extend(&[
400 ("__comparetf2", "comparetf2.c"),
401 ("__extenddftf2", "extenddftf2.c"),
402 ("__extendsftf2", "extendsftf2.c"),
403 ("__fixtfdi", "fixtfdi.c"),
404 ("__fixtfsi", "fixtfsi.c"),
405 ("__fixtfti", "fixtfti.c"),
406 ("__fixunstfdi", "fixunstfdi.c"),
407 ("__fixunstfsi", "fixunstfsi.c"),
408 ("__fixunstfti", "fixunstfti.c"),
409 ("__floatditf", "floatditf.c"),
410 ("__floatsitf", "floatsitf.c"),
411 ("__floatunditf", "floatunditf.c"),
412 ("__floatunsitf", "floatunsitf.c"),
413 ("__trunctfdf2", "trunctfdf2.c"),
414 ("__trunctfsf2", "trunctfsf2.c"),
415 ]);
8faf50e0
XL
416
417 if target_os != "windows" {
48663c56 418 sources.extend(&[("__multc3", "multc3.c")]);
8faf50e0 419 }
041b39d2
XL
420 }
421
422 // Remove the assembly implementations that won't compile for the target
532ac7d7 423 if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
48663c56
XL
424 let mut to_remove = Vec::new();
425 for (k, v) in sources.map.iter() {
426 if v.ends_with(".S") {
427 to_remove.push(*k);
428 }
429 }
430 sources.remove(&to_remove);
041b39d2
XL
431
432 // But use some generic implementations where possible
48663c56 433 sources.extend(&[("__clzdi2", "clzdi2.c"), ("__clzsi2", "clzsi2.c")])
041b39d2
XL
434 }
435
436 if llvm_target[0] == "thumbv7m" || llvm_target[0] == "thumbv7em" {
48663c56 437 sources.remove(&["__aeabi_cdcmp", "__aeabi_cfcmp"]);
041b39d2
XL
438 }
439
dc9dc135
XL
440 // When compiling the C code we require the user to tell us where the
441 // source code is, and this is largely done so when we're compiling as
442 // part of rust-lang/rust we can use the same llvm-project repository as
443 // rust-lang/rust.
444 let root = match env::var_os("RUST_COMPILER_RT_ROOT") {
445 Some(s) => PathBuf::from(s),
446 None => panic!("RUST_COMPILER_RT_ROOT is not set"),
041b39d2 447 };
dc9dc135
XL
448 if !root.exists() {
449 panic!("RUST_COMPILER_RT_ROOT={} does not exist", root.display());
450 }
041b39d2 451
60c5eb7d
XL
452 // Support deterministic builds by remapping the __FILE__ prefix if the
453 // compiler supports it. This fixes the nondeterminism caused by the
454 // use of that macro in lib/builtins/int_util.h in compiler-rt.
455 cfg.flag_if_supported(&format!("-ffile-prefix-map={}=.", root.display()));
456
dc9dc135 457 let src_dir = root.join("lib/builtins");
48663c56 458 for (sym, src) in sources.map.iter() {
041b39d2
XL
459 let src = src_dir.join(src);
460 cfg.file(&src);
461 println!("cargo:rerun-if-changed={}", src.display());
48663c56 462 println!("cargo:rustc-cfg={}=\"optimized-c\"", sym);
8bb4bdeb 463 }
041b39d2
XL
464
465 cfg.compile("libcompiler-rt.a");
466 }
9e0c209e 467}