| 1 |
use cfg_expr::{Expression, Predicate, targets::get_builtin_target_by_triple}; |
| 2 |
|
| 3 |
fn main() { |
| 4 |
let specific = Expression::parse( |
| 5 |
r#"all( |
| 6 |
target_os = "windows", |
| 7 |
target_arch = "x86", |
| 8 |
windows, |
| 9 |
target_env = "msvc", |
| 10 |
target_feature = "fxsr", |
| 11 |
target_feature = "sse", |
| 12 |
target_feature = "sse2", |
| 13 |
target_pointer_width = "32", |
| 14 |
target_endian = "little", |
| 15 |
not(target_vendor = "uwp"), |
| 16 |
feature = "cool_thing", |
| 17 |
)"#, |
| 18 |
) |
| 19 |
.unwrap(); |
| 20 |
|
| 21 |
// cfg_expr includes a list of every builtin target in rustc |
| 22 |
let x86_win = get_builtin_target_by_triple("i686-pc-windows-msvc").unwrap(); |
| 23 |
let x86_pentium_win = get_builtin_target_by_triple("i586-pc-windows-msvc").unwrap(); |
| 24 |
let uwp_win = get_builtin_target_by_triple("i686-uwp-windows-msvc").unwrap(); |
| 25 |
let mac = get_builtin_target_by_triple("x86_64-apple-darwin").unwrap(); |
| 26 |
|
| 27 |
let avail_targ_feats = ["fxsr", "sse", "sse2"]; |
| 28 |
|
| 29 |
// This will satisfy all requirements |
| 30 |
assert!(specific.eval(|pred| { |
| 31 |
match pred { |
| 32 |
Predicate::Target(tp) => tp.matches(x86_win), |
| 33 |
Predicate::TargetFeature(feat) => avail_targ_feats.contains(feat), |
| 34 |
Predicate::Feature(feat) => *feat == "cool_thing", |
| 35 |
_ => false, |
| 36 |
} |
| 37 |
})); |
| 38 |
|
| 39 |
// This won't, it doesnt' have the cool_thing feature! |
| 40 |
assert!(!specific.eval(|pred| { |
| 41 |
match pred { |
| 42 |
Predicate::Target(tp) => tp.matches(x86_pentium_win), |
| 43 |
Predicate::TargetFeature(feat) => avail_targ_feats.contains(feat), |
| 44 |
_ => false, |
| 45 |
} |
| 46 |
})); |
| 47 |
|
| 48 |
// This will *not* satisfy the vendor predicate |
| 49 |
assert!(!specific.eval(|pred| { |
| 50 |
match pred { |
| 51 |
Predicate::Target(tp) => tp.matches(uwp_win), |
| 52 |
Predicate::TargetFeature(feat) => avail_targ_feats.contains(feat), |
| 53 |
_ => false, |
| 54 |
} |
| 55 |
})); |
| 56 |
|
| 57 |
// This will *not* satisfy the vendor, os, or env predicates |
| 58 |
assert!(!specific.eval(|pred| { |
| 59 |
match pred { |
| 60 |
Predicate::Target(tp) => tp.matches(mac), |
| 61 |
Predicate::TargetFeature(feat) => avail_targ_feats.contains(feat), |
| 62 |
_ => false, |
| 63 |
} |
| 64 |
})); |
| 65 |
} |