Debian Codegraph @

third_party/rust/libc/build.rs

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