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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
use cfg_expr::{
expr::{Predicate, TargetMatcher},
targets::{get_builtin_target_by_triple, ALL_BUILTINS as all},
Expression, TargetPredicate,
};
struct Target {
builtin: &'static cfg_expr::targets::TargetInfo,
#[cfg(feature = "targets")]
lexicon: Option<target_lexicon::Triple>,
}
impl Target {
fn make(s: &str) -> Self {
Self {
builtin: get_builtin_target_by_triple(s).unwrap(),
#[cfg(feature = "targets")]
lexicon: {
// Hack to workaround the addition in 1.48.0 of this weird, non-conformant
// target triple, until https://github.com/bytecodealliance/target-lexicon/issues/63 is
// resolved in a satisfactory manner, not really concerned about
// the presence of this triple in most normal cases
use target_lexicon as tl;
match s {
"avr-unknown-gnu-atmega328" => Some(tl::Triple {
architecture: tl::Architecture::Avr,
vendor: tl::Vendor::Unknown,
operating_system: tl::OperatingSystem::Unknown,
environment: tl::Environment::Unknown,
binary_format: tl::BinaryFormat::Unknown,
}),
triple => match triple.parse::<tl::Triple>() {
Ok(l) => Some(l),
Err(e) => {
// There are enough new weird architectures added in each version of
// Rust that it is difficult to keep target-lexicon aware of all of
// them. So try parsing this triple, but don't fail if it doesn't work.
eprintln!("failed to parse '{triple}': {e:?}");
None
}
},
}
},
}
}
}
macro_rules! tg_match {
($pred:expr, $target:expr) => {
match $pred {
Predicate::Target(tg) => {
let tinfo = tg.matches($target.builtin);
#[cfg(feature = "targets")]
if !matches!(tg, TargetPredicate::HasAtomic(_))
&& !matches!(tg, TargetPredicate::Panic(_))
{
if let Some(l) = &$target.lexicon {
let linfo = tg.matches(l);
assert_eq!(
tinfo, linfo,
"{:#?} builtin didn't match lexicon {:#?} for predicate {tg:#?}",
$target.builtin, $target.lexicon,
);
return linfo;
}
}
tinfo
}
_ => panic!("not a target predicate"),
}
};
($pred:expr, $target:expr, $feats:expr) => {
match $pred {
Predicate::Target(tg) => {
let tinfo = tg.matches($target.builtin);
#[cfg(feature = "targets")]
if !matches!(tg, TargetPredicate::HasAtomic(_))
&& !matches!(tg, TargetPredicate::Panic(_))
{
if let Some(l) = &$target.lexicon {
let linfo = tg.matches(l);
assert_eq!(
tinfo, linfo,
"{:#?} builtin didn't match lexicon {:#?} for predicate {tg:#?}",
$target.builtin, $target.lexicon,
);
return linfo;
}
}
tinfo
}
Predicate::TargetFeature(feat) => $feats.iter().find(|f| *f == feat).is_some(),
_ => panic!("not a target predicate"),
}
};
}
#[test]
fn target_family() {
let matches_any_family =
Expression::parse("any(unix, target_family = \"windows\", target_family = \"wasm\")")
.unwrap();
let impossible = Expression::parse("all(windows, target_family = \"unix\")").unwrap();
for target in all {
let target = Target::make(target.triple.as_str());
if target.builtin.families.is_empty() {
assert!(!matches_any_family.eval(|pred| { tg_match!(pred, target) }));
} else {
assert!(matches_any_family.eval(|pred| { tg_match!(pred, target) }));
}
assert!(!impossible.eval(|pred| { tg_match!(pred, target) }));
}
}
#[test]
fn tiny() {
assert!(Expression::parse("all()").unwrap().eval(|_| false));
assert!(!Expression::parse("any()").unwrap().eval(|_| true));
assert!(!Expression::parse("not(all())").unwrap().eval(|_| false));
assert!(Expression::parse("not(any())").unwrap().eval(|_| true));
assert!(Expression::parse("all(not(blah))").unwrap().eval(|_| false));
assert!(!Expression::parse("any(not(blah))").unwrap().eval(|_| true));
}
#[test]
fn very_specific() {
let specific = Expression::parse(
r#"all(
target_os = "windows",
target_arch = "x86",
windows,
target_env = "msvc",
target_feature = "fxsr",
target_feature = "sse",
target_feature = "sse2",
target_pointer_width = "32",
target_endian = "little",
not(target_vendor = "uwp"),
target_has_atomic = "8",
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
not(target_has_atomic = "128"),
target_has_atomic = "ptr",
panic = "unwind",
not(panic = "abort"),
)"#,
)
.unwrap();
for target in all {
let t = Target::make(target.triple.as_str());
assert_eq!(
matches!(
target.triple.as_str(),
"i686-pc-windows-msvc" | "i586-pc-windows-msvc" | "i686-win7-windows-msvc"
),
specific.eval(|pred| { tg_match!(pred, t, &["fxsr", "sse", "sse2"]) }),
"expected true for i686-pc-windows-msvc, but got true for {}",
target.triple,
);
}
for target in all {
let expr = format!(
r#"cfg(
all(
target_arch = "{}",
{}
{}
target_env = "{}"
)
)"#,
target.arch.0,
if let Some(v) = &target.vendor {
format!(r#"target_vendor = "{}","#, v.0)
} else {
"".to_owned()
},
if let Some(v) = &target.os {
format!(r#"target_os = "{}","#, v.0)
} else {
"".to_owned()
},
target.env.as_ref().map_or("", |e| e.as_str()),
);
let specific = Expression::parse(&expr).unwrap();
let t = Target::make(target.triple.as_str());
assert!(
specific.eval(|pred| {
if target.triple.as_str() == "mips64-openwrt-linux-musl" {
if let Predicate::Target(TargetPredicate::Vendor(vendor)) = pred {
// This is a special predicate that doesn't follow the usual rules for
// target-lexicon.
return t.builtin.matches(&TargetPredicate::Vendor(vendor.clone()));
}
}
tg_match!(pred, t)
}),
"failed expression '{}' for {:#?}",
expr,
t.builtin,
);
}
}
#[test]
fn complex() {
let complex = Expression::parse(r#"cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))"#).unwrap();
// Should match linuxes
let linux_gnu = Target::make("x86_64-unknown-linux-gnu");
let linux_musl = Target::make("x86_64-unknown-linux-musl");
assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
// Should *not* match windows or mac or android
let windows_msvc = Target::make("x86_64-pc-windows-msvc");
let mac = Target::make("x86_64-apple-darwin");
let android = Target::make("aarch64-linux-android");
assert!(!complex.eval(|pred| tg_match!(pred, windows_msvc)));
assert!(!complex.eval(|pred| tg_match!(pred, mac)));
assert!(!complex.eval(|pred| tg_match!(pred, android)));
let complex =
Expression::parse(r#"all(not(target_os = "ios"), not(target_os = "android"))"#).unwrap();
assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
assert!(complex.eval(|pred| tg_match!(pred, windows_msvc)));
assert!(complex.eval(|pred| tg_match!(pred, mac)));
assert!(!complex.eval(|pred| tg_match!(pred, android)));
let complex = Expression::parse(r#"all(any(unix, target_arch="x86"), not(any(target_os="android", target_os="emscripten")))"#).unwrap();
// Should match linuxes and mac
assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
assert!(complex.eval(|pred| tg_match!(pred, mac)));
// Should *not* match x86_64 windows or android
assert!(!complex.eval(|pred| tg_match!(pred, windows_msvc)));
assert!(!complex.eval(|pred| tg_match!(pred, android)));
// Ensure that target_os = "none" matches against Os == None.
let complex = Expression::parse(r#"all(target_os="none")"#).unwrap();
let armebv7r_none_eabi = Target::make("armebv7r-none-eabi");
assert!(!complex.eval(|pred| tg_match!(pred, linux_gnu)));
assert!(complex.eval(|pred| tg_match!(pred, armebv7r_none_eabi)));
}
#[test]
fn unstable_target_abi() {
let linux_gnu = Target::make("x86_64-unknown-linux-gnu");
let linux_musl = Target::make("x86_64-unknown-linux-musl");
let windows_msvc = Target::make("x86_64-pc-windows-msvc");
let mac = Target::make("x86_64-apple-darwin");
let android = Target::make("aarch64-linux-android");
let target_with_abi_that_matches = cfg_expr::targets::TargetInfo {
triple: cfg_expr::targets::Triple::new_const("aarch64-apple-darwin"),
os: None,
abi: Some(cfg_expr::targets::Abi::new_const("eabihf")),
arch: cfg_expr::targets::Arch::aarch64,
env: None,
vendor: None,
families: cfg_expr::targets::Families::unix,
pointer_width: 64,
endian: cfg_expr::targets::Endian::little,
has_atomics: cfg_expr::targets::HasAtomics::atomic_8_16_32_64_128_ptr,
panic: cfg_expr::targets::Panic::unwind,
};
let target_with_abi_that_doesnt_match = cfg_expr::targets::TargetInfo {
abi: Some(cfg_expr::targets::Abi::new_const("ilp32")),
..target_with_abi_that_matches.clone()
};
let abi_pred =
Expression::parse(r#"cfg(any(target_arch = "wasm32", target_abi = "eabihf"))"#).unwrap();
// Should match a specified target_abi that's the same
assert!(abi_pred.eval(|pred| {
match pred {
Predicate::Target(tp) => tp.matches(&target_with_abi_that_matches),
_ => false,
}
}));
// Should *not* match a specified target_abi that isn't the same
assert!(!abi_pred.eval(|pred| {
match pred {
Predicate::Target(tp) => tp.matches(&target_with_abi_that_doesnt_match),
_ => false,
}
}));
// Should *not* match any builtins at this point because target_abi isn't stable
assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_gnu)));
assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_musl)));
assert!(!abi_pred.eval(|pred| tg_match!(pred, mac)));
assert!(!abi_pred.eval(|pred| tg_match!(pred, windows_msvc)));
assert!(!abi_pred.eval(|pred| tg_match!(pred, android)));
}
#[test]
fn wasm_family() {
let wasm = Expression::parse(r#"cfg(target_family = "wasm")"#).unwrap();
let wasm32_unknown = Target::make("wasm32-unknown-unknown");
let wasm32_emscripten = Target::make("wasm32-unknown-emscripten");
let wasm32_wasi = Target::make("wasm32-wasi");
let wasm64_unknown = Target::make("wasm64-unknown-unknown");
// All of the above targets match.
assert!(wasm.eval(|pred| tg_match!(pred, wasm32_unknown)));
assert!(wasm.eval(|pred| tg_match!(pred, wasm32_emscripten)));
assert!(wasm.eval(|pred| tg_match!(pred, wasm32_wasi)));
assert!(wasm.eval(|pred| tg_match!(pred, wasm64_unknown)));
}
#[test]
fn features() {
let enabled = ["good", "bad", "ugly"];
let many_features = Expression::parse(
r#"all(feature = "good", feature = "bad", feature = "ugly", not(feature = "nope"))"#,
)
.unwrap();
assert!(many_features.eval(|pred| {
match pred {
Predicate::Feature(name) => enabled.contains(name),
_ => false,
}
}));
let feature_and_target_feature =
Expression::parse(r#"all(feature = "make_fast", target_feature = "sse4.2")"#).unwrap();
assert!(feature_and_target_feature.eval(|pred| {
match pred {
Predicate::Feature(name) => *name == "make_fast",
Predicate::TargetFeature(feat) => *feat == "sse4.2",
_ => false,
}
}));
assert_eq!(
feature_and_target_feature.eval(|pred| {
match pred {
Predicate::Feature(_) => Some(false),
Predicate::TargetFeature(_) => None,
_ => panic!("unexpected predicate"),
}
}),
Some(false),
"all() with Some(false) and None evaluates to Some(false)"
);
assert_eq!(
feature_and_target_feature.eval(|pred| {
match pred {
Predicate::Feature(_) => Some(true),
Predicate::TargetFeature(_) => None,
_ => panic!("unexpected predicate"),
}
}),
None,
"all() with Some(true) and None evaluates to None"
);
}
|