build.rs 16 KB

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