use std::env;
pub fn native() -> &'static str {
env!("NATIVE_ARCH")
}
pub fn native_arch() -> &'static str {
match native()
.split("-")
.next()
.expect("Target triple has unexpected format")
{
"x86_64" => "x86_64",
"aarch64" => "aarch64",
"i686" => "x86",
_ => panic!("This test should be gated on cross_compile::disabled."),
}
}
pub fn alternate() -> &'static str {
try_alternate().expect("This test should be gated on cross_compile::disabled.")
}
pub(crate) fn try_alternate() -> Option<&'static str> {
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
Some("x86_64-apple-darwin")
} else if cfg!(target_os = "macos") {
Some("x86_64-apple-ios")
} else if cfg!(target_os = "linux") {
Some("i686-unknown-linux-gnu")
} else if cfg!(all(target_os = "windows", target_env = "msvc")) {
Some("i686-pc-windows-msvc")
} else if cfg!(all(target_os = "windows", target_env = "gnu")) {
Some("i686-pc-windows-gnu")
} else {
None
}
}
pub fn alternate_arch() -> &'static str {
if cfg!(target_os = "macos") {
"x86_64"
} else {
"x86"
}
}
pub fn unused() -> &'static str {
"wasm32-unknown-unknown"
}
pub fn requires_target_installed(target: &str) -> bool {
let has_target = std::process::Command::new("rustup")
.args(["target", "list", "--installed"])
.output()
.ok()
.map(|output| {
String::from_utf8(output.stdout)
.map(|stdout| stdout.contains(target))
.unwrap_or_default()
})
.unwrap_or_default();
if !has_target {
let msg =
format!("to run this test, run `rustup target add {target} --toolchain <toolchain>`",);
if cargo_util::is_ci() {
panic!("{msg}");
} else {
eprintln!("{msg}");
}
}
has_target
}