use crate::ghc::Platform;
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Arch {
X86_64,
Aarch64,
}
impl Arch {
pub fn key(self) -> &'static str {
match self {
Self::X86_64 => "A_64",
Self::Aarch64 => "A_ARM64",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct LinuxDistro {
pub family: Option<&'static str>,
pub version: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostOs {
Darwin,
Windows,
Linux(LinuxDistro),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Host {
pub arch: Arch,
pub os: HostOs,
}
impl Host {
pub fn detect(platform: Platform) -> Self {
let distro = matches!(platform, Platform::X86_64Linux | Platform::Aarch64Linux)
.then(detect_linux_distro)
.unwrap_or_default();
Self::with_distro(platform, distro)
}
pub fn with_distro(platform: Platform, distro: LinuxDistro) -> Self {
let (arch, os) = match platform {
Platform::X86_64Linux => (Arch::X86_64, HostOs::Linux(distro)),
Platform::Aarch64Linux => (Arch::Aarch64, HostOs::Linux(distro)),
Platform::X86_64Darwin => (Arch::X86_64, HostOs::Darwin),
Platform::Aarch64Darwin => (Arch::Aarch64, HostOs::Darwin),
Platform::X86_64Windows => (Arch::X86_64, HostOs::Windows),
};
Self { arch, os }
}
}
const DISTRO_IDS: &[(&str, &str)] = &[
("debian", "Debian"),
("ubuntu", "Ubuntu"),
("linuxmint", "Mint"),
("fedora", "Fedora"),
("centos", "CentOS"),
("rhel", "RedHat"),
("alpine", "Alpine"),
("amzn", "AmazonLinux"),
("rocky", "Rocky"),
("void", "Void"),
("gentoo", "Gentoo"),
("exherbo", "Exherbo"),
("opensuse", "OpenSUSE"),
("opensuse-leap", "OpenSUSE"),
("opensuse-tumbleweed", "OpenSUSE"),
("sles", "OpenSUSE"),
("suse", "OpenSUSE"),
];
fn family_for_id(id: &str) -> Option<&'static str> {
let id = id.to_ascii_lowercase();
DISTRO_IDS
.iter()
.find(|(known, _)| *known == id)
.map(|(_, family)| *family)
}
pub fn parse_os_release(contents: &str) -> HashMap<String, String> {
contents
.lines()
.filter_map(|line| {
let line = line.trim();
if line.starts_with('#') {
return None;
}
let (key, value) = line.split_once('=')?;
let value = value.trim();
let value = value
.strip_prefix('"')
.and_then(|v| v.strip_suffix('"'))
.or_else(|| value.strip_prefix('\'').and_then(|v| v.strip_suffix('\'')))
.unwrap_or(value);
Some((key.trim().to_string(), value.to_string()))
})
.collect()
}
pub fn linux_distro_from(os_release: Option<&str>, musl: bool) -> LinuxDistro {
let fields = os_release.map(parse_os_release).unwrap_or_default();
let version = fields.get("VERSION_ID").filter(|v| !v.is_empty()).cloned();
if let Some(family) = fields.get("ID").and_then(|id| family_for_id(id)) {
return LinuxDistro {
family: Some(family),
version,
};
}
let like = fields
.get("ID_LIKE")
.into_iter()
.flat_map(|v| v.split_whitespace())
.find_map(family_for_id);
if let Some(family) = like
&& (!musl || family == "Alpine")
{
return LinuxDistro {
family: Some(family),
version: None,
};
}
if musl {
return LinuxDistro {
family: Some("Alpine"),
version: None,
};
}
LinuxDistro::default()
}
pub fn detect_linux_distro() -> LinuxDistro {
let os_release = ["/etc/os-release", "/usr/lib/os-release"]
.iter()
.find_map(|p| std::fs::read_to_string(p).ok());
linux_distro_from(os_release.as_deref(), is_musl_system(Path::new("/")))
}
fn is_musl_system(root: &Path) -> bool {
let has_loader = |dirs: &[&str], prefix: &str| {
dirs.iter().any(|dir| {
std::fs::read_dir(root.join(dir))
.map(|entries| {
entries
.flatten()
.any(|e| e.file_name().to_string_lossy().starts_with(prefix))
})
.unwrap_or(false)
})
};
has_loader(&["lib"], "ld-musl-") && !has_loader(&["lib", "lib64"], "ld-linux")
}
#[cfg(test)]
mod tests {
use super::*;
const DEBIAN_12: &str = r#"PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
"#;
const UBUNTU_2404: &str = r#"PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
ID=ubuntu
ID_LIKE=debian
UBUNTU_CODENAME=noble
"#;
const FEDORA_40: &str = r#"NAME="Fedora Linux"
VERSION="40 (Workstation Edition)"
ID=fedora
VERSION_ID=40
PLATFORM_ID="platform:f40"
"#;
const ARCH: &str = r#"NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
"#;
const ALPINE_320: &str = r#"NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.20.3
PRETTY_NAME="Alpine Linux v3.20"
"#;
const POP_OS: &str = r#"NAME="Pop!_OS"
VERSION="22.04 LTS"
ID=pop
ID_LIKE="ubuntu debian"
VERSION_ID="22.04"
"#;
#[test]
fn test_parse_os_release() {
let fields = parse_os_release(UBUNTU_2404);
assert_eq!(fields["ID"], "ubuntu");
assert_eq!(fields["VERSION_ID"], "24.04");
assert_eq!(fields["PRETTY_NAME"], "Ubuntu 24.04.1 LTS");
}
#[test]
fn test_known_distributions_keep_their_version() {
let debian = linux_distro_from(Some(DEBIAN_12), false);
assert_eq!(debian.family, Some("Debian"));
assert_eq!(debian.version.as_deref(), Some("12"));
let ubuntu = linux_distro_from(Some(UBUNTU_2404), false);
assert_eq!(ubuntu.family, Some("Ubuntu"));
assert_eq!(ubuntu.version.as_deref(), Some("24.04"));
let fedora = linux_distro_from(Some(FEDORA_40), false);
assert_eq!(fedora.family, Some("Fedora"));
assert_eq!(fedora.version.as_deref(), Some("40"));
let alpine = linux_distro_from(Some(ALPINE_320), true);
assert_eq!(alpine.family, Some("Alpine"));
assert_eq!(alpine.version.as_deref(), Some("3.20.3"));
}
#[test]
fn test_unknown_distributions() {
assert_eq!(linux_distro_from(Some(ARCH), false), LinuxDistro::default());
assert_eq!(linux_distro_from(None, false), LinuxDistro::default());
assert_eq!(
linux_distro_from(Some("garbage without equals"), false),
LinuxDistro::default()
);
}
#[test]
fn test_id_like_drops_the_version() {
let pop = linux_distro_from(Some(POP_OS), false);
assert_eq!(pop.family, Some("Ubuntu"));
assert_eq!(pop.version, None);
}
#[test]
fn test_musl_means_alpine() {
let musl_arch = linux_distro_from(Some(ARCH), true);
assert_eq!(musl_arch.family, Some("Alpine"));
assert_eq!(musl_arch.version, None);
let musl_unknown = linux_distro_from(None, true);
assert_eq!(musl_unknown.family, Some("Alpine"));
}
#[test]
fn test_is_musl_system() {
let root = tempfile::tempdir().unwrap();
let lib = root.path().join("lib");
std::fs::create_dir(&lib).unwrap();
assert!(!is_musl_system(root.path()));
std::fs::write(lib.join("ld-musl-x86_64.so.1"), "").unwrap();
assert!(is_musl_system(root.path()));
let lib64 = root.path().join("lib64");
std::fs::create_dir(&lib64).unwrap();
std::fs::write(lib64.join("ld-linux-x86-64.so.2"), "").unwrap();
assert!(!is_musl_system(root.path()));
}
#[test]
fn test_host_for_platforms() {
let host = Host::with_distro(Platform::Aarch64Darwin, LinuxDistro::default());
assert_eq!(host.arch, Arch::Aarch64);
assert_eq!(host.os, HostOs::Darwin);
let host = Host::with_distro(Platform::X86_64Windows, LinuxDistro::default());
assert_eq!(host.os, HostOs::Windows);
let distro = linux_distro_from(Some(DEBIAN_12), false);
let host = Host::with_distro(Platform::X86_64Linux, distro.clone());
assert_eq!(host.arch.key(), "A_64");
assert_eq!(host.os, HostOs::Linux(distro));
}
}