Skip to content

Commit 2ff39fe

Browse files
committed
[Bazel] Make LLVM config headers configurable
This change implements the following flags and provides the infrastructure for future additions: bazel query 'kind(".*_flag", @llvm-project//llvm/config:all)' @llvm-project//llvm/config:LLVM_ENABLE_BACKTRACES @llvm-project//llvm/config:LLVM_ENABLE_CRASH_DUMPS @llvm-project//llvm/config:LLVM_ENABLE_CRASH_OVERRIDES @llvm-project//llvm/config:LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING @llvm-project//llvm/config:LLVM_ENABLE_PLUGINS @llvm-project//llvm/config:LLVM_ENABLE_ZLIB @llvm-project//llvm/config:LLVM_ENABLE_ZSTD @llvm-project//llvm/config:LLVM_HAS_LOGF128 @llvm-project//llvm/config:LLVM_WINDOWS_PREFER_FORWARD_SLASH For instance: bazel build ... \ --@llvm-project//llvm/config:LLVM_ENABLE_BACKTRACES=false The following flags have been moved to mirror CMake and simplify migration to bzlmod: @llvm_zlib//:llvm_enable_zlib -> @llvm-project//llvm/config:LLVM_ENABLE_ZLIB @llvm_zstd//:llvm_enable_zstd -> @llvm-project//llvm/config:LLVM_ENABLE_ZSTD The overlay now has platform definitions at `@llvm-project//platform`. You can use these to cross compile the config headers to see how they'd look like on other systems. For instance: bazel build @llvm-project//llvm:config_h \ --platforms=@llvm-project//platform:aarch64-unknown-linux-gnu bazel build @llvm-project//llvm:llvm-config_h \ --platforms=@llvm-project//platform:x86_64-pc-win32 The new implementation uses the original CMake templates as source of truth. This makes it easier to detect and prevent drift as unhandled substitutions tend to turn into compiler errors. Saves 16 defines on windows and ~35 defines on other platforms which previously leaked into the command lines of all targets that depended transitively on the llvm configuration. For a full build of the overlay this amounts to ~380k fewer defines.
1 parent 918848d commit 2ff39fe

File tree

17 files changed

+829
-1286
lines changed

17 files changed

+829
-1286
lines changed

utils/bazel/.bazelrc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ build --experimental_cc_shared_library
5858
# https://github.com/bazelbuild/bazel/commit/03246077f948f2790a83520e7dccc2625650e6df
5959
build --build_runfile_links=false
6060

61-
###############################################################################
62-
# Options to select different strategies for linking potential dependent
63-
# libraries. The default leaves it disabled.
64-
###############################################################################
65-
66-
build:zlib_external --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=external
67-
build:zlib_system --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=system
68-
6961
###############################################################################
7062
# Options for "generic_clang" builds: these options should generally apply to
7163
# builds using a Clang-based compiler, and default to the `clang` executable on

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ cc_library(
111111
"//llvm:TargetParser",
112112
"//llvm:TransformUtils",
113113
"//llvm:config",
114-
"@llvm_zlib//:zlib",
115-
"@llvm_zstd//:zstd",
116-
],
114+
] + select({
115+
"//llvm/config:LLVM_ENABLE_ZLIB_disabled": [],
116+
"//conditions:default": ["@llvm_zlib//:zlib"],
117+
}) + select({
118+
"//llvm/config:LLVM_ENABLE_ZSTD_disabled": [],
119+
"//conditions:default": ["@llvm_zstd//:zstd"],
120+
}),
117121
)
118122

119123
gentbl(

utils/bazel/llvm-project-overlay/lldb/source/Plugins/BUILD.bazel

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,10 @@ cc_library(
493493
"//llvm:Support",
494494
"//llvm:TargetParser",
495495
"//llvm:config",
496-
"@llvm_zlib//:zlib",
497-
],
496+
] + select({
497+
"//llvm/config:LLVM_ENABLE_ZLIB_disabled": [],
498+
"//conditions:default": ["@llvm_zlib//:zlib"],
499+
}),
498500
)
499501

500502
cc_library(
@@ -1416,8 +1418,10 @@ cc_library(
14161418
"//lldb:Utility",
14171419
"//llvm:Support",
14181420
"//llvm:config",
1419-
"@llvm_zlib//:zlib",
1420-
],
1421+
] + select({
1422+
"//llvm/config:LLVM_ENABLE_ZLIB_disabled": [],
1423+
"//conditions:default": ["@llvm_zlib//:zlib"],
1424+
}),
14211425
)
14221426

