build.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #[cfg(feature = "c")]
  2. extern crate gcc;
  3. extern crate rustc_cfg;
  4. #[cfg(feature = "c")]
  5. use std::collections::BTreeMap;
  6. use std::io::Write;
  7. #[cfg(feature = "c")]
  8. use std::path::Path;
  9. use std::{env, io, process};
  10. use rustc_cfg::Cfg;
  11. #[cfg(feature = "c")]
  12. struct Sources {
  13. // SYMBOL -> PATH TO SOURCE
  14. map: BTreeMap<&'static str, &'static str>,
  15. }
  16. #[cfg(feature = "c")]
  17. impl Sources {
  18. fn new() -> Sources {
  19. Sources { map: BTreeMap::new() }
  20. }
  21. fn extend(&mut self, sources: &[&'static str]) {
  22. // NOTE Some intrinsics have both a generic implementation (e.g.
  23. // `floatdidf.c`) and an arch optimized implementation
  24. // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
  25. // implementation and discard the generic implementation. If we don't
  26. // and keep both implementations, the linker will yell at us about
  27. // duplicate symbols!
  28. for &src in sources {
  29. let symbol = Path::new(src).file_stem().unwrap().to_str().unwrap();
  30. if src.contains("/") {
  31. // Arch-optimized implementation (preferred)
  32. self.map.insert(symbol, src);
  33. } else {
  34. // Generic implementation
  35. if !self.map.contains_key(symbol) {
  36. self.map.insert(symbol, src);
  37. }
  38. }
  39. }
  40. }
  41. fn remove(&mut self, symbols: &[&str]) {
  42. for symbol in symbols {
  43. self.map.remove(*symbol).unwrap();
  44. }
  45. }
  46. }
  47. fn main() {
  48. println!("cargo:rerun-if-changed=build.rs");
  49. let target = env::var("TARGET").unwrap();
  50. // Emscripten's runtime includes all the builtins
  51. if target.contains("emscripten") {
  52. return;
  53. }
  54. let Cfg { ref target_arch, ref target_os, ref target_env, ref target_vendor, .. } =
  55. Cfg::new(&target).unwrap_or_else(|e| {
  56. writeln!(io::stderr(), "{}", e).ok();
  57. process::exit(1)
  58. });
  59. // NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
  60. // target triple. This is usually correct for our built-in targets but can break in presence of
  61. // custom targets, which can have arbitrary names.
  62. let llvm_target = target.split('-').collect::<Vec<_>>();
  63. // Build missing intrinsics from compiler-rt C source code
  64. match () {
  65. #[cfg(feature = "c")]
  66. () => {
  67. let target_vendor = target_vendor.as_ref().unwrap();
  68. let cfg = &mut gcc::Config::new();
  69. if target_env == "msvc" {
  70. // Don't pull in extra libraries on MSVC
  71. cfg.flag("/Zl");
  72. // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
  73. cfg.define("__func__", Some("__FUNCTION__"));
  74. } else {
  75. // Turn off various features of gcc and such, mostly copying
  76. // compiler-rt's build system already
  77. cfg.flag("-fno-builtin");
  78. cfg.flag("-fvisibility=hidden");
  79. cfg.flag("-fomit-frame-pointer");
  80. cfg.flag("-ffreestanding");
  81. cfg.define("VISIBILITY_HIDDEN", None);
  82. }
  83. // NOTE Most of the ARM intrinsics are written in assembly. Tell gcc which arch we are going to
  84. // target to make sure that the assembly implementations really work for the target. If the
  85. // implementation is not valid for the arch, then gcc will error when compiling it.
  86. if llvm_target[0].starts_with("thumb") {
  87. cfg.flag("-mthumb");
  88. if llvm_target.last() == Some(&"eabihf") {
  89. cfg.flag("-mfloat-abi=hard");
  90. }
  91. }
  92. if llvm_target[0] == "thumbv6m" {
  93. cfg.flag("-march=armv6-m");
  94. }
  95. if llvm_target[0] == "thumbv7m" {
  96. cfg.flag("-march=armv7-m");
  97. }
  98. if llvm_target[0] == "thumbv7em" {
  99. cfg.flag("-march=armv7e-m");
  100. }
  101. let mut sources = Sources::new();
  102. sources.extend(&["absvdi2.c",
  103. "absvsi2.c",
  104. "addvdi3.c",
  105. "addvsi3.c",
  106. "apple_versioning.c",
  107. "clzdi2.c",
  108. "clzsi2.c",
  109. "cmpdi2.c",
  110. "comparedf2.c",
  111. "comparesf2.c",
  112. "ctzdi2.c",
  113. "ctzsi2.c",
  114. "divdc3.c",
  115. "divdf3.c",
  116. "divsc3.c",
  117. "divsf3.c",
  118. "divxc3.c",
  119. "extendsfdf2.c",
  120. "extendhfsf2.c",
  121. "ffsdi2.c",
  122. "fixdfdi.c",
  123. "fixdfsi.c",
  124. "fixsfdi.c",
  125. "fixsfsi.c",
  126. "fixunsdfdi.c",
  127. "fixunsdfsi.c",
  128. "fixunssfdi.c",
  129. "fixunssfsi.c",
  130. "fixunsxfdi.c",
  131. "fixunsxfsi.c",
  132. "fixxfdi.c",
  133. "floatdidf.c",
  134. "floatdisf.c",
  135. "floatdixf.c",
  136. "floatsidf.c",
  137. "floatsisf.c",
  138. "floatundidf.c",
  139. "floatundisf.c",
  140. "floatundixf.c",
  141. "floatunsidf.c",
  142. "floatunsisf.c",
  143. "int_util.c",
  144. "muldc3.c",
  145. "muldf3.c",
  146. "mulsc3.c",
  147. "mulsf3.c",
  148. "mulvdi3.c",
  149. "mulvsi3.c",
  150. "mulxc3.c",
  151. "negdf2.c",
  152. "negdi2.c",
  153. "negsf2.c",
  154. "negvdi2.c",
  155. "negvsi2.c",
  156. "paritydi2.c",
  157. "paritysi2.c",
  158. "popcountdi2.c",
  159. "popcountsi2.c",
  160. "powixf2.c",
  161. "subvdi3.c",
  162. "subvsi3.c",
  163. "truncdfhf2.c",
  164. "truncdfsf2.c",
  165. "truncsfhf2.c",
  166. "ucmpdi2.c"]);
  167. if target_os != "ios" {
  168. sources.extend(&["absvti2.c",
  169. "addvti3.c",
  170. "clzti2.c",
  171. "cmpti2.c",
  172. "ctzti2.c",
  173. "ffsti2.c",
  174. "fixdfti.c",
  175. "fixsfti.c",
  176. "fixunsdfti.c",
  177. "fixunssfti.c",
  178. "fixunsxfti.c",
  179. "fixxfti.c",
  180. "floattidf.c",
  181. "floattisf.c",
  182. "floattixf.c",
  183. "floatuntidf.c",
  184. "floatuntisf.c",
  185. "floatuntixf.c",
  186. "mulvti3.c",
  187. "negti2.c",
  188. "negvti2.c",
  189. "parityti2.c",
  190. "popcountti2.c",
  191. "subvti3.c",
  192. "ucmpti2.c"]);
  193. }
  194. if target_vendor == "apple" {
  195. sources.extend(&["atomic_flag_clear.c",
  196. "atomic_flag_clear_explicit.c",
  197. "atomic_flag_test_and_set.c",
  198. "atomic_flag_test_and_set_explicit.c",
  199. "atomic_signal_fence.c",
  200. "atomic_thread_fence.c"]);
  201. }
  202. if target_env == "msvc" {
  203. if target_arch == "x86_64" {
  204. sources.extend(&["x86_64/floatdidf.c", "x86_64/floatdisf.c", "x86_64/floatdixf.c"]);
  205. }
  206. } else {
  207. if target_os != "freebsd" && target_os != "netbsd" {
  208. sources.extend(&["gcc_personality_v0.c"]);
  209. }
  210. if target_arch == "x86_64" {
  211. sources.extend(&["x86_64/chkstk.S",
  212. "x86_64/chkstk2.S",
  213. "x86_64/floatdidf.c",
  214. "x86_64/floatdisf.c",
  215. "x86_64/floatdixf.c",
  216. "x86_64/floatundidf.S",
  217. "x86_64/floatundisf.S",
  218. "x86_64/floatundixf.S"]);
  219. }
  220. if target_arch == "x86" {
  221. sources.extend(&["i386/ashldi3.S",
  222. "i386/ashrdi3.S",
  223. "i386/chkstk.S",
  224. "i386/chkstk2.S",
  225. "i386/divdi3.S",
  226. "i386/floatdidf.S",
  227. "i386/floatdisf.S",
  228. "i386/floatdixf.S",
  229. "i386/floatundidf.S",
  230. "i386/floatundisf.S",
  231. "i386/floatundixf.S",
  232. "i386/lshrdi3.S",
  233. "i386/moddi3.S",
  234. "i386/muldi3.S",
  235. "i386/udivdi3.S",
  236. "i386/umoddi3.S"]);
  237. }
  238. }
  239. if target_arch == "arm" && target_os != "ios" {
  240. sources.extend(&["arm/aeabi_cdcmp.S",
  241. "arm/aeabi_cdcmpeq_check_nan.c",
  242. "arm/aeabi_cfcmp.S",
  243. "arm/aeabi_cfcmpeq_check_nan.c",
  244. "arm/aeabi_dcmp.S",
  245. "arm/aeabi_div0.c",
  246. "arm/aeabi_drsub.c",
  247. "arm/aeabi_fcmp.S",
  248. "arm/aeabi_frsub.c",
  249. "arm/bswapdi2.S",
  250. "arm/bswapsi2.S",
  251. "arm/clzdi2.S",
  252. "arm/clzsi2.S",
  253. "arm/comparesf2.S",
  254. "arm/divmodsi4.S",
  255. "arm/divsi3.S",
  256. "arm/modsi3.S",
  257. "arm/switch16.S",
  258. "arm/switch32.S",
  259. "arm/switch8.S",
  260. "arm/switchu8.S",
  261. "arm/sync_synchronize.S",
  262. "arm/udivmodsi4.S",
  263. "arm/udivsi3.S",
  264. "arm/umodsi3.S"]);
  265. }
  266. if llvm_target[0] == "armv7" {
  267. sources.extend(&["arm/sync_fetch_and_add_4.S",
  268. "arm/sync_fetch_and_add_8.S",
  269. "arm/sync_fetch_and_and_4.S",
  270. "arm/sync_fetch_and_and_8.S",
  271. "arm/sync_fetch_and_max_4.S",
  272. "arm/sync_fetch_and_max_8.S",
  273. "arm/sync_fetch_and_min_4.S",
  274. "arm/sync_fetch_and_min_8.S",
  275. "arm/sync_fetch_and_nand_4.S",
  276. "arm/sync_fetch_and_nand_8.S",
  277. "arm/sync_fetch_and_or_4.S",
  278. "arm/sync_fetch_and_or_8.S",
  279. "arm/sync_fetch_and_sub_4.S",
  280. "arm/sync_fetch_and_sub_8.S",
  281. "arm/sync_fetch_and_umax_4.S",
  282. "arm/sync_fetch_and_umax_8.S",
  283. "arm/sync_fetch_and_umin_4.S",
  284. "arm/sync_fetch_and_umin_8.S",
  285. "arm/sync_fetch_and_xor_4.S",
  286. "arm/sync_fetch_and_xor_8.S"]);
  287. }
  288. if llvm_target.last().unwrap().ends_with("eabihf") {
  289. if !llvm_target[0].starts_with("thumbv7em") {
  290. sources.extend(&["arm/adddf3vfp.S",
  291. "arm/addsf3vfp.S",
  292. "arm/divdf3vfp.S",
  293. "arm/divsf3vfp.S",
  294. "arm/eqdf2vfp.S",
  295. "arm/eqsf2vfp.S",
  296. "arm/extendsfdf2vfp.S",
  297. "arm/fixdfsivfp.S",
  298. "arm/fixsfsivfp.S",
  299. "arm/fixunsdfsivfp.S",
  300. "arm/fixunssfsivfp.S",
  301. "arm/floatsidfvfp.S",
  302. "arm/floatsisfvfp.S",
  303. "arm/floatunssidfvfp.S",
  304. "arm/floatunssisfvfp.S",
  305. "arm/gedf2vfp.S",
  306. "arm/gesf2vfp.S",
  307. "arm/gtdf2vfp.S",
  308. "arm/gtsf2vfp.S",
  309. "arm/ledf2vfp.S",
  310. "arm/lesf2vfp.S",
  311. "arm/ltdf2vfp.S",
  312. "arm/ltsf2vfp.S",
  313. "arm/muldf3vfp.S",
  314. "arm/mulsf3vfp.S",
  315. "arm/nedf2vfp.S",
  316. "arm/nesf2vfp.S",
  317. "arm/restore_vfp_d8_d15_regs.S",
  318. "arm/save_vfp_d8_d15_regs.S",
  319. "arm/subdf3vfp.S",
  320. "arm/subsf3vfp.S"]);
  321. }
  322. sources.extend(&["arm/negdf2vfp.S", "arm/negsf2vfp.S"]);
  323. }
  324. if target_arch == "aarch64" {
  325. sources.extend(&["comparetf2.c",
  326. "extenddftf2.c",
  327. "extendsftf2.c",
  328. "fixtfdi.c",
  329. "fixtfsi.c",
  330. "fixtfti.c",
  331. "fixunstfdi.c",
  332. "fixunstfsi.c",
  333. "fixunstfti.c",
  334. "floatditf.c",
  335. "floatsitf.c",
  336. "floatunditf.c",
  337. "floatunsitf.c",
  338. "multc3.c",
  339. "trunctfdf2.c",
  340. "trunctfsf2.c"]);
  341. }
  342. // Remove the assembly implementations that won't compile for the target
  343. if llvm_target[0] == "thumbv6m" {
  344. sources.remove(&["aeabi_cdcmp",
  345. "aeabi_cfcmp",
  346. "aeabi_dcmp",
  347. "aeabi_fcmp",
  348. "clzdi2",
  349. "clzsi2",
  350. "comparesf2",
  351. "divmodsi4",
  352. "divsi3",
  353. "modsi3",
  354. "switch16",
  355. "switch32",
  356. "switch8",
  357. "switchu8",
  358. "udivmodsi4",
  359. "udivsi3",
  360. "umodsi3"]);
  361. // But use some generic implementations where possible
  362. sources.extend(&["clzdi2.c", "clzsi2.c"])
  363. }
  364. if llvm_target[0] == "thumbv7m" || llvm_target[0] == "thumbv7em" {
  365. sources.remove(&["aeabi_cdcmp", "aeabi_cfcmp"]);
  366. }
  367. let root = if env::var_os("CARGO_FEATURE_RUSTBUILD").is_some() {
  368. Path::new("../../libcompiler_builtins")
  369. } else {
  370. Path::new(".")
  371. };
  372. let src_dir = root.join("compiler-rt/compiler-rt-cdylib/compiler-rt/lib/builtins");
  373. for src in sources.map.values() {
  374. let src = src_dir.join(src);
  375. cfg.file(&src);
  376. println!("cargo:rerun-if-changed={}", src.display());
  377. }
  378. cfg.compile("libcompiler-rt.a");
  379. }
  380. #[cfg(not(feature = "c"))]
  381. () => {}
  382. }
  383. // To filter away some flaky test (see src/float/add.rs for details)
  384. if llvm_target[0].starts_with("arm") &&
  385. llvm_target.last().unwrap().contains("gnueabi") {
  386. println!("cargo:rustc-cfg=arm_linux")
  387. }
  388. // To compile intrinsics.rs for thumb targets, where there is no libc
  389. if llvm_target[0].starts_with("thumb") {
  390. println!("cargo:rustc-cfg=thumb")
  391. }
  392. // compiler-rt `cfg`s away some intrinsics for thumbv6m because that target doesn't have full
  393. // THUMBv2 support. We have to cfg our code accordingly.
  394. if llvm_target[0] == "thumbv6m" {
  395. println!("cargo:rustc-cfg=thumbv6m")
  396. }
  397. }