[llvm] [Bazel] Make LLVM and Clang config headers configurable (PR #126729)

NAKAMURA Takumi via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 15 08:45:48 PST 2025


================
@@ -2,118 +2,326 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-"""Defines variables that use selects to configure LLVM based on platform."""
+"""Substitutions for use in templates related to the LLVM configuration."""
 
+load("@bazel_skylib//lib:selects.bzl", "selects")
 load(
     "//:vars.bzl",
     "LLVM_VERSION_MAJOR",
     "LLVM_VERSION_MINOR",
     "LLVM_VERSION_PATCH",
     "PACKAGE_VERSION",
 )
+load("//llvm:targets.bzl", "llvm_targets")
+load(
+    "//config:cmakehelpers.bzl",
+    "cmakedefine",
+    "cmakedefine01",
+    "cmakedefine01_off",
+    "cmakedefine01_on",
+    "cmakedefine_set",
+    "cmakedefine_sset",
+    "cmakedefine_sunset",
+    "cmakedefine_unset",
+    "cmakedefine_vset",
+    "cmakedefine_vunset",
+)
 
-def native_arch_defines(arch, triple):
-    return [
-        r'LLVM_NATIVE_ARCH=\"{}\"'.format(arch),
-        "LLVM_NATIVE_ASMPARSER=LLVMInitialize{}AsmParser".format(arch),
-        "LLVM_NATIVE_ASMPRINTER=LLVMInitialize{}AsmPrinter".format(arch),
-        "LLVM_NATIVE_DISASSEMBLER=LLVMInitialize{}Disassembler".format(arch),
-        "LLVM_NATIVE_TARGET=LLVMInitialize{}Target".format(arch),
-        "LLVM_NATIVE_TARGETINFO=LLVMInitialize{}TargetInfo".format(arch),
-        "LLVM_NATIVE_TARGETMC=LLVMInitialize{}TargetMC".format(arch),
-        "LLVM_NATIVE_TARGETMCA=LLVMInitialize{}TargetMCA".format(arch),
-        r'LLVM_HOST_TRIPLE=\"{}\"'.format(triple),
-        r'LLVM_DEFAULT_TARGET_TRIPLE=\"{}\"'.format(triple),
-    ]
-
-posix_defines = [
-    "LLVM_ON_UNIX=1",
-    "HAVE_BACKTRACE=1",
-    "BACKTRACE_HEADER=<execinfo.h>",
-    r'LTDL_SHLIB_EXT=\".so\"',
-    r'LLVM_PLUGIN_EXT=\".so\"',
-    "LLVM_ENABLE_THREADS=1",
-    "HAVE_DEREGISTER_FRAME=1",
-    "HAVE_LIBPTHREAD=1",
-    "HAVE_PTHREAD_GETNAME_NP=1",
-    "HAVE_PTHREAD_H=1",
-    "HAVE_PTHREAD_SETNAME_NP=1",
-    "HAVE_REGISTER_FRAME=1",
-    "HAVE_SETENV_R=1",
-    "HAVE_STRERROR_R=1",
-    "HAVE_SYSEXITS_H=1",
-    "HAVE_UNISTD_H=1",
-]
-
-linux_defines = posix_defines + [
-    "_GNU_SOURCE",
-    "HAVE_MALLINFO=1",
-    "HAVE_SBRK=1",
-    "HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC=1",
-]
-
-macos_defines = posix_defines + [
-    "HAVE_MACH_MACH_H=1",
-    "HAVE_MALLOC_MALLOC_H=1",
-    "HAVE_MALLOC_ZONE_STATISTICS=1",
-    "HAVE_PROC_PID_RUSAGE=1",
-    "HAVE_UNW_ADD_DYNAMIC_FDE=1",
-]
-
-win32_defines = [
-    # Windows system library specific defines.
-    "_CRT_SECURE_NO_DEPRECATE",
-    "_CRT_SECURE_NO_WARNINGS",
-    "_CRT_NONSTDC_NO_DEPRECATE",
-    "_CRT_NONSTDC_NO_WARNINGS",
-    "_SCL_SECURE_NO_DEPRECATE",
-    "_SCL_SECURE_NO_WARNINGS",
-    "UNICODE",
-    "_UNICODE",
+CONFIG_H_SUBSTITUTIONS = (
+    {"${BUG_REPORT_URL}": "https://github.com/llvm/llvm-project/issues/"} |
+    cmakedefine01(
+        "ENABLE_BACKTRACES",
+        disable = "//config:LLVM_ENABLE_BACKTRACES_disabled",
+    ) |
+    cmakedefine01(
+        "ENABLE_CRASH_OVERRIDES",
+        disable = "//config:LLVM_ENABLE_CRASH_OVERRIDES_disabled",
+    ) |
+    cmakedefine01(
+        "LLVM_ENABLE_CRASH_DUMPS",
+        enable = "//config:LLVM_ENABLE_CRASH_DUMPS_enabled",
+    ) |
+    cmakedefine01(
+        "ENABLE_DEBUGLOC_COVERAGE_TRACKING",
+        enable = "//config:LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING_coverage",
+    ) |
+    cmakedefine01(
+        "LLVM_WINDOWS_PREFER_FORWARD_SLASH",
+        enable = (
+            "@bazel_tools//tools/cpp:mingw",
+            "//config:LLVM_WINDOWS_PREFER_FORWARD_SLASH_enabled",
+        ),
+        disable = (
+            "//config:LLVM_WINDOWS_PREFER_FORWARD_SLASH_disabled",
+            "//conditions:default",
+        ),
+    ) |
+    cmakedefine("HAVE_BACKTRACE", enable = "//config:posix") |
+    {"${BACKTRACE_HEADER}": "execinfo.h"} |
+    cmakedefine_unset("HAVE_CRASHREPORTERCLIENT_H") |
+    cmakedefine01_off("HAVE_CRASHREPORTER_INFO") |
+    cmakedefine01_off("HAVE_DECL_ARC4RANDOM") |
+    cmakedefine01_on("HAVE_DECL_FE_ALL_EXCEPT") |
+    cmakedefine01_on("HAVE_DECL_FE_INEXACT") |
+    cmakedefine01_off("HAVE_DECL_STRERROR_S") |
+    cmakedefine_vset("HAVE_DLOPEN") |
+    cmakedefine("HAVE_REGISTER_FRAME", enable = "//config:posix") |
+    cmakedefine("HAVE_DEREGISTER_FRAME", enable = "//config:posix") |
+    cmakedefine("HAVE_UNW_ADD_DYNAMIC_FDE", enable = "@platforms//os:macos") |
+    cmakedefine_vunset("HAVE_FFI_CALL") |
+    cmakedefine_vunset("HAVE_FFI_FFI_H") |
+    cmakedefine_vunset("HAVE_FFI_H") |
+    cmakedefine_vset("HAVE_FUTIMENS") |
+    cmakedefine_vset("HAVE_FUTIMES") |
+    cmakedefine_vset("HAVE_GETPAGESIZE") |
+    cmakedefine_vset("HAVE_GETRUSAGE") |
+    {"#cmakedefine HAVE_ISATTY 1": "#define HAVE_ISATTY 1"} |
+    cmakedefine_vunset("HAVE_LIBEDIT") |
+    # TODO: Wire this up Exegesis and remove custom logic.
+    cmakedefine_vunset("HAVE_LIBPFM") |
+    cmakedefine_vunset("LIBPFM_HAS_FIELD_CYCLES") |
+    cmakedefine_vunset("HAVE_LIBPSAPI") |
+    cmakedefine("HAVE_LIBPTHREAD", enable = "//config:posix") |
+    cmakedefine("HAVE_PTHREAD_GETNAME_NP", enable = "//config:posix") |
+    cmakedefine("HAVE_PTHREAD_SETNAME_NP", enable = "//config:posix") |
+    cmakedefine_vunset("HAVE_PTHREAD_GET_NAME_NP") |  # TODO: Likely wrong?
+    cmakedefine_vunset("HAVE_PTHREAD_SET_NAME_NP") |  # TODO: Likely wrong?
+    cmakedefine("HAVE_MACH_MACH_H", enable = "@platforms//os:macos") |
+    cmakedefine_vunset("HAVE_MALLCTL") |
+    cmakedefine("HAVE_MALLINFO", enable = "@platforms//os:linux") |
+    # TODO(aaronmondal): Make configurable and enable by default.
+    cmakedefine_vunset("HAVE_MALLINFO2") |
+    cmakedefine("HAVE_MALLOC_MALLOC_H", enable = "@platforms//os:macos") |
+    cmakedefine(
+        "HAVE_MALLOC_ZONE_STATISTICS",
+        enable = "@platforms//os:macos",
+    ) |
+    cmakedefine_vset("HAVE_POSIX_SPAWN") |
+    cmakedefine_vset("HAVE_PREAD") |
+    cmakedefine("HAVE_PTHREAD_H", enable = "//config:posix") |
+    cmakedefine_vset("HAVE_PTHREAD_MUTEX_LOCK") |
+    cmakedefine_vset("HAVE_PTHREAD_RWLOCK_INIT") |
+    cmakedefine("HAVE_SBRK", enable = "@platforms//os:linux") |
+    cmakedefine("HAVE_SETENV", enable = "//config:posix") |
+    cmakedefine_vset("HAVE_SIGALTSTACK") |
+    cmakedefine("HAVE_STRERROR_R", enable = "//config:posix") |
+    cmakedefine_vset("HAVE_SYSCONF") |
+    cmakedefine_vset("HAVE_SYS_MMAN_H") |
+    cmakedefine_vunset("HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC") |
+    cmakedefine(
+        "HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC",
+        enable = "@platforms//os:linux",
+    ) |
+    cmakedefine("HAVE_UNISTD_H", enable = "//config:posix") |
+    cmakedefine_vunset("HAVE_VALGRIND_VALGRIND_H") |
+    cmakedefine_vunset("HAVE__ALLOCA") |
+    cmakedefine_vunset("HAVE__CHSIZE_S") |
+    cmakedefine_vset("HAVE__UNWIND_BACKTRACE") |
+    cmakedefine_vunset("HAVE___ALLOCA") |
+    cmakedefine_vunset("HAVE___ASHLDI3") |
+    cmakedefine_vunset("HAVE___ASHRDI3") |
+    cmakedefine_vunset("HAVE___CHKSTK") |
+    cmakedefine_vunset("HAVE___CHKSTK_MS") |
+    cmakedefine_vunset("HAVE___CMPDI2") |
+    cmakedefine_vunset("HAVE___DIVDI3") |
+    cmakedefine_vunset("HAVE___FIXDFDI") |
+    cmakedefine_vunset("HAVE___FIXSFDI") |
+    cmakedefine_vunset("HAVE___FLOATDIDF") |
+    cmakedefine_vunset("HAVE___LSHRDI3") |
+    cmakedefine_vunset("HAVE___MAIN") |
+    cmakedefine_vunset("HAVE___MODDI3") |
+    cmakedefine_vunset("HAVE___UDIVDI3") |
+    cmakedefine_vunset("HAVE___UMODDI3") |
+    cmakedefine_vunset("HAVE____CHKSTK") |
+    cmakedefine_vunset("HAVE____CHKSTK_MS") |
+    cmakedefine_sunset("HOST_LINK_VERSION") |
+    cmakedefine_sunset("LLVM_TARGET_TRIPLE_ENV") |
+    cmakedefine01_on("LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO") |
+    cmakedefine01_on("LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG") |
+    cmakedefine_vunset("LLVM_ENABLE_LIBXML2") |
+    select({
+        "@platforms//os:windows": cmakedefine_sset("LTDL_SHLIB_EXT", ".dll"),
+        "@platforms//os:macos": cmakedefine_sset("LTDL_SHLIB_EXT", ".dylib"),
+        "//conditions:default": cmakedefine_sset("LTDL_SHLIB_EXT", ".so"),
+    }) |
+    select({
+        "@platforms//os:windows": cmakedefine_sset("LLVM_PLUGIN_EXT", ".dll"),
+        "@platforms//os:macos": cmakedefine_sset("LLVM_PLUGIN_EXT", ".dylib"),
+        "//conditions:default": cmakedefine_sset("LLVM_PLUGIN_EXT", ".so"),
+    }) |
+    cmakedefine_sset(
+        "PACKAGE_BUGREPORT",
+        "https://github.com/llvm/llvm-project/issues/",
+    ) |
+    cmakedefine_sset("PACKAGE_NAME", "LLVM") |
+    cmakedefine_sset(
+        "PACKAGE_STRING",
+        'PACKAGE_NAME " " LLVM_VERSION_STRING',
----------------
chapuni wrote:

```suggestion
        '" PACKAGE_NAME " " LLVM_VERSION_STRING "',
```

https://github.com/llvm/llvm-project/pull/126729


More information about the llvm-commits mailing list