build.rs 24 KB

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