pub fn map_target(triple: &str) -> (String, String) {
let os = if triple.contains("android") {
"android"
} else if triple.contains("ios") {
"ios"
} else if triple.contains("linux") {
"linux"
} else if triple.contains("darwin") || triple.contains("apple") {
"darwin"
} else if triple.contains("windows") {
"windows"
} else if triple.contains("freebsd") {
"freebsd"
} else if triple.contains("netbsd") {
"netbsd"
} else if triple.contains("openbsd") {
"openbsd"
} else if triple.contains("aix") {
"aix"
} else {
"unknown"
};
let arch = if triple == "darwin-universal" {
"all"
} else if triple.contains("x86_64") || triple.contains("amd64") {
"amd64"
} else if triple.contains("aarch64") || triple.contains("arm64") {
"arm64"
} else {
let first = triple.split('-').next().unwrap_or("unknown");
match first {
"i686" | "i386" | "i586" => "386",
"armv7" | "armv7l" => "armv7",
"armv6" | "armv6l" | "arm" => "armv6",
"s390x" => "s390x",
"ppc64le" | "powerpc64le" => "ppc64le",
"ppc64" | "powerpc64" => "ppc64",
"riscv64gc" | "riscv64" => "riscv64",
"mips64" | "mips64el" => first,
"mips" | "mipsel" => first,
"loongarch64" => "loong64",
other => other,
}
};
(os.to_string(), arch.to_string())
}
pub fn is_darwin(triple: &str) -> bool {
triple.contains("darwin") || triple.contains("apple")
}
pub fn is_linux(triple: &str) -> bool {
triple.contains("linux") && !triple.contains("android")
}
pub fn is_windows(triple: &str) -> bool {
triple.contains("windows")
}
pub fn is_ios(triple: &str) -> bool {
triple.contains("ios")
}
pub fn is_aix(triple: &str) -> bool {
triple.contains("aix")
}
pub fn is_nfpm_target(triple: &str) -> bool {
is_linux(triple) || is_ios(triple) || triple.contains("android") || is_aix(triple)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_target_to_os_arch() {
let (os, arch) = map_target("x86_64-unknown-linux-gnu");
assert_eq!(os, "linux");
assert_eq!(arch, "amd64");
}
#[test]
fn test_darwin_arm64() {
let (os, arch) = map_target("aarch64-apple-darwin");
assert_eq!(os, "darwin");
assert_eq!(arch, "arm64");
}
#[test]
fn test_windows() {
let (os, arch) = map_target("x86_64-pc-windows-msvc");
assert_eq!(os, "windows");
assert_eq!(arch, "amd64");
}
#[test]
fn test_riscv64() {
let (os, arch) = map_target("riscv64gc-unknown-linux-gnu");
assert_eq!(os, "linux");
assert_eq!(arch, "riscv64");
}
#[test]
fn test_i686() {
let (os, arch) = map_target("i686-unknown-linux-gnu");
assert_eq!(os, "linux");
assert_eq!(arch, "386");
}
#[test]
fn test_armv7() {
let (os, arch) = map_target("armv7-unknown-linux-gnueabihf");
assert_eq!(os, "linux");
assert_eq!(arch, "armv7");
}
#[test]
fn test_freebsd() {
let (os, arch) = map_target("x86_64-unknown-freebsd");
assert_eq!(os, "freebsd");
assert_eq!(arch, "amd64");
}
#[test]
fn test_s390x() {
let (os, arch) = map_target("s390x-unknown-linux-gnu");
assert_eq!(os, "linux");
assert_eq!(arch, "s390x");
}
#[test]
fn test_ppc64le() {
let (os, arch) = map_target("powerpc64le-unknown-linux-gnu");
assert_eq!(os, "linux");
assert_eq!(arch, "ppc64le");
}
#[test]
fn test_android() {
let (os, arch) = map_target("aarch64-linux-android");
assert_eq!(os, "android");
assert_eq!(arch, "arm64");
}
#[test]
fn test_linux_musl() {
let (os, arch) = map_target("aarch64-unknown-linux-musl");
assert_eq!(os, "linux");
assert_eq!(arch, "arm64");
}
#[test]
fn test_unknown_target() {
let (os, arch) = map_target("wasm32-unknown-unknown");
assert_eq!(os, "unknown");
assert_eq!(arch, "wasm32");
}
#[test]
fn test_ios() {
let (os, arch) = map_target("aarch64-apple-ios");
assert_eq!(os, "ios");
assert_eq!(arch, "arm64");
}
#[test]
fn test_aix() {
let (os, arch) = map_target("powerpc64-ibm-aix");
assert_eq!(os, "aix");
assert_eq!(arch, "ppc64");
}
#[test]
fn test_is_nfpm_target() {
assert!(is_nfpm_target("x86_64-unknown-linux-gnu"));
assert!(is_nfpm_target("aarch64-linux-android"));
assert!(is_nfpm_target("aarch64-apple-ios"));
assert!(is_nfpm_target("powerpc64-ibm-aix"));
assert!(!is_nfpm_target("x86_64-apple-darwin"));
assert!(!is_nfpm_target("x86_64-pc-windows-msvc"));
assert!(!is_nfpm_target("x86_64-unknown-freebsd"));
}
}