Skip to content

Commit c15640a

Browse files
committed
[Bazel] Make LLVM and Clang config headers configurable
This change implements the following flags and provides the infrastructure for future additions: bazel query 'kind(".*_flag", @llvm-project//config:all)' @llvm-project//config:LLVM_ENABLE_BACKTRACES @llvm-project//config:LLVM_ENABLE_CRASH_DUMPS @llvm-project//config:LLVM_ENABLE_CRASH_OVERRIDES @llvm-project//config:LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING @llvm-project//config:LLVM_ENABLE_PLUGINS @llvm-project//config:LLVM_ENABLE_ZLIB @llvm-project//config:LLVM_ENABLE_ZSTD @llvm-project//config:LLVM_HAS_LOGF128 @llvm-project//config:LLVM_WINDOWS_PREFER_FORWARD_SLASH For instance: bazel build ... \ --@llvm-project//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//config:LLVM_ENABLE_ZLIB @llvm_zstd//:llvm_enable_zstd -> @llvm-project//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 0454dd8 commit c15640a

File tree

19 files changed

+943
-1408
lines changed

19 files changed

+943
-1408
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/clang/BUILD.bazel

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

5+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
6+
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
57
load("@rules_python//python:defs.bzl", "py_binary")
68
load(
79
"//:vars.bzl",
@@ -13,6 +15,16 @@ load(
1315
load("//:workspace_root.bzl", "workspace_root")
1416
load("//llvm:binary_alias.bzl", "binary_alias")
1517
load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
18+
load(
19+
"//config:cmakehelpers.bzl",
20+
"cmakedefine",
21+
"cmakedefine01_off",
22+
"cmakedefine01_on",
23+
"cmakedefine_sunset",
24+
"cmakedefine_unset",
25+
"cmakedefine_vset",
26+
"cmakedefine_vunset",
27+
)
1628
load("//llvm:driver.bzl", "llvm_driver_cc_binary")
1729
load("//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")
1830

@@ -614,6 +626,49 @@ genrule(
614626
),
615627
)
616628

629+
expand_template(
630+
name = "config_h",
631+
out = "include/clang/Config/config.h",
632+
substitutions = (
633+
{"${BUG_REPORT_URL}": "https://github.com/llvm/llvm-project/issues/"} |
634+
cmakedefine01_on("CLANG_DEFAULT_PIE_ON_LINUX") |
635+
{
636+
"${CLANG_DEFAULT_LINKER}": "",
637+
"${CLANG_DEFAULT_CXX_STDLIB}": "",
638+
"${CLANG_DEFAULT_RTLIB}": "",
639+
"${CLANG_DEFAULT_UNWINDLIB}": "",
640+
"${CLANG_DEFAULT_OBJCOPY}": "objcopy",
641+
"${CLANG_DEFAULT_OPENMP_RUNTIME}": "libomp",
642+
"${CLANG_SYSTEMZ_DEFAULT_ARCH}": "z10",
643+
"${CLANG_INSTALL_LIBDIR_BASENAME}": "lib",
644+
"${CLANG_RESOURCE_DIR}": "",
645+
"${C_INCLUDE_DIRS}": "",
646+
} |
647+
cmakedefine_sunset("CLANG_CONFIG_FILE_SYSTEM_DIR") |
648+
cmakedefine_sunset("CLANG_CONFIG_FILE_USER_DIR") |
649+
{
650+
"${DEFAULT_SYSROOT}": "",
651+
"${GCC_INSTALL_PREFIX}": "",
652+
} |
653+
cmakedefine_vunset("CLANG_HAVE_LIBXML") |
654+
cmakedefine(
655+
"CLANG_HAVE_RLIMITS",
656+
disable = "@platforms//os:windows",
657+
) |
658+
cmakedefine_vset("CLANG_HAVE_DLFCN_H") |
659+
cmakedefine_vset("CLANG_HAVE_DLADDR") |
660+
cmakedefine_sunset("HOST_LINK_VERSION") |
661+
cmakedefine_unset("ENABLE_LINKER_BUILD_ID") |
662+
cmakedefine01_on("ENABLE_X86_RELAX_RELOCATIONS") |
663+
cmakedefine01_off("PPC_LINUX_DEFAULT_IEEELONGDOUBLE") |
664+
cmakedefine01_off("CLANG_ENABLE_OBJC_REWRITER") |
665+
cmakedefine01_on("CLANG_ENABLE_STATIC_ANALYZER") |
666+
cmakedefine01_off("CLANG_SPAWN_CC1") |
667+
cmakedefine01_off("CLANG_ENABLE_CIR")
668+
),
669+
template = "include/clang/Config/config.h.cmake",
670+
)
671+
617672
cc_library(
618673
name = "config",
619674
hdrs = [

utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
"""Configurable build attributes for the overlay.
6+
7+
To view the configurable build attributes in this file:
8+
9+
bazel query 'kind(".*_flag", @llvm-project//config:all)'
10+
"""
11+
12+
load("@bazel_skylib//lib:selects.bzl", "selects")
13+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
14+
15+
# Common config settings mapping to platform triples.
16+
#
17+
# See also: //platform:BUILD.bazel.
18+
#
19+
# TODO(aaronmondal): Add triples for more useful configurations as per
20+
# `llvm/include/llvm/TargetParser/Triple.h`.
21+
22+
selects.config_setting_group(
23+
name = "aarch64-unknown-linux-gnu",
24+
match_all = [
25+
"@platforms//cpu:aarch64",
26+
"@platforms//os:linux",
27+
],
28+
)
29+
30+
selects.config_setting_group(
31+
name = "arm64-apple-darwin",
32+
match_all = [
33+
"@platforms//cpu:arm64",
34+
"@platforms//os:macos",
35+
],
36+
)
37+
38+
selects.config_setting_group(
39+
name = "powerpc64le-unknown-linux-gnu",
40+
match_all = [
41+
"@platforms//cpu:ppc64le",
42+
"@platforms//os:linux",
43+
],
44+
)
45+
46+
selects.config_setting_group(
47+
name = "systemz-unknown-linux_gnu",
48+
match_all = [
49+
"@platforms//cpu:s390x",
50+
"@platforms//os:linux",
51+
],
52+
)
53+
54+
selects.config_setting_group(
55+
name = "x86_64-pc-win32",
56+
match_all = [
57+
"@platforms//cpu:x86_64",
58+
"@platforms//os:windows",
59+
],
60+
)
61+
62+
selects.config_setting_group(
63+
name = "x86_64-unknown-darwin",
64+
match_all = [
65+
"@platforms//cpu:x86_64",
66+
"@platforms//os:macos",
67+
],
68+
)
69+
70+
selects.config_setting_group(
71+
name = "x86_64-unknown-linux-gnu",
72+
match_all = [
73+
"@platforms//cpu:x86_64",
74+
"@platforms//os:linux",
75+
],
76+
)
77+
78+
# Useful helpers.
79+
80+
selects.config_setting_group(
81+
name = "posix",
82+
match_any = [
83+
"@platforms//os:linux",
84+
"@platforms//os:macos",
85+
"@platforms//os:freebsd",
86+
],
87+
)
88+
89+
# Configurable build attributes for llvm.
90+
91+
# These map to CMake `option` fields on a best-effort basis. The default maps to
92+
# CMake's `ON` or `OFF` value.
93+
[
94+
(
95+
bool_flag(
96+
name = option,
97+
build_setting_default = default,
98+
),
99+
# Only define the opposite of the default value to reduce target count
100+
# and prevent accidental default redeclarations.
101+
#
102+
# This also acts as a failsafe to prevent drift if a default is flipped
103+
# as a changed default triggers straightfoward errors in all places
104+
# where no longer existing config_settings need to be adjusted.
105+
(
106+
config_setting(
107+
name = "{}_enabled".format(option),
108+
flag_values = {":{}".format(option): "true"},
109+
) if not default else config_setting(
110+
name = "{}_disabled".format(option),
111+
flag_values = {":{}".format(option): "false"},
112+
)
113+
),
114+
)
115+
for option, default in [
116+
("LLVM_ENABLE_BACKTRACES", True),
117+
("LLVM_ENABLE_CRASH_OVERRIDES", True),
118+
("LLVM_ENABLE_CRASH_DUMPS", False),
119+
120+
# These deviate slightly from CMake in that we use a boolean rather than
121+
# ON/OFF/FORCE_ON since in Bazel ON collapses into FORCE_ON anyways.
122+
("LLVM_ENABLE_ZLIB", True),
123+
("LLVM_ENABLE_ZSTD", True),
124+
("LLVM_HAS_LOGF128", False),
125+
]
126+
]
127+
128+
# This doesn't have a common default as most builds expect this to be disabled
129+
# by default but MinGW builds expect it enabled by default.
130+
bool_flag(
131+
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH",
132+
build_setting_default = False,
133+
)
134+
135+
config_setting(
136+
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH_enabled",
137+
flag_values = {":LLVM_WINDOWS_PREFER_FORWARD_SLASH": "true"},
138+
)
139+
140+
config_setting(
141+
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH_disabled",
142+
flag_values = {":LLVM_WINDOWS_PREFER_FORWARD_SLASH": "false"},
143+
)
144+
145+
string_flag(
146+
name = "LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING",
147+
build_setting_default = "DISABLED",
148+
values = [
149+
"COVERAGE",
150+
"DISABLED",
151+
],
152+
)
153+
154+
config_setting(
155+
name = "LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING_coverage",
156+
flag_values = {":LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING": "COVERAGE"},
157+
)
158+
159+
bool_flag(
160+
name = "LLVM_ENABLE_PLUGINS",
161+
# TODO(aaronmondal): This should be enabled by default, but that currently
162+
# breaks @llvm-project//llvm/unittests:analysis_tests.
163+
build_setting_default = False,
164+
)
165+
166+
config_setting(
167+
name = "LLVM_ENABLE_PLUGINS_enabled",
168+
flag_values = {":LLVM_ENABLE_PLUGINS": "true"},
169+
)
170+
171+
config_setting(
172+
name = "LLVM_ENABLE_PLUGINS_disabled",
173+
flag_values = {":LLVM_ENABLE_PLUGINS": "false"},
174+
)

0 commit comments

Comments
 (0)