1 use std::env;
2 use std::process::Command;
3 use std::str;
4 use std::string::String;
5
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",
11 "freebsd10",
12 "freebsd11",
13 "freebsd12",
14 "freebsd13",
15 "freebsd14",
16 "freebsd15",
17 "libc_align",
18 "libc_cfg_target_vendor",
19 "libc_const_extern_fn",
20 "libc_const_extern_fn_unstable",
21 "libc_const_size_of",
22 "libc_core_cvoid",
23 "libc_deny_warnings",
24 "libc_int128",
25 "libc_long_array",
26 "libc_non_exhaustive",
27 "libc_packedN",
28 "libc_priv_mod_use",
29 "libc_ptr_addr_of",
30 "libc_thread_local",
31 "libc_underscore_const_names",
32 "libc_union",
33 ];
34
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"]),
39 (
40 "target_arch",
41 &["loongarch64", "mips32r6", "mips64r6", "csky"],
42 ),
43 ];
44
main()45 fn main() {
46 // Avoid unnecessary re-building.
47 println!("cargo:rerun-if-changed=build.rs");
48
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();
55
56 if env::var("CARGO_FEATURE_USE_STD").is_ok() {
57 println!(
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\""
60 );
61 }
62
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.
65 //
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"),
76 }
77
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.
81 Some(_) | None => (),
82 }
83
84 // On CI: deny all warnings
85 if libc_ci {
86 set_cfg("libc_deny_warnings");
87 }
88
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");
92 }
93
94 // Rust >= 1.19 supports unions:
95 if rustc_minor_ver >= 19 || rustc_dep_of_std {
96 set_cfg("libc_union");
97 }
98
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");
102 }
103
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");
107 }
108
109 // Rust >= 1.26 supports i128 and u128:
110 if rustc_minor_ver >= 26 || rustc_dep_of_std {
111 set_cfg("libc_int128");
112 }
113
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");
119 }
120
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");
125 }
126
127 // Rust >= 1.40 supports #[non_exhaustive].
128 if rustc_minor_ver >= 40 || rustc_dep_of_std {
129 set_cfg("libc_non_exhaustive");
130 }
131
132 // Rust >= 1.47 supports long array:
133 if rustc_minor_ver >= 47 || rustc_dep_of_std {
134 set_cfg("libc_long_array");
135 }
136
137 if rustc_minor_ver >= 51 || rustc_dep_of_std {
138 set_cfg("libc_ptr_addr_of");
139 }
140
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");
144 }
145
146 // #[thread_local] is currently unstable
147 if rustc_dep_of_std {
148 set_cfg("libc_thread_local");
149 }
150
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");
154 } else {
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");
159 }
160 set_cfg("libc_const_extern_fn_unstable");
161 set_cfg("libc_const_extern_fn");
162 }
163 }
164
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.
168 //
169 // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
170 if libc_check_cfg {
171 for cfg in ALLOWED_CFGS {
172 if rustc_minor_ver >= 75 {
173 println!("cargo:rustc-check-cfg=cfg({})", cfg);
174 } else {
175 println!("cargo:rustc-check-cfg=values({})", cfg);
176 }
177 }
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);
182 } else {
183 println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
184 }
185 }
186 }
187 }
188
rustc_minor_nightly() -> (u32, bool)189 fn rustc_minor_nightly() -> (u32, bool) {
190 macro_rules! otry {
191 ($e:expr) => {
192 match $e {
193 Some(e) => e,
194 None => panic!("Failed to get rustc version"),
195 }
196 };
197 }
198
199 let rustc = otry!(env::var_os("RUSTC"));
200 let output = Command::new(rustc)
201 .arg("--version")
202 .output()
203 .ok()
204 .expect("Failed to get rustc version");
205 if !output.status.success() {
206 panic!(
207 "failed to run rustc: {}",
208 String::from_utf8_lossy(output.stderr.as_slice())
209 );
210 }
211
212 let version = otry!(str::from_utf8(&output.stdout).ok());
213 let mut pieces = version.split('.');
214
215 if pieces.next() != Some("rustc 1") {
216 panic!("Failed to get rustc version");
217 }
218
219 let minor = pieces.next();
220
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
225 // or a git checkout
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"))
229 .unwrap_or(false);
230 let minor = otry!(otry!(minor).parse().ok());
231
232 (minor, nightly)
233 }
234
which_freebsd() -> Option<i32>235 fn which_freebsd() -> Option<i32> {
236 let output = std::process::Command::new("freebsd-version").output().ok();
237 if output.is_none() {
238 return None;
239 }
240 let output = output.unwrap();
241 if !output.status.success() {
242 return None;
243 }
244
245 let stdout = String::from_utf8(output.stdout).ok();
246 if stdout.is_none() {
247 return None;
248 }
249 let stdout = stdout.unwrap();
250
251 match &stdout {
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),
258 _ => None,
259 }
260 }
261
emcc_version_code() -> Option<u64>262 fn emcc_version_code() -> Option<u64> {
263 let output = std::process::Command::new("emcc")
264 .arg("-dumpversion")
265 .output()
266 .ok();
267 if output.is_none() {
268 return None;
269 }
270 let output = output.unwrap();
271 if !output.status.success() {
272 return None;
273 }
274
275 let stdout = String::from_utf8(output.stdout).ok();
276 if stdout.is_none() {
277 return None;
278 }
279 let version = stdout.unwrap();
280
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 == '-');
284
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);
288
289 Some(major * 10000 + minor * 100 + patch)
290 }
291
set_cfg(cfg: &str)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);
295 }
296 println!("cargo:rustc-cfg={}", cfg);
297 }
298