| 1 | <!-- markdownlint-disable no-inline-html first-line-heading --> |
| 2 | |
| 3 | <div align="center"> |
| 4 | |
| 5 | # `⚙️ cfg-expr` |
| 6 | |
| 7 | **A parser and evaluator for Rust `cfg()` expressions. Builtin targets as of [1.93.0] are supported.** |
| 8 | |
| 9 | [](https://github.com/EmbarkStudios/cfg-expr/actions?workflow=CI) |
| 10 | [](https://crates.io/crates/cfg-expr) |
| 11 | [](https://docs.rs/cfg-expr) |
| 12 | [](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html) |
| 13 | [](https://forge.rust-lang.org/release/platform-support.html) |
| 14 | [](CODE_OF_CONDUCT.md) |
| 15 | [](https://embark.dev) |
| 16 | </div> |
| 17 | |
| 18 | ## Alternatives |
| 19 | |
| 20 | - [cargo-platform](https://crates.io/crates/cargo-platform) |
| 21 | - [parse_cfg](https://crates.io/crates/parse_cfg) |
| 22 | |
| 23 | ## Usage |
| 24 | |
| 25 | `cfg-expr` is a crate that can be used to parse and evaluate Rust `cfg()` expressions, both as declarable in Rust code itself, as well in cargo manifests' `[target.'cfg()'.dependencies]` sections. |
| 26 | |
| 27 | It contains a list of all builtin targets known to rustc as of [1.93.0] that can be used to determine if a particular cfg expression is satisfiable. |
| 28 | |
| 29 | ```rust |
| 30 | use cfg_expr::{targets::get_builtin_target_by_triple, Expression, Predicate}; |
| 31 | |
| 32 | let specific = Expression::parse( |
| 33 | r#"all( |
| 34 | target_os = "windows", |
| 35 | target_arch = "x86", |
| 36 | windows, |
| 37 | target_env = "msvc", |
| 38 | target_feature = "fxsr", |
| 39 | target_feature = "sse", |
| 40 | target_feature = "sse2", |
| 41 | target_pointer_width = "32", |
| 42 | target_endian = "little", |
| 43 | not(target_vendor = "uwp"), |
| 44 | feature = "cool_thing", |
| 45 | )"#, |
| 46 | ).unwrap(); |
| 47 | |
| 48 | // cfg_expr includes a list of every builtin target in rustc |
| 49 | let x86_win = get_builtin_target_by_triple("i686-pc-windows-msvc").unwrap(); |
| 50 | let uwp_win = get_builtin_target_by_triple("i686-uwp-windows-msvc").unwrap(); |
| 51 | let mac = get_builtin_target_by_triple("x86_64-apple-darwin").unwrap(); |
| 52 | |
| 53 | let avail_target_feats = ["fxsr", "sse", "sse2"]; |
| 54 | |
| 55 | // This will satisfy all requirements |
| 56 | assert!(specific.eval(|pred| { |
| 57 | match pred { |
| 58 | Predicate::Target(tp) => tp.matches(x86_win), |
| 59 | Predicate::TargetFeature(feat) => avail_target_feats.contains(feat), |
| 60 | Predicate::Feature(feat) => *feat == "cool_thing", |
| 61 | _ => false, |
| 62 | } |
| 63 | })); |
| 64 | |
| 65 | // This will *not* satisfy the vendor predicate |
| 66 | assert!(!specific.eval(|pred| { |
| 67 | match pred { |
| 68 | Predicate::Target(tp) => tp.matches(uwp_win), |
| 69 | Predicate::TargetFeature(feat) => avail_target_feats.contains(feat), |
| 70 | _ => false, |
| 71 | } |
| 72 | })); |
| 73 | |
| 74 | // This will *not* satisfy the vendor, os, or env predicates |
| 75 | assert!(!specific.eval(|pred| { |
| 76 | match pred { |
| 77 | Predicate::Target(tp) => tp.matches(mac), |
| 78 | Predicate::TargetFeature(feat) => avail_target_feats.contains(feat), |
| 79 | _ => false, |
| 80 | } |
| 81 | })); |
| 82 | ``` |
| 83 | |
| 84 | ## Contributing |
| 85 | |
| 86 | We welcome community contributions to this project. |
| 87 | |
| 88 | Please read our [Contributor Guide](CONTRIBUTING.md) for more information on how to get started. |
| 89 | |
| 90 | ## License |
| 91 | |
| 92 | Licensed under either of |
| 93 | |
| 94 | - Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 95 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) |
| 96 | |
| 97 | at your option. |
| 98 | |
| 99 | ### Contribution |
| 100 | |
| 101 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |
| 102 | |
| 103 | [1.93.0]: (https://forge.rust-lang.org/release/platform-support.html) |