| 1 |
use std::env::VarError; |
| 2 |
use std::process::{ |
| 3 |
Command, |
| 4 |
Output, |
| 5 |
}; |
| 6 |
use std::sync::atomic::AtomicBool; |
| 7 |
use std::sync::atomic::Ordering::Relaxed; |
| 8 |
use std::{ |
| 9 |
env, |
| 10 |
str, |
| 11 |
}; |
| 12 |
|
| 13 |
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we |
| 14 |
// need to know all the possible cfgs that this script will set. If you need to set another cfg |
| 15 |
// make sure to add it to this list as well. |
| 16 |
const ALLOWED_CFGS: &[&str] = &[ |
| 17 |
"emscripten_old_stat_abi", |
| 18 |
// Should be enabled by users if esp-idf (>=6.0) is build with picolibc instead of newlib. |
| 19 |
"espidf_picolibc", |
| 20 |
"espidf_time32", |
| 21 |
"freebsd10", |
| 22 |
"freebsd11", |
| 23 |
"freebsd12", |
| 24 |
"freebsd13", |
| 25 |
"freebsd14", |
| 26 |
"freebsd15", |
| 27 |
// Corresponds to `_FILE_OFFSET_BITS=64` in glibc |
| 28 |
"gnu_file_offset_bits64", |
| 29 |
// Corresponds to `_TIME_BITS=64` in glibc. Also used in x86 Windows with |
| 30 |
// GNU to expose a 64-bit `time_t`. |
| 31 |
"gnu_time_bits64", |
| 32 |
"libc_deny_warnings", |
| 33 |
// Corresponds to `__USE_TIME_BITS64` in UAPI |
| 34 |
"linux_time_bits64", |
| 35 |
"musl_v1_2_3", |
| 36 |
// musl v1.2.3+ && 32-bit: time_t is i64, struct layouts change |
| 37 |
"musl32_time64", |
| 38 |
// Corresponds to `_REDIR_TIME64` in musl: symbol redirects to __*_time64 |
| 39 |
"musl_redir_time64", |
| 40 |
"vxworks_lt_25_09", |
| 41 |
"libc_pauthtest", |
| 42 |
]; |
| 43 |
|
| 44 |
// Extra values to allow for check-cfg. |
| 45 |
const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[ |
| 46 |
( |
| 47 |
"target_os", |
| 48 |
&[ |
| 49 |
"switch", "aix", "ohos", "hurd", "rtems", "visionos", "nuttx", "cygwin", "qurt", "qnx", |
| 50 |
], |
| 51 |
), |
| 52 |
( |
| 53 |
"target_env", |
| 54 |
&["illumos", "wasi", "aix", "ohos", "nto71_iosock"], |
| 55 |
), |
| 56 |
( |
| 57 |
"target_arch", |
| 58 |
&["loongarch64", "mips32r6", "mips64r6", "csky"], |
| 59 |
), |
| 60 |
]; |
| 61 |
|
| 62 |
/// Musl architectures that define `_REDIR_TIME64` (i.e. those that transitioned |
| 63 |
/// from 32-bit to 64-bit `time_t` and need `__*_time64` symbol redirects). |
| 64 |
const MUSL_REDIR_TIME64_ARCHES: &[&str] = &["arm", "mips", "powerpc", "x86"]; |
| 65 |
|
| 66 |
/// Read from env, print more debug output via `cargo:warning` if set. |
| 67 |
static VERBOSE_BUILD: AtomicBool = AtomicBool::new(false); |
| 68 |
|
| 69 |
/// Print info via warnings if `LIBC_BUILD_VERBOSE` is set. |
| 70 |
macro_rules! info { |
| 71 |
($($tt:tt)+) => { |
| 72 |
if VERBOSE_BUILD.load(Relaxed) { |
| 73 |
println!("cargo:warning=info: {}", format_args!($($tt)*)); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
fn main() { |
| 79 |
// Avoid unnecessary re-building. |
| 80 |
println!("cargo:rerun-if-changed=build.rs"); |
| 81 |
|
| 82 |
println!("cargo:rerun-if-env-changed=LIBC_BUILD_VERBOSE"); |
| 83 |
if env_flag("LIBC_BUILD_VERBOSE") { |
| 84 |
VERBOSE_BUILD.store(true, Relaxed); |
| 85 |
} |
| 86 |
|
| 87 |
let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly(); |
| 88 |
let libc_ci = env_flag("LIBC_CI"); |
| 89 |
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); |
| 90 |
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
| 91 |
let target_ptr_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap_or_default(); |
| 92 |
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); |
| 93 |
let target_abi = env::var("CARGO_CFG_TARGET_ABI").unwrap_or_default(); |
| 94 |
|
| 95 |
// FIXME(msrv): Once the MSRV is 1.78, use `cfg(target_abi = "pauthtest")` |
| 96 |
// directly instead of translating it to `libc_pauthtest`. `target_abi` |
| 97 |
// cannot be used directly in cfg expressions on the current MSRV. |
| 98 |
if target_abi == "pauthtest" { |
| 99 |
set_cfg("libc_pauthtest"); |
| 100 |
} |
| 101 |
|
| 102 |
// FIXME: this can be removed in 1-2 releases |
| 103 |
println!("cargo:rerun-if-env-changed=RUST_LIBC_UNSTABLE_FREEBSD_VERSION"); |
| 104 |
if env::var("RUST_LIBC_UNSTABLE_FREEBSD_VERSION").is_ok() { |
| 105 |
println!( |
| 106 |
"cargo:warning=RUST_LIBC_UNSTABLE_FREEBSD_VERSION has been removed; set \ |
| 107 |
the cfg libc_unstable_freebsd_version via RUSTFLAGS instead" |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// The ABI of libc used by std is backward compatible with FreeBSD 12. |
| 112 |
// The ABI of libc from crates.io is backward compatible with FreeBSD 12. |
| 113 |
// |
| 114 |
// On CI, we detect the actual FreeBSD version and match its ABI exactly, |
| 115 |
// running tests to ensure that the ABI is correct. |
| 116 |
// Allow overriding the default version for testing |
| 117 |
let which_freebsd = if let Ok(version) = env::var("CARGO_CFG_LIBC_UNSTABLE_FREEBSD_VERSION") { |
| 118 |
let vers = version.parse().unwrap(); |
| 119 |
println!("cargo:warning=setting FreeBSD version to {vers}"); |
| 120 |
vers |
| 121 |
} else if libc_ci { |
| 122 |
which_freebsd().unwrap_or(12) |
| 123 |
} else { |
| 124 |
12 |
| 125 |
}; |
| 126 |
|
| 127 |
match which_freebsd { |
| 128 |
x if x < 10 => panic!("FreeBSD older than 10 is not supported"), |
| 129 |
10 => set_cfg("freebsd10"), |
| 130 |
11 => set_cfg("freebsd11"), |
| 131 |
12 => set_cfg("freebsd12"), |
| 132 |
13 => set_cfg("freebsd13"), |
| 133 |
14 => set_cfg("freebsd14"), |
| 134 |
_ => set_cfg("freebsd15"), |
| 135 |
} |
| 136 |
|
| 137 |
match emcc_version_code() { |
| 138 |
Some(v) if (v < 30142) => set_cfg("emscripten_old_stat_abi"), |
| 139 |
// Non-Emscripten or version >= 3.1.42. |
| 140 |
_ => (), |
| 141 |
} |
| 142 |
|
| 143 |
match vxworks_version_code() { |
| 144 |
Some(v) if (v < (25, 9)) => set_cfg("vxworks_lt_25_09"), |
| 145 |
// VxWorks version >= 25.09 |
| 146 |
_ => (), |
| 147 |
} |
| 148 |
|
| 149 |
let mut musl_v1_2_3 = env_flag("CARGO_CFG_LIBC_UNSTABLE_MUSL_V1_2_3"); |
| 150 |
|
| 151 |
// OpenHarmony uses a fork of the musl libc |
| 152 |
let musl = target_env == "musl" || target_env == "ohos"; |
| 153 |
|
| 154 |
// loongarch64, hexagon, ohos and pauthtest only exist with recent musl |
| 155 |
if target_arch == "loongarch64" |
| 156 |
|| target_arch == "hexagon" |
| 157 |
|| target_env == "ohos" |
| 158 |
|| target_abi == "pauthtest" |
| 159 |
{ |
| 160 |
musl_v1_2_3 = true; |
| 161 |
} |
| 162 |
|
| 163 |
if musl && musl_v1_2_3 { |
| 164 |
set_cfg("musl_v1_2_3"); |
| 165 |
if target_ptr_width == "32" { |
| 166 |
set_cfg("musl32_time64"); |
| 167 |
set_cfg("linux_time_bits64"); |
| 168 |
} |
| 169 |
if MUSL_REDIR_TIME64_ARCHES.contains(&target_arch.as_str()) { |
| 170 |
set_cfg("musl_redir_time64"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
let uclibc_use_time64 = env_flag("CARGO_CFG_LIBC_UNSTABLE_UCLIBC_TIME64"); |
| 175 |
if target_env == "uclibc" && uclibc_use_time64 { |
| 176 |
set_cfg("linux_time_bits64"); |
| 177 |
} |
| 178 |
|
| 179 |
if target_env == "gnu" |
| 180 |
&& matches!(target_os.as_str(), "linux" | "windows") |
| 181 |
&& target_ptr_width == "32" |
| 182 |
&& target_arch != "riscv32" |
| 183 |
&& target_arch != "x86_64" |
| 184 |
{ |
| 185 |
let defaultbits = "32"; |
| 186 |
|
| 187 |
let mut tb_env = env::var("CARGO_CFG_LIBC_UNSTABLE_GNU_TIME_BITS"); |
| 188 |
|
| 189 |
// FIXME: remove these fallbacks in a few releases |
| 190 |
if let Ok(old_tb_env) = env::var("RUST_LIBC_UNSTABLE_GNU_TIME_BITS") { |
| 191 |
println!( |
| 192 |
"cargo:warning=RUST_LIBC_UNSTABLE_GNU_TIME_BITS will be removed; \ |
| 193 |
set `--cfg=libc_unstable_gnu_time_bits=\"...\"` via RUSTFLAGS instead" |
| 194 |
); |
| 195 |
tb_env = tb_env.or(Ok(old_tb_env)); |
| 196 |
} |
| 197 |
if env::var("RUST_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS").is_ok() |
| 198 |
|| env::var("CARGO_CFG_LIBC_UNSTABLE_GNU_FILE_OFFSET_BITS").is_ok() |
| 199 |
{ |
| 200 |
println!( |
| 201 |
"cargo:warning=glibc file offset can no longer be set independently of \ |
| 202 |
`gnu_time_bits`" |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
let timebits = match tb_env.as_deref() { |
| 207 |
Err(_) => defaultbits, |
| 208 |
Ok(tb) if tb == "64" => tb, |
| 209 |
Ok(tb) if tb == "32" => tb, |
| 210 |
Ok(_) => { |
| 211 |
panic!("Invalid value for libc_unstable_gnu_time_bits. Must be 32, 64, or unset.") |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
if timebits == "64" { |
| 216 |
set_cfg("linux_time_bits64"); |
| 217 |
set_cfg("gnu_file_offset_bits64"); |
| 218 |
set_cfg("gnu_time_bits64"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// On CI: deny all warnings |
| 223 |
if libc_ci { |
| 224 |
set_cfg("libc_deny_warnings"); |
| 225 |
} |
| 226 |
|
| 227 |
// Since Rust 1.80, configuration that isn't recognized by default needs to be provided to |
| 228 |
// avoid warnings. |
| 229 |
if rustc_minor_ver >= 80 { |
| 230 |
for cfg in ALLOWED_CFGS { |
| 231 |
println!("cargo:rustc-check-cfg=cfg({cfg})"); |
| 232 |
} |
| 233 |
for &(name, values) in CHECK_CFG_EXTRA { |
| 234 |
let values = values.join("\",\""); |
| 235 |
println!("cargo:rustc-check-cfg=cfg({name},values(\"{values}\"))"); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/// Run `rustc --version` and capture the output, adjusting arguments as needed if `clippy-driver` |
| 241 |
/// is used instead. |
| 242 |
fn rustc_version_cmd(is_clippy_driver: bool) -> Output { |
| 243 |
let rustc = env::var_os("RUSTC").expect("Failed to get rustc version: missing RUSTC env"); |
| 244 |
|
| 245 |
let mut cmd = match env::var_os("RUSTC_WRAPPER") { |
| 246 |
Some(ref wrapper) if wrapper.is_empty() => Command::new(rustc), |
| 247 |
Some(wrapper) => { |
| 248 |
let mut cmd = Command::new(wrapper); |
| 249 |
cmd.arg(rustc); |
| 250 |
if is_clippy_driver { |
| 251 |
cmd.arg("--rustc"); |
| 252 |
} |
| 253 |
|
| 254 |
cmd |
| 255 |
} |
| 256 |
None => Command::new(rustc), |
| 257 |
}; |
| 258 |
|
| 259 |
cmd.arg("--version"); |
| 260 |
|
| 261 |
let output = cmd.output().expect("Failed to get rustc version"); |
| 262 |
|
| 263 |
assert!( |
| 264 |
output.status.success(), |
| 265 |
"failed to run rustc: {}", |
| 266 |
String::from_utf8_lossy(output.stderr.as_slice()) |
| 267 |
); |
| 268 |
|
| 269 |
output |
| 270 |
} |
| 271 |
|
| 272 |
/// Return the minor version of `rustc`, as well as a bool indicating whether or not the version |
| 273 |
/// is a nightly. |
| 274 |
fn rustc_minor_nightly() -> (u32, bool) { |
| 275 |
macro_rules! otry { |
| 276 |
($e:expr) => { |
| 277 |
match $e { |
| 278 |
Some(e) => e, |
| 279 |
None => panic!("Failed to get rustc version"), |
| 280 |
} |
| 281 |
}; |
| 282 |
} |
| 283 |
|
| 284 |
let mut output = rustc_version_cmd(false); |
| 285 |
|
| 286 |
if otry!(str::from_utf8(&output.stdout).ok()).starts_with("clippy") { |
| 287 |
output = rustc_version_cmd(true); |
| 288 |
} |
| 289 |
|
| 290 |
let version = otry!(str::from_utf8(&output.stdout).ok()); |
| 291 |
|
| 292 |
let mut pieces = version.split('.'); |
| 293 |
|
| 294 |
assert_eq!( |
| 295 |
pieces.next(), |
| 296 |
Some("rustc 1"), |
| 297 |
"Failed to get rustc version" |
| 298 |
); |
| 299 |
|
| 300 |
let minor = pieces.next(); |
| 301 |
|
| 302 |
// If `rustc` was built from a tarball, its version string |
| 303 |
// will have neither a git hash nor a commit date |
| 304 |
// (e.g. "rustc 1.39.0"). Treat this case as non-nightly, |
| 305 |
// since a nightly build should either come from CI |
| 306 |
// or a git checkout |
| 307 |
let nightly_raw = otry!(pieces.next()).split('-').nth(1); |
| 308 |
let nightly = nightly_raw.map_or(false, |raw| { |
| 309 |
raw.starts_with("dev") || raw.starts_with("nightly") |
| 310 |
}); |
| 311 |
let minor = otry!(otry!(minor).parse().ok()); |
| 312 |
|
| 313 |
info!("detected rust 1.{minor}, nightly={nightly}"); |
| 314 |
|
| 315 |
(minor, nightly) |
| 316 |
} |
| 317 |
|
| 318 |
fn which_freebsd() -> Option<i32> { |
| 319 |
let output = Command::new("freebsd-version").output().ok()?; |
| 320 |
if !output.status.success() { |
| 321 |
return None; |
| 322 |
} |
| 323 |
|
| 324 |
let stdout = String::from_utf8(output.stdout).ok()?; |
| 325 |
|
| 326 |
match &stdout { |
| 327 |
s if s.starts_with("10") => Some(10), |
| 328 |
s if s.starts_with("11") => Some(11), |
| 329 |
s if s.starts_with("12") => Some(12), |
| 330 |
s if s.starts_with("13") => Some(13), |
| 331 |
s if s.starts_with("14") => Some(14), |
| 332 |
s if s.starts_with("15") => Some(15), |
| 333 |
_ => None, |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
fn emcc_version_code() -> Option<u64> { |
| 338 |
let emcc = if cfg!(target_os = "windows") { |
| 339 |
"emcc.bat" |
| 340 |
} else { |
| 341 |
"emcc" |
| 342 |
}; |
| 343 |
|
| 344 |
let output = Command::new(emcc).arg("-dumpversion").output().ok()?; |
| 345 |
if !output.status.success() { |
| 346 |
return None; |
| 347 |
} |
| 348 |
|
| 349 |
let version = String::from_utf8(output.stdout).ok()?; |
| 350 |
|
| 351 |
// Some Emscripten versions come with `-git` attached, so split the |
| 352 |
// version string also on the `-` char. |
| 353 |
let mut pieces = version.trim().split(['.', '-']); |
| 354 |
|
| 355 |
let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 356 |
let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 357 |
let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 358 |
|
| 359 |
Some(major * 10000 + minor * 100 + patch) |
| 360 |
} |
| 361 |
|
| 362 |
/// Retrieve the VxWorks release version from the environment variable set by the VxWorks build |
| 363 |
/// environment, in `(minor, patch)` form. Currently the only major version supported by Rust |
| 364 |
/// is 7. |
| 365 |
fn vxworks_version_code() -> Option<(u32, u32)> { |
| 366 |
let version = env::var("WIND_RELEASE_ID").ok()?; |
| 367 |
|
| 368 |
let mut pieces = version.trim().split(['.']); |
| 369 |
|
| 370 |
let major: u32 = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 371 |
let minor: u32 = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 372 |
|
| 373 |
Some((major, minor)) |
| 374 |
} |
| 375 |
|
| 376 |
fn set_cfg(cfg: &str) { |
| 377 |
assert!( |
| 378 |
ALLOWED_CFGS.contains(&cfg), |
| 379 |
"trying to set cfg {cfg}, but it is not in ALLOWED_CFGS", |
| 380 |
); |
| 381 |
println!("cargo:rustc-cfg={cfg}"); |
| 382 |
info!("setting config `{cfg}`"); |
| 383 |
} |
| 384 |
|
| 385 |
/// Return true if the env is set to a value other than `0`. |
| 386 |
fn env_flag(key: &str) -> bool { |
| 387 |
match env::var(key) { |
| 388 |
Ok(x) if x == "0" => false, |
| 389 |
Err(VarError::NotPresent) => false, |
| 390 |
Err(VarError::NotUnicode(_)) => panic!("non-unicode var for `{key}`"), |
| 391 |
Ok(_) => true, |
| 392 |
} |
| 393 |
} |