build.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // Emscripten's runtime includes all the builtins
  8. if target.contains("emscripten") {
  9. return;
  10. }
  11. // OpenBSD provides compiler_rt by default, use it instead of rebuilding it from source
  12. if target.contains("openbsd") {
  13. println!("cargo:rustc-link-search=native=/usr/lib");
  14. println!("cargo:rustc-link-lib=compiler_rt");
  15. return;
  16. }
  17. // Forcibly enable memory intrinsics on wasm32 & SGX as we don't have a libc to
  18. // provide them.
  19. if (target.contains("wasm32") && !target.contains("wasi"))
  20. || (target.contains("sgx") && target.contains("fortanix"))
  21. {
  22. println!("cargo:rustc-cfg=feature=\"mem\"");
  23. }
  24. // NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
  25. // target triple. This is usually correct for our built-in targets but can break in presence of
  26. // custom targets, which can have arbitrary names.
  27. let llvm_target = target.split('-').collect::<Vec<_>>();
  28. // Build missing intrinsics from compiler-rt C source code. If we're
  29. // mangling names though we assume that we're also in test mode so we don't
  30. // build anything and we rely on the upstream implementation of compiler-rt
  31. // functions
  32. if !cfg!(feature = "mangled-names") && cfg!(feature = "c") {
  33. // Don't use a C compiler for these targets:
  34. //
  35. // * wasm32 - clang 8 for wasm is somewhat hard to come by and it's
  36. // unlikely that the C is really that much better than our own Rust.
  37. // * nvptx - everything is bitcode, not compatible with mixed C/Rust
  38. // * riscv - the rust-lang/rust distribution container doesn't have a C
  39. // compiler nor is cc-rs ready for compilation to riscv (at this
  40. // time). This can probably be removed in the future
  41. if !target.contains("wasm32") && !target.contains("nvptx") && !target.starts_with("riscv") {
  42. #[cfg(feature = "c")]
  43. c::compile(&llvm_target);
  44. println!("cargo:rustc-cfg=use_c");
  45. }
  46. }
  47. // To compile intrinsics.rs for thumb targets, where there is no libc
  48. if llvm_target[0].starts_with("thumb") {
  49. println!("cargo:rustc-cfg=thumb")
  50. }
  51. // compiler-rt `cfg`s away some intrinsics for thumbv6m and thumbv8m.base because
  52. // these targets do not have full Thumb-2 support but only original Thumb-1.
  53. // We have to cfg our code accordingly.
  54. if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
  55. println!("cargo:rustc-cfg=thumb_1")
  56. }
  57. // Only emit the ARM Linux atomic emulation on pre-ARMv6 architectures.
  58. if llvm_target[0] == "armv4t" || llvm_target[0] == "armv5te" {
  59. println!("cargo:rustc-cfg=kernel_user_helpers")
  60. }
  61. }
  62. #[cfg(feature = "c")]
  63. mod c {
  64. extern crate cc;
  65. use std::collections::BTreeMap;
  66. use std::env;
  67. use std::path::Path;
  68. struct Sources {
  69. // SYMBOL -> PATH TO SOURCE
  70. map: BTreeMap<&'static str, &'static str>,
  71. }
  72. impl Sources {
  73. fn new() -> Sources {
  74. Sources {
  75. map: BTreeMap::new(),
  76. }
  77. }
  78. fn extend(&mut self, sources: &[&'static str]) {
  79. // NOTE Some intrinsics have both a generic implementation (e.g.
  80. // `floatdidf.c`) and an arch optimized implementation
  81. // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
  82. // implementation and discard the generic implementation. If we don't
  83. // and keep both implementations, the linker will yell at us about
  84. // duplicate symbols!
  85. for &src in sources {
  86. let symbol = Path::new(src).file_stem().unwrap().to_str().unwrap();
  87. if src.contains("/") {
  88. // Arch-optimized implementation (preferred)
  89. self.map.insert(symbol, src);
  90. } else {
  91. // Generic implementation
  92. if !self.map.contains_key(symbol) {
  93. self.map.insert(symbol, src);
  94. }
  95. }
  96. }
  97. }
  98. fn remove(&mut self, symbols: &[&str]) {
  99. for symbol in symbols {
  100. self.map.remove(*symbol).unwrap();
  101. }
  102. }
  103. }
  104. /// Compile intrinsics from the compiler-rt C source code
  105. pub fn compile(llvm_target: &[&str]) {
  106. let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
  107. let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
  108. let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
  109. let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
  110. let cfg = &mut cc::Build::new();
  111. cfg.warnings(false);
  112. if target_env == "msvc" {
  113. // Don't pull in extra libraries on MSVC
  114. cfg.flag("/Zl");
  115. // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
  116. cfg.define("__func__", Some("__FUNCTION__"));
  117. } else {
  118. // Turn off various features of gcc and such, mostly copying
  119. // compiler-rt's build system already
  120. cfg.flag("-fno-builtin");
  121. cfg.flag("-fvisibility=hidden");
  122. cfg.flag("-ffreestanding");
  123. // Avoid the following warning appearing once **per file**:
  124. // clang: warning: optimization flag '-fomit-frame-pointer' is not supported for target 'armv7' [-Wignored-optimization-argument]
  125. //
  126. // Note that compiler-rt's build system also checks
  127. //
  128. // `check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)`
  129. //
  130. // in https://github.com/rust-lang/compiler-rt/blob/c8fbcb3/cmake/config-ix.cmake#L19.
  131. cfg.flag_if_supported("-fomit-frame-pointer");
  132. cfg.define("VISIBILITY_HIDDEN", None);
  133. }
  134. let mut sources = Sources::new();
  135. sources.extend(&[
  136. "absvdi2.c",
  137. "absvsi2.c",
  138. "addvdi3.c",
  139. "addvsi3.c",
  140. "apple_versioning.c",
  141. "clzdi2.c",
  142. "clzsi2.c",
  143. "cmpdi2.c",
  144. "ctzdi2.c",
  145. "ctzsi2.c",
  146. "divdc3.c",
  147. "divsc3.c",
  148. "divxc3.c",
  149. "extendhfsf2.c",
  150. "int_util.c",
  151. "muldc3.c",
  152. "mulsc3.c",
  153. "mulvdi3.c",
  154. "mulvsi3.c",
  155. "mulxc3.c",
  156. "negdf2.c",
  157. "negdi2.c",
  158. "negsf2.c",
  159. "negvdi2.c",
  160. "negvsi2.c",
  161. "paritydi2.c",
  162. "paritysi2.c",
  163. "popcountdi2.c",
  164. "popcountsi2.c",
  165. "powixf2.c",
  166. "subvdi3.c",
  167. "subvsi3.c",
  168. "truncdfhf2.c",
  169. "truncdfsf2.c",
  170. "truncsfhf2.c",
  171. "ucmpdi2.c",
  172. ]);
  173. // When compiling in rustbuild (the rust-lang/rust repo) this library
  174. // also needs to satisfy intrinsics that jemalloc or C in general may
  175. // need, so include a few more that aren't typically needed by
  176. // LLVM/Rust.
  177. if cfg!(feature = "rustbuild") {
  178. sources.extend(&["ffsdi2.c"]);
  179. }
  180. // On iOS and 32-bit OSX these are all just empty intrinsics, no need to
  181. // include them.
  182. if target_os != "ios" && (target_vendor != "apple" || target_arch != "x86") {
  183. sources.extend(&[
  184. "absvti2.c",
  185. "addvti3.c",
  186. "clzti2.c",
  187. "cmpti2.c",
  188. "ctzti2.c",
  189. "ffsti2.c",
  190. "mulvti3.c",
  191. "negti2.c",
  192. "negvti2.c",
  193. "parityti2.c",
  194. "popcountti2.c",
  195. "subvti3.c",
  196. "ucmpti2.c",
  197. ]);
  198. }
  199. if target_vendor == "apple" {
  200. sources.extend(&[
  201. "atomic_flag_clear.c",
  202. "atomic_flag_clear_explicit.c",
  203. "atomic_flag_test_and_set.c",
  204. "atomic_flag_test_and_set_explicit.c",
  205. "atomic_signal_fence.c",
  206. "atomic_thread_fence.c",
  207. ]);
  208. }
  209. if target_env == "msvc" {
  210. if target_arch == "x86_64" {
  211. sources.extend(&["x86_64/floatdisf.c", "x86_64/floatdixf.c"]);
  212. }
  213. } else {
  214. // None of these seem to be used on x86_64 windows, and they've all
  215. // got the wrong ABI anyway, so we want to avoid them.
  216. if target_os != "windows" {
  217. if target_arch == "x86_64" {
  218. sources.extend(&[
  219. "x86_64/floatdisf.c",
  220. "x86_64/floatdixf.c",
  221. "x86_64/floatundidf.S",
  222. "x86_64/floatundisf.S",
  223. "x86_64/floatundixf.S",
  224. ]);
  225. }
  226. }
  227. if target_arch == "x86" {
  228. sources.extend(&[
  229. "i386/ashldi3.S",
  230. "i386/ashrdi3.S",
  231. "i386/divdi3.S",
  232. "i386/floatdidf.S",
  233. "i386/floatdisf.S",
  234. "i386/floatdixf.S",
  235. "i386/floatundidf.S",
  236. "i386/floatundisf.S",
  237. "i386/floatundixf.S",
  238. "i386/lshrdi3.S",
  239. "i386/moddi3.S",
  240. "i386/muldi3.S",
  241. "i386/udivdi3.S",
  242. "i386/umoddi3.S",
  243. ]);
  244. }
  245. }
  246. if target_arch == "arm" && target_os != "ios" && target_env != "msvc" {
  247. sources.extend(&[
  248. "arm/aeabi_div0.c",
  249. "arm/aeabi_drsub.c",
  250. "arm/aeabi_frsub.c",
  251. "arm/bswapdi2.S",
  252. "arm/bswapsi2.S",
  253. "arm/clzdi2.S",
  254. "arm/clzsi2.S",
  255. "arm/divmodsi4.S",
  256. "arm/divsi3.S",
  257. "arm/modsi3.S",
  258. "arm/switch16.S",
  259. "arm/switch32.S",
  260. "arm/switch8.S",
  261. "arm/switchu8.S",
  262. "arm/sync_synchronize.S",
  263. "arm/udivmodsi4.S",
  264. "arm/udivsi3.S",
  265. "arm/umodsi3.S",
  266. ]);
  267. if target_os == "freebsd" {
  268. sources.extend(&["clear_cache.c"]);
  269. }
  270. // First of all aeabi_cdcmp and aeabi_cfcmp are never called by LLVM.
  271. // Second are little-endian only, so build fail on big-endian targets.
  272. // Temporally workaround: exclude these files for big-endian targets.
  273. if !llvm_target[0].starts_with("thumbeb") && !llvm_target[0].starts_with("armeb") {
  274. sources.extend(&[
  275. "arm/aeabi_cdcmp.S",
  276. "arm/aeabi_cdcmpeq_check_nan.c",
  277. "arm/aeabi_cfcmp.S",
  278. "arm/aeabi_cfcmpeq_check_nan.c",
  279. ]);
  280. }
  281. }
  282. if llvm_target[0] == "armv7" {
  283. sources.extend(&[
  284. "arm/sync_fetch_and_add_4.S",
  285. "arm/sync_fetch_and_add_8.S",
  286. "arm/sync_fetch_and_and_4.S",
  287. "arm/sync_fetch_and_and_8.S",
  288. "arm/sync_fetch_and_max_4.S",
  289. "arm/sync_fetch_and_max_8.S",
  290. "arm/sync_fetch_and_min_4.S",
  291. "arm/sync_fetch_and_min_8.S",
  292. "arm/sync_fetch_and_nand_4.S",
  293. "arm/sync_fetch_and_nand_8.S",
  294. "arm/sync_fetch_and_or_4.S",
  295. "arm/sync_fetch_and_or_8.S",
  296. "arm/sync_fetch_and_sub_4.S",
  297. "arm/sync_fetch_and_sub_8.S",
  298. "arm/sync_fetch_and_umax_4.S",
  299. "arm/sync_fetch_and_umax_8.S",
  300. "arm/sync_fetch_and_umin_4.S",
  301. "arm/sync_fetch_and_umin_8.S",
  302. "arm/sync_fetch_and_xor_4.S",
  303. "arm/sync_fetch_and_xor_8.S",
  304. ]);
  305. }
  306. if llvm_target.last().unwrap().ends_with("eabihf") {
  307. if !llvm_target[0].starts_with("thumbv7em")
  308. && !llvm_target[0].starts_with("thumbv8m.main")
  309. {
  310. // The FPU option chosen for these architectures in cc-rs, ie:
  311. // -mfpu=fpv4-sp-d16 for thumbv7em
  312. // -mfpu=fpv5-sp-d16 for thumbv8m.main
  313. // do not support double precision floating points conversions so the files
  314. // that include such instructions are not included for these targets.
  315. sources.extend(&[
  316. "arm/fixdfsivfp.S",
  317. "arm/fixunsdfsivfp.S",
  318. "arm/floatsidfvfp.S",
  319. "arm/floatunssidfvfp.S",
  320. ]);
  321. }
  322. sources.extend(&[
  323. "arm/fixsfsivfp.S",
  324. "arm/fixunssfsivfp.S",
  325. "arm/floatsisfvfp.S",
  326. "arm/floatunssisfvfp.S",
  327. "arm/floatunssisfvfp.S",
  328. "arm/restore_vfp_d8_d15_regs.S",
  329. "arm/save_vfp_d8_d15_regs.S",
  330. "arm/negdf2vfp.S",
  331. "arm/negsf2vfp.S",
  332. ]);
  333. }
  334. if target_arch == "aarch64" {
  335. sources.extend(&[
  336. "comparetf2.c",
  337. "extenddftf2.c",
  338. "extendsftf2.c",
  339. "fixtfdi.c",
  340. "fixtfsi.c",
  341. "fixtfti.c",
  342. "fixunstfdi.c",
  343. "fixunstfsi.c",
  344. "fixunstfti.c",
  345. "floatditf.c",
  346. "floatsitf.c",
  347. "floatunditf.c",
  348. "floatunsitf.c",
  349. "trunctfdf2.c",
  350. "trunctfsf2.c",
  351. ]);
  352. if target_os != "windows" {
  353. sources.extend(&["multc3.c"]);
  354. }
  355. }
  356. // Remove the assembly implementations that won't compile for the target
  357. if llvm_target[0] == "thumbv6m" || llvm_target[0] == "thumbv8m.base" {
  358. sources.remove(&[
  359. "clzdi2",
  360. "clzsi2",
  361. "divmodsi4",
  362. "divsi3",
  363. "modsi3",
  364. "switch16",
  365. "switch32",
  366. "switch8",
  367. "switchu8",
  368. "udivmodsi4",
  369. "udivsi3",
  370. "umodsi3",
  371. ]);
  372. // But use some generic implementations where possible
  373. sources.extend(&["clzdi2.c", "clzsi2.c"])
  374. }
  375. if llvm_target[0] == "thumbv7m" || llvm_target[0] == "thumbv7em" {
  376. sources.remove(&["aeabi_cdcmp", "aeabi_cfcmp"]);
  377. }
  378. // When compiling in rustbuild (the rust-lang/rust repo) this build
  379. // script runs from a directory other than this root directory.
  380. let root = if cfg!(feature = "rustbuild") {
  381. Path::new("../../libcompiler_builtins")
  382. } else {
  383. Path::new(".")
  384. };
  385. let src_dir = root.join("compiler-rt/lib/builtins");
  386. for src in sources.map.values() {
  387. let src = src_dir.join(src);
  388. cfg.file(&src);
  389. println!("cargo:rerun-if-changed={}", src.display());
  390. }
  391. cfg.compile("libcompiler-rt.a");
  392. }
  393. }