| 1 |
# CFG Aliases |
| 2 |
|
| 3 |
CFG Aliases is a tiny utility to help save you a lot of effort with long winded `#[cfg()]` checks. This crate provides a single [`cfg_aliases!`] macro that doesn't have any dependencies and specifically avoids pulling in `syn` or `quote` so that the impact on your compile times should be negligible. |
| 4 |
|
| 5 |
You use the the [`cfg_aliases!`] macro in your `build.rs` script to define aliases such as `x11` that could then be used in the `cfg` attribute or macro for conditional compilation: `#[cfg(x11)]`. |
| 6 |
|
| 7 |
## Example |
| 8 |
|
| 9 |
**Cargo.toml:** |
| 10 |
|
| 11 |
```toml |
| 12 |
[build-dependencies] |
| 13 |
cfg_aliases = "0.2.1" |
| 14 |
``` |
| 15 |
|
| 16 |
**build.rs:** |
| 17 |
|
| 18 |
```rust |
| 19 |
use cfg_aliases::cfg_aliases; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
// Setup cfg aliases |
| 23 |
cfg_aliases! { |
| 24 |
// Platforms |
| 25 |
wasm: { target_arch = "wasm32" }, |
| 26 |
android: { target_os = "android" }, |
| 27 |
macos: { target_os = "macos" }, |
| 28 |
linux: { target_os = "linux" }, |
| 29 |
// Backends |
| 30 |
surfman: { all(unix, feature = "surfman", not(wasm)) }, |
| 31 |
glutin: { all(feature = "glutin", not(wasm)) }, |
| 32 |
wgl: { all(windows, feature = "wgl", not(wasm)) }, |
| 33 |
dummy: { not(any(wasm, glutin, wgl, surfman)) }, |
| 34 |
} |
| 35 |
} |
| 36 |
``` |
| 37 |
|
| 38 |
Now that we have our aliases setup we can use them just like you would expect: |
| 39 |
|
| 40 |
```rust |
| 41 |
#[cfg(wasm)] |
| 42 |
println!("This is running in WASM"); |
| 43 |
|
| 44 |
#[cfg(surfman)] |
| 45 |
{ |
| 46 |
// Do stuff related to surfman |
| 47 |
} |
| 48 |
|
| 49 |
#[cfg(dummy)] |
| 50 |
println!("We're in dummy mode, specify another feature if you want a smarter app!"); |
| 51 |
``` |
| 52 |
|
| 53 |
This greatly improves what would otherwise look like this without the aliases: |
| 54 |
|
| 55 |
```rust |
| 56 |
#[cfg(target_arch = "wasm32")] |
| 57 |
println!("We're running in WASM"); |
| 58 |
|
| 59 |
#[cfg(all(unix, feature = "surfman", not(target_arch = "wasm32")))] |
| 60 |
{ |
| 61 |
// Do stuff related to surfman |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(not(any( |
| 65 |
target_arch = "wasm32", |
| 66 |
all(unix, feature = "surfman", not(target_arch = "wasm32")), |
| 67 |
all(windows, feature = "wgl", not(target_arch = "wasm32")), |
| 68 |
all(feature = "glutin", not(target_arch = "wasm32")), |
| 69 |
)))] |
| 70 |
println!("We're in dummy mode, specify another feature if you want a smarter app!"); |
| 71 |
``` |
| 72 |
|
| 73 |
You can also use the `cfg!` macro or combine your aliases with other checks using `all()`, `not()`, and `any()`. Your aliases are genuine `cfg` flags now! |
| 74 |
|
| 75 |
```rust |
| 76 |
if cfg!(glutin) { |
| 77 |
// use glutin |
| 78 |
} else { |
| 79 |
// Do something else |
| 80 |
} |
| 81 |
|
| 82 |
#[cfg(all(glutin, surfman))] |
| 83 |
compile_error!("You cannot specify both `glutin` and `surfman` features"); |
| 84 |
``` |
| 85 |
|
| 86 |
## Syntax and Error Messages |
| 87 |
|
| 88 |
The aliase names are restricted to the same rules as rust identifiers which, for one, means that they cannot have dashes ( `-` ) in them. Additionally, if you get certain syntax elements wrong, such as the alias name, the macro will error saying that the recursion limit was reached instead of giving a clear indication of what actually went wrong. This is due to a nuance with the macro parser and it might be fixed in a later release of this crate. It is also possible that aliases with dashes in the name might be supported in a later release. Open an issue if that is something that you would like implemented. |
| 89 |
|
| 90 |
Finally, you can also induce an infinite recursion by having rules that both reference each-other, but this isn't a real limitation because that doesn't make logical sense anyway: |
| 91 |
|
| 92 |
```rust,ignore |
| 93 |
// This causes an error! |
| 94 |
cfg_aliases! { |
| 95 |
test1: { not(test2) }, |
| 96 |
test2: { not(test1) }, |
| 97 |
} |
| 98 |
``` |
| 99 |
|
| 100 |
## Attribution and Thanks |
| 101 |
|
| 102 |
- Thanks to my God and Father who led me through figuring this out and to whome I owe everything. |
| 103 |
- Thanks to @Yandros on the Rust forum for [showing me][sm] some crazy macro hacks! |
| 104 |
- Thanks to @sfackler for [pointing out][po] the way to make cargo add the cfg flags. |
| 105 |
- Thanks to the authors of the [`tectonic_cfg_support::target_cfg`] macro from which most of the cfg attribute parsing logic is taken from. Also thanks to @ratmice for [bringing it up][bip] on the Rust forum. |
| 106 |
|
| 107 |
[`tectonic_cfg_support::target_cfg`]: https://docs.rs/tectonic_cfg_support/0.0.1/src/tectonic_cfg_support/lib.rs.html#166-298 |
| 108 |
[po]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/2 |
| 109 |
[bip]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/13 |
| 110 |
[sm]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/3 |