build.rs 16 KB

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