Implement DNF-based #[cfg] introspection

This commit is contained in:
Jonas Schievink 2020-10-21 19:54:04 +02:00
parent 2bc4c1ff31
commit 68b17986c7
5 changed files with 622 additions and 2 deletions

View file

@ -2,12 +2,12 @@
//!
//! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation
use std::slice::Iter as SliceIter;
use std::{fmt, slice::Iter as SliceIter};
use tt::SmolStr;
/// A simple configuration value passed in from the outside.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum CfgAtom {
/// eg. `#[cfg(test)]`
Flag(SmolStr),
@ -18,6 +18,37 @@ pub enum CfgAtom {
KeyValue { key: SmolStr, value: SmolStr },
}
impl CfgAtom {
/// Returns `true` when the atom comes from the target specification.
///
/// If this returns `true`, then changing this atom requires changing the compilation target. If
/// it returns `false`, the atom might come from a build script or the build system.
pub fn is_target_defined(&self) -> bool {
match self {
CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"),
CfgAtom::KeyValue { key, value: _ } => matches!(
&**key,
"target_arch"
| "target_os"
| "target_env"
| "target_family"
| "target_endian"
| "target_pointer_width"
| "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature`
),
}
}
}
impl fmt::Display for CfgAtom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CfgAtom::Flag(name) => write!(f, "{}", name),
CfgAtom::KeyValue { key, value } => write!(f, "{} = \"{}\"", key, value),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CfgExpr {
Invalid,