use crate::{Error, Triple};
use std::{borrow::Cow, collections::BTreeSet, ops::Deref};
include!(concat!(env!("OUT_DIR"), "/build_target.rs"));
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[must_use]
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 new_strict(
triple_str: impl Into<Cow<'static, str>>,
target_features: TargetFeatures,
) -> Result<Self, Error> {
let triple = Triple::new_strict(triple_str.into()).map_err(Error::UnknownPlatformTriple)?;
Ok(Self::from_triple(triple, target_features))
}
pub fn from_rustc_version_verbose(
output: impl AsRef<[u8]>,
target_features: TargetFeatures,
) -> Result<Self, Error> {
let triple = Triple::from_rustc_version_verbose(output.as_ref())?;
Ok(Self::from_triple(triple, target_features))
}
#[deprecated(
since = "3.4.0",
note = "this method has been renamed to `build_target`"
)]
#[inline]
pub fn current() -> Result<Self, Error> {
Self::build_target()
}
pub fn build_target() -> Result<Self, Error> {
let triple = Triple::new(BUILD_TARGET).map_err(Error::UnknownPlatformTriple)?;
let target_features = TargetFeatures::features(BUILD_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(),
}
}
#[cfg(feature = "custom")]
pub fn new_custom(
triple_str: impl Into<Cow<'static, str>>,
json: &str,
target_features: TargetFeatures,
) -> Result<Self, Error> {
let triple = Triple::new_custom(triple_str, json).map_err(Error::CustomPlatformCreate)?;
Ok(Self {
triple,
target_features,
flags: BTreeSet::new(),
})
}
#[cfg(feature = "custom-cfg")]
pub fn new_custom_cfg(
triple_str: impl Into<Cow<'static, str>>,
cfg_text: &str,
target_features: TargetFeatures,
) -> Result<Self, Error> {
let triple =
Triple::new_custom_cfg(triple_str, cfg_text).map_err(Error::CustomPlatformCreate)?;
Ok(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 ExactSizeIterator<Item = &str> {
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 is_standard(&self) -> bool {
self.triple.is_standard()
}
pub fn is_builtin(&self) -> bool {
self.triple.is_builtin()
}
pub fn is_heuristic(&self) -> bool {
self.triple.is_heuristic()
}
pub fn is_custom(&self) -> bool {
self.triple.is_custom()
}
pub fn triple(&self) -> &Triple {
&self.triple
}
pub fn target_features(&self) -> &TargetFeatures {
&self.target_features
}
#[cfg(feature = "summaries")]
pub(crate) fn custom_json(&self) -> Option<&str> {
self.triple.custom_json()
}
#[cfg(feature = "summaries")]
pub(crate) fn custom_cfg_text(&self) -> Option<&str> {
self.triple.custom_cfg_text()
}
}
#[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),
}
}
}
#[cfg(all(test, feature = "custom-cfg"))]
mod custom_cfg_tests {
use crate::TargetSpecExpression;
use super::*;
const LINUX_CFG: &str = "\
panic=\"unwind\"\n\
target_arch=\"x86_64\"\n\
target_endian=\"little\"\n\
target_env=\"gnu\"\n\
target_family=\"unix\"\n\
target_feature=\"fxsr\"\n\
target_feature=\"sse\"\n\
target_feature=\"sse2\"\n\
target_os=\"linux\"\n\
target_pointer_width=\"64\"\n\
target_vendor=\"unknown\"\n";
#[test]
fn new_custom_cfg_basic() {
let platform =
Platform::new_custom_cfg("my-custom-linux", LINUX_CFG, TargetFeatures::Unknown)
.expect("parsed successfully");
assert!(platform.is_custom());
assert!(!platform.is_standard());
assert!(!platform.is_builtin());
assert_eq!(platform.triple_str(), "my-custom-linux");
assert_eq!(*platform.target_features(), TargetFeatures::Unknown);
}
#[test]
fn new_custom_cfg_evaluates_expressions() {
let platform =
Platform::new_custom_cfg("my-custom-linux", LINUX_CFG, TargetFeatures::Unknown)
.expect("parsed successfully");
let spec =
TargetSpecExpression::new("cfg(target_os = \"linux\")").expect("valid expression");
assert_eq!(
spec.eval(&platform),
Some(true),
"target_os = linux matches",
);
let spec =
TargetSpecExpression::new("cfg(target_os = \"windows\")").expect("valid expression");
assert_eq!(
spec.eval(&platform),
Some(false),
"target_os = windows does not match",
);
let spec = TargetSpecExpression::new("cfg(unix)").expect("valid expression");
assert_eq!(spec.eval(&platform), Some(true), "unix family matches",);
}
#[test]
fn new_custom_cfg_with_features() {
let platform = Platform::new_custom_cfg("my-custom-linux", LINUX_CFG, TargetFeatures::All)
.expect("parsed successfully");
assert_eq!(
platform.target_features().matches("avx512"),
Some(true),
"All matches any feature",
);
assert_eq!(*platform.target_features(), TargetFeatures::All);
}
#[test]
fn new_custom_cfg_with_no_features() {
let platform =
Platform::new_custom_cfg("my-custom-linux", LINUX_CFG, TargetFeatures::none())
.expect("parsed successfully");
assert_eq!(
platform.target_features().matches("sse2"),
Some(false),
"sse2 not matched with empty features",
);
}
}