Skip to content

[Bazel] Make LLVM and Clang config headers configurable #126729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions utils/bazel/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ build --experimental_cc_shared_library
# https://github.com/bazelbuild/bazel/commit/03246077f948f2790a83520e7dccc2625650e6df
build --build_runfile_links=false

###############################################################################
# Options to select different strategies for linking potential dependent
# libraries. The default leaves it disabled.
###############################################################################

build:zlib_external --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=external
build:zlib_system --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=system

###############################################################################
# Options for "generic_clang" builds: these options should generally apply to
# builds using a Clang-based compiler, and default to the `clang` executable on
Expand Down
55 changes: 55 additions & 0 deletions utils/bazel/llvm-project-overlay/clang/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# 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", "bool_flag")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_python//python:defs.bzl", "py_binary")
load(
"//:vars.bzl",
Expand All @@ -13,6 +15,16 @@ load(
load("//:workspace_root.bzl", "workspace_root")
load("//llvm:binary_alias.bzl", "binary_alias")
load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
load(
"//config:cmakehelpers.bzl",
"cmakedefine",
"cmakedefine01_off",
"cmakedefine01_on",
"cmakedefine_sunset",
"cmakedefine_unset",
"cmakedefine_vset",
"cmakedefine_vunset",
)
load("//llvm:driver.bzl", "llvm_driver_cc_binary")
load("//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")

Expand Down Expand Up @@ -614,6 +626,49 @@ genrule(
),
)

expand_template(
name = "config_h",
out = "include/clang/Config/config.h",
substitutions = (
{"${BUG_REPORT_URL}": "https://github.com/llvm/llvm-project/issues/"} |
cmakedefine01_on("CLANG_DEFAULT_PIE_ON_LINUX") |
{
"${CLANG_DEFAULT_LINKER}": "",
"${CLANG_DEFAULT_CXX_STDLIB}": "",
"${CLANG_DEFAULT_RTLIB}": "",
"${CLANG_DEFAULT_UNWINDLIB}": "",
"${CLANG_DEFAULT_OBJCOPY}": "objcopy",
"${CLANG_DEFAULT_OPENMP_RUNTIME}": "libomp",
"${CLANG_SYSTEMZ_DEFAULT_ARCH}": "z10",
"${CLANG_INSTALL_LIBDIR_BASENAME}": "lib",
"${CLANG_RESOURCE_DIR}": "",
"${C_INCLUDE_DIRS}": "",
} |
cmakedefine_sunset("CLANG_CONFIG_FILE_SYSTEM_DIR") |
cmakedefine_sunset("CLANG_CONFIG_FILE_USER_DIR") |
{
"${DEFAULT_SYSROOT}": "",
"${GCC_INSTALL_PREFIX}": "",
} |
cmakedefine_vunset("CLANG_HAVE_LIBXML") |
cmakedefine(
"CLANG_HAVE_RLIMITS",
disable = "@platforms//os:windows",
) |
cmakedefine_vset("CLANG_HAVE_DLFCN_H") |
cmakedefine_vset("CLANG_HAVE_DLADDR") |
cmakedefine_sunset("HOST_LINK_VERSION") |
cmakedefine_unset("ENABLE_LINKER_BUILD_ID") |
cmakedefine01_on("ENABLE_X86_RELAX_RELOCATIONS") |
cmakedefine01_off("PPC_LINUX_DEFAULT_IEEELONGDOUBLE") |
cmakedefine01_off("CLANG_ENABLE_OBJC_REWRITER") |
cmakedefine01_on("CLANG_ENABLE_STATIC_ANALYZER") |
cmakedefine01_off("CLANG_SPAWN_CC1") |
cmakedefine01_off("CLANG_ENABLE_CIR")
),
template = "include/clang/Config/config.h.cmake",
)

cc_library(
name = "config",
hdrs = [
Expand Down
115 changes: 0 additions & 115 deletions utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h

This file was deleted.

177 changes: 177 additions & 0 deletions utils/bazel/llvm-project-overlay/config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""Configurable build attributes for the overlay.

To view the configurable build attributes in this file:

bazel query 'kind(".*_flag", @llvm-project//config:all)'
"""

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")

# Common config settings mapping to platform triples.
#
# See also: //platform:BUILD.bazel.
#
# TODO(aaronmondal): Add triples for more useful configurations as per
# `llvm/include/llvm/TargetParser/Triple.h`.

selects.config_setting_group(
name = "aarch64-apple-darwin",
match_all = [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
],
)

selects.config_setting_group(
name = "aarch64-unknown-linux-gnu",
match_all = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
)

selects.config_setting_group(
name = "powerpc64le-unknown-linux-gnu",
match_all = [
"@platforms//cpu:ppc64le",
"@platforms//os:linux",
],
)

selects.config_setting_group(
name = "systemz-unknown-linux_gnu",
match_all = [
"@platforms//cpu:s390x",
"@platforms//os:linux",
],
)

# TODO(aaronmondal): Split this into `x86_64-pc-windows-gnu` and
# `x86_64-pc-windows-msvc`.
selects.config_setting_group(
name = "x86_64-pc-windows",
match_all = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
)

selects.config_setting_group(
name = "x86_64-apple-darwin",
match_all = [
"@platforms//cpu:x86_64",
"@platforms//os:macos",
],
)

selects.config_setting_group(
name = "x86_64-unknown-linux-gnu",
match_all = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

# Useful helpers.

selects.config_setting_group(
name = "posix",
match_any = [
"@platforms//os:linux",
"@platforms//os:macos",
"@platforms//os:freebsd",
],
)

# Configurable build attributes for llvm.

# These map to CMake `option` fields on a best-effort basis. The default maps to
# CMake's `ON` or `OFF` value.
[
(
bool_flag(
name = option,
build_setting_default = default,
),
# Only define the opposite of the default value to reduce target count
# and prevent accidental default redeclarations.
#
# This also acts as a failsafe to prevent drift if a default is flipped
# as a changed default triggers straightfoward errors in all places
# where no longer existing config_settings need to be adjusted.
(
config_setting(
name = "{}_enabled".format(option),
flag_values = {":{}".format(option): "true"},
) if not default else config_setting(
name = "{}_disabled".format(option),
flag_values = {":{}".format(option): "false"},
)
),
)
for option, default in [
("LLVM_ENABLE_BACKTRACES", True),
("LLVM_ENABLE_CRASH_OVERRIDES", True),
("LLVM_ENABLE_CRASH_DUMPS", False),

# These deviate slightly from CMake in that we use a boolean rather than
# ON/OFF/FORCE_ON since in Bazel ON collapses into FORCE_ON anyways.
("LLVM_ENABLE_ZLIB", True),
("LLVM_ENABLE_ZSTD", True),
("LLVM_HAS_LOGF128", False),
]
]

# Most builds expect this to be disabled by default but MinGW builds expect it
# enabled by default. We handle this separately to clarify the intent that the
# default is ambiguous.
bool_flag(
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH",
build_setting_default = False,
)

config_setting(
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH_enabled",
flag_values = {":LLVM_WINDOWS_PREFER_FORWARD_SLASH": "true"},
)

config_setting(
name = "LLVM_WINDOWS_PREFER_FORWARD_SLASH_disabled",
flag_values = {":LLVM_WINDOWS_PREFER_FORWARD_SLASH": "false"},
)

string_flag(
name = "LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING",
build_setting_default = "DISABLED",
values = [
"COVERAGE",
"DISABLED",
],
)

config_setting(
name = "LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING_coverage",
flag_values = {":LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING": "COVERAGE"},
)

bool_flag(
name = "LLVM_ENABLE_PLUGINS",
# TODO(aaronmondal): This should be enabled by default, but that currently
# breaks @llvm-project//llvm/unittests:analysis_tests.
build_setting_default = False,
)

config_setting(
name = "LLVM_ENABLE_PLUGINS_enabled",
flag_values = {":LLVM_ENABLE_PLUGINS": "true"},
)

config_setting(
name = "LLVM_ENABLE_PLUGINS_disabled",
flag_values = {":LLVM_ENABLE_PLUGINS": "false"},
)
Loading