Skip to content

Commit e2ee8bf

Browse files
committed
[Bazel][GN] Reuse the GN LLVM config file generation code
Currently, the Bazel build uses static, checked in [llvm-]config.h files in combination with global macro definitions to mimic CMake's generated headers. This change reuses the write_cmake_config.py script from the GN build to generate the headers from source in the same way. The purpose is to ensure that the Bazel build stays up to date with any changes to the CMake config files. The write_cmake_config.py script has good error checking to ensure that unneeded, stale variables are not passed, and that any missing variables are reported as errors. I tried to closely follow the logic in the GN build here: llvm/utils/gn/secondary/llvm/include/Config/BUILD.gn The duplication between this file and config.bzl is significant, and we could consider going further, but I'd like to hold off on it for now. The GN build changes are to move the write_cmake_config.py script up to //llvm/utils/write_cmake_config.py, and update the paths accordingly. The next logical change is to generate Clang's config.h header. Differential Revision: https://reviews.llvm.org/D126581
1 parent 3c31c68 commit e2ee8bf

File tree

7 files changed

+315
-580
lines changed

7 files changed

+315
-580
lines changed

llvm/utils/gn/build/write_cmake_config.gni

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ template("write_cmake_config") {
2828
assert(defined(invoker.values), "must set 'values' in $target_name")
2929

3030
action(target_name) {
31-
script = "//llvm/utils/gn/build/write_cmake_config.py"
31+
script = "//llvm/utils/write_cmake_config.py"
3232

3333
sources = [ invoker.input ]
3434
outputs = [ invoker.output ]

llvm/utils/gn/build/write_cmake_config.py renamed to llvm/utils/write_cmake_config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
r"""Emulates the bits of CMake's configure_file() function needed in LLVM.
33
44
The CMake build uses configure_file() for several things. This emulates that
5-
function for the GN build. In the GN build, this runs at build time instead
6-
of at generator time.
5+
function for alternative build systems such as GN and Bazel. In both GN and
6+
Bazel, config file generation happens during the build rather than before it.
77
88
Takes a list of KEY=VALUE pairs (where VALUE can be empty).
99
@@ -28,8 +28,9 @@
2828
#define FOO [...]
2929
/* #undef FOO */
3030
31-
Fails if any of the KEY=VALUE arguments aren't needed for processing the
32-
input file, or if the input file references keys that weren't passed in.
31+
To ensure that config file changes do not go unnoticed, this script fails if
32+
any of the KEY=VALUE arguments aren't needed for processing the input file, or
33+
if the input file references keys that weren't passed in.
3334
"""
3435

3536
import argparse

utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
load(":template_rule.bzl", "template_rule")
5+
load(":write_cmake_config.bzl", "write_cmake_config")
66
load(":tblgen.bzl", "gentbl")
7-
load(":config.bzl", "llvm_config_defines")
7+
load(":config.bzl", "config_h_values", "llvm_config_h_values", "llvm_global_defines")
88
load(":targets.bzl", "llvm_targets")
99
load(":enum_targets_gen.bzl", "enum_targets_gen")
1010
load(":binary_alias.bzl", "binary_alias")
@@ -101,17 +101,30 @@ genrule(
101101
cmd = "echo '#define LLVM_VERSION_INFO \"git\"' > $@",
102102
)
103103

104-
template_rule(
104+
write_cmake_config(
105105
name = "abi_breaking_h_gen",
106106
src = "include/llvm/Config/abi-breaking.h.cmake",
107107
out = "include/llvm/Config/abi-breaking.h",
108-
substitutions = {
109-
# Define to enable checks that alter the LLVM C++ ABI
110-
"#cmakedefine01 LLVM_ENABLE_ABI_BREAKING_CHECKS": "#define LLVM_ENABLE_ABI_BREAKING_CHECKS 0",
108+
# Currently, in the Bazel build, we opt out of ABI breaking checks and
109+
# reverse iteration.
110+
values = [
111+
"LLVM_ENABLE_ABI_BREAKING_CHECKS=",
112+
"LLVM_ENABLE_REVERSE_ITERATION=",
113+
],
114+
)
111115

112-
# Define to enable reverse iteration of unordered llvm containers
113-
"#cmakedefine01 LLVM_ENABLE_REVERSE_ITERATION": "#define LLVM_ENABLE_REVERSE_ITERATION 0",
114-
},
116+
write_cmake_config(
117+
name = "config_h_gen",
118+
src = "include/llvm/Config/config.h.cmake",
119+
out = "include/llvm/Config/config.h",
120+
values = config_h_values,
121+
)
122+
123+
write_cmake_config(
124+
name = "llvm_config_h_gen",
125+
src = "include/llvm/Config/llvm-config.h.cmake",
126+
out = "include/llvm/Config/llvm-config.h",
127+
values = llvm_config_h_values,
115128
)
116129

117130
# To enable diff testing out of tree
@@ -129,7 +142,7 @@ cc_library(
129142
"include/llvm/Config/llvm-config.h",
130143
],
131144
copts = llvm_copts,
132-
defines = llvm_config_defines,
145+
defines = llvm_global_defines,
133146
includes = ["include"],
134147
textual_hdrs = [
135148
"include/llvm/Config/AsmParsers.def",
@@ -2907,11 +2920,11 @@ cc_binary(
29072920
copts = llvm_copts,
29082921
stamp = 0,
29092922
deps = [
2910-
":Symbolize",
29112923
":BitReader",
29122924
":Core",
2913-
":Support",
29142925
":Debuginfod",
2926+
":Support",
2927+
":Symbolize",
29152928
],
29162929
)
29172930

@@ -4429,3 +4442,8 @@ cc_binary(
44294442
":Support",
44304443
],
44314444
)
4445+
4446+
py_binary(
4447+
name = "write_cmake_config",
4448+
srcs = ["utils/write_cmake_config.py"],
4449+
)

0 commit comments

Comments
 (0)