-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
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 whetherdebug_assertions
is enabled or not, but this is not correct if thedebug-assertions
setting is not the default for a given profile. The Cargo documentation also recommends not using this environment variable.OPT_LEVEL
andDEBUG
, which I assume correspondopt-level
anddebug
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.