Reputation: 3248
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.
Upvotes: 12
Views: 5853
Reputation: 1080
Even though you'll get a warning and it might not be the best way to do this, you might have been looking for something along the lines of the following:
#[cfg(not(build))]
fn main() {
println!("cargo:rustc-cfg=build");
}
#[cfg(build)]
fn main() {
println!("Hello, world!");
}
When building this as a build script the build
cfg option is not used so the first main
function is ran. The output tells cargo to add a cfg option to rustc called build
whenever building this crate. When the same file is then built as the bin target the build
cfg option is turned on and hence the second main is the one that gets compiled.
Upvotes: 0
Reputation: 105133
This is what works for me:
fn main() {
if std::env::var("PROFILE").unwrap() == "debug" {
// Here I do something that is needed for tests
// only, not for 'release'
}
}
Upvotes: 0
Reputation: 1286
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.)
Upvotes: 2