2021-06-12 12:16:22 +00:00
|
|
|
//! cfg defines conditional compiling options, `cfg` attribute parser and evaluator
|
2019-11-12 12:41:02 +00:00
|
|
|
|
|
|
|
mod cfg_expr;
|
2020-10-21 17:54:04 +00:00
|
|
|
mod dnf;
|
2020-10-23 10:14:58 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-10-21 17:54:04 +00:00
|
|
|
|
2023-11-21 13:05:31 +00:00
|
|
|
use std::fmt;
|
2019-11-12 12:41:02 +00:00
|
|
|
|
2019-10-02 17:20:08 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2024-07-16 08:36:59 +00:00
|
|
|
use intern::Symbol;
|
|
|
|
|
2020-10-21 11:57:12 +00:00
|
|
|
pub use cfg_expr::{CfgAtom, CfgExpr};
|
2020-10-21 17:54:04 +00:00
|
|
|
pub use dnf::DnfExpr;
|
2019-09-29 22:52:15 +00:00
|
|
|
|
2021-05-07 20:35:43 +00:00
|
|
|
/// Configuration options used for conditional compilation on items with `cfg` attributes.
|
2019-10-02 17:20:08 +00:00
|
|
|
/// We have two kind of options in different namespaces: atomic options like `unix`, and
|
|
|
|
/// key-value options like `target_arch="x86"`.
|
|
|
|
///
|
|
|
|
/// Note that for key-value options, one key can have multiple values (but not none).
|
|
|
|
/// `feature` is an example. We have both `feature="foo"` and `feature="bar"` if features
|
|
|
|
/// `foo` and `bar` are both enabled. And here, we store key-value options as a set of tuple
|
|
|
|
/// of key and value in `key_values`.
|
|
|
|
///
|
2021-06-14 04:57:10 +00:00
|
|
|
/// See: <https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options>
|
internal: add simple smoke test for project model
Our project model code is rather complicated -- the logic for lowering
from `cargo metadata` to `CrateGraph` is fiddly and special-case. So
far, we survived without testing this at all, but this increasingly
seems like a poor option.
So this PR introduces a simple tests just to detect the most obvious
failures. The idea here is that, although we rely on external processes
(cargo & rustc), we are actually using their stable interfaces, so we
might just mock out the outputs.
Long term, I would like to try to virtualize IO here, so as to do such
mocking in a more principled way, but lets start simple.
Should we forgo the mocking and just call `cargo metadata` directly
perhaps? Touch question -- I personally feel that fast, in-process tests
are more important in this case than any extra assurance we get from
running the real thing.
Super-long term, we would probably want to extend our heavy tests to
cover more use-cases, but we should figure a way to do that without
slowing the tests down for everyone.
Perhaps we need two-tiered bors system, where we pull from `master` into
`release` branch only when an additional set of tests passes?
2021-07-20 12:38:20 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Default)]
|
2019-09-29 22:52:15 +00:00
|
|
|
pub struct CfgOptions {
|
2020-10-21 11:57:12 +00:00
|
|
|
enabled: FxHashSet<CfgAtom>,
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
internal: add simple smoke test for project model
Our project model code is rather complicated -- the logic for lowering
from `cargo metadata` to `CrateGraph` is fiddly and special-case. So
far, we survived without testing this at all, but this increasingly
seems like a poor option.
So this PR introduces a simple tests just to detect the most obvious
failures. The idea here is that, although we rely on external processes
(cargo & rustc), we are actually using their stable interfaces, so we
might just mock out the outputs.
Long term, I would like to try to virtualize IO here, so as to do such
mocking in a more principled way, but lets start simple.
Should we forgo the mocking and just call `cargo metadata` directly
perhaps? Touch question -- I personally feel that fast, in-process tests
are more important in this case than any extra assurance we get from
running the real thing.
Super-long term, we would probably want to extend our heavy tests to
cover more use-cases, but we should figure a way to do that without
slowing the tests down for everyone.
Perhaps we need two-tiered bors system, where we pull from `master` into
`release` branch only when an additional set of tests passes?
2021-07-20 12:38:20 +00:00
|
|
|
impl fmt::Debug for CfgOptions {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut items = self
|
|
|
|
.enabled
|
|
|
|
.iter()
|
|
|
|
.map(|atom| match atom {
|
|
|
|
CfgAtom::Flag(it) => it.to_string(),
|
2022-12-23 18:42:58 +00:00
|
|
|
CfgAtom::KeyValue { key, value } => format!("{key}={value}"),
|
internal: add simple smoke test for project model
Our project model code is rather complicated -- the logic for lowering
from `cargo metadata` to `CrateGraph` is fiddly and special-case. So
far, we survived without testing this at all, but this increasingly
seems like a poor option.
So this PR introduces a simple tests just to detect the most obvious
failures. The idea here is that, although we rely on external processes
(cargo & rustc), we are actually using their stable interfaces, so we
might just mock out the outputs.
Long term, I would like to try to virtualize IO here, so as to do such
mocking in a more principled way, but lets start simple.
Should we forgo the mocking and just call `cargo metadata` directly
perhaps? Touch question -- I personally feel that fast, in-process tests
are more important in this case than any extra assurance we get from
running the real thing.
Super-long term, we would probably want to extend our heavy tests to
cover more use-cases, but we should figure a way to do that without
slowing the tests down for everyone.
Perhaps we need two-tiered bors system, where we pull from `master` into
`release` branch only when an additional set of tests passes?
2021-07-20 12:38:20 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
items.sort();
|
|
|
|
f.debug_tuple("CfgOptions").field(&items).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 22:52:15 +00:00
|
|
|
impl CfgOptions {
|
|
|
|
pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
|
2020-10-21 11:57:12 +00:00
|
|
|
cfg.fold(&|atom| self.enabled.contains(atom))
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:36:59 +00:00
|
|
|
pub fn insert_atom(&mut self, key: Symbol) {
|
2020-10-21 11:57:12 +00:00
|
|
|
self.enabled.insert(CfgAtom::Flag(key));
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:36:59 +00:00
|
|
|
pub fn insert_key_value(&mut self, key: Symbol, value: Symbol) {
|
2020-10-21 11:57:12 +00:00
|
|
|
self.enabled.insert(CfgAtom::KeyValue { key, value });
|
2019-10-02 18:02:53 +00:00
|
|
|
}
|
2020-06-12 17:08:51 +00:00
|
|
|
|
2020-10-21 17:54:04 +00:00
|
|
|
pub fn apply_diff(&mut self, diff: CfgDiff) {
|
|
|
|
for atom in diff.enable {
|
|
|
|
self.enabled.insert(atom);
|
|
|
|
}
|
|
|
|
|
|
|
|
for atom in diff.disable {
|
|
|
|
self.enabled.remove(&atom);
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 13:52:19 +00:00
|
|
|
|
2024-07-16 08:36:59 +00:00
|
|
|
pub fn get_cfg_keys(&self) -> impl Iterator<Item = &Symbol> {
|
2023-07-06 14:03:17 +00:00
|
|
|
self.enabled.iter().map(|it| match it {
|
2021-11-05 11:30:39 +00:00
|
|
|
CfgAtom::Flag(key) => key,
|
|
|
|
CfgAtom::KeyValue { key, .. } => key,
|
|
|
|
})
|
2021-05-30 13:52:19 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 08:36:59 +00:00
|
|
|
pub fn get_cfg_values<'a>(&'a self, cfg_key: &'a str) -> impl Iterator<Item = &'a Symbol> + 'a {
|
2023-07-06 14:03:17 +00:00
|
|
|
self.enabled.iter().filter_map(move |it| match it {
|
2024-07-16 08:36:59 +00:00
|
|
|
CfgAtom::KeyValue { key, value } if cfg_key == key.as_str() => Some(value),
|
2021-11-05 11:30:39 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-05-30 13:52:19 +00:00
|
|
|
}
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:57:24 +00:00
|
|
|
impl Extend<CfgAtom> for CfgOptions {
|
|
|
|
fn extend<T: IntoIterator<Item = CfgAtom>>(&mut self, iter: T) {
|
|
|
|
iter.into_iter().for_each(|cfg_flag| _ = self.enabled.insert(cfg_flag));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for CfgOptions {
|
|
|
|
type Item = <FxHashSet<CfgAtom> as IntoIterator>::Item;
|
|
|
|
|
|
|
|
type IntoIter = <FxHashSet<CfgAtom> as IntoIterator>::IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
<FxHashSet<CfgAtom> as IntoIterator>::into_iter(self.enabled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a CfgOptions {
|
|
|
|
type Item = <&'a FxHashSet<CfgAtom> as IntoIterator>::Item;
|
|
|
|
|
|
|
|
type IntoIter = <&'a FxHashSet<CfgAtom> as IntoIterator>::IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
<&FxHashSet<CfgAtom> as IntoIterator>::into_iter(&self.enabled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 20:12:22 +00:00
|
|
|
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
2020-10-21 17:54:04 +00:00
|
|
|
pub struct CfgDiff {
|
|
|
|
// Invariants: No duplicates, no atom that's both in `enable` and `disable`.
|
2023-10-15 15:32:12 +00:00
|
|
|
enable: Vec<CfgAtom>,
|
|
|
|
disable: Vec<CfgAtom>,
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CfgDiff {
|
2021-06-12 12:16:22 +00:00
|
|
|
/// Create a new CfgDiff. Will return None if the same item appears more than once in the set
|
|
|
|
/// of both.
|
|
|
|
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
|
|
|
|
let mut occupied = FxHashSet::default();
|
2024-01-25 09:23:00 +00:00
|
|
|
if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) {
|
2024-01-24 21:32:46 +00:00
|
|
|
// was present
|
|
|
|
return None;
|
2021-06-12 12:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(CfgDiff { enable, disable })
|
|
|
|
}
|
|
|
|
|
2020-10-21 17:54:04 +00:00
|
|
|
/// Returns the total number of atoms changed by this diff.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.enable.len() + self.disable.len()
|
|
|
|
}
|
2021-10-14 18:57:21 +00:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CfgDiff {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
if !self.enable.is_empty() {
|
|
|
|
f.write_str("enable ")?;
|
|
|
|
for (i, atom) in self.enable.iter().enumerate() {
|
|
|
|
let sep = match i {
|
|
|
|
0 => "",
|
|
|
|
_ if i == self.enable.len() - 1 => " and ",
|
|
|
|
_ => ", ",
|
|
|
|
};
|
|
|
|
f.write_str(sep)?;
|
|
|
|
|
2022-03-21 08:43:36 +00:00
|
|
|
atom.fmt(f)?;
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.disable.is_empty() {
|
|
|
|
f.write_str("; ")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.disable.is_empty() {
|
|
|
|
f.write_str("disable ")?;
|
|
|
|
for (i, atom) in self.disable.iter().enumerate() {
|
|
|
|
let sep = match i {
|
|
|
|
0 => "",
|
|
|
|
_ if i == self.enable.len() - 1 => " and ",
|
|
|
|
_ => ", ",
|
|
|
|
};
|
|
|
|
f.write_str(sep)?;
|
|
|
|
|
2022-03-21 08:43:36 +00:00
|
|
|
atom.fmt(f)?;
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InactiveReason {
|
|
|
|
enabled: Vec<CfgAtom>,
|
|
|
|
disabled: Vec<CfgAtom>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for InactiveReason {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
if !self.enabled.is_empty() {
|
|
|
|
for (i, atom) in self.enabled.iter().enumerate() {
|
|
|
|
let sep = match i {
|
|
|
|
0 => "",
|
|
|
|
_ if i == self.enabled.len() - 1 => " and ",
|
|
|
|
_ => ", ",
|
|
|
|
};
|
|
|
|
f.write_str(sep)?;
|
|
|
|
|
2022-03-21 08:43:36 +00:00
|
|
|
atom.fmt(f)?;
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
let is_are = if self.enabled.len() == 1 { "is" } else { "are" };
|
2022-12-23 18:42:58 +00:00
|
|
|
write!(f, " {is_are} enabled")?;
|
2020-10-21 17:54:04 +00:00
|
|
|
|
|
|
|
if !self.disabled.is_empty() {
|
|
|
|
f.write_str(" and ")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.disabled.is_empty() {
|
|
|
|
for (i, atom) in self.disabled.iter().enumerate() {
|
|
|
|
let sep = match i {
|
|
|
|
0 => "",
|
2020-10-22 17:19:05 +00:00
|
|
|
_ if i == self.disabled.len() - 1 => " and ",
|
2020-10-21 17:54:04 +00:00
|
|
|
_ => ", ",
|
|
|
|
};
|
|
|
|
f.write_str(sep)?;
|
|
|
|
|
2022-03-21 08:43:36 +00:00
|
|
|
atom.fmt(f)?;
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
let is_are = if self.disabled.len() == 1 { "is" } else { "are" };
|
2022-12-23 18:42:58 +00:00
|
|
|
write!(f, " {is_are} disabled")?;
|
2020-10-21 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-29 22:52:15 +00:00
|
|
|
}
|