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 comile times should be negligible.
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)]
.
Cargo.toml:
toml
[build-dependencies]
cfg_aliases = "0.1.0-alpha.1"
build.rs:
```rust use cfgaliases::cfgaliases;
fn main() { // Setup cfg aliases cfgaliases! { // Platforms wasm: { targetarch = "wasm32" }, android: { targetos = "android" }, macos: { targetos = "macos" }, linux: { target_os = "linux" }, // Backends surfman: { all(unix, feature = "surfman", not(wasm)) }, glutin: { all(feature = "glutin", not(wasm)) }, wgl: { all(windows, feature = "wgl", not(wasm)) }, dummy: { not(any(wasm, glutin, wgl, surfman)) }, } } ```
Now that we have our aliases setup we can use them just like you would expect:
```rust
println!("This is running in WASM");
{ // Do stuff related to surfman }
println!("We're in dummy mode, specify another feature if you want a smarter app!"); ```
This greatly improves what would otherwise look like this without the aliases:
```rust
println!("We're running in WASM");
{ // Do stuff related to surfman }
target_arch = "wasm32",
all(unix, feature = "surfman", not(target_arch = "wasm32")),
all(windows, feature = "wgl", not(target_arch = "wasm32")),
all(feature = "glutin", not(target_arch = "wasm32")),
)))] println!("We're in dummy mode, specify another feature if you want a smarter app!"); ```
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!
```rust if cfg!(glutin) { // use glutin } else { // Do something else }
compile_error!("You cannot specify both glutin
and surfman
features");
```
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 on the Rust forum.