build.rs 17 KB

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