[][src]Macro cfg_aliases::cfg_aliases

macro_rules! cfg_aliases {
    (@cfg_is_set $cfgname:ident) => { ... };
    (@cfg_has_feature $feature:expr) => { ... };
    (@cfg_contains $cfgname:ident = $cfgvalue:expr) => { ... };
    (
        @parser_emit
        all
        $({$($grouped:tt)+})+
    ) => { ... };
    (
        @parser_emit
        any
        $({$($grouped:tt)+})+
    ) => { ... };
    (
        @parser_clause
        $op:ident
        [$({$($grouped:tt)+})*]
        [, $($rest:tt)*]
        $($current:tt)+
    ) => { ... };
    (
        @parser_clause
        $op:ident
        [$({$($grouped:tt)+})*]
        [$tok:tt $($rest:tt)*]
        $($current:tt)*
    ) => { ... };
    (
        @parser_clause
        $op:ident
        [$({$($grouped:tt)+})*]
        []
        $($current:tt)+
    ) => { ... };
    (
        @parser
        all($($tokens:tt)+)
    ) => { ... };
    (
        @parser
        any($($tokens:tt)+)
    ) => { ... };
    (
        @parser
        not($($tokens:tt)+)
    ) => { ... };
    (@parser feature = $value:expr) => { ... };
    (@parser $key:ident = $value:expr) => { ... };
    (@parser $e:ident) => { ... };
    (
        @with_dollar[$dol:tt]
        $( $alias:ident : { $($config:tt)* } ),* $(,)?
    ) => { ... };
    ($($tokens:tt)*) => { ... };
}

Create cfg aliases

build.rs:

// Setup cfg aliases
cfg_aliases! {
    // Platforms
    wasm: { target_arch = "wasm32" },
    android: { target_os = "android" },
    macos: { target_os = "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)) },
}

After you put this in your build script you can then check for those conditions like so:

#[cfg(surfman)]
{
    // Do stuff related to surfman
}

#[cfg(dummy)]
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:

#[cfg(all(unix, feature = "surfman", not(target_arch = "wasm32")))]
{
    // Do stuff related to surfman
}

#[cfg(not(any(
    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!");