2 use std::process::Command;
4 use std::string::String;
6 // List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
7 // need to know all the possible cfgs that this script will set. If you need to set another cfg
8 // make sure to add it to this list as well.
9 const ALLOWED_CFGS: &'static [&'static str] = &[
10 "emscripten_new_stat_abi",
17 "libc_cfg_target_vendor",
18 "libc_const_extern_fn",
19 "libc_const_extern_fn_unstable",
25 "libc_non_exhaustive",
30 "libc_underscore_const_names",
34 // Extra values to allow for check-cfg.
35 const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
36 ("target_os", &["switch", "aix", "ohos", "hurd"]),
37 ("target_env", &["illumos", "wasi", "aix", "ohos"]),
40 &["loongarch64", "mips32r6", "mips64r6", "csky"],
45 // Avoid unnecessary re-building.
46 println!("cargo:rerun-if-changed=build.rs");
48 let (rustc_minor_ver, is_nightly) = rustc_minor_nightly();
49 let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
50 let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
51 let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok();
52 let libc_ci = env::var("LIBC_CI").is_ok();
53 let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok();
55 if env::var("CARGO_FEATURE_USE_STD").is_ok() {
57 "cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
58 please consider using the `std` cargo feature instead\""
62 // The ABI of libc used by libstd is backward compatible with FreeBSD 10.
63 // The ABI of libc from crates.io is backward compatible with FreeBSD 11.
65 // On CI, we detect the actual FreeBSD version and match its ABI exactly,
66 // running tests to ensure that the ABI is correct.
67 match which_freebsd() {
68 Some(10) if libc_ci || rustc_dep_of_std => set_cfg("freebsd10"),
69 Some(11) if libc_ci => set_cfg("freebsd11"),
70 Some(12) if libc_ci => set_cfg("freebsd12"),
71 Some(13) if libc_ci => set_cfg("freebsd13"),
72 Some(14) if libc_ci => set_cfg("freebsd14"),
73 Some(_) | None => set_cfg("freebsd11"),
76 match emcc_version_code() {
77 Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"),
78 // Non-Emscripten or version < 3.1.42.
82 // On CI: deny all warnings
84 set_cfg("libc_deny_warnings");
87 // Rust >= 1.15 supports private module use:
88 if rustc_minor_ver >= 15 || rustc_dep_of_std {
89 set_cfg("libc_priv_mod_use");
92 // Rust >= 1.19 supports unions:
93 if rustc_minor_ver >= 19 || rustc_dep_of_std {
94 set_cfg("libc_union");
97 // Rust >= 1.24 supports const mem::size_of:
98 if rustc_minor_ver >= 24 || rustc_dep_of_std {
99 set_cfg("libc_const_size_of");
102 // Rust >= 1.25 supports repr(align):
103 if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
104 set_cfg("libc_align");
107 // Rust >= 1.26 supports i128 and u128:
108 if rustc_minor_ver >= 26 || rustc_dep_of_std {
109 set_cfg("libc_int128");
112 // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
113 // Otherwise, it defines an incompatible type to retaining
114 // backwards-compatibility.
115 if rustc_minor_ver >= 30 || rustc_dep_of_std {
116 set_cfg("libc_core_cvoid");
119 // Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
120 if rustc_minor_ver >= 33 || rustc_dep_of_std {
121 set_cfg("libc_packedN");
122 set_cfg("libc_cfg_target_vendor");
125 // Rust >= 1.40 supports #[non_exhaustive].
126 if rustc_minor_ver >= 40 || rustc_dep_of_std {
127 set_cfg("libc_non_exhaustive");
130 // Rust >= 1.47 supports long array:
131 if rustc_minor_ver >= 47 || rustc_dep_of_std {
132 set_cfg("libc_long_array");
135 if rustc_minor_ver >= 51 || rustc_dep_of_std {
136 set_cfg("libc_ptr_addr_of");
139 // Rust >= 1.37.0 allows underscores as anonymous constant names.
140 if rustc_minor_ver >= 37 || rustc_dep_of_std {
141 set_cfg("libc_underscore_const_names");
144 // #[thread_local] is currently unstable
145 if rustc_dep_of_std {
146 set_cfg("libc_thread_local");
149 // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
150 if rustc_minor_ver >= 62 {
151 set_cfg("libc_const_extern_fn");
153 // Rust < 1.62.0 requires a crate feature and feature gate.
154 if const_extern_fn_cargo_feature {
155 if !is_nightly || rustc_minor_ver < 40 {
156 panic!("const-extern-fn requires a nightly compiler >= 1.40");
158 set_cfg("libc_const_extern_fn_unstable");
159 set_cfg("libc_const_extern_fn");
163 // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the
164 // codebase. libc can configure it if the appropriate environment variable is passed. Since
165 // rust-lang/rust enforces it, this is useful when using a custom libc fork there.
167 // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
169 for cfg in ALLOWED_CFGS {
170 if rustc_minor_ver >= 75 {
171 println!("cargo:rustc-check-cfg=cfg({})", cfg);
173 println!("cargo:rustc-check-cfg=values({})", cfg);
176 for &(name, values) in CHECK_CFG_EXTRA {
177 let values = values.join("\",\"");
178 if rustc_minor_ver >= 75 {
179 println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values);
181 println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
187 fn rustc_minor_nightly() -> (u32, bool) {
192 None => panic!("Failed to get rustc version"),
197 let rustc = otry!(env::var_os("RUSTC"));
198 let output = Command::new(rustc)
202 .expect("Failed to get rustc version");
203 if !output.status.success() {
205 "failed to run rustc: {}",
206 String::from_utf8_lossy(output.stderr.as_slice())
210 let version = otry!(str::from_utf8(&output.stdout).ok());
211 let mut pieces = version.split('.');
213 if pieces.next() != Some("rustc 1") {
214 panic!("Failed to get rustc version");
217 let minor = pieces.next();
219 // If `rustc` was built from a tarball, its version string
220 // will have neither a git hash nor a commit date
221 // (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
222 // since a nightly build should either come from CI
224 let nightly_raw = otry!(pieces.next()).split('-').nth(1);
225 let nightly = nightly_raw
226 .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
228 let minor = otry!(otry!(minor).parse().ok());
233 fn which_freebsd() -> Option<i32> {
234 let output = std::process::Command::new("freebsd-version").output().ok();
235 if output.is_none() {
238 let output = output.unwrap();
239 if !output.status.success() {
243 let stdout = String::from_utf8(output.stdout).ok();
244 if stdout.is_none() {
247 let stdout = stdout.unwrap();
250 s if s.starts_with("10") => Some(10),
251 s if s.starts_with("11") => Some(11),
252 s if s.starts_with("12") => Some(12),
253 s if s.starts_with("13") => Some(13),
254 s if s.starts_with("14") => Some(14),
259 fn emcc_version_code() -> Option<u64> {
260 let output = std::process::Command::new("emcc")
264 if output.is_none() {
267 let output = output.unwrap();
268 if !output.status.success() {
272 let stdout = String::from_utf8(output.stdout).ok();
273 if stdout.is_none() {
276 let version = stdout.unwrap();
277 let mut pieces = version.trim().split('.');
279 let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
280 let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
281 let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
283 Some(major * 10000 + minor * 100 + patch)
286 fn set_cfg(cfg: &str) {
287 if !ALLOWED_CFGS.contains(&cfg) {
288 panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
290 println!("cargo:rustc-cfg={}", cfg);