4 println
!("cargo:rerun-if-changed=build.rs");
6 let target
= env
::var("TARGET").unwrap();
7 let cwd
= env
::current_dir().unwrap();
9 println
!("cargo:compiler-rt={}", cwd
.join("compiler-rt").display());
11 // Activate libm's unstable features to make full use of Nightly.
12 println
!("cargo:rustc-cfg=feature=\"unstable\"");
14 // Emscripten's runtime includes all the builtins
15 if target
.contains("emscripten") {
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");
22 println
!("cargo:rustc-link-lib=compiler_rt");
26 // Forcibly enable memory intrinsics on wasm & SGX as we don't have a libc to
28 if (target
.contains("wasm") && !target
.contains("wasi"))
29 || (target
.contains("sgx") && target
.contains("fortanix"))
30 || target
.contains("-none")
31 || target
.contains("nvptx")
32 || target
.contains("uefi")
34 println
!("cargo:rustc-cfg=feature=\"mem\"");
37 // These targets have hardware unaligned access support.
38 if target
.contains("x86_64")
39 || target
.contains("i686")
40 || target
.contains("aarch64")
41 || target
.contains("bpf")
43 println
!("cargo:rustc-cfg=feature=\"mem-unaligned\"");
46 // NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
47 // target triple. This is usually correct for our built-in targets but can break in presence of
48 // custom targets, which can have arbitrary names.
49 let llvm_target
= target
.split('
-'
).collect
::<Vec
<_
>>();
51 // Build missing intrinsics from compiler-rt C source code. If we're
52 // mangling names though we assume that we're also in test mode so we don't
53 // build anything and we rely on the upstream implementation of compiler-rt
55 if !cfg
!(feature
= "mangled-names") && cfg
!(feature
= "c") {
56 // Don't use a C compiler for these targets:
58 // * wasm - clang for wasm is somewhat hard to come by and it's
59 // unlikely that the C is really that much better than our own Rust.
60 // * nvptx - everything is bitcode, not compatible with mixed C/Rust
61 // * riscv - the rust-lang/rust distribution container doesn't have a C
63 if !target
.contains("wasm")
64 && !target
.contains("nvptx")
65 && (!target
.starts_with("riscv") || target
.contains("xous"))
68 c
::compile(&llvm_target
, &target
);
72 // To compile intrinsics.rs for thumb targets, where there is no libc
73 if llvm_target
[0].starts_with("thumb") {
74 println
!("cargo:rustc-cfg=thumb")
77 // compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
78 // these targets do not have full Thumb-2 support but only original Thumb-1.
79 // We have to cfg our code accordingly.
80 if llvm_target
[0] == "thumbv6m" || llvm_target
[0] == "thumbv8m.base" {
81 println
!("cargo:rustc-cfg=thumb_1")
84 // Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures. This
85 // includes the old androideabi. It is deprecated but it is available as a
86 // rustc target (arm-linux-androideabi).
87 if llvm_target
[0] == "armv4t"
88 || llvm_target
[0] == "armv5te"
89 || target
== "arm-linux-androideabi"
91 println
!("cargo:rustc-cfg=kernel_user_helpers")
99 use std
::collections
::{BTreeMap, HashSet}
;
103 use std
::path
::{Path, PathBuf}
;
106 // SYMBOL -> PATH TO SOURCE
107 map
: BTreeMap
<&'
static str, &'
static str>,
111 fn new() -> Sources
{
113 map
: BTreeMap
::new(),
117 fn extend(&mut self, sources
: &[(&'
static str, &'
static str)]) {
118 // NOTE Some intrinsics have both a generic implementation (e.g.
119 // `floatdidf.c`) and an arch optimized implementation
120 // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
121 // implementation and discard the generic implementation. If we don't
122 // and keep both implementations, the linker will yell at us about
123 // duplicate symbols!
124 for (symbol
, src
) in sources
{
125 if src
.contains("/") {
126 // Arch-optimized implementation (preferred)
127 self.map
.insert(symbol
, src
);
129 // Generic implementation
130 if !self.map
.contains_key(symbol
) {
131 self.map
.insert(symbol
, src
);
137 fn remove(&mut self, symbols
: &[&str]) {
138 for symbol
in symbols
{
139 self.map
.remove(*symbol
).unwrap();
144 /// Compile intrinsics from the compiler-rt C source code
145 pub fn compile(llvm_target
: &[&str], target
: &String
) {
146 let target_arch
= env
::var("CARGO_CFG_TARGET_ARCH").unwrap();
147 let target_env
= env
::var("CARGO_CFG_TARGET_ENV").unwrap();
148 let target_os
= env
::var("CARGO_CFG_TARGET_OS").unwrap();
149 let target_vendor
= env
::var("CARGO_CFG_TARGET_VENDOR").unwrap();
150 let mut consider_float_intrinsics
= true;
151 let cfg
= &mut cc
::Build
::new();
153 // AArch64 GCCs exit with an error condition when they encounter any kind of floating point
154 // code if the `nofp` and/or `nosimd` compiler flags have been set.
156 // Therefore, evaluate if those flags are present and set a boolean that causes any
157 // compiler-rt intrinsics that contain floating point source to be excluded for this target.
158 if target_arch
== "aarch64" {
159 let cflags_key
= String
::from("CFLAGS_") + &(target
.to_owned().replace("-", "_"));
160 if let Ok(cflags_value
) = env
::var(cflags_key
) {
161 if cflags_value
.contains("+nofp") || cflags_value
.contains("+nosimd") {
162 consider_float_intrinsics
= false;
169 if target_env
== "msvc" {
170 // Don't pull in extra libraries on MSVC
173 // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
174 cfg
.define("__func__", Some("__FUNCTION__"));
176 // Turn off various features of gcc and such, mostly copying
177 // compiler-rt's build system already
178 cfg
.flag("-fno-builtin");
179 cfg
.flag("-fvisibility=hidden");
180 cfg
.flag("-ffreestanding");
181 // Avoid the following warning appearing once **per file**:
182 // clang: warning: optimization flag '-fomit-frame-pointer' is not supported for target 'armv7' [-Wignored-optimization-argument]
184 // Note that compiler-rt's build system also checks
186 // `check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)`
188 // in https://github.com/rust-lang/compiler-rt/blob/c8fbcb3/cmake/config-ix.cmake#L19.
189 cfg
.flag_if_supported("-fomit-frame-pointer");
190 cfg
.define("VISIBILITY_HIDDEN", None
);
193 let mut sources
= Sources
::new();
195 ("__absvdi2", "absvdi2.c"),
196 ("__absvsi2", "absvsi2.c"),
197 ("__addvdi3", "addvdi3.c"),
198 ("__addvsi3", "addvsi3.c"),
199 ("__clzdi2", "clzdi2.c"),
200 ("__clzsi2", "clzsi2.c"),
201 ("__cmpdi2", "cmpdi2.c"),
202 ("__ctzdi2", "ctzdi2.c"),
203 ("__ctzsi2", "ctzsi2.c"),
204 ("__int_util", "int_util.c"),
205 ("__mulvdi3", "mulvdi3.c"),
206 ("__mulvsi3", "mulvsi3.c"),
207 ("__negdi2", "negdi2.c"),
208 ("__negvdi2", "negvdi2.c"),
209 ("__negvsi2", "negvsi2.c"),
210 ("__paritydi2", "paritydi2.c"),
211 ("__paritysi2", "paritysi2.c"),
212 ("__popcountdi2", "popcountdi2.c"),
213 ("__popcountsi2", "popcountsi2.c"),
214 ("__subvdi3", "subvdi3.c"),
215 ("__subvsi3", "subvsi3.c"),
216 ("__ucmpdi2", "ucmpdi2.c"),
219 if consider_float_intrinsics
{
221 ("__divdc3", "divdc3.c"),
222 ("__divsc3", "divsc3.c"),
223 ("__divxc3", "divxc3.c"),
224 ("__extendhfsf2", "extendhfsf2.c"),
225 ("__muldc3", "muldc3.c"),
226 ("__mulsc3", "mulsc3.c"),
227 ("__mulxc3", "mulxc3.c"),
228 ("__negdf2", "negdf2.c"),
229 ("__negsf2", "negsf2.c"),
230 ("__powixf2", "powixf2.c"),
231 ("__truncdfhf2", "truncdfhf2.c"),
232 ("__truncsfhf2", "truncsfhf2.c"),
236 // When compiling in rustbuild (the rust-lang/rust repo) this library
237 // also needs to satisfy intrinsics that jemalloc or C in general may
238 // need, so include a few more that aren't typically needed by
240 if cfg
!(feature
= "rustbuild") {
241 sources
.extend(&[("__ffsdi2", "ffsdi2.c")]);
244 // On iOS and 32-bit OSX these are all just empty intrinsics, no need to
246 if target_os
!= "ios"
247 && target_os
!= "watchos"
248 && (target_vendor
!= "apple" || target_arch
!= "x86")
251 ("__absvti2", "absvti2.c"),
252 ("__addvti3", "addvti3.c"),
253 ("__clzti2", "clzti2.c"),
254 ("__cmpti2", "cmpti2.c"),
255 ("__ctzti2", "ctzti2.c"),
256 ("__ffsti2", "ffsti2.c"),
257 ("__mulvti3", "mulvti3.c"),
258 ("__negti2", "negti2.c"),
259 ("__parityti2", "parityti2.c"),
260 ("__popcountti2", "popcountti2.c"),
261 ("__subvti3", "subvti3.c"),
262 ("__ucmpti2", "ucmpti2.c"),
265 if consider_float_intrinsics
{
266 sources
.extend(&[("__negvti2", "negvti2.c")]);
270 if target_vendor
== "apple" {
272 ("atomic_flag_clear", "atomic_flag_clear.c"),
273 ("atomic_flag_clear_explicit", "atomic_flag_clear_explicit.c"),
274 ("atomic_flag_test_and_set", "atomic_flag_test_and_set.c"),
276 "atomic_flag_test_and_set_explicit",
277 "atomic_flag_test_and_set_explicit.c",
279 ("atomic_signal_fence", "atomic_signal_fence.c"),
280 ("atomic_thread_fence", "atomic_thread_fence.c"),
284 if target_env
== "msvc" {
285 if target_arch
== "x86_64" {
286 sources
.extend(&[("__floatdixf", "x86_64/floatdixf.c")]);
289 // None of these seem to be used on x86_64 windows, and they've all
290 // got the wrong ABI anyway, so we want to avoid them.
291 if target_os
!= "windows" {
292 if target_arch
== "x86_64" {
294 ("__floatdixf", "x86_64/floatdixf.c"),
295 ("__floatundixf", "x86_64/floatundixf.S"),
300 if target_arch
== "x86" {
302 ("__ashldi3", "i386/ashldi3.S"),
303 ("__ashrdi3", "i386/ashrdi3.S"),
304 ("__divdi3", "i386/divdi3.S"),
305 ("__floatdixf", "i386/floatdixf.S"),
306 ("__floatundixf", "i386/floatundixf.S"),
307 ("__lshrdi3", "i386/lshrdi3.S"),
308 ("__moddi3", "i386/moddi3.S"),
309 ("__muldi3", "i386/muldi3.S"),
310 ("__udivdi3", "i386/udivdi3.S"),
311 ("__umoddi3", "i386/umoddi3.S"),
316 if target_arch
== "arm"
317 && target_os
!= "ios"
318 && target_os
!= "watchos"
319 && target_env
!= "msvc"
322 ("__aeabi_div0", "arm/aeabi_div0.c"),
323 ("__aeabi_drsub", "arm/aeabi_drsub.c"),
324 ("__aeabi_frsub", "arm/aeabi_frsub.c"),
325 ("__bswapdi2", "arm/bswapdi2.S"),
326 ("__bswapsi2", "arm/bswapsi2.S"),
327 ("__clzdi2", "arm/clzdi2.S"),
328 ("__clzsi2", "arm/clzsi2.S"),
329 ("__divmodsi4", "arm/divmodsi4.S"),
330 ("__divsi3", "arm/divsi3.S"),
331 ("__modsi3", "arm/modsi3.S"),
332 ("__switch16", "arm/switch16.S"),
333 ("__switch32", "arm/switch32.S"),
334 ("__switch8", "arm/switch8.S"),
335 ("__switchu8", "arm/switchu8.S"),
336 ("__sync_synchronize", "arm/sync_synchronize.S"),
337 ("__udivmodsi4", "arm/udivmodsi4.S"),
338 ("__udivsi3", "arm/udivsi3.S"),
339 ("__umodsi3", "arm/umodsi3.S"),
342 if target_os
== "freebsd" {
343 sources
.extend(&[("__clear_cache", "clear_cache.c")]);
346 // First of all aeabi_cdcmp and aeabi_cfcmp are never called by LLVM.
347 // Second are little-endian only, so build fail on big-endian targets.
348 // Temporally workaround: exclude these files for big-endian targets.
349 if !llvm_target
[0].starts_with("thumbeb") && !llvm_target
[0].starts_with("armeb") {
351 ("__aeabi_cdcmp", "arm/aeabi_cdcmp.S"),
352 ("__aeabi_cdcmpeq_check_nan", "arm/aeabi_cdcmpeq_check_nan.c"),
353 ("__aeabi_cfcmp", "arm/aeabi_cfcmp.S"),
354 ("__aeabi_cfcmpeq_check_nan", "arm/aeabi_cfcmpeq_check_nan.c"),
359 if llvm_target
[0] == "armv7" {
361 ("__sync_fetch_and_add_4", "arm/sync_fetch_and_add_4.S"),
362 ("__sync_fetch_and_add_8", "arm/sync_fetch_and_add_8.S"),
363 ("__sync_fetch_and_and_4", "arm/sync_fetch_and_and_4.S"),
364 ("__sync_fetch_and_and_8", "arm/sync_fetch_and_and_8.S"),
365 ("__sync_fetch_and_max_4", "arm/sync_fetch_and_max_4.S"),
366 ("__sync_fetch_and_max_8", "arm/sync_fetch_and_max_8.S"),
367 ("__sync_fetch_and_min_4", "arm/sync_fetch_and_min_4.S"),
368 ("__sync_fetch_and_min_8", "arm/sync_fetch_and_min_8.S"),
369 ("__sync_fetch_and_nand_4", "arm/sync_fetch_and_nand_4.S"),
370 ("__sync_fetch_and_nand_8", "arm/sync_fetch_and_nand_8.S"),
371 ("__sync_fetch_and_or_4", "arm/sync_fetch_and_or_4.S"),
372 ("__sync_fetch_and_or_8", "arm/sync_fetch_and_or_8.S"),
373 ("__sync_fetch_and_sub_4", "arm/sync_fetch_and_sub_4.S"),
374 ("__sync_fetch_and_sub_8", "arm/sync_fetch_and_sub_8.S"),
375 ("__sync_fetch_and_umax_4", "arm/sync_fetch_and_umax_4.S"),
376 ("__sync_fetch_and_umax_8", "arm/sync_fetch_and_umax_8.S"),
377 ("__sync_fetch_and_umin_4", "arm/sync_fetch_and_umin_4.S"),
378 ("__sync_fetch_and_umin_8", "arm/sync_fetch_and_umin_8.S"),
379 ("__sync_fetch_and_xor_4", "arm/sync_fetch_and_xor_4.S"),
380 ("__sync_fetch_and_xor_8", "arm/sync_fetch_and_xor_8.S"),
384 if llvm_target
.last().unwrap().ends_with("eabihf") {
385 if !llvm_target
[0].starts_with("thumbv7em")
386 && !llvm_target
[0].starts_with("thumbv8m.main")
388 // The FPU option chosen for these architectures in cc-rs, ie:
389 // -mfpu=fpv4-sp-d16 for thumbv7em
390 // -mfpu=fpv5-sp-d16 for thumbv8m.main
391 // do not support double precision floating points conversions so the files
392 // that include such instructions are not included for these targets.
394 ("__fixdfsivfp", "arm/fixdfsivfp.S"),
395 ("__fixunsdfsivfp", "arm/fixunsdfsivfp.S"),
396 ("__floatsidfvfp", "arm/floatsidfvfp.S"),
397 ("__floatunssidfvfp", "arm/floatunssidfvfp.S"),
402 ("__fixsfsivfp", "arm/fixsfsivfp.S"),
403 ("__fixunssfsivfp", "arm/fixunssfsivfp.S"),
404 ("__floatsisfvfp", "arm/floatsisfvfp.S"),
405 ("__floatunssisfvfp", "arm/floatunssisfvfp.S"),
406 ("__floatunssisfvfp", "arm/floatunssisfvfp.S"),
407 ("__restore_vfp_d8_d15_regs", "arm/restore_vfp_d8_d15_regs.S"),
408 ("__save_vfp_d8_d15_regs", "arm/save_vfp_d8_d15_regs.S"),
409 ("__negdf2vfp", "arm/negdf2vfp.S"),
410 ("__negsf2vfp", "arm/negsf2vfp.S"),
414 if target_arch
== "aarch64" && consider_float_intrinsics
{
416 ("__comparetf2", "comparetf2.c"),
417 ("__extenddftf2", "extenddftf2.c"),
418 ("__extendsftf2", "extendsftf2.c"),
419 ("__fixtfdi", "fixtfdi.c"),
420 ("__fixtfsi", "fixtfsi.c"),
421 ("__fixtfti", "fixtfti.c"),
422 ("__fixunstfdi", "fixunstfdi.c"),
423 ("__fixunstfsi", "fixunstfsi.c"),
424 ("__fixunstfti", "fixunstfti.c"),
425 ("__floatditf", "floatditf.c"),
426 ("__floatsitf", "floatsitf.c"),
427 ("__floatunditf", "floatunditf.c"),
428 ("__floatunsitf", "floatunsitf.c"),
429 ("__trunctfdf2", "trunctfdf2.c"),
430 ("__trunctfsf2", "trunctfsf2.c"),
431 ("__addtf3", "addtf3.c"),
432 ("__multf3", "multf3.c"),
433 ("__subtf3", "subtf3.c"),
434 ("__divtf3", "divtf3.c"),
435 ("__powitf2", "powitf2.c"),
436 ("__fe_getround", "fp_mode.c"),
437 ("__fe_raise_inexact", "fp_mode.c"),
440 if target_os
!= "windows" {
441 sources
.extend(&[("__multc3", "multc3.c")]);
445 if target_arch
== "mips" {
446 sources
.extend(&[("__bswapsi2", "bswapsi2.c")]);
449 if target_arch
== "mips64" {
451 ("__extenddftf2", "extenddftf2.c"),
452 ("__netf2", "comparetf2.c"),
453 ("__addtf3", "addtf3.c"),
454 ("__multf3", "multf3.c"),
455 ("__subtf3", "subtf3.c"),
456 ("__fixtfsi", "fixtfsi.c"),
457 ("__floatsitf", "floatsitf.c"),
458 ("__fixunstfsi", "fixunstfsi.c"),
459 ("__floatunsitf", "floatunsitf.c"),
460 ("__fe_getround", "fp_mode.c"),
461 ("__divtf3", "divtf3.c"),
462 ("__trunctfdf2", "trunctfdf2.c"),
463 ("__trunctfsf2", "trunctfsf2.c"),
467 // Remove the assembly implementations that won't compile for the target
468 if llvm_target
[0] == "thumbv6m" || llvm_target
[0] == "thumbv8m.base" {
469 let mut to_remove
= Vec
::new();
470 for (k
, v
) in sources
.map
.iter() {
471 if v
.ends_with(".S") {
475 sources
.remove(&to_remove
);
477 // But use some generic implementations where possible
478 sources
.extend(&[("__clzdi2", "clzdi2.c"), ("__clzsi2", "clzsi2.c")])
481 if llvm_target
[0] == "thumbv7m" || llvm_target
[0] == "thumbv7em" {
482 sources
.remove(&["__aeabi_cdcmp", "__aeabi_cfcmp"]);
485 // Android uses emulated TLS so we need a runtime support function.
486 if target_os
== "android" {
487 sources
.extend(&[("__emutls_get_address", "emutls.c")]);
489 // Work around a bug in the NDK headers (fixed in
490 // https://r.android.com/2038949 which will be released in a future
491 // NDK version) by providing a definition of LONG_BIT.
492 cfg
.define("LONG_BIT", "(8 * sizeof(long))");
495 // When compiling the C code we require the user to tell us where the
496 // source code is, and this is largely done so when we're compiling as
497 // part of rust-lang/rust we can use the same llvm-project repository as
499 let root
= match env
::var_os("RUST_COMPILER_RT_ROOT") {
500 Some(s
) => PathBuf
::from(s
),
501 None
=> panic
!("RUST_COMPILER_RT_ROOT is not set"),
504 panic
!("RUST_COMPILER_RT_ROOT={} does not exist", root
.display());
507 // Support deterministic builds by remapping the __FILE__ prefix if the
508 // compiler supports it. This fixes the nondeterminism caused by the
509 // use of that macro in lib/builtins/int_util.h in compiler-rt.
510 cfg
.flag_if_supported(&format
!("-ffile-prefix-map={}=.", root
.display()));
512 // Include out-of-line atomics for aarch64, which are all generated by supplying different
513 // sets of flags to the same source file.
514 // Note: Out-of-line aarch64 atomics are not supported by the msvc toolchain (#430).
515 let src_dir
= root
.join("lib/builtins");
516 if target_arch
== "aarch64" && target_env
!= "msvc" {
517 // See below for why we're building these as separate libraries.
518 build_aarch64_out_of_line_atomics_libraries(&src_dir
, cfg
);
520 // Some run-time CPU feature detection is necessary, as well.
521 sources
.extend(&[("__aarch64_have_lse_atomics", "cpu_model.c")]);
524 let mut added_sources
= HashSet
::new();
525 for (sym
, src
) in sources
.map
.iter() {
526 let src
= src_dir
.join(src
);
527 if added_sources
.insert(src
.clone()) {
529 println
!("cargo:rerun-if-changed={}", src
.display());
531 println
!("cargo:rustc-cfg={}=\"optimized-c\"", sym
);
534 cfg
.compile("libcompiler-rt.a");
537 fn build_aarch64_out_of_line_atomics_libraries(builtins_dir
: &Path
, cfg
: &mut cc
::Build
) {
538 let out_dir
= PathBuf
::from(env
::var("OUT_DIR").unwrap());
539 let outlined_atomics_file
= builtins_dir
.join("aarch64/lse.S");
540 println
!("cargo:rerun-if-changed={}", outlined_atomics_file
.display());
542 cfg
.include(&builtins_dir
);
544 for instruction_type
in &["cas", "swp", "ldadd", "ldclr", "ldeor", "ldset"] {
545 for size
in &[1, 2, 4, 8, 16] {
546 if *size
== 16 && *instruction_type
!= "cas" {
550 for (model_number
, model_name
) in
551 &[(1, "relax"), (2, "acq"), (3, "rel"), (4, "acq_rel")]
553 // The original compiler-rt build system compiles the same
554 // source file multiple times with different compiler
555 // options. Here we do something slightly different: we
556 // create multiple .S files with the proper #defines and
557 // then include the original file.
559 // This is needed because the cc crate doesn't allow us to
560 // override the name of object files and libtool requires
561 // all objects in an archive to have unique names.
563 out_dir
.join(format
!("lse_{}{}_{}.S", instruction_type
, size
, model_name
));
564 let mut file
= File
::create(&path
).unwrap();
565 writeln
!(file
, "#define L_{}", instruction_type
).unwrap();
566 writeln
!(file
, "#define SIZE {}", size
).unwrap();
567 writeln
!(file
, "#define MODEL {}", model_number
).unwrap();
571 outlined_atomics_file
.canonicalize().unwrap().display()
577 let sym
= format
!("__aarch64_{}{}_{}", instruction_type
, size
, model_name
);
578 println
!("cargo:rustc-cfg={}=\"optimized-c\"", sym
);