14231427
cc_library(

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

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
77
load("@rules_python//python:defs.bzl", "py_binary")
88
load("//mlir:tblgen.bzl", "gentbl_cc_library", "gentbl_filegroup", "td_library")
99
load(":binary_alias.bzl", "binary_alias")
10-
load(":config.bzl", "llvm_config_defines")
10+
load(
11+
":config.bzl",
12+
"CONFIG_H_SUBSTITUTIONS",
13+
"LLVM_CONFIG_H_SUBSTITUTIONS",
14+
)
1115
load(":driver.bzl", "generate_driver_selects", "generate_driver_tools_def", "llvm_driver_cc_binary", "select_driver_tools")
1216
load(":enum_targets_gen.bzl", "enum_targets_gen")
1317
load(":targets.bzl", "llvm_targets")
@@ -153,13 +157,6 @@ expand_template(
153157
template = "include/llvm/Config/abi-breaking.h.cmake",
154158
)
155159

156-
# To enable diff testing out of tree
157-
exports_files([
158-
"include/llvm/Config/config.h.cmake",
159-
"include/llvm/Config/llvm-config.h.cmake",
160-
"include/llvm/Config/abi-breaking.h.cmake",
161-
])
162-
163160
td_library(
164161
name = "OptParserTdFiles",
165162
srcs = ["include/llvm/Option/OptParser.td"],
@@ -171,14 +168,52 @@ llvm_config_target_defines = [
171168
for t in llvm_targets
172169
]
173170

171+
expand_template(
172+
name = "config_h",
173+
out = "include/llvm/Config/config.h",
174+
substitutions = CONFIG_H_SUBSTITUTIONS,
175+
template = "include/llvm/Config/config.h.cmake",
176+
)
177+
178+
expand_template(
179+
name = "llvm-config_h",
180+
out = "include/llvm/Config/llvm-config.h",
181+
substitutions = LLVM_CONFIG_H_SUBSTITUTIONS,
182+
template = "include/llvm/Config/llvm-config.h.cmake",
183+
)
184+
174185
cc_library(
175186
name = "config",
176187
hdrs = [
177188
"include/llvm/Config/abi-breaking.h",
189+
"include/llvm/Config/config.h",
178190
"include/llvm/Config/llvm-config.h",
179191
],
180192
copts = llvm_copts,
181-
defines = llvm_config_defines + llvm_config_target_defines,
193+
defines = select({
194+
"@platforms//os:windows": [
195+
"_CRT_SECURE_NO_DEPRECATE",
196+
"_CRT_SECURE_NO_WARNINGS",
197+
"_CRT_NONSTDC_NO_DEPRECATE",
198+
"_CRT_NONSTDC_NO_WARNINGS",
199+
"_SCL_SECURE_NO_DEPRECATE",
200+
"_SCL_SECURE_NO_WARNINGS",
201+
"UNICODE",
202+
"_UNICODE",
203+
],
204+
"//conditions:default": [
205+
"_GNU_SOURCE",
206+
],
207+
}) + [
208+
# These shouldn't be needed by the C++11 standard, but are for some
209+
# platforms (e.g. glibc < 2.18. See
210+
# https://sourceware.org/bugzilla/show_bug.cgi?id=15366). These are also
211+
# included unconditionally in the CMake build:
212+
# https://github.com/llvm/llvm-project/blob/cd0dd8ece8e/llvm/cmake/modules/HandleLLVMOptions.cmake#L907-L909
213+
"__STDC_LIMIT_MACROS",
214+
"__STDC_CONSTANT_MACROS",
215+
"__STDC_FORMAT_MACROS",
216+
],
182217
includes = ["include"],
183218
textual_hdrs = [
184219
"include/llvm/Config/AsmParsers.def",
@@ -316,17 +351,15 @@ cc_library(
316351
"include/llvm/Support/*.def",
317352
]),
318353
deps = [
319-
":config",
320354
":Demangle",
321-
# We unconditionally depend on the custom LLVM zlib wrapper. This will
322-
# be an empty library unless zlib is enabled, in which case it will
323-
# both provide the necessary dependencies and configuration defines.
324-
"@llvm_zlib//:zlib",
325-
# We unconditionally depend on the custom LLVM zstd wrapper. This will
326-
# be an empty library unless zstd is enabled, in which case it will
327-
# both provide the necessary dependencies and configuration defines.
328-
"@llvm_zstd//:zstd",
329-
],
355+
":config",
356+
] + select({
357+
"//llvm/config:LLVM_ENABLE_ZLIB_disabled": [],
358+
"//conditions:default": ["@llvm_zlib//:zlib"],
359+
}) + select({
360+
"//llvm/config:LLVM_ENABLE_ZSTD_disabled": [],
361+
"//conditions:default": ["@llvm_zstd//:zstd"],
362+
}),
330363
)
331364

332365
# Note: although FileCheck (the binary) is a test utility, some non-test
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
"""Helper functions to resolve CMake templates."""
6+
7+
load("@bazel_skylib//lib:selects.bzl", "selects")
8+
9+
def cmakedefine_string(define, value):
10+
"""Translate `#cmakedefine DEFINE "${DEFINE}"` to `#define DEFINE "VALUE"`.
11+
12+
Args:
13+
define: The string of a configurable define that may be set to a string
14+
value.
15+
16+
Returns:
17+
A dict like
18+
`{'#cmakedefine DEFINE "${DEFINE}"': '#define DEFINE "VALUE"'}`.
19+
"""
20+
return {
21+
'#cmakedefine {} "${{{}}}"'.format(
22+
define,
23+
define,
24+
): '#define {} "{}"'.format(define, value),
25+
}
26+
27+
def cmakedefine_set(define):
28+
"""Translate `#cmakedefine DEFINE ${DEFINE}` to `#define DEFINE 1`.
29+
30+
Args:
31+
define: The string of a configurable define that may be set to 1.
32+
33+
Returns:
34+
A dict like `{"#cmakedefine DEFINE ${DEFINE}": "#define DEFINE 1"}`.
35+
"""
36+
37+
return {
38+
"#cmakedefine {} ${{{}}}".format(
39+
define,
40+
define,
41+
): "#define {} 1".format(define),
42+
}
43+
44+
def cmakedefine_unset(define):
45+
"""Translate `#cmakedefine DEFINE ${DEFINE}` to `/* #undef DEFINE */`.
46+
47+
Args:
48+
define: The string of a configurable define that may be unset or 1.
49+
50+
Returns:
51+
A dict like `{"#cmakedefine DEFINE ${DEFINE}": "/* #undef DEFINE */"}`.
52+
"""
53+
return {
54+
"#cmakedefine {} ${{{}}}".format(
55+
define,
56+
define,
57+
): "/* #undef {} */".format(define),
58+
}
59+
60+
def cmakedefine(
61+
define,
62+
enable = "//conditions:default",
63+
disable = "//conditions:default"):
64+
"""Translate `#cmakedefine DEFINE ${DEFINE}` to `#define DEFINE 1` or
65+
`/* #undef DEFINE */`.
66+
67+
Args:
68+
define: The string of a configurable define that may be unset or set to
69+
a value.
70+
enable: A `Label` or tuple of `Labels` declaring conditions that set
71+
the define to the value. Defaults to `//conditions:default`.
72+
disable: A `Label` or tuple of `Labels` declaring conditions that leave
73+
the define undefined. Defaults to `//conditions:default`.
74+
75+
Returns:
76+
A `selects.with_or` of the structure:
77+
78+
selects.with_or({
79+
enable: {
80+
"#cmakedefine DEFINE ${DEFINE}": "#define DEFINE 1",
81+
},
82+
disable: {
83+
"#cmakedefine DEFINE ${DEFINE}": "/* #undef DEFINE */",
84+
},
85+
})
86+
87+
Raises an error if `enable` and `disable` are both left at their defaults.
88+
"""
89+
return selects.with_or({
90+
enable: cmakedefine_set(define),
91+
disable: cmakedefine_unset(define),
92+
})
93+
94+
def cmakedefine01_on(define):
95+
"""Translate `#cmakedefine01 DEFINE` to `#define DEFINE 1`.
96+
97+
Args:
98+
define: The string of a configurable define that may be 0 or 1.
99+
100+
Returns:
101+
A dict like `{"#cmakedefine01 DEFINE": "#define DEFINE 1"}`.
102+
"""
103+
return {
104+
"#cmakedefine01 {}".format(define): "#define {} 1".format(define),
105+
}
106+
107+
def cmakedefine01_off(define):
108+
"""Translate `#cmakedefine01 DEFINE` to `#define DEFINE 0`.
109+
110+
Args:
111+
define: The string of a configurable define that may be 0 or 1.
112+
113+
Returns:
114+
A dict like `{"#cmakedefine01 DEFINE": "#define DEFINE 0"}`.
115+
"""
116+
return {
117+
"#cmakedefine01 {}".format(define): "#define {} 0".format(define),
118+
}
119+
120+
def cmakedefine01(
121+
define,
122+
enable = "//conditions:default",
123+
disable = "//conditions:default"):
124+
"""Translate `#cmakedefine01 DEFINE` to `#define DEFINE 1` or
125+
`#define DEFINE 0`.
126+
127+
Args:
128+
define: The string of a configurable define that may be set to 0 or 1.
129+
enable: A `Label` or tuple of `Labels` declaring conditions that set
130+
the define to 1. Defaults to `//conditions:default`.
131+
disable: A `Label` or tuple of `Labels` declaring conditions that set
132+
the define to 0. Defaults to `//conditions:default`.
133+
134+
Returns:
135+
A `selects.with_or` of the structure:
136+
137+
selects.with_or({
138+
enable: {"#cmakedefine01 DEFINE": "#define DEFINE 1"},
139+
disable: {"#cmakedefine01 DEFINE": "#define DEFINE 0"},
140+
})
141+
142+
Raises an error if `enable` and `disable` are both left at their defaults.
143+
"""
144+
return selects.with_or({
145+
enable: cmakedefine01_on(define),
146+
disable: cmakedefine01_off(define),
147+
})

0 commit comments

Comments
 (0)