build.rs 17 KB

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