guppy/platform/platform_eval.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::platform::{Platform, PlatformSpec};
use std::ops::{BitAnd, BitOr};
use target_spec::TargetSpec;
/// The status of a dependency or feature, which is possibly platform-dependent.
///
/// This is a sub-status of [`EnabledStatus`](crate::graph::EnabledStatus).
#[derive(Copy, Clone, Debug)]
pub enum PlatformStatus<'g> {
/// This dependency or feature is never enabled on any platforms.
Never,
/// This dependency or feature is always enabled on all platforms.
Always,
/// The status is platform-dependent.
PlatformDependent {
/// An evaluator to run queries against.
eval: PlatformEval<'g>,
},
}
assert_covariant!(PlatformStatus);
impl<'g> PlatformStatus<'g> {
pub(crate) fn new(specs: &'g PlatformStatusImpl) -> Self {
match specs {
PlatformStatusImpl::Always => PlatformStatus::Always,
PlatformStatusImpl::Specs(specs) => {
if specs.is_empty() {
PlatformStatus::Never
} else {
PlatformStatus::PlatformDependent {
eval: PlatformEval { specs },
}
}
}
}
}
/// Returns true if this dependency is always enabled on all platforms.
pub fn is_always(&self) -> bool {
match self {
PlatformStatus::Always => true,
PlatformStatus::PlatformDependent { .. } | PlatformStatus::Never => false,
}
}
/// Returns true if this dependency is never enabled on any platform.
pub fn is_never(&self) -> bool {
match self {
PlatformStatus::Never => true,
PlatformStatus::PlatformDependent { .. } | PlatformStatus::Always => false,
}
}
/// Returns true if this dependency is possibly enabled on any platform.
pub fn is_present(&self) -> bool {
!self.is_never()
}
/// Evaluates whether this dependency is enabled on the given platform spec.
///
/// Returns `Unknown` if the result was unknown, which may happen if evaluating against an
/// individual platform and its target features are unknown.
pub fn enabled_on(&self, platform_spec: &PlatformSpec) -> EnabledTernary {
match (self, platform_spec) {
(PlatformStatus::Always, _) => EnabledTernary::Enabled,
(PlatformStatus::Never, _) => EnabledTernary::Disabled,
(PlatformStatus::PlatformDependent { .. }, PlatformSpec::Any) => {
EnabledTernary::Enabled
}
(PlatformStatus::PlatformDependent { eval }, PlatformSpec::Platform(platform)) => {
eval.eval(platform)
}
(PlatformStatus::PlatformDependent { .. }, PlatformSpec::Always) => {
EnabledTernary::Disabled
}
}
}
}
/// Whether a dependency or feature is enabled on a specific platform.
///
/// This is a ternary or [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic)
/// because the result may be unknown in some situations.
///
/// Returned by the methods on `EnabledStatus`, `PlatformStatus`, and `PlatformEval`.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum EnabledTernary {
/// The dependency is disabled on this platform.
Disabled,
/// The status of this dependency is unknown on this platform.
///
/// This may happen if evaluation involves unknown target features. Notably, this will not be
/// returned for `Platform::current()`, since the target features for the current platform are
/// known.
Unknown,
/// The dependency is enabled on this platform.
Enabled,
}
impl EnabledTernary {
fn new(x: Option<bool>) -> Self {
match x {
Some(false) => EnabledTernary::Disabled,
None => EnabledTernary::Unknown,
Some(true) => EnabledTernary::Enabled,
}
}
/// Returns true if the status is known (either enabled or disabled).
pub fn is_known(self) -> bool {
match self {
EnabledTernary::Disabled | EnabledTernary::Enabled => true,
EnabledTernary::Unknown => false,
}
}
}
/// AND operation in Kleene K3 logic.
impl BitAnd for EnabledTernary {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
use EnabledTernary::*;
match (self, rhs) {
(Enabled, Enabled) => Enabled,
(Disabled, _) | (_, Disabled) => Disabled,
_ => Unknown,
}
}
}
/// OR operation in Kleene K3 logic.
impl BitOr for EnabledTernary {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
use EnabledTernary::*;
match (self, rhs) {
(Disabled, Disabled) => Disabled,
(Enabled, _) | (_, Enabled) => Enabled,
_ => Unknown,
}
}
}
/// An evaluator for platform-specific dependencies.
///
/// This represents a collection of platform specifications, of the sort `cfg(unix)`.
#[derive(Copy, Clone, Debug)]
pub struct PlatformEval<'g> {
specs: &'g [TargetSpec],
}
assert_covariant!(PlatformEval);
impl<'g> PlatformEval<'g> {
/// Runs this evaluator against the given platform.
pub fn eval(&self, platform: &Platform) -> EnabledTernary {
let mut res = EnabledTernary::Disabled;
for spec in self.specs.iter() {
let matches = spec.eval(platform);
// Short-circuit evaluation if possible.
if matches == Some(true) {
return EnabledTernary::Enabled;
}
res = res | EnabledTernary::new(matches);
}
res
}
}
#[derive(Clone, Debug)]
pub(crate) enum PlatformStatusImpl {
Always,
// Empty vector means never.
Specs(Vec<TargetSpec>),
}
impl PlatformStatusImpl {
/// Returns true if this is an empty predicate (i.e. will never match).
pub(crate) fn is_never(&self) -> bool {
match self {
PlatformStatusImpl::Always => false,
PlatformStatusImpl::Specs(specs) => specs.is_empty(),
}
}
pub(crate) fn extend(&mut self, other: &PlatformStatusImpl) {
// &mut *self is a reborrow to allow *self to work below.
match (&mut *self, other) {
(PlatformStatusImpl::Always, _) => {
// Always stays the same since it means all specs are included.
}
(PlatformStatusImpl::Specs(_), PlatformStatusImpl::Always) => {
// Mark self as Always.
*self = PlatformStatusImpl::Always;
}
(PlatformStatusImpl::Specs(specs), PlatformStatusImpl::Specs(other)) => {
specs.extend_from_slice(other.as_slice());
}
}
}
pub(crate) fn add_spec(&mut self, spec: Option<&TargetSpec>) {
// &mut *self is a reborrow to allow *self to work below.
match (&mut *self, spec) {
(PlatformStatusImpl::Always, _) => {
// Always stays the same since it means all specs are included.
}
(PlatformStatusImpl::Specs(_), None) => {
// Mark self as Always.
*self = PlatformStatusImpl::Always;
}
(PlatformStatusImpl::Specs(specs), Some(spec)) => {
specs.push(spec.clone());
}
}
}
}
impl Default for PlatformStatusImpl {
fn default() -> Self {
// Empty vector means never.
PlatformStatusImpl::Specs(vec![])
}
}