Skip to content

Commit ab2ba7e

Browse files
committed
Pass CARGO_CFG_DEBUG_ASSERTIONS to build scripts based on profile setting
1 parent 284eefc commit ab2ba7e

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

crates/build-rs/src/input.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ mod cfg {
172172
}
173173

174174
/// If we are compiling with debug assertions enabled.
175-
///
176-
/// Build scripts are not passed this cfg because
177-
/// this cfg is always true and misleading.
178-
/// That is because Cargo queries rustc without any profile settings.
179-
#[cfg(any())]
180175
#[track_caller]
181176
pub fn cargo_cfg_debug_assertions() -> bool {
182177
ENV.is_present("CARGO_CFG_DEBUG_ASSERTIONS")

src/cargo/core/compiler/custom_build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
430430
cmd.env(&k, v.join(","));
431431
}
432432

433+
// Manually set CARGO_CFG_DEBUG_ASSERTIONS based on the profile's debug_assertions setting.
434+
// This is set separately because the cfg query from rustc doesn't include profile settings
435+
// and would always be true (see filtering above). Build scripts need the actual profile
436+
// setting to properly configure C/C++ code with matching assertion behavior.
437+
if unit.profile.debug_assertions {
438+
cmd.env("CARGO_CFG_DEBUG_ASSERTIONS", "");
439+
}
440+
433441
// Also inform the build script of the rustc compiler context.
434442
if let Some(wrapper) = bcx.rustc().wrapper.as_ref() {
435443
cmd.env("RUSTC_WRAPPER", wrapper);

src/cargo/core/profiles.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl Profiles {
330330
result.root = for_unit_profile.root;
331331
result.debuginfo = for_unit_profile.debuginfo;
332332
result.opt_level = for_unit_profile.opt_level;
333+
result.debug_assertions = for_unit_profile.debug_assertions;
333334
result.trim_paths = for_unit_profile.trim_paths.clone();
334335
result
335336
}

0 commit comments

Comments
 (0)