6
votes

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.

1
whether we're compiling for a build script — this doesn't make sense. A build script is only contained within a special file (e.g. build.rs). There is no other context that it can be compiled in. - Shepmaster
Thanks for your answer. I'll explain what situation I'd like to use it for: It's for generating shell completion scripts through clap at compile time. I've quite a comprehensive App definition across multiple files (in the main application). I want to pull this in in build.rs to generate the definitions. This pulls some dependencies with it, which I'd like to exclude when used by the build.rs file. (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ée
which I'd like to exclude when used by the build.rs file — why? You will have to build these for the main application anyway - Shepmaster
It requires me to define quite a few dev-dependencies. Along with that, it feels unnecessary include quite a lot of code that isn't covered in the build.rs script. I believe the script should be as simple as possible, without pulling in the whole main application. I'd like to refrain from defining App a second time, and want to prevent duplicate code. - Tim Visée

1 Answers

1
votes

You can just put the build code in build.rs (or presumably have build.rs declare a mod xyz to pull in another file).

I wonder if the question you are trying to ask is whether you can reference the same code from build.rs and main.rs, and if so can that code tell if it's being called by one or the other. It seems you could switch on an environment variable set when using build.rs (using something like option_env, but possibly a nicer way might be to enable a feature in the main code from within build.rs.

(Have a read of the documentation for build scripts if you haven't already.)