Skip to content

Allow build scripts to detect debug_assertions setting #15760

@Patrick-6

Description

@Patrick-6

Problem

There seems to be no way to reliably detect whether debug_assertions are enabled from a build script.

One use case for this is building a C or C++ library from the build script, where debug assertions should be enabled or not in correspondence with the setting in the currently used Rust profile (i.e., is debug_assert!(...) enabled).

Cargo currently sets some environment variables about the build configuration (see Environment variables Cargo sets for build scripts). These include:

  • PROFILE, which can be used as a proxy for whether debug_assertions is enabled or not, but this is not correct if the debug-assertions setting is not the default for a given profile. The Cargo documentation also recommends not using this environment variable.
  • OPT_LEVEL and DEBUG, which I assume correspond opt-level and debug in Cargo.toml.

There is no DEBUG_ASSERTIONS (corresponding to debug-assertions in Cargo.toml).

Proposed Solution

Set the DEBUG_ASSERTIONS environment variable for build scripts according to the current profile.

Notes

There was a discussion about this topic on Zulip in 2023, which mentions some other non-working ways to detect this setting.

One way mentioned there is the CARGO_CFG_DEBUG_ASSERTIONS env var, which is filtered by Cargo since #7943 for reasons mentioned in #7933.

I don't know whether these reasons would also apply to a dedicated DEBUG_ASSERTIONS env var.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions