build.rs 16 KB

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