The Rust language supports conditional compilation using attributes like #[cfg(test)].
Rust also supports build scripts using a build.rs file to run code as part of the build process to prepare for compilation.
I would like to use conditional compilation in Rust code to conditionally compile depending on whether we're compiling for a build script, similar to how that is possible for test builds.
Imagine the following:
#[cfg(build)]
fn main() {
// Part of build script
}
#[cfg(not(build))]
fn main() {
// Not part of build script, probably regular build
}
This does not work, because build is not a valid identifier here.
Is it possible to do this using a different attribute, or could some other trick be used to achieve something similar?
For some context on this issue:
My goal is to generate shell completion scripts through clap at compile time. I've quite a comprehensive App definition across multiple files in the application. I'd like to use this in build.rs by including these parts using the include!(...) macro (as suggested by clap), so I don't have to define App a second time. This pulls some dependencies with it, which I'd like to exclude when used by the build.rs file as they aren't needed in that case. This is what I'm trying to make available in my build.rs script.
build.rs). There is no other context that it can be compiled in. - Shepmasterclapat compile time. I've quite a comprehensiveAppdefinition across multiple files (in the main application). I want to pull this in inbuild.rsto generate the definitions. This pulls some dependencies with it, which I'd like to exclude when used by thebuild.rsfile. (what is suggested: docs.rs/clap/2.32.0/clap/struct.App.html#examples-47) (what I want to use: github.com/timvisee/ffsend/blob/master/src/cmd/handler.rs#L51) - Tim Viséedev-dependencies. Along with that, it feels unnecessary include quite a lot of code that isn't covered in thebuild.rsscript. I believe the script should be as simple as possible, without pulling in the whole main application. I'd like to refrain from definingAppa second time, and want to prevent duplicate code. - Tim Visée