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