| 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 |
"espidf_time64", |
| 12 |
"freebsd10", |
| 13 |
"freebsd11", |
| 14 |
"freebsd12", |
| 15 |
"freebsd13", |
| 16 |
"freebsd14", |
| 17 |
"freebsd15", |
| 18 |
"libc_align", |
| 19 |
"libc_cfg_target_vendor", |
| 20 |
"libc_const_extern_fn", |
| 21 |
"libc_const_extern_fn_unstable", |
| 22 |
"libc_const_size_of", |
| 23 |
"libc_core_cvoid", |
| 24 |
"libc_deny_warnings", |
| 25 |
"libc_int128", |
| 26 |
"libc_long_array", |
| 27 |
"libc_non_exhaustive", |
| 28 |
"libc_packedN", |
| 29 |
"libc_priv_mod_use", |
| 30 |
"libc_ptr_addr_of", |
| 31 |
"libc_thread_local", |
| 32 |
"libc_underscore_const_names", |
| 33 |
"libc_union", |
| 34 |
]; |
| 35 |
|
| 36 |
// Extra values to allow for check-cfg. |
| 37 |
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[ |
| 38 |
("target_os", &["switch", "aix", "ohos", "hurd", "visionos"]), |
| 39 |
("target_env", &["illumos", "wasi", "aix", "ohos"]), |
| 40 |
( |
| 41 |
"target_arch", |
| 42 |
&["loongarch64", "mips32r6", "mips64r6", "csky"], |
| 43 |
), |
| 44 |
]; |
| 45 |
|
| 46 |
fn main() { |
| 47 |
// Avoid unnecessary re-building. |
| 48 |
println!("cargo:rerun-if-changed=build.rs"); |
| 49 |
|
| 50 |
let (rustc_minor_ver, is_nightly) = rustc_minor_nightly(); |
| 51 |
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok(); |
| 52 |
let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok(); |
| 53 |
let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok(); |
| 54 |
let libc_ci = env::var("LIBC_CI").is_ok(); |
| 55 |
let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok() || rustc_minor_ver >= 80; |
| 56 |
|
| 57 |
if env::var("CARGO_FEATURE_USE_STD").is_ok() { |
| 58 |
println!( |
| 59 |
"cargo:warning=\"libc's use_std cargo feature is deprecated since libc 0.2.55; \ |
| 60 |
please consider using the `std` cargo feature instead\"" |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
// The ABI of libc used by std is backward compatible with FreeBSD 12. |
| 65 |
// The ABI of libc from crates.io is backward compatible with FreeBSD 11. |
| 66 |
// |
| 67 |
// On CI, we detect the actual FreeBSD version and match its ABI exactly, |
| 68 |
// running tests to ensure that the ABI is correct. |
| 69 |
match which_freebsd() { |
| 70 |
Some(10) if libc_ci => set_cfg("freebsd10"), |
| 71 |
Some(11) if libc_ci => set_cfg("freebsd11"), |
| 72 |
Some(12) if libc_ci || rustc_dep_of_std => set_cfg("freebsd12"), |
| 73 |
Some(13) if libc_ci => set_cfg("freebsd13"), |
| 74 |
Some(14) if libc_ci => set_cfg("freebsd14"), |
| 75 |
Some(15) if libc_ci => set_cfg("freebsd15"), |
| 76 |
Some(_) | None => set_cfg("freebsd11"), |
| 77 |
} |
| 78 |
|
| 79 |
match emcc_version_code() { |
| 80 |
Some(v) if (v >= 30142) => set_cfg("emscripten_new_stat_abi"), |
| 81 |
// Non-Emscripten or version < 3.1.42. |
| 82 |
Some(_) | None => (), |
| 83 |
} |
| 84 |
|
| 85 |
// On CI: deny all warnings |
| 86 |
if libc_ci { |
| 87 |
set_cfg("libc_deny_warnings"); |
| 88 |
} |
| 89 |
|
| 90 |
// Rust >= 1.15 supports private module use: |
| 91 |
if rustc_minor_ver >= 15 || rustc_dep_of_std { |
| 92 |
set_cfg("libc_priv_mod_use"); |
| 93 |
} |
| 94 |
|
| 95 |
// Rust >= 1.19 supports unions: |
| 96 |
if rustc_minor_ver >= 19 || rustc_dep_of_std { |
| 97 |
set_cfg("libc_union"); |
| 98 |
} |
| 99 |
|
| 100 |
// Rust >= 1.24 supports const mem::size_of: |
| 101 |
if rustc_minor_ver >= 24 || rustc_dep_of_std { |
| 102 |
set_cfg("libc_const_size_of"); |
| 103 |
} |
| 104 |
|
| 105 |
// Rust >= 1.25 supports repr(align): |
| 106 |
if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature { |
| 107 |
set_cfg("libc_align"); |
| 108 |
} |
| 109 |
|
| 110 |
// Rust >= 1.26 supports i128 and u128: |
| 111 |
if rustc_minor_ver >= 26 || rustc_dep_of_std { |
| 112 |
set_cfg("libc_int128"); |
| 113 |
} |
| 114 |
|
| 115 |
// Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it. |
| 116 |
// Otherwise, it defines an incompatible type to retaining |
| 117 |
// backwards-compatibility. |
| 118 |
if rustc_minor_ver >= 30 || rustc_dep_of_std { |
| 119 |
set_cfg("libc_core_cvoid"); |
| 120 |
} |
| 121 |
|
| 122 |
// Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor). |
| 123 |
if rustc_minor_ver >= 33 || rustc_dep_of_std { |
| 124 |
set_cfg("libc_packedN"); |
| 125 |
set_cfg("libc_cfg_target_vendor"); |
| 126 |
} |
| 127 |
|
| 128 |
// Rust >= 1.40 supports #[non_exhaustive]. |
| 129 |
if rustc_minor_ver >= 40 || rustc_dep_of_std { |
| 130 |
set_cfg("libc_non_exhaustive"); |
| 131 |
} |
| 132 |
|
| 133 |
// Rust >= 1.47 supports long array: |
| 134 |
if rustc_minor_ver >= 47 || rustc_dep_of_std { |
| 135 |
set_cfg("libc_long_array"); |
| 136 |
} |
| 137 |
|
| 138 |
if rustc_minor_ver >= 51 || rustc_dep_of_std { |
| 139 |
set_cfg("libc_ptr_addr_of"); |
| 140 |
} |
| 141 |
|
| 142 |
// Rust >= 1.37.0 allows underscores as anonymous constant names. |
| 143 |
if rustc_minor_ver >= 37 || rustc_dep_of_std { |
| 144 |
set_cfg("libc_underscore_const_names"); |
| 145 |
} |
| 146 |
|
| 147 |
// #[thread_local] is currently unstable |
| 148 |
if rustc_dep_of_std { |
| 149 |
set_cfg("libc_thread_local"); |
| 150 |
} |
| 151 |
|
| 152 |
// Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C". |
| 153 |
if rustc_minor_ver >= 62 { |
| 154 |
set_cfg("libc_const_extern_fn"); |
| 155 |
} else { |
| 156 |
// Rust < 1.62.0 requires a crate feature and feature gate. |
| 157 |
if const_extern_fn_cargo_feature { |
| 158 |
if !is_nightly || rustc_minor_ver < 40 { |
| 159 |
panic!("const-extern-fn requires a nightly compiler >= 1.40"); |
| 160 |
} |
| 161 |
set_cfg("libc_const_extern_fn_unstable"); |
| 162 |
set_cfg("libc_const_extern_fn"); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the |
| 167 |
// codebase. libc can configure it if the appropriate environment variable is passed. Since |
| 168 |
// rust-lang/rust enforces it, this is useful when using a custom libc fork there. |
| 169 |
// |
| 170 |
// https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg |
| 171 |
if libc_check_cfg { |
| 172 |
for cfg in ALLOWED_CFGS { |
| 173 |
if rustc_minor_ver >= 75 { |
| 174 |
println!("cargo:rustc-check-cfg=cfg({})", cfg); |
| 175 |
} else { |
| 176 |
println!("cargo:rustc-check-cfg=values({})", cfg); |
| 177 |
} |
| 178 |
} |
| 179 |
for &(name, values) in CHECK_CFG_EXTRA { |
| 180 |
let values = values.join("\",\""); |
| 181 |
if rustc_minor_ver >= 75 { |
| 182 |
println!("cargo:rustc-check-cfg=cfg({},values(\"{}\"))", name, values); |
| 183 |
} else { |
| 184 |
println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
fn rustc_minor_nightly() -> (u32, bool) { |
| 191 |
macro_rules! otry { |
| 192 |
($e:expr) => { |
| 193 |
match $e { |
| 194 |
Some(e) => e, |
| 195 |
None => panic!("Failed to get rustc version"), |
| 196 |
} |
| 197 |
}; |
| 198 |
} |
| 199 |
|
| 200 |
let rustc = otry!(env::var_os("RUSTC")); |
| 201 |
let output = Command::new(rustc) |
| 202 |
.arg("--version") |
| 203 |
.output() |
| 204 |
.ok() |
| 205 |
.expect("Failed to get rustc version"); |
| 206 |
if !output.status.success() { |
| 207 |
panic!( |
| 208 |
"failed to run rustc: {}", |
| 209 |
String::from_utf8_lossy(output.stderr.as_slice()) |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
let version = otry!(str::from_utf8(&output.stdout).ok()); |
| 214 |
let mut pieces = version.split('.'); |
| 215 |
|
| 216 |
if pieces.next() != Some("rustc 1") { |
| 217 |
panic!("Failed to get rustc version"); |
| 218 |
} |
| 219 |
|
| 220 |
let minor = pieces.next(); |
| 221 |
|
| 222 |
// If `rustc` was built from a tarball, its version string |
| 223 |
// will have neither a git hash nor a commit date |
| 224 |
// (e.g. "rustc 1.39.0"). Treat this case as non-nightly, |
| 225 |
// since a nightly build should either come from CI |
| 226 |
// or a git checkout |
| 227 |
let nightly_raw = otry!(pieces.next()).split('-').nth(1); |
| 228 |
let nightly = nightly_raw |
| 229 |
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly")) |
| 230 |
.unwrap_or(false); |
| 231 |
let minor = otry!(otry!(minor).parse().ok()); |
| 232 |
|
| 233 |
(minor, nightly) |
| 234 |
} |
| 235 |
|
| 236 |
fn which_freebsd() -> Option<i32> { |
| 237 |
let output = std::process::Command::new("freebsd-version").output().ok(); |
| 238 |
if output.is_none() { |
| 239 |
return None; |
| 240 |
} |
| 241 |
let output = output.unwrap(); |
| 242 |
if !output.status.success() { |
| 243 |
return None; |
| 244 |
} |
| 245 |
|
| 246 |
let stdout = String::from_utf8(output.stdout).ok(); |
| 247 |
if stdout.is_none() { |
| 248 |
return None; |
| 249 |
} |
| 250 |
let stdout = stdout.unwrap(); |
| 251 |
|
| 252 |
match &stdout { |
| 253 |
s if s.starts_with("10") => Some(10), |
| 254 |
s if s.starts_with("11") => Some(11), |
| 255 |
s if s.starts_with("12") => Some(12), |
| 256 |
s if s.starts_with("13") => Some(13), |
| 257 |
s if s.starts_with("14") => Some(14), |
| 258 |
s if s.starts_with("15") => Some(15), |
| 259 |
_ => None, |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
fn emcc_version_code() -> Option<u64> { |
| 264 |
let output = std::process::Command::new("emcc") |
| 265 |
.arg("-dumpversion") |
| 266 |
.output() |
| 267 |
.ok(); |
| 268 |
if output.is_none() { |
| 269 |
return None; |
| 270 |
} |
| 271 |
let output = output.unwrap(); |
| 272 |
if !output.status.success() { |
| 273 |
return None; |
| 274 |
} |
| 275 |
|
| 276 |
let stdout = String::from_utf8(output.stdout).ok(); |
| 277 |
if stdout.is_none() { |
| 278 |
return None; |
| 279 |
} |
| 280 |
let version = stdout.unwrap(); |
| 281 |
|
| 282 |
// Some Emscripten versions come with `-git` attached, so split the |
| 283 |
// version string also on the `-` char. |
| 284 |
let mut pieces = version.trim().split(|c| c == '.' || c == '-'); |
| 285 |
|
| 286 |
let major = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 287 |
let minor = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 288 |
let patch = pieces.next().and_then(|x| x.parse().ok()).unwrap_or(0); |
| 289 |
|
| 290 |
Some(major * 10000 + minor * 100 + patch) |
| 291 |
} |
| 292 |
|
| 293 |
fn set_cfg(cfg: &str) { |
| 294 |
if !ALLOWED_CFGS.contains(&cfg) { |
| 295 |
panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg); |
| 296 |
} |
| 297 |
println!("cargo:rustc-cfg={}", cfg); |
| 298 |
} |