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
use crate::{custom_platforms::TargetInfo, eval_target, Error, Platform};
use cfg_expr::{targets::get_builtin_target_by_triple, Expression, Predicate};
use std::{str::FromStr, sync::Arc};
#[derive(Clone, Debug)]
pub struct TargetSpec<'a> {
target: Target<'a>,
}
impl<'a> TargetSpec<'a> {
pub fn custom(target_info: &'a TargetInfo<'a>) -> Self {
Self {
target: Target::TargetInfo(target_info),
}
}
#[inline]
pub fn eval(&self, platform: &Platform<'_>) -> Option<bool> {
eval_target(&self.target, platform)
}
}
impl FromStr for TargetSpec<'static> {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
Ok(Self {
target: Target::parse(input)?,
})
}
}
#[derive(Clone, Debug)]
pub(crate) enum Target<'a> {
TargetInfo(&'a TargetInfo<'a>),
Spec(Arc<Expression>),
}
impl Target<'static> {
fn parse(input: &str) -> Result<Self, Error> {
if input.starts_with("cfg(") {
let expr = Expression::parse(input).map_err(Error::InvalidCfg)?;
Self::verify_expr(expr)
} else {
Ok(Target::TargetInfo(
get_builtin_target_by_triple(input)
.ok_or_else(|| Error::UnknownTargetTriple(input.to_string()))?,
))
}
}
}
impl<'a> Target<'a> {
fn verify_expr(expr: Expression) -> Result<Self, Error> {
for pred in expr.predicates() {
if let Predicate::KeyValue { key, .. } = pred {
return Err(Error::UnknownPredicate(key.to_string()));
}
}
Ok(Target::Spec(Arc::new(expr)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use cfg_expr::{
targets::{Family, Os},
Predicate, TargetPredicate,
};
#[test]
fn test_triple() {
let res = Target::parse("x86_64-apple-darwin");
assert!(matches!(
res,
Ok(Target::TargetInfo(target_info)) if target_info.triple == "x86_64-apple-darwin"
));
}
#[test]
fn test_single() {
let expr = match Target::parse("cfg(windows)").unwrap() {
Target::TargetInfo(target_info) => {
panic!("expected spec, got target info: {:?}", target_info)
}
Target::Spec(expr) => expr,
};
assert_eq!(
expr.predicates().collect::<Vec<_>>(),
vec![Predicate::Target(TargetPredicate::Family(Family::windows))],
);
}
#[test]
fn test_not() {
assert!(matches!(
Target::parse("cfg(not(windows))"),
Ok(Target::Spec(_))
));
}
#[test]
fn test_testequal() {
let expr = match Target::parse("cfg(target_os = \"windows\")").unwrap() {
Target::TargetInfo(target_info) => {
panic!("expected spec, got target info: {:?}", target_info)
}
Target::Spec(expr) => expr,
};
assert_eq!(
expr.predicates().collect::<Vec<_>>(),
vec![Predicate::Target(TargetPredicate::Os(Os::windows))],
);
}
#[test]
fn test_unknown_triple() {
let err = Target::parse("x86_64-pc-darwin").expect_err("unknown triple");
assert_eq!(
err,
Error::UnknownTargetTriple("x86_64-pc-darwin".to_string())
);
}
#[test]
fn test_unknown_flag() {
let expr = match Target::parse("cfg(foo)").unwrap() {
Target::TargetInfo(target_info) => {
panic!("expected spec, got target info: {:?}", target_info)
}
Target::Spec(expr) => expr,
};
assert_eq!(
expr.predicates().collect::<Vec<_>>(),
vec![Predicate::Flag("foo")],
);
}
#[test]
fn test_unknown_predicate() {
let err = Target::parse("cfg(bogus_key = \"bogus_value\")").expect_err("unknown predicate");
assert_eq!(err, Error::UnknownPredicate("bogus_key".to_string()));
}
#[test]
fn test_extra() {
let res = Target::parse("cfg(unix)this-is-extra");
res.expect_err("extra content at the end");
}
#[test]
fn test_incomplete() {
let res = Target::parse("cfg(not(unix)");
res.expect_err("missing ) at the end");
}
}