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",
18 "libc_cfg_target_vendor",
19 "libc_const_extern_fn",
20 "libc_const_extern_fn_unstable",
26 "libc_non_exhaustive",
31 "libc_underscore_const_names",
35 // Extra values to allow for check-cfg.
36 const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
37 ("target_os", &["switch", "aix", "ohos", "hurd"]),
38 ("target_env", &["illumos", "wasi", "aix", "ohos"]),
41 &["loongarch64", "mips32r6", "mips64r6", "csky"],
46 // Avoid unnecessary re-building.
47 println!("cargo:rerun-if-changed=build.rs");
49 let (rustc_minor_ver, is_nightly) = rustc_minor_nightly();
50 let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
51 let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok();
52 let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok();
53 let libc_ci = env::var("LIBC_CI").is_ok();
54 let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok();
56 if env::var("CARGO_FEATURE_USE_STD").is_ok() {
58 "cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \
59 please consider using the `std` cargo feature instead\""
63 // The ABI of libc used by std is backward compatible with FreeBSD 12.
64 // The ABI of libc from crates.io is backward compatible with FreeBSD 11.
66 // On CI, we detect the actual FreeBSD version and match its ABI exactly,
67 // running tests to ensure that the ABI is correct.
68 match which_freebsd() {
69 Some(10) if libc_ci => set_cfg("freebsd10"),
70 Some(11) if libc_ci => set_cfg("freebsd11"),
71 Some(12) if libc_ci || rustc_dep_of_std => set_cfg("freebsd12"),
72 Some(13) if libc_ci => set_cfg("freebsd13"),
73 Some(14) if libc_ci => set_cfg("freebsd14"),
74 Some(15) if libc_ci => set_cfg("freebsd15"),
75 Some(_) | None => set_cfg("freebsd11"),
78 match emcc_version_code() {
79 Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"),
80 // Non-Emscripten or version < 3.1.42.
84 // On CI: deny all warnings
86 set_cfg("libc_deny_warnings");
89 // Rust >= 1.15 supports private module use:
90 if rustc_minor_ver >= 15 || rustc_dep_of_std {
91 set_cfg("libc_priv_mod_use");
94 // Rust >= 1.19 supports unions:
95 if rustc_minor_ver >= 19 || rustc_dep_of_std {
96 set_cfg("libc_union");
99 // Rust >= 1.24 supports const mem::size_of:
100 if rustc_minor_ver >= 24 || rustc_dep_of_std {
101 set_cfg("libc_const_size_of");
104 // Rust >= 1.25 supports repr(align):
105 if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature {
106 set_cfg("libc_align");
109 // Rust >= 1.26 supports i128 and u128:
110 if rustc_minor_ver >= 26 || rustc_dep_of_std {
111 set_cfg("libc_int128");
114 // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it.
115 // Otherwise, it defines an incompatible type to retaining
116 // backwards-compatibility.
117 if rustc_minor_ver >= 30 || rustc_dep_of_std {
118 set_cfg("libc_core_cvoid");
121 // Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor).
122 if rustc_minor_ver >= 33 || rustc_dep_of_std {
123 set_cfg("libc_packedN");
124 set_cfg("libc_cfg_target_vendor");
127 // Rust >= 1.40 supports #[non_exhaustive].
128 if rustc_minor_ver >= 40 || rustc_dep_of_std {
129 set_cfg("libc_non_exhaustive");
132 // Rust >= 1.47 supports long array:
133 if rustc_minor_ver >= 47 || rustc_dep_of_std {
134 set_cfg("libc_long_array");
137 if rustc_minor_ver >= 51 || rustc_dep_of_std {
138 set_cfg("libc_ptr_addr_of");
141 // Rust >= 1.37.0 allows underscores as anonymous constant names.
142 if rustc_minor_ver >= 37 || rustc_dep_of_std {
143 set_cfg("libc_underscore_const_names");
146 // #[thread_local] is currently unstable
147 if rustc_dep_of_std {
148 set_cfg("libc_thread_local");
151 // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C".
152 if rustc_minor_ver >= 62 {
153 set_cfg("libc_const_extern_fn");
155 // Rust < 1.62.0 requires a crate feature and feature gate.
156 if const_extern_fn_cargo_feature {
157 if !is_nightly || rustc_minor_ver < 40 {
158 panic!("const-extern-fn requires a nightly compiler >= 1.40");
160 set_cfg("libc_const_extern_fn_unstable");
161 set_cfg("libc_const_extern_fn");
165 // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the
166 // codebase. libc can configure it if the appropriate environment variable is passed. Since
167 // rust-lang/rust enforces it, this is useful when using a custom libc fork there.
169 // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
171 for cfg in ALLOWED_CFGS {
172 if rustc_minor_ver >= 75 {
173 println!("cargo:rustc-check-cfg=cfg({})", cfg);
175 println!("cargo:rustc-check-cfg=values({})", cfg);
178 for &(name, values) in CHECK_CFG_EXTRA {
179 let values = values.join("\",\"");
180 if rustc_minor_ver >= 75 {
181 println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values);
183 println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
189 fn rustc_minor_nightly() -> (u32, bool) {
194 None => panic!("Failed to get rustc version"),
199 let rustc = otry!(env::var_os("RUSTC"));
200 let output = Command::new(rustc)
204 .expect("Failed to get rustc version");
205 if !output.status.success() {
207 "failed to run rustc: {}",
208 String::from_utf8_lossy(output.stderr.as_slice())
212 let version = otry!(str::from_utf8(&output.stdout).ok());
213 let mut pieces = version.split('.');
215 if pieces.next() != Some("rustc 1") {
216 panic!("Failed to get rustc version");
219 let minor = pieces.next();
221 // If `rustc` was built from a tarball, its version string
222 // will have neither a git hash nor a commit date
223 // (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
224 // since a nightly build should either come from CI
226 let nightly_raw = otry!(pieces.next()).split('-').nth(1);
227 let nightly = nightly_raw
228 .map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
230 let minor = otry!(otry!(minor).parse().ok());
235 fn which_freebsd() -> Option<i32> {
236 let output = std::process::Command::new("freebsd-version").output().ok();
237 if output.is_none() {
240 let output = output.unwrap();
241 if !output.status.success() {
245 let stdout = String::from_utf8(output.stdout).ok();
246 if stdout.is_none() {
249 let stdout = stdout.unwrap();
252 s if s.starts_with("10") => Some(10),
253 s if s.starts_with("11") => Some(11),
254 s if s.starts_with("12") => Some(12),
255 s if s.starts_with("13") => Some(13),
256 s if s.starts_with("14") => Some(14),
257 s if s.starts_with("15") => Some(15),
262 fn emcc_version_code() -> Option<u64> {
263 let output = std::process::Command::new("emcc")
267 if output.is_none() {
270 let output = output.unwrap();
271 if !output.status.success() {
275 let stdout = String::from_utf8(output.stdout).ok();
276 if stdout.is_none() {
279 let version = stdout.unwrap();
281 // Some Emscripten versions come with `-git` attached, so split the
282 // version string also on the `-` char.
283 let mut pieces = version.trim().split(|c| c == '.' || c == '-');
285 let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
286 let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
287 let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
289 Some(major * 10000 + minor * 100 + patch)
292 fn set_cfg(cfg: &str) {
293 if !ALLOWED_CFGS.contains(&cfg) {
294 panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
296 println!("cargo:rustc-cfg={}", cfg);