2
0

build.rs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. use std::env;
  2. use std::process::Command;
  3. use std::str;
  4. use std::string::String;
  5. // List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
  6. // need to know all the possible cfgs that this script will set. If you need to set another cfg
  7. // make sure to add it to this list as well.
  8. const ALLOWED_CFGS: &'static [&'static str] = &[
  9. "emscripten_new_stat_abi",
  10. "freebsd10",
  11. "freebsd11",
  12. "freebsd12",
  13. "freebsd13",
  14. "freebsd14",
  15. "libc_align",
  16. "libc_cfg_target_vendor",
  17. "libc_const_extern_fn",
  18. "libc_const_extern_fn_unstable",
  19. "libc_const_size_of",
  20. "libc_core_cvoid",
  21. "libc_deny_warnings",
  22. "libc_int128",
  23. "libc_long_array",
  24. "libc_non_exhaustive",
  25. "libc_packedN",
  26. "libc_priv_mod_use",
  27. "libc_ptr_addr_of",
  28. "libc_thread_local",
  29. "libc_underscore_const_names",
  30. "libc_union",
  31. ];
  32. // Extra values to allow for check-cfg.
  33. const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
  34. ("target_os", &["switch", "aix", "ohos"]),
  35. ("target_env", &["illumos", "wasi", "aix", "ohos"]),
  36. (
  37. "target_arch",
  38. &["loongarch64", "mips32r6", "mips64r6", "csky"],
  39. ),
  40. ];
  41. fn main() {
  42. // Avoid unnecessary re-building.
  43. println!("cargo:rerun-if-changed=build.rs");
  44. let (rustc_minor_ver, is_nightly) = rustc_minor_nightly();
  45. let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
  46. let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
  47. let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok();
  48. let libc_ci = env::var("LIBC_CI").is_ok();
  49. let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok();
  50. if env::var("CARGO_FEATURE_USE_STD").is_ok() {
  51. println!(
  52. "cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
  53. please consider using the `std` cargo feature instead\""
  54. );
  55. }
  56. // The ABI of libc used by libstd is backward compatible with FreeBSD 10.
  57. // The ABI of libc from crates.io is backward compatible with FreeBSD 11.
  58. //
  59. // On CI, we detect the actual FreeBSD version and match its ABI exactly,
  60. // running tests to ensure that the ABI is correct.
  61. match which_freebsd() {
  62. Some(10) if libc_ci || rustc_dep_of_std => set_cfg("freebsd10"),
  63. Some(11) if libc_ci => set_cfg("freebsd11"),
  64. Some(12) if libc_ci => set_cfg("freebsd12"),
  65. Some(13) if libc_ci => set_cfg("freebsd13"),
  66. Some(14) if libc_ci => set_cfg("freebsd14"),
  67. Some(_) | None => set_cfg("freebsd11"),
  68. }
  69. match emcc_version_code() {
  70. Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"),
  71. // Non-Emscripten or version < 3.1.42.
  72. Some(_) | None => (),
  73. }
  74. // On CI: deny all warnings
  75. if libc_ci {
  76. set_cfg("libc_deny_warnings");
  77. }
  78. // Rust >= 1.15 supports private module use:
  79. if rustc_minor_ver >= 15 || rustc_dep_of_std {
  80. set_cfg("libc_priv_mod_use");
  81. }
  82. // Rust >= 1.19 supports unions:
  83. if rustc_minor_ver >= 19 || rustc_dep_of_std {
  84. set_cfg("libc_union");
  85. }
  86. // Rust >= 1.24 supports const mem::size_of:
  87. if rustc_minor_ver >= 24 || rustc_dep_of_std {
  88. set_cfg("libc_const_size_of");
  89. }
  90. // Rust >= 1.25 supports repr(align):
  91. if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
  92. set_cfg("libc_align");
  93. }
  94. // Rust >= 1.26 supports i128 and u128:
  95. if rustc_minor_ver >= 26 || rustc_dep_of_std {
  96. set_cfg("libc_int128");
  97. }
  98. // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
  99. // Otherwise, it defines an incompatible type to retaining
  100. // backwards-compatibility.
  101. if rustc_minor_ver >= 30 || rustc_dep_of_std {
  102. set_cfg("libc_core_cvoid");
  103. }
  104. // Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
  105. if rustc_minor_ver >= 33 || rustc_dep_of_std {
  106. set_cfg("libc_packedN");
  107. set_cfg("libc_cfg_target_vendor");
  108. }
  109. // Rust >= 1.40 supports #[non_exhaustive].
  110. if rustc_minor_ver >= 40 || rustc_dep_of_std {
  111. set_cfg("libc_non_exhaustive");
  112. }
  113. // Rust >= 1.47 supports long array:
  114. if rustc_minor_ver >= 47 || rustc_dep_of_std {
  115. set_cfg("libc_long_array");
  116. }
  117. if rustc_minor_ver >= 51 || rustc_dep_of_std {
  118. set_cfg("libc_ptr_addr_of");
  119. }
  120. // Rust >= 1.37.0 allows underscores as anonymous constant names.
  121. if rustc_minor_ver >= 37 || rustc_dep_of_std {
  122. set_cfg("libc_underscore_const_names");
  123. }
  124. // #[thread_local] is currently unstable
  125. if rustc_dep_of_std {
  126. set_cfg("libc_thread_local");
  127. }
  128. // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
  129. if rustc_minor_ver >= 62 {
  130. set_cfg("libc_const_extern_fn");
  131. } else {
  132. // Rust < 1.62.0 requires a crate feature and feature gate.
  133. if const_extern_fn_cargo_feature {
  134. if !is_nightly || rustc_minor_ver < 40 {
  135. panic!("const-extern-fn requires a nightly compiler >= 1.40");
  136. }
  137. set_cfg("libc_const_extern_fn_unstable");
  138. set_cfg("libc_const_extern_fn");
  139. }
  140. }
  141. // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the
  142. // codebase. libc can configure it if the appropriate environment variable is passed. Since
  143. // rust-lang/rust enforces it, this is useful when using a custom libc fork there.
  144. //
  145. // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
  146. if libc_check_cfg {
  147. for cfg in ALLOWED_CFGS {
  148. println!("cargo:rustc-check-cfg=values({})", cfg);
  149. }
  150. for &(name, values) in CHECK_CFG_EXTRA {
  151. let values = values.join("\",\"");
  152. println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
  153. }
  154. }
  155. //TODO:增加平台内存分配器
  156. //#[cfg(target_os = "dragonos")]
  157. let mut c = cc::Build::new();
  158. //#[cfg(target_os = "dragonos")]
  159. c.flag("-nostdinc")
  160. .flag("-nostdlib")
  161. .flag("-fno-stack-protector")
  162. .flag("-Wno-expansion-to-defined")
  163. .file("src/unix/platform/dragonos/c/dragonos_malloc.c");
  164. //#[cfg(target_os = "dragonos")]
  165. c.define("HAVE_MMAP", "0");
  166. //#[cfg(target_os = "dragonos")]
  167. c.compile("dlibc_c");
  168. //#[cfg(target_os = "dragonos")]
  169. println!("cargo:rustc-link-lib=static=dlibc_c");
  170. }
  171. fn rustc_minor_nightly() -> (u32, bool) {
  172. macro_rules! otry {
  173. ($e:expr) => {
  174. match $e {
  175. Some(e) => e,
  176. None => panic!("Failed to get rustc version"),
  177. }
  178. };
  179. }
  180. let rustc = otry!(env::var_os("RUSTC"));
  181. let output = Command::new(rustc)
  182. .arg("--version")
  183. .output()
  184. .ok()
  185. .expect("Failed to get rustc version");
  186. if !output.status.success() {
  187. panic!(
  188. "failed to run rustc: {}",
  189. String::from_utf8_lossy(output.stderr.as_slice())
  190. );
  191. }
  192. let version = otry!(str::from_utf8(&output.stdout).ok());
  193. let mut pieces = version.split('.');
  194. if pieces.next() != Some("rustc 1") {
  195. panic!("Failed to get rustc version");
  196. }
  197. let minor = pieces.next();
  198. // If `rustc` was built from a tarball, its version string
  199. // will have neither a git hash nor a commit date
  200. // (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
  201. // since a nightly build should either come from CI
  202. // or a git checkout
  203. let nightly_raw = otry!(pieces.next()).split('-').nth(1);
  204. let nightly = nightly_raw
  205. .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
  206. .unwrap_or(false);
  207. let minor = otry!(otry!(minor).parse().ok());
  208. (minor, nightly)
  209. }
  210. fn which_freebsd() -> Option<i32> {
  211. let output = std::process::Command::new("freebsd-version").output().ok();
  212. if output.is_none() {
  213. return None;
  214. }
  215. let output = output.unwrap();
  216. if !output.status.success() {
  217. return None;
  218. }
  219. let stdout = String::from_utf8(output.stdout).ok();
  220. if stdout.is_none() {
  221. return None;
  222. }
  223. let stdout = stdout.unwrap();
  224. match &stdout {
  225. s if s.starts_with("10") => Some(10),
  226. s if s.starts_with("11") => Some(11),
  227. s if s.starts_with("12") => Some(12),
  228. s if s.starts_with("13") => Some(13),
  229. s if s.starts_with("14") => Some(14),
  230. _ => None,
  231. }
  232. }
  233. fn emcc_version_code() -> Option<u64> {
  234. let output = std::process::Command::new("emcc")
  235. .arg("-dumpversion")
  236. .output()
  237. .ok();
  238. if output.is_none() {
  239. return None;
  240. }
  241. let output = output.unwrap();
  242. if !output.status.success() {
  243. return None;
  244. }
  245. let stdout = String::from_utf8(output.stdout).ok();
  246. if stdout.is_none() {
  247. return None;
  248. }
  249. let version = stdout.unwrap();
  250. let mut pieces = version.trim().split('.');
  251. let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
  252. let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
  253. let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
  254. Some(major * 10000 + minor * 100 + patch)
  255. }
  256. fn set_cfg(cfg: &str) {
  257. if !ALLOWED_CFGS.contains(&cfg) {
  258. panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
  259. }
  260. println!("cargo:rustc-cfg={}", cfg);
  261. }