Skip to content

Commit

Permalink
Parse target_abi if it's present even though it's unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Nov 4, 2022
1 parent 1065ebc commit 4b7f047
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use crate::targets as targ;
/// All predicates that pertains to a target, except for `target_feature`
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TargetPredicate {
/// [target_abi](https://github.com/rust-lang/rust/issues/80970)
Abi(targ::Abi),
/// [target_arch](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch)
Arch(targ::Arch),
/// [target_endian](https://doc.rust-lang.org/reference/conditional-compilation.html#target_endian)
Expand Down Expand Up @@ -59,10 +61,15 @@ pub trait TargetMatcher {
impl TargetMatcher for targ::TargetInfo {
fn matches(&self, tp: &TargetPredicate) -> bool {
use TargetPredicate::{
Arch, Endian, Env, Family, HasAtomic, Os, Panic, PointerWidth, Vendor,
Abi, Arch, Endian, Env, Family, HasAtomic, Os, Panic, PointerWidth, Vendor,
};

match tp {
// The ABI is allowed to be an empty string
Abi(abi) => match &self.abi {
Some(a) => abi == a,
None => abi.0.is_empty(),
},
Arch(a) => a == &self.arch,
Endian(end) => *end == self.endian,
// The environment is allowed to be an empty string
Expand Down Expand Up @@ -90,10 +97,14 @@ impl TargetMatcher for target_lexicon::Triple {
fn matches(&self, tp: &TargetPredicate) -> bool {
use target_lexicon::*;
use TargetPredicate::{
Arch, Endian, Env, Family, HasAtomic, Os, Panic, PointerWidth, Vendor,
Abi, Arch, Endian, Env, Family, HasAtomic, Os, Panic, PointerWidth, Vendor,
};

match tp {
Abi(_) => {
// `target_abi` is unstable. Assume false for this.
false
}
Arch(arch) => {
if arch == &targ::Arch::x86 {
matches!(self.architecture, Architecture::X86_32(_))
Expand Down Expand Up @@ -356,6 +367,7 @@ impl TargetPredicate {

#[derive(Clone, Debug)]
pub(crate) enum Which {
Abi,
Arch,
Endian(targ::Endian),
Env,
Expand Down Expand Up @@ -419,6 +431,9 @@ impl InnerPredicate {

match self {
IP::Target(it) => match &it.which {
Which::Abi => Target(TargetPredicate::Abi(targ::Abi::new(
s[it.span.clone().unwrap()].to_owned(),
))),
Which::Arch => Target(TargetPredicate::Arch(targ::Arch::new(
s[it.span.clone().unwrap()].to_owned(),
))),
Expand Down
1 change: 1 addition & 0 deletions src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl Expression {
}

let tp = match &target_key[7..] {
"abi" => tp!(Abi),
"arch" => tp!(Arch),
"feature" => {
if val.is_empty() {
Expand Down
8 changes: 8 additions & 0 deletions src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub use builtins::ALL_BUILTINS;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Triple(pub Cow<'static, str>);

/// The "abi" field
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Abi(pub Cow<'static, str>);

/// The "architecture" field
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Arch(pub Cow<'static, str>);
Expand Down Expand Up @@ -79,6 +83,7 @@ macro_rules! field_impls {
}

field_impls!(Triple);
field_impls!(Abi);
field_impls!(Arch);
field_impls!(Vendor);
field_impls!(Os);
Expand Down Expand Up @@ -316,6 +321,9 @@ pub struct TargetInfo {
/// [target_os](https://doc.rust-lang.org/reference/conditional-compilation.html#target_os)
/// predicate.
pub os: Option<Os>,
/// The target's ABI, if any. Used by the
/// [target_abi](https://github.com/rust-lang/rust/issues/80970) predicate.
pub abi: Option<Abi>,
/// The target's CPU architecture. Used by the
/// [target_arch](https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch)
/// predicate.
Expand Down
Loading

0 comments on commit 4b7f047

Please sign in to comment.