| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
use crate::{cfg_expr::expr::Expression, resolve::CfgMap}; |
| 4 |
|
| 5 |
#[test] |
| 6 |
#[cfg_attr(miri, ignore)] // Miri doesn't support std::process::Command: https://github.com/rust-lang/miri/issues/3374 |
| 7 |
fn target_family() { |
| 8 |
let matches_any_family = |
| 9 |
Expression::parse("any(unix, target_family = \"windows\", target_family = \"wasm\")") |
| 10 |
.unwrap(); |
| 11 |
let impossible = Expression::parse("all(windows, target_family = \"unix\")").unwrap(); |
| 12 |
|
| 13 |
let mut map = CfgMap::default(); |
| 14 |
let rustc = &|| cmd!("rustc"); |
| 15 |
for target in [ |
| 16 |
"aarch64-apple-darwin", |
| 17 |
"x86_64-pc-windows-msvc", |
| 18 |
"wasm32-unknown-unknown", |
| 19 |
"thumbv7m-none-eabi", |
| 20 |
] { |
| 21 |
let t = map.eval_cfg(&matches_any_family, &target.into(), rustc).unwrap(); |
| 22 |
if target.contains("-none") { |
| 23 |
assert!(!t, "{target}"); |
| 24 |
} else { |
| 25 |
assert!(t, "{target}"); |
| 26 |
} |
| 27 |
assert!(!map.eval_cfg(&impossible, &target.into(), rustc).unwrap()); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn tiny() { |
| 33 |
assert!(Expression::parse("all()").unwrap().eval(|_| false)); |
| 34 |
assert!(!Expression::parse("any()").unwrap().eval(|_| true)); |
| 35 |
assert!(!Expression::parse("not(all())").unwrap().eval(|_| false)); |
| 36 |
assert!(Expression::parse("not(any())").unwrap().eval(|_| true)); |
| 37 |
assert!(Expression::parse("all(not(blah))").unwrap().eval(|_| false)); |
| 38 |
assert!(!Expression::parse("any(not(blah))").unwrap().eval(|_| true)); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
#[cfg_attr(miri, ignore)] // Miri doesn't support std::process::Command: https://github.com/rust-lang/miri/issues/3374 |
| 43 |
fn very_specific() { |
| 44 |
let specific = Expression::parse( |
| 45 |
r#"all( |
| 46 |
target_os = "windows", |
| 47 |
target_arch = "x86", |
| 48 |
windows, |
| 49 |
target_env = "msvc", |
| 50 |
target_feature = "fxsr", |
| 51 |
target_feature = "sse", |
| 52 |
target_feature = "sse2", |
| 53 |
target_pointer_width = "32", |
| 54 |
target_endian = "little", |
| 55 |
not(target_vendor = "uwp"), |
| 56 |
target_has_atomic = "8", |
| 57 |
target_has_atomic = "16", |
| 58 |
target_has_atomic = "32", |
| 59 |
target_has_atomic = "64", |
| 60 |
not(target_has_atomic = "128"), |
| 61 |
target_has_atomic = "ptr", |
| 62 |
panic = "unwind", |
| 63 |
not(panic = "abort"), |
| 64 |
)"#, |
| 65 |
) |
| 66 |
.unwrap(); |
| 67 |
|
| 68 |
let mut map = CfgMap::default(); |
| 69 |
let rustc = &|| cmd!("rustc"); |
| 70 |
for target in ["i686-pc-windows-msvc", "i686-pc-windows-gnu"] { |
| 71 |
let t = map.eval_cfg(&specific, &target.into(), rustc).unwrap(); |
| 72 |
assert_eq!( |
| 73 |
target == "i686-pc-windows-msvc", |
| 74 |
t, |
| 75 |
"expected true for i686-pc-windows-msvc, but got true for {target}", |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
// for target in all { |
| 80 |
// let expr = format!( |
| 81 |
// r#"cfg( |
| 82 |
// all( |
| 83 |
// target_arch = "{}", |
| 84 |
// {} |
| 85 |
// {} |
| 86 |
// target_env = "{}" |
| 87 |
// ) |
| 88 |
// )"#, |
| 89 |
// target.arch.0, |
| 90 |
// if let Some(v) = &target.vendor { |
| 91 |
// format!(r#"target_vendor = "{}","#, v.0) |
| 92 |
// } else { |
| 93 |
// "".to_owned() |
| 94 |
// }, |
| 95 |
// if let Some(v) = &target.os { |
| 96 |
// format!(r#"target_os = "{}","#, v.0) |
| 97 |
// } else { |
| 98 |
// "".to_owned() |
| 99 |
// }, |
| 100 |
// target.env.as_ref().map_or("", |e| e.as_str()), |
| 101 |
// ); |
| 102 |
|
| 103 |
// let specific = Expression::parse(&expr).unwrap(); |
| 104 |
|
| 105 |
// let t = Target::make(target.triple.as_str()); |
| 106 |
// assert!( |
| 107 |
// specific.eval(|pred| { |
| 108 |
// if target.triple.as_str() == "mips64-openwrt-linux-musl" { |
| 109 |
// if let Predicate::Target(TargetPredicate::Vendor(vendor)) = pred { |
| 110 |
// // This is a special predicate that doesn't follow the usual rules for |
| 111 |
// // target-lexicon. |
| 112 |
// return t.builtin.matches(&TargetPredicate::Vendor(vendor.clone())); |
| 113 |
// } |
| 114 |
// } |
| 115 |
// tg_match!(pred, t) |
| 116 |
// }), |
| 117 |
// "failed expression '{}' for {:#?}", |
| 118 |
// expr, |
| 119 |
// t.builtin, |
| 120 |
// ); |
| 121 |
// } |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
#[cfg_attr(miri, ignore)] // Miri doesn't support std::process::Command: https://github.com/rust-lang/miri/issues/3374 |
| 126 |
fn complex() { |
| 127 |
let complex = Expression::parse(r#"cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))"#).unwrap(); |
| 128 |
|
| 129 |
let mut map = CfgMap::default(); |
| 130 |
let rustc = &|| cmd!("rustc"); |
| 131 |
|
| 132 |
// Should match linuxes |
| 133 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap()); |
| 134 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap()); |
| 135 |
|
| 136 |
// Should *not* match windows or mac or android |
| 137 |
assert!(!map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap()); |
| 138 |
assert!(!map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap()); |
| 139 |
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap()); |
| 140 |
|
| 141 |
let complex = |
| 142 |
Expression::parse(r#"all(not(target_os = "ios"), not(target_os = "android"))"#).unwrap(); |
| 143 |
|
| 144 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap()); |
| 145 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap()); |
| 146 |
assert!(map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap()); |
| 147 |
assert!(map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap()); |
| 148 |
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap()); |
| 149 |
|
| 150 |
let complex = Expression::parse(r#"all(any(unix, target_arch="x86"), not(any(target_os="android", target_os="emscripten")))"#).unwrap(); |
| 151 |
|
| 152 |
// Should match linuxes and mac |
| 153 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap()); |
| 154 |
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap()); |
| 155 |
assert!(map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap()); |
| 156 |
|
| 157 |
// Should *not* match x86_64 windows or android |
| 158 |
assert!(!map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap()); |
| 159 |
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap()); |
| 160 |
|
| 161 |
// Ensure that target_os = "none" matches against Os == None. |
| 162 |
let complex = Expression::parse(r#"all(target_os="none")"#).unwrap(); |
| 163 |
assert!(!map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap()); |
| 164 |
assert!(map.eval_cfg(&complex, &"armebv7r-none-eabi".into(), rustc).unwrap()); |
| 165 |
} |
| 166 |
|
| 167 |
// #[test] |
| 168 |
// fn unstable_target_abi() { |
| 169 |
// let linux_gnu = Target::make("x86_64-unknown-linux-gnu"); |
| 170 |
// let linux_musl = Target::make("x86_64-unknown-linux-musl"); |
| 171 |
// let windows_msvc = Target::make("x86_64-pc-windows-msvc"); |
| 172 |
// let mac = Target::make("x86_64-apple-darwin"); |
| 173 |
// let android = Target::make("aarch64-linux-android"); |
| 174 |
|
| 175 |
// let target_with_abi_that_matches = cfg_expr::targets::TargetInfo { |
| 176 |
// triple: cfg_expr::targets::Triple::new_const("aarch64-apple-darwin"), |
| 177 |
// os: None, |
| 178 |
// abi: Some(cfg_expr::targets::Abi::new_const("eabihf")), |
| 179 |
// arch: cfg_expr::targets::Arch::aarch64, |
| 180 |
// env: None, |
| 181 |
// vendor: None, |
| 182 |
// families: cfg_expr::targets::Families::unix, |
| 183 |
// pointer_width: 64, |
| 184 |
// endian: cfg_expr::targets::Endian::little, |
| 185 |
// has_atomics: cfg_expr::targets::HasAtomics::atomic_8_16_32_64_128_ptr, |
| 186 |
// panic: cfg_expr::targets::Panic::unwind, |
| 187 |
// }; |
| 188 |
|
| 189 |
// let target_with_abi_that_does_not_match = cfg_expr::targets::TargetInfo { |
| 190 |
// abi: Some(cfg_expr::targets::Abi::new_const("ilp32")), |
| 191 |
// ..target_with_abi_that_matches.clone() |
| 192 |
// }; |
| 193 |
|
| 194 |
// let abi_pred = |
| 195 |
// Expression::parse(r#"cfg(any(target_arch = "wasm32", target_abi = "eabihf"))"#).unwrap(); |
| 196 |
|
| 197 |
// // Should match a specified target_abi that's the same |
| 198 |
// assert!(abi_pred.eval(|pred| { |
| 199 |
// match pred { |
| 200 |
// Predicate::Target(tp) => tp.matches(&target_with_abi_that_matches), |
| 201 |
// _ => false, |
| 202 |
// } |
| 203 |
// })); |
| 204 |
|
| 205 |
// // Should *not* match a specified target_abi that isn't the same |
| 206 |
// assert!(!abi_pred.eval(|pred| { |
| 207 |
// match pred { |
| 208 |
// Predicate::Target(tp) => tp.matches(&target_with_abi_that_does_not_match), |
| 209 |
// _ => false, |
| 210 |
// } |
| 211 |
// })); |
| 212 |
|
| 213 |
// // Should *not* match any builtins at this point because target_abi isn't stable |
| 214 |
// assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_gnu))); |
| 215 |
// assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_musl))); |
| 216 |
// assert!(!abi_pred.eval(|pred| tg_match!(pred, mac))); |
| 217 |
// assert!(!abi_pred.eval(|pred| tg_match!(pred, windows_msvc))); |
| 218 |
// assert!(!abi_pred.eval(|pred| tg_match!(pred, android))); |
| 219 |
// } |
| 220 |
|
| 221 |
#[test] |
| 222 |
#[cfg_attr(miri, ignore)] // Miri doesn't support std::process::Command: https://github.com/rust-lang/miri/issues/3374 |
| 223 |
fn wasm_family() { |
| 224 |
let wasm = Expression::parse(r#"cfg(target_family = "wasm")"#).unwrap(); |
| 225 |
|
| 226 |
let mut map = CfgMap::default(); |
| 227 |
let rustc = &|| cmd!("rustc"); |
| 228 |
|
| 229 |
// All of the above targets match. |
| 230 |
for target in [ |
| 231 |
"wasm32-unknown-unknown", |
| 232 |
"wasm32-unknown-emscripten", |
| 233 |
"wasm32-wasip1", |
| 234 |
"wasm64-unknown-unknown", |
| 235 |
] { |
| 236 |
assert!(map.eval_cfg(&wasm, &target.into(), rustc).unwrap(), "{target}"); |
| 237 |
} |
| 238 |
} |