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