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

Aaron Siddhartha Mondal via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 11 10:29:21 PST 2025


================
@@ -2,118 +2,331 @@
 # 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(
+    ":cmakehelpers.bzl",
+    "cmakedefine",
+    "cmakedefine01",
+    "cmakedefine01_off",
+    "cmakedefine01_on",
+    "cmakedefine_set",
+    "cmakedefine_string",
+    "cmakedefine_unset",
+)
 
-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 = "//llvm/config:LLVM_ENABLE_BACKTRACES_disabled",
+    ) |
+    cmakedefine01(
+        "ENABLE_CRASH_OVERRIDES",
+        disable = "//llvm/config:LLVM_ENABLE_CRASH_OVERRIDES_disabled",
+    ) |
+    cmakedefine01(
+        "LLVM_ENABLE_CRASH_DUMPS",
+        enable = "//llvm/config:LLVM_ENABLE_CRASH_DUMPS_enabled",
+    ) |
+    cmakedefine01(
+        "ENABLE_DEBUGLOC_COVERAGE_TRACKING",
+        enable = "//llvm/config:LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING_coverage",
+    ) |
+    cmakedefine01(
+        "LLVM_WINDOWS_PREFER_FORWARD_SLASH",
+        enable = (
+            "@bazel_tools//tools/cpp:mingw",
+            "//llvm/config:LLVM_WINDOWS_PREFER_FORWARD_SLASH_enabled",
+        ),
+        disable = (
+            "//llvm/config:LLVM_WINDOWS_PREFER_FORWARD_SLASH_disabled",
+            "//conditions:default",
+        ),
+    ) |
+    cmakedefine("HAVE_BACKTRACE", enable = "//llvm/config:posix") |
+    {"${BACKTRACE_HEADER}": "execinfo.h"} |
+    {
+        "#cmakedefine HAVE_CRASHREPORTERCLIENT_H": "/* #undef 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_set("HAVE_DLOPEN") |
+    cmakedefine("HAVE_REGISTER_FRAME", enable = "//llvm/config:posix") |
+    cmakedefine("HAVE_DEREGISTER_FRAME", enable = "//llvm/config:posix") |
+    cmakedefine("HAVE_UNW_ADD_DYNAMIC_FDE", enable = "@platforms//os:macos") |
+    cmakedefine_unset("HAVE_FFI_CALL") |
+    cmakedefine_unset("HAVE_FFI_FFI_H") |
+    cmakedefine_unset("HAVE_FFI_H") |
+    cmakedefine_set("HAVE_FUTIMENS") |
+    cmakedefine_set("HAVE_FUTIMES") |
+    cmakedefine_set("HAVE_GETPAGESIZE") |
+    cmakedefine_set("HAVE_GETRUSAGE") |
+    {"#cmakedefine HAVE_ISATTY 1": "#define HAVE_ISATTY 1"} |
+    cmakedefine_unset("HAVE_LIBEDIT") |
+    # TODO: Wire this up Exegesis and remove custom logic.
+    cmakedefine_unset("HAVE_LIBPFM") |
+    cmakedefine_unset("LIBPFM_HAS_FIELD_CYCLES") |
+    cmakedefine_unset("HAVE_LIBPSAPI") |
+    cmakedefine("HAVE_LIBPTHREAD", enable = "//llvm/config:posix") |
+    cmakedefine("HAVE_PTHREAD_GETNAME_NP", enable = "//llvm/config:posix") |
+    cmakedefine("HAVE_PTHREAD_SETNAME_NP", enable = "//llvm/config:posix") |
+    cmakedefine_unset("HAVE_PTHREAD_GET_NAME_NP") |  # TODO: Likely wrong?
+    cmakedefine_unset("HAVE_PTHREAD_SET_NAME_NP") |  # TODO: Likely wrong?
+    cmakedefine("HAVE_MACH_MACH_H", enable = "@platforms//os:macos") |
+    cmakedefine_unset("HAVE_MALLCTL") |
+    cmakedefine("HAVE_MALLINFO", enable = "@platforms//os:linux") |
+    # TODO(aaronmondal): Make configurable and enable by default.
+    cmakedefine_unset("HAVE_MALLINFO2") |
+    cmakedefine("HAVE_MALLOC_MALLOC_H", enable = "@platforms//os:macos") |
+    cmakedefine(
+        "HAVE_MALLOC_ZONE_STATISTICS",
+        enable = "@platforms//os:macos",
+    ) |
+    cmakedefine_set("HAVE_POSIX_SPAWN") |
+    cmakedefine_set("HAVE_PREAD") |
+    cmakedefine("HAVE_PTHREAD_H", enable = "//llvm/config:posix") |
+    cmakedefine_set("HAVE_PTHREAD_MUTEX_LOCK") |
+    cmakedefine_set("HAVE_PTHREAD_RWLOCK_INIT") |
+    cmakedefine("HAVE_SBRK", enable = "@platforms//os:linux") |
+    cmakedefine("HAVE_SETENV", enable = "//llvm/config:posix") |
+    cmakedefine_set("HAVE_SIGALTSTACK") |
+    cmakedefine("HAVE_STRERROR_R", enable = "//llvm/config:posix") |
+    cmakedefine_set("HAVE_SYSCONF") |
+    cmakedefine_set("HAVE_SYS_MMAN_H") |
+    cmakedefine_unset("HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC") |
+    cmakedefine(
+        "HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC",
+        enable = "@platforms//os:linux",
+    ) |
+    cmakedefine("HAVE_UNISTD_H", enable = "//llvm/config:posix") |
+    cmakedefine_unset("HAVE_VALGRIND_VALGRIND_H") |
+    cmakedefine_unset("HAVE__ALLOCA") |
+    cmakedefine_unset("HAVE__CHSIZE_S") |
+    cmakedefine_set("HAVE__UNWIND_BACKTRACE") |
+    cmakedefine_unset("HAVE___ALLOCA") |
+    cmakedefine_unset("HAVE___ASHLDI3") |
+    cmakedefine_unset("HAVE___ASHRDI3") |
+    cmakedefine_unset("HAVE___CHKSTK") |
+    cmakedefine_unset("HAVE___CHKSTK_MS") |
+    cmakedefine_unset("HAVE___CMPDI2") |
+    cmakedefine_unset("HAVE___DIVDI3") |
+    cmakedefine_unset("HAVE___FIXDFDI") |
+    cmakedefine_unset("HAVE___FIXSFDI") |
+    cmakedefine_unset("HAVE___FLOATDIDF") |
+    cmakedefine_unset("HAVE___LSHRDI3") |
+    cmakedefine_unset("HAVE___MAIN") |
+    cmakedefine_unset("HAVE___MODDI3") |
+    cmakedefine_unset("HAVE___UDIVDI3") |
+    cmakedefine_unset("HAVE___UMODDI3") |
+    cmakedefine_unset("HAVE____CHKSTK") |
+    cmakedefine_unset("HAVE____CHKSTK_MS") |
+    {
+        '#cmakedefine HOST_LINK_VERSION "${HOST_LINK_VERSION}"': "/* #undef HOST_LINK_VERSION */",
+        '#cmakedefine LLVM_TARGET_TRIPLE_ENV "${LLVM_TARGET_TRIPLE_ENV}"': "/* #undef LLVM_TARGET_TRIPLE_ENV */",
+    } |
+    cmakedefine01_on("LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO") |
+    cmakedefine01_on("LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG") |
+    cmakedefine_unset("LLVM_ENABLE_LIBXML2") |
+    select({
+        "@platforms//os:windows": cmakedefine_string("LTDL_SHLIB_EXT", ".dll"),
+        "@platforms//os:macos": cmakedefine_string("LTDL_SHLIB_EXT", ".dylib"),
----------------
aaronmondal wrote:

FYI @keith Previously this and `LLVM_PLUGIN_EXT` were set to `.so` on macos which didn't match CMake.

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


More information about the llvm-commits mailing list