Define #[cfg] variables?


Is it possible to use #[cfg] or #[cfg_attr] to define a variable which can be used later in other #[cfg] blocks?

For example, I have this really unwieldy #[cfg] that I use multiple times:

#[cfg(all(not(target_os="linux"), not(target_os="macos"), not(target_arch = "x86_64")))]

And in later places, I use it nested in a larger cfg (in this case, it’s the x in any(test, x)):

#[cfg(any(test, all(not(target_os="linux"), not(target_os="macos"), not(target_arch = "x86_64"))))]

It’d be nice if there were a way to do something like this:

#![cfg_attr(all(not(target_os="linux"), not(target_os="macos"), not(target_arch = "x86_64")), foo)]


#[cfg(any(test, foo))]

Is something like that possible?


You can write a build script which can set new cfgs:


Unfortunately that’s more heavyweight than what I’m going for here, but thanks for the tip! That will probably come in handy in the future.