use std::env::var;
use rustflags::Flag;
fn check_supported_archs() {
let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
if !["x86_64", "x86", "aarch64", "arm"].contains(&arch.as_str()) {
println!("cargo::error=closure-ffi does not support the '{arch}' target architecture.");
}
}
fn no_safe_jit_warn() {
if var("CARGO_FEATURE_SAFE_JIT").is_ok() {
return;
}
if var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86" && var("CARGO_CFG_WINDOWS").is_err() {
println!(
"cargo::error=closure-ffi requires the 'safe_jit' feature to be enabled \
on non-Windows x86 targets. It *will* produce incorrect code without it."
);
return;
}
if var("CARGO_FEATURE_NO_SAFE_JIT").is_ok() {
return;
}
println!(
"cargo::error=closure-ffi is being built without the 'safe_jit' feature. \
This may lead to incorrect code being generated. Consider enabling it if possible, \
even on no_std targets. If you still want it off, enable the 'no_safe_jit' feature \
to get rid of this error."
);
}
fn set_thumb_mode_cfg() {
println!("cargo::rustc-check-cfg=cfg(thumb_mode)");
let arch = var("CARGO_CFG_TARGET_ARCH").unwrap();
if arch != "arm" {
return;
}
if var("CARGO_CFG_TARGET_FEATURE")
.map(|f| f.contains("thumb-mode"))
.unwrap_or_else(|_| {
let target = var("TARGET").unwrap();
if target.to_lowercase().ends_with(".json") {
println!(
"cargo::error=Custom ARM target file detected. If using the Thumb ISA by \
default, use nightly and add 'thumb-mode' to the feature list."
);
false
}
else {
target.starts_with("thumb")
}
})
{
println!("cargo::rustc-cfg=thumb_mode")
}
}
fn check_coverage_supported() {
if var("CARGO_FEATURE_COVERAGE").is_ok() {
return;
}
if rustflags::from_env()
.any(|f| matches!(f, Flag::Codegen { opt, .. } if opt == "instrument-coverage"))
{
println!(
"cargo::error=closure-ffi requires a nightly compiler and the 'coverage' crate feature \
to be enabled for '-C instrument-coverage' support."
);
}
}
fn main() {
check_supported_archs();
no_safe_jit_warn();
check_coverage_supported();
set_thumb_mode_cfg();
}