Search code examples
buildrustembedded

How to access current cargo profile (debug/release, …) from the build script (build.rs)


In an embedded project, I usually run the debug mode with qemu, but need to build the release for a concrete microcontroller.

The build.rs would need to know what the actual mode is (debug or release) to generate the correct memory layout.

How can the build.rs make this decision?

Related: How to access current cargo profile (build, test, bench, doc, ....) from the build script (build.rs)


Solution

  • It's written in the doc:

    PROFILE - "release" for release builds, "debug" for other builds.

    This goes into build.rs:

    fn main() {
        let profile = std::env::var("PROFILE").unwrap();
        match profile.as_str() {
            "debug" => (),
            "release" => (),
            _ => (),
        }
    }