How to check if building code with/without LTO and panic=abort/unwind?

I want to log information about the compilation settings used for my app. Specifically, I want to determine whether LTO was enabled, whether panic=unwind or panic=abort was used, and possibly other relevant flags. Is there a way to embed this information into the binary?

Example:

fn log_basic_data() {
    info!(
        "LTO used: {}, panic mode: {}",
        build_params.get("lto"),
        build_params.get("panic")
    );
}
2 Likes

I'm not sure about the LTO setting, but you can read the environment variable CARGO_CFG_PANIC in a build script, and write it to a generated rust file in OUT_DIR.

2 Likes

Inside the main source files you can use cfg!(panic = "abort"). As for LTO you need to use a build script and check if the CARGO_ENCODED_RUSTFLAGS env var contains -Clto.

2 Likes