build.rs 17 KB

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