build.rs 17 KB

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