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