use crate::{Error, Triple};
use std::{borrow::Cow, collections::BTreeSet, ops::Deref};
include!(concat!(env!("OUT_DIR"), "/current_platform.rs"));
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Platform {
triple: Triple,
target_features: TargetFeatures,
flags: BTreeSet<Cow<'static, str>>,
}
impl Platform {
pub fn new(
triple_str: impl Into<Cow<'static, str>>,
target_features: TargetFeatures,
) -> Result<Self, Error> {
let triple = Triple::new(triple_str.into()).map_err(Error::UnknownPlatformTriple)?;
Ok(Self::from_triple(triple, target_features))
}
pub fn current() -> Result<Self, Error> {
let triple = Triple::new(CURRENT_TARGET).map_err(Error::UnknownPlatformTriple)?;
let target_features = TargetFeatures::features(CURRENT_TARGET_FEATURES.iter().copied());
Ok(Self {
triple,
target_features,
flags: BTreeSet::new(),
})
}
pub fn from_triple(triple: Triple, target_features: TargetFeatures) -> Self {
Self {
triple,
target_features,
flags: BTreeSet::new(),
}
}
pub fn add_flags(&mut self, flags: impl IntoIterator<Item = impl Into<Cow<'static, str>>>) {
self.flags.extend(flags.into_iter().map(|s| s.into()));
}
pub fn triple_str(&self) -> &str {
self.triple.as_str()
}
pub fn flags(&self) -> impl Iterator<Item = &str> + ExactSizeIterator {
self.flags.iter().map(|flag| flag.deref())
}
pub fn has_flag(&self, flag: impl AsRef<str>) -> bool {
self.flags.contains(flag.as_ref())
}
pub fn triple(&self) -> &Triple {
&self.triple
}
pub fn target_features(&self) -> &TargetFeatures {
&self.target_features
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum TargetFeatures {
Unknown,
Features(BTreeSet<Cow<'static, str>>),
All,
}
impl TargetFeatures {
pub fn features(features: impl IntoIterator<Item = impl Into<Cow<'static, str>>>) -> Self {
TargetFeatures::Features(features.into_iter().map(|s| s.into()).collect())
}
pub fn none() -> Self {
TargetFeatures::Features(BTreeSet::new())
}
pub fn matches(&self, feature: &str) -> Option<bool> {
match self {
TargetFeatures::Unknown => None,
TargetFeatures::Features(features) => Some(features.contains(feature)),
TargetFeatures::All => Some(true),
}
}
}