Conditional compilation for debug/release


#1

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


Conditional Returns in test build
#2

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


#3

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


#4

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


#5

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


#6

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:


#7

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);
    }
}

#8

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: http://doc.crates.io/manifest.html#the-profile-sections