Conditional compilation of feature gates?


#1

Is there a way to do conditional compilation of the feature gate attribute? My use case here is I have a crate using LLVM intrinsics, but I also have written a Rust version of the intrinsic. I want to be able to use the intrinsic version when compiling on nightly Rust, but the non-intrinsic version on stable. Unfortunately, since LLVM intrinsics are feature gated, I have to have a #![feature(link_llvm_intrinsics)] attribute on my crate. I’ve setup a Cargo.toml feature ‘use-intrinsics’ for conditional compilation to choose between the different versions, but I can’t do the same for the feature gate attribute. Is there a way to accomplish this? I would really like this to available on stable, but can’t if I have to have that feature gate.


#2

Yep, cfg_attr will do this for you:

#![cfg_attr(feature = "use-intrinsics", feature(link_llvm_intrinsics))]

#3

That worked perfectly, thank you!