pub const ANN_PLATFORM: &str = "eu.pulseengine.platform";
pub const ANN_LIBC: &str = "eu.pulseengine.platform.libc";
pub const ANN_TARGET: &str = "eu.pulseengine.target";
pub fn entry_target(entry: &crate::manifest::ManifestEntry) -> Option<&str> {
entry.annotations.get(ANN_TARGET).map(String::as_str)
}
pub fn host_platform() -> String {
let arch = std::env::consts::ARCH;
match std::env::consts::OS {
"macos" => format!("{arch}-apple-darwin"),
"linux" => format!("{arch}-unknown-linux-gnu"),
"windows" => format!("{arch}-pc-windows-msvc"),
other => format!("{arch}-{other}"),
}
}
pub fn entry_matches(entry_platform: Option<&str>, platform: &str) -> bool {
match entry_platform {
None => true,
Some(p) if p.starts_with("wasm32") => true,
Some(p) => p == platform,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unstamped_entries_are_platform_independent_and_stamped_ones_are_exact() {
assert!(entry_matches(None, "aarch64-apple-darwin"));
assert!(entry_matches(
Some("aarch64-apple-darwin"),
"aarch64-apple-darwin"
));
assert!(!entry_matches(
Some("x86_64-unknown-linux-gnu"),
"aarch64-apple-darwin"
));
}
#[test]
fn wasm32_entries_are_portable_to_every_host() {
assert!(entry_matches(Some("wasm32-wasip2"), "aarch64-apple-darwin"));
assert!(entry_matches(
Some("wasm32-wasip2"),
"x86_64-unknown-linux-gnu"
));
assert!(entry_matches(Some("wasm32-unknown-unknown"), "anything"));
}
#[test]
fn the_host_platform_is_a_target_triple() {
let host = host_platform();
assert!(
host.split('-').count() >= 2 && host.contains(std::env::consts::ARCH),
"host platform should be triple-shaped: {host}"
);
}
}