[llvm] [bazel] Add an LLVM assertions build setting (PR #203739)

David Zbarsky via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 13 19:12:54 PDT 2026


https://github.com/dzbarsky created https://github.com/llvm/llvm-project/pull/203739

LLVM's Bazel overlay always followed the selected compilation mode for `NDEBUG` and generated `LLVM_ENABLE_ABI_BREAKING_CHECKS=0`. This adds the default-off `//llvm:enable_assertions` build setting. Enabling the setting appends `-UNDEBUG` to `llvm_copts` and generates `LLVM_ENABLE_ABI_BREAKING_CHECKS=1`, matching CMake's default `LLVM_ABI_BREAKING_CHECKS=WITH_ASSERTS` behavior.

The default remains disabled, so existing Bazel builds and ABI remain unchanged. Assertion-enabled and assertion-disabled LLVM C++ artifacts are intentionally ABI-incompatible.

Usage:

```text
bazel build --//llvm:enable_assertions=true //llvm:llvm
```

Validation:

- `buildifier -mode=check utils/bazel/llvm-project-overlay/llvm/BUILD.bazel`
- `llvm-check-pr-format`


>From 38eec5f85a58f9548e5d028ee97b5f7151e5d1f6 Mon Sep 17 00:00:00 2001
From: David Zbarsky <dzbarsky at gmail.com>
Date: Sat, 13 Jun 2026 22:12:03 -0400
Subject: [PATCH] [bazel] Add an LLVM assertions build setting

Add the default-off //llvm:enable_assertions build setting. When the
setting is enabled, undefine NDEBUG for LLVM targets and generate
LLVM_ENABLE_ABI_BREAKING_CHECKS=1. This matches the default CMake
LLVM_ABI_BREAKING_CHECKS=WITH_ASSERTS behavior.

The default remains disabled, preserving existing Bazel builds and ABI.
---
 .../llvm-project-overlay/llvm/BUILD.bazel     | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
index 3893710cf3c8f..353f0f2267cf0 100644
--- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -2,7 +2,7 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
+load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
 load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
 load("@bazel_skylib//rules:write_file.bzl", "write_file")
 load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
@@ -26,6 +26,20 @@ package(
 
 licenses(["notice"])
 
+# Build LLVM with assertions. Usage:
+#   bazel build -- at llvm-project//llvm:enable_assertions=true @llvm-project//llvm:llvm
+bool_flag(
+    name = "enable_assertions",
+    build_setting_default = False,
+)
+
+config_setting(
+    name = "assertions_enabled",
+    flag_values = {
+        ":enable_assertions": "true",
+    },
+)
+
 exports_files([
     "LICENSE.TXT",
     "cmake/modules/llvm-driver-template.cpp.in",
@@ -103,7 +117,10 @@ config_setting(
 # this `Make` variable.
 llvm_copts = [
     "$(STACK_FRAME_UNLIMITED)",
-]
+] + select({
+    ":assertions_enabled": ["-UNDEBUG"],
+    "//conditions:default": [],
+})
 
 enum_targets_gen(
     name = "targets_def_gen",
@@ -205,12 +222,16 @@ expand_template(
     name = "abi_breaking_h_gen",
     out = "include/llvm/Config/abi-breaking.h",
     substitutions = {
-        # Define to enable checks that alter the LLVM C++ ABI
-        "#cmakedefine01 LLVM_ENABLE_ABI_BREAKING_CHECKS": "#define LLVM_ENABLE_ABI_BREAKING_CHECKS 0",
-
         # Define to enable reverse iteration of unordered llvm containers
         "#cmakedefine01 LLVM_ENABLE_REVERSE_ITERATION": "#define LLVM_ENABLE_REVERSE_ITERATION 0",
-    },
+    } | select({
+        ":assertions_enabled": {
+            "#cmakedefine01 LLVM_ENABLE_ABI_BREAKING_CHECKS": "#define LLVM_ENABLE_ABI_BREAKING_CHECKS 1",
+        },
+        "//conditions:default": {
+            "#cmakedefine01 LLVM_ENABLE_ABI_BREAKING_CHECKS": "#define LLVM_ENABLE_ABI_BREAKING_CHECKS 0",
+        },
+    }),
     template = "include/llvm/Config/abi-breaking.h.cmake",
 )
 



More information about the llvm-commits mailing list