1 use std::process::{Command, Output};
2 use std::{env, str};
3
4 // List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
5 // need to know all the possible cfgs that this script will set. If you need to set another cfg
6 // make sure to add it to this list as well.
7 const ALLOWED_CFGS: &[&str] = &[
8 "emscripten_old_stat_abi",
9 "espidf_time32",
10 "freebsd10",
11 "freebsd11",
12 "freebsd12",
13 "freebsd13",
14 "freebsd14",
15 "freebsd15",
16 // Corresponds to `_FILE_OFFSET_BITS=64` in glibc
17 "gnu_file_offset_bits64",
18 // Corresponds to `_TIME_BITS=64` in glibc
19 "gnu_time_bits64",
20 // FIXME(ctest): this config shouldn't be needed but ctest can't parse `const extern fn`
21 "libc_const_extern_fn",
22 "libc_deny_warnings",
23 "libc_thread_local",
24 "libc_ctest",
25 // Corresponds to `__USE_TIME_BITS64` in UAPI
26 "linux_time_bits64",
27 "musl_v1_2_3",
28 ];
29
30 // Extra values to allow for check-cfg.
31 const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[
32 (
33 "target_os",
34 &[
35 "switch", "aix", "ohos", "hurd", "rtems", "visionos", "nuttx", "cygwin",
36 ],
37 ),
38 (
39 "target_env",
40 &["illumos", "wasi", "aix", "ohos", "nto71_iosock", "nto80"],
41 ),
42 (
43 "target_arch",
44 &["loongarch64", "mips32r6", "mips64r6", "csky"],
45 ),
46 ];
47
main()48 fn main() {
49 // Avoid unnecessary re-building.
50 println!("cargo:rerun-if-changed=build.rs");
51
52 let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly();
53 let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
54 let libc_ci = env::var("LIBC_CI").is_ok();
55 let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
56 let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
57 let target_ptr_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap_or_default();
58 let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
59
60 // The ABI of libc used by std is backward compatible with FreeBSD 12.
61 // The ABI of libc from crates.io is backward compatible with FreeBSD 11.
62 //
63 // On CI, we detect the actual FreeBSD version and match its ABI exactly,
64 // running tests to ensure that the ABI is correct.
65 println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_FREEBSD_VERSION");
66 // Allow overriding the default version for testing
67 let which_freebsd = if let Ok(version) = env::var("RUST_LIBC_UNSTABLE_FREEBSD_VERSION") {
68 let vers = version.parse().unwrap();
69 println!("cargo:warning=setting FreeBSD version to {vers}");
70 vers
71 } else if libc_ci {
72 which_freebsd().unwrap_or(11)
73 } else if rustc_dep_of_std {
74 12
75 } else {
76 11
77 };
78
79 match which_freebsd {
80 x if x < 10 => panic!("FreeBSD older than 10 is not supported"),
81 10 => set_cfg("freebsd10"),
82 11 => set_cfg("freebsd11"),
83 12 => set_cfg("freebsd12"),
84 13 => set_cfg("freebsd13"),
85 14 => set_cfg("freebsd14"),
86 _ => set_cfg("freebsd15"),
87 }
88
89 match emcc_version_code() {
90 Some(v) if (v < 30142) => set_cfg("emscripten_old_stat_abi"),
91 // Non-Emscripten or version >= 3.1.42.
92 _ => (),
93 }
94
95 let musl_v1_2_3 = env::var("RUST_LIBC_UNSTABLE_MUSL_V1_2_3").is_ok();
96 println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_MUSL_V1_2_3");
97 // loongarch64 and ohos have already updated
98 if musl_v1_2_3 || target_os == "loongarch64" || target_env == "ohos" {
99 // FIXME(musl): enable time64 api as well
100 set_cfg("musl_v1_2_3");
101 }
102 let linux_time_bits64 = env::var("RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64").is_ok();
103 println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_LINUX_TIME_BITS64");
104 if linux_time_bits64 {
105 set_cfg("linux_time_bits64");
106 }
107 println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS");
108 println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_GNU_TIME_BITS");
109 if target_env == "gnu"
110 && target_os == "linux"
111 && target_ptr_width == "32"
112 && target_arch != "riscv32"
113 && target_arch != "x86_64"
114 {
115 match env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") {
116 Ok(val) if val == "64" => {
117 set_cfg("gnu_file_offset_bits64");
118 set_cfg("linux_time_bits64");
119 set_cfg("gnu_time_bits64");
120 }
121 Ok(val) if val != "32" => {
122 panic!("RUST_LIBC_UNSTABLE_GNU_TIME_BITS may only be set to '32' or '64'")
123 }
124 _ => {
125 match env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS") {
126 Ok(val) if val == "64" => {
127 set_cfg("gnu_file_offset_bits64");
128 }
129 Ok(val) if val != "32" => {
130 panic!("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS may only be set to '32' or '64'")
131 }
132 _ => {}
133 }
134 }
135 }
136 }
137 // On CI: deny all warnings
138 if libc_ci {
139 set_cfg("libc_deny_warnings");
140 }
141
142 // #[thread_local] is currently unstable
143 if rustc_dep_of_std {
144 set_cfg("libc_thread_local");
145 }
146
147 // Set unconditionally when ctest is not being invoked.
148 set_cfg("libc_const_extern_fn");
149
150 // Since Rust 1.80, configuration that isn't recognized by default needs to be provided to
151 // avoid warnings.
152 if rustc_minor_ver >= 80 {
153 for cfg in ALLOWED_CFGS {
154 if rustc_minor_ver >= 75 {
155 println!("cargo:rustc-check-cfg=cfg({cfg})");
156 } else {
157 println!("cargo:rustc-check-cfg=values({cfg})");
158 }
159 }
160 for &(name, values) in CHECK_CFG_EXTRA {
161 let values = values.join("\",\"");
162 if rustc_minor_ver >= 75 {
163 println!("cargo:rustc-check-cfg=cfg({name},values(\"{values}\"))");
164 } else {
165 println!("cargo:rustc-check-cfg=values({name},\"{values}\")");
166 }
167 }
168 }
169 }
170
171 /// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver`
172 /// is used instead.
rustc_version_cmd(is_clippy_driver: bool) -> Output173 fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
174 let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env");
175
176 let mut cmd = match env::var_os("RUSTC_WRAPPER") {
177 Some(ref wrapper) if wrapper.is_empty() => Command::new(rustc),
178 Some(wrapper) => {
179 let mut cmd = Command::new(wrapper);
180 cmd.arg(rustc);
181 if is_clippy_driver {
182 cmd.arg("--rustc");
183 }
184
185 cmd
186 }
187 None => Command::new(rustc),
188 };
189
190 cmd.arg("--version");
191
192 let output = cmd.output().expect("Failed to get rustc version");
193
194 assert!(
195 output.status.success(),
196 "failed to run rustc: {}",
197 String::from_utf8_lossy(output.stderr.as_slice())
198 );
199
200 output
201 }
202
203 /// Return the minor version of `rustc`, as well as a bool indicating whether or not the version
204 /// is a nightly.
rustc_minor_nightly() -> (u32, bool)205 fn rustc_minor_nightly() -> (u32, bool) {
206 macro_rules! otry {
207 ($e:expr) => {
208 match $e {
209 Some(e) => e,
210 None => panic!("Failed to get rustc version"),
211 }
212 };
213 }
214
215 let mut output = rustc_version_cmd(false);
216
217 if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") {
218 output = rustc_version_cmd(true);
219 }
220
221 let version = otry!(str::from_utf8(&output.stdout).ok());
222
223 let mut pieces = version.split('.');
224
225 assert_eq!(
226 pieces.next(),
227 Some("rustc 1"),
228 "Failed to get rustc version"
229 );
230
231 let minor = pieces.next();
232
233 // If `rustc` was built from a tarball, its version string
234 // will have neither a git hash nor a commit date
235 // (e.g. "rustc 1.39.0"). Treat this case as non-nightly,
236 // since a nightly build should either come from CI
237 // or a git checkout
238 let nightly_raw = otry!(pieces.next()).split('-').nth(1);
239 let nightly = nightly_raw.map_or(false, |raw| {
240 raw.starts_with("dev") || raw.starts_with("nightly")
241 });
242 let minor = otry!(otry!(minor).parse().ok());
243
244 (minor, nightly)
245 }
246
which_freebsd() -> Option<i32>247 fn which_freebsd() -> Option<i32> {
248 let output = Command::new("freebsd-version").output().ok()?;
249 if !output.status.success() {
250 return None;
251 }
252
253 let stdout = String::from_utf8(output.stdout).ok()?;
254
255 match &stdout {
256 s if s.starts_with("10") => Some(10),
257 s if s.starts_with("11") => Some(11),
258 s if s.starts_with("12") => Some(12),
259 s if s.starts_with("13") => Some(13),
260 s if s.starts_with("14") => Some(14),
261 s if s.starts_with("15") => Some(15),
262 _ => None,
263 }
264 }
265
emcc_version_code() -> Option<u64>266 fn emcc_version_code() -> Option<u64> {
267 let emcc = if cfg!(target_os = "windows") {
268 "emcc.bat"
269 } else {
270 "emcc"
271 };
272
273 let output = Command::new(emcc).arg("-dumpversion").output().ok()?;
274 if !output.status.success() {
275 return None;
276 }
277
278 let version = String::from_utf8(output.stdout).ok()?;
279
280 // Some Emscripten versions come with `-git` attached, so split the
281 // version string also on the `-` char.
282 let mut pieces = version.trim().split(['.', '-']);
283
284 let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
285 let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
286 let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0);
287
288 Some(major * 10000 + minor * 100 + patch)
289 }
290
set_cfg(cfg: &str)291 fn set_cfg(cfg: &str) {
292 assert!(
293 ALLOWED_CFGS.contains(&cfg),
294 "trying to set cfg {cfg}, but it is not in ALLOWED_CFGS",
295 );
296 println!("cargo:rustc-cfg={cfg}");
297 }
298