Conditional compilation for debug/release

Hi,

I'm building a small tool to detect code injection in Unity assemblies, and I'd like to use compiler directives for debug and release builds. Something like;

#[cfg(build = "debug")]
fn do_something(){
// Output sensitive debugging info
}

#[cfg(build = "release")]
fn do_something(){
// Don't output anything
}

Is there a way to do this in Rust? I've searched around the web, but I guess I'm missing some fundamentally different concept in Rust.

Best regards,
Shaun

12 Likes

Still looking for a good way to do this. I'm surprised nobody else is interested in conditional compilation for debug and release builds.

5 Likes

You can use cfg(debug_assertions) but there should probably be a cfg(debug).

8 Likes

Thanks. How did you know about "debug_assertions"? I read through the reference and doco but never came across that one.

1 Like

I looked at the source code for the debug_assert macro (so this cfg may not be very stable).

There really should be a #[cfg(debug)] currently you can define something like #[cfg(feature= "debug")] but you than have to manually add --features debug to your cargo build.

On the other hand, I can see why you would want an explicit call.

edit: hihi, I misjudged the age of this thread, reading "Apr'15" I though this was only 3 days old :stuck_out_tongue:

5 Likes

You can use the following build script

use std::env;

pub fn main() {
    if let Ok(profile) = env::var("PROFILE") {
        println!("cargo:rustc-cfg=build={:?}", profile);
    }
}
7 Likes

The debug_assertions config option seems to be the legit way to do this.

It is mentioned in the rust reference: https://github.com/rust-lang/rust/blob/master/src/doc/reference.md#conditional-compilation
And it is mentioned in the cargo docs: Page Moved

2 Likes