build.rs 17 KB

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