#[cfg(feature = "cc")]
use cc::Build;
use std::env::var;
use std::io::Write;
const OUTLINE_PATH: &str = "src/imp/linux_raw/arch/outline";
fn main() {
println!("cargo:rerun-if-changed=build.rs");
use_feature_or_nothing("rustc_attrs");
#[cfg(not(feature = "std"))]
{
use_feature_or_nothing("vec_into_raw_parts");
use_feature_or_nothing("toowned_clone_into");
use_feature_or_nothing("specialization");
use_feature_or_nothing("slice_internals");
use_feature_or_nothing("const_raw_ptr_deref");
}
let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
let asm_name = format!("{}/{}.s", OUTLINE_PATH, arch);
let asm_name_present = std::fs::metadata(&asm_name).is_ok();
let os_name = var("CARGO_CFG_TARGET_OS").unwrap();
let pointer_width = var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
let endian = var("CARGO_CFG_TARGET_ENDIAN").unwrap();
let is_x32 = arch == "x86_64" && pointer_width == "32";
let is_arm64_ilp32 = arch == "aarch64" && pointer_width == "32";
let is_powerpc64be = arch == "powerpc64" && endian == "big";
let is_mipseb = arch == "mips" && endian == "big";
let is_mips64eb = arch == "mips64" && endian == "big";
let is_unsupported_abi = is_x32 || is_arm64_ilp32 || is_powerpc64be || is_mipseb || is_mips64eb;
let feature_use_libc = var("CARGO_FEATURE_USE_LIBC").is_ok();
let cfg_use_libc = var("CARGO_CFG_RUSTIX_USE_LIBC").is_ok();
let rustix_use_experimental_asm = var("CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM").is_ok();
if feature_use_libc
|| cfg_use_libc
|| os_name != "linux"
|| !asm_name_present
|| is_unsupported_abi
{
use_feature("libc");
} else {
use_feature("linux_raw");
use_feature_or_nothing("core_intrinsics");
if can_compile("use std::arch::asm;")
&& (arch != "x86" || has_feature("naked_functions"))
&& ((arch != "powerpc64" && arch != "mips" && arch != "mips64")
|| rustix_use_experimental_asm)
{
use_feature("asm");
if arch == "x86" {
use_feature("naked_functions");
}
if rustix_use_experimental_asm {
use_feature("asm_experimental_arch");
}
} else {
link_in_librustix_outline(&arch, &asm_name);
}
}
println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM");
}
fn link_in_librustix_outline(arch: &str, asm_name: &str) {
let name = format!("rustix_outline_{}", arch);
let profile = var("PROFILE").unwrap();
let to = format!("{}/{}/lib{}.a", OUTLINE_PATH, profile, name);
println!("cargo:rerun-if-changed={}", to);
#[cfg(not(feature = "cc"))]
{
let _ = asm_name;
println!("cargo:rustc-link-search={}/{}", OUTLINE_PATH, profile);
println!("cargo:rustc-link-lib=static={}", name);
}
#[cfg(feature = "cc")]
{
let out_dir = var("OUT_DIR").unwrap();
Build::new().file(&asm_name).compile(&name);
println!("cargo:rerun-if-changed={}", asm_name);
if std::fs::metadata(".git").is_ok() {
let from = format!("{}/lib{}.a", out_dir, name);
let prev_metadata = std::fs::metadata(&to);
std::fs::copy(&from, &to).unwrap();
assert!(
prev_metadata.is_ok(),
"{} didn't previously exist; please inspect the new file and `git add` it",
to
);
assert!(
std::process::Command::new("git")
.arg("diff")
.arg("--quiet")
.arg(&to)
.status()
.unwrap()
.success(),
"{} changed; please inspect the change and `git commit` it",
to
);
}
}
}
fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
}
fn use_feature(feature: &str) {
println!("cargo:rustc-cfg={}", feature);
}
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}
fn can_compile(code: &str) -> bool {
use std::process::Stdio;
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") .arg("--emit=metadata") .arg("--out-dir")
.arg(out_dir) .arg("-") .stdin(Stdio::piped()) .stderr(Stdio::null())
.spawn()
.unwrap();
writeln!(child.stdin.take().unwrap(), "{}", code).unwrap();
child.wait().unwrap().success()
}