diff --git a/utils/bazel/configure.bzl b/utils/bazel/configure.bzl --- a/utils/bazel/configure.bzl +++ b/utils/bazel/configure.bzl @@ -70,9 +70,80 @@ stderr = exec_result.stderr, )) +def _extract_cmake_settings(repository_ctx, llvm_cmake): + + # The list to be written to vars.bzl + # `CMAKE_CXX_STANDARD` may be used from WORKSPACE for the toolchain. + c = { + "CMAKE_CXX_STANDARD": None, + "LLVM_VERSION_MAJOR": None, + "LLVM_VERSION_MINOR": None, + "LLVM_VERSION_PATCH": None, + } + + # It would be easier to use external commands like sed(1) and python. + # For portability, the parser should run on Starlark. + llvm_cmake_path = repository_ctx.path(Label("//:"+llvm_cmake)) + for line in repository_ctx.read(llvm_cmake_path).splitlines(): + # Extract "set ( FOO bar ... " + setfoo = line.partition("(") + if setfoo[1]!="(": + continue + if setfoo[0].strip().lower()!="set": + continue + # `kv` is assumed as \s*KEY\s+VAL\s*\).* + # Typical case is like + # LLVM_REQUIRED_CXX_STANDARD 17) + # Possible case -- It should be ignored. + # CMAKE_CXX_STANDARD ${...} CACHE STRING "...") + kv = setfoo[2].strip() + i = kv.find(" ") + if i<0: + continue + k = kv[:i] + # Prefer LLVM_REQUIRED_CXX_STANDARD instead of CMAKE_CXX_STANDARD + if k=="LLVM_REQUIRED_CXX_STANDARD": + k = "CMAKE_CXX_STANDARD" + c[k] = None + if k not in c: + continue + # Skip if `CMAKE_CXX_STANDARD` is set with + # `LLVM_REQUIRED_CXX_STANDARD`. + # Then `v` will not be desired form, like "${...} CACHE" + if c[k]!=None: + continue + # Pick up 1st word as the value. + # Note: It assumes unquoted word. + v = kv[i:].strip().partition(")")[0].partition(" ")[0] + c[k] = v + + # Synthesize `LLVM_VERSION` for convenience. + c["LLVM_VERSION"] = ( + c["LLVM_VERSION_MAJOR"] + +"."+c["LLVM_VERSION_MINOR"] + +"."+c["LLVM_VERSION_PATCH"]) + + # Write out individual vars + fc = "# Generated from "+llvm_cmake+"\n\n" + for k,v in c.items(): + fc += '%s = "%s"\n' % (k,v) + # Write out values as dict for convenience for minimum export. + fc += "\nllvm_vars={\n" + for k,v in c.items(): + fc += ' "%s": "%s",\n' % (k,v) + fc += "}\n" + repository_ctx.file("vars.bzl", content=fc) + + return c + def _llvm_configure_impl(repository_ctx): _overlay_directories(repository_ctx) + vars = _extract_cmake_settings( + repository_ctx, + "llvm/CMakeLists.txt", + ) + # Create a starlark file with the requested LLVM targets. targets = repository_ctx.attr.targets repository_ctx.file( diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -7,6 +7,14 @@ load("//llvm:binary_alias.bzl", "binary_alias") load("//llvm:cc_plugin_library.bzl", "cc_plugin_library") +load( + "//:vars.bzl", + "LLVM_VERSION", + "LLVM_VERSION_MAJOR", + "LLVM_VERSION_MINOR", + "LLVM_VERSION_PATCH", +) + package( default_visibility = ["//visibility:public"], ) @@ -359,11 +367,11 @@ name = "basic_version_gen", outs = ["include/clang/Basic/Version.inc"], cmd = ( - "echo '#define CLANG_VERSION 16.0.0' >> $@\n" + - "echo '#define CLANG_VERSION_MAJOR 16' >> $@\n" + - "echo '#define CLANG_VERSION_MINOR 0' >> $@\n" + - "echo '#define CLANG_VERSION_PATCHLEVEL 0' >> $@\n" + - "echo '#define CLANG_VERSION_STRING \"16.0.0\"' >> $@\n" + "echo '#define CLANG_VERSION "+LLVM_VERSION+"' >> $@\n" + + "echo '#define CLANG_VERSION_MAJOR "+LLVM_VERSION_MAJOR+"' >> $@\n" + + "echo '#define CLANG_VERSION_MINOR "+LLVM_VERSION_MINOR+"' >> $@\n" + + "echo '#define CLANG_VERSION_PATCHLEVEL "+LLVM_VERSION_PATCH+"' >> $@\n" + + "echo '#define CLANG_VERSION_STRING \""+LLVM_VERSION+"\"' >> $@\n" ), ) @@ -373,6 +381,9 @@ "include/clang/Basic/Version.inc", "include/clang/Config/config.h", ], + defines = [ + r'BACKEND_PACKAGE_STRING=\"LLVM\ '+LLVM_VERSION+r'git\"', + ], includes = ["include"], deps = [ # We rely on the LLVM config library to provide configuration defines. diff --git a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h --- a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h +++ b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h @@ -75,7 +75,7 @@ /* CLANG_HAVE_RLIMITS defined conditionally below */ /* The LLVM product name and version */ -#define BACKEND_PACKAGE_STRING "LLVM 16.0.0git" +/* #undef BACKEND_PACKAGE_STRING */ /* Linker version detected at compile time. */ /* #undef HOST_LINK_VERSION */ diff --git a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel @@ -5,6 +5,11 @@ load("@bazel_skylib//rules:expand_template.bzl", "expand_template") load("//llvm:tblgen.bzl", "gentbl") +load( + "//:vars.bzl", + "LLVM_VERSION", +) + package( default_visibility = ["//visibility:public"], ) @@ -15,7 +20,7 @@ genrule( name = "config_version_gen", outs = ["include/lld/Common/Version.inc"], - cmd = "echo '#define LLD_VERSION_STRING \"16.0.0\"' > $@", + cmd = "echo '#define LLD_VERSION_STRING \""+LLVM_VERSION+"\"' > $@", ) genrule( diff --git a/utils/bazel/llvm-project-overlay/llvm/config.bzl b/utils/bazel/llvm-project-overlay/llvm/config.bzl --- a/utils/bazel/llvm-project-overlay/llvm/config.bzl +++ b/utils/bazel/llvm-project-overlay/llvm/config.bzl @@ -4,6 +4,14 @@ """Defines variables that use selects to configure LLVM based on platform.""" +load( + "//:vars.bzl", + "LLVM_VERSION", + "LLVM_VERSION_MAJOR", + "LLVM_VERSION_MINOR", + "LLVM_VERSION_PATCH", +) + def native_arch_defines(arch, triple): return [ r'LLVM_NATIVE_ARCH=\"{}\"'.format(arch), @@ -89,6 +97,10 @@ "@bazel_tools//src/conditions:linux_s390x": native_arch_defines("SystemZ", "systemz-unknown-linux_gnu"), "//conditions:default": native_arch_defines("X86", "x86_64-unknown-linux-gnu"), }) + [ + "LLVM_VERSION_MAJOR="+LLVM_VERSION_MAJOR, + "LLVM_VERSION_MINOR="+LLVM_VERSION_MINOR, + "LLVM_VERSION_PATCH="+LLVM_VERSION_PATCH, + r'LLVM_VERSION_STRING=\"'+LLVM_VERSION+r'git\"', # These shouldn't be needed by the C++11 standard, but are for some # platforms (e.g. glibc < 2.18. See # https://sourceware.org/bugzilla/show_bug.cgi?id=15366). These are also diff --git a/utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h b/utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h --- a/utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h +++ b/utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/llvm-config.h @@ -74,16 +74,16 @@ #define LLVM_USE_PERF 0 /* Major version of the LLVM API */ -#define LLVM_VERSION_MAJOR 16 +/* #undef LLVM_VERSION_MAJOR */ /* Minor version of the LLVM API */ -#define LLVM_VERSION_MINOR 0 +/* #undef LLVM_VERSION_MINOR */ /* Patch version of the LLVM API */ -#define LLVM_VERSION_PATCH 0 +/* #undef LLVM_VERSION_PATCH */ /* LLVM version string */ -#define LLVM_VERSION_STRING "16.0.0git" +/* #undef LLVM_VERSION_STRING */ /* Whether LLVM records statistics for use with GetStatistics(), * PrintStatistics() or PrintStatisticsJSON()