[llvm] [bazel] Improve building on/for Windows (PR #171761)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 20:56:11 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: David Zbarsky (dzbarsky)

<details>
<summary>Changes</summary>

Few things going on here:
- I think we should be able to use the blake3 x86-64 asm on windows with some slight adjustments (sorry it's still named *_unix.S, renaming seemed like a bit bigger project)
- `genrule` is really evil because it bakes that path to the host bash into the command, which fails spectacularly when running on a non-windows remote executor. Swap to `write_file`/`run_binary` rules to mitigate it
- The existing windows linkopts do not work correctly for clang in mingw mode

With this set of changes (as well as another one that rewrites `bundle_resources.py` into C) I am able to hermetically cross-build clang/lld/etc from a windows host to a (mingw) windows target on a linux executor, and use the resulting compilers to compile C binaries/tests

---
Full diff: https://github.com/llvm/llvm-project/pull/171761.diff


7 Files Affected:

- (modified) llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S (+7-5) 
- (modified) llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S (+13-11) 
- (modified) llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S (+11-9) 
- (modified) llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S (+11-9) 
- (modified) utils/bazel/llvm-project-overlay/clang/BUILD.bazel (+29-23) 
- (modified) utils/bazel/llvm-project-overlay/lld/BUILD.bazel (+12-7) 
- (modified) utils/bazel/llvm-project-overlay/llvm/BUILD.bazel (+40-9) 


``````````diff
diff --git a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
index e98893c7ef8b8..d6fa51638da39 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
@@ -16,15 +16,17 @@
 #define _CET_ENDBR
 #endif
 
-#ifdef __APPLE__
-#define HIDDEN .private_extern
+#if defined(__APPLE__)
+#define HIDDEN(symbol) .private_extern symbol
+#elif defined(__ELF__)
+#define HIDDEN(symbol) .hidden symbol
 #else
-#define HIDDEN .hidden
+#define HIDDEN(symbol)
 #endif
 
 .intel_syntax noprefix
-HIDDEN _blake3_hash_many_avx2
-HIDDEN blake3_hash_many_avx2
+HIDDEN(_blake3_hash_many_avx2)
+HIDDEN(blake3_hash_many_avx2)
 .global _blake3_hash_many_avx2
 .global blake3_hash_many_avx2
 #ifdef __APPLE__
diff --git a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
index b4b14946de10e..cf2c88329af5c 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
@@ -16,21 +16,23 @@
 #define _CET_ENDBR
 #endif
 
-#ifdef __APPLE__
-#define HIDDEN .private_extern
+#if defined(__APPLE__)
+#define HIDDEN(symbol) .private_extern symbol
+#elif defined(__ELF__)
+#define HIDDEN(symbol) .hidden symbol
 #else
-#define HIDDEN .hidden
+#define HIDDEN(symbol)
 #endif
 
 .intel_syntax noprefix
-HIDDEN _blake3_hash_many_avx512
-HIDDEN blake3_hash_many_avx512
-HIDDEN blake3_compress_in_place_avx512
-HIDDEN _blake3_compress_in_place_avx512
-HIDDEN blake3_compress_xof_avx512
-HIDDEN _blake3_compress_xof_avx512
-HIDDEN blake3_xof_many_avx512
-HIDDEN _blake3_xof_many_avx512
+HIDDEN(_blake3_hash_many_avx512)
+HIDDEN(blake3_hash_many_avx512)
+HIDDEN(blake3_compress_in_place_avx512)
+HIDDEN(_blake3_compress_in_place_avx512)
+HIDDEN(blake3_compress_xof_avx512)
+HIDDEN(_blake3_compress_xof_avx512)
+HIDDEN(blake3_xof_many_avx512)
+HIDDEN(_blake3_xof_many_avx512)
 .global _blake3_hash_many_avx512
 .global blake3_hash_many_avx512
 .global blake3_compress_in_place_avx512
diff --git a/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S
index d69a1706fefe7..5563fc3b5ba09 100644
--- a/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S
@@ -16,19 +16,21 @@
 #define _CET_ENDBR
 #endif
 
-#ifdef __APPLE__
-#define HIDDEN .private_extern
+#if defined(__APPLE__)
+#define HIDDEN(symbol) .private_extern symbol
+#elif defined(__ELF__)
+#define HIDDEN(symbol) .hidden symbol
 #else
-#define HIDDEN .hidden
+#define HIDDEN(symbol)
 #endif
 
 .intel_syntax noprefix
-HIDDEN blake3_hash_many_sse2
-HIDDEN _blake3_hash_many_sse2
-HIDDEN blake3_compress_in_place_sse2
-HIDDEN _blake3_compress_in_place_sse2
-HIDDEN blake3_compress_xof_sse2
-HIDDEN _blake3_compress_xof_sse2
+HIDDEN(blake3_hash_many_sse2)
+HIDDEN(_blake3_hash_many_sse2)
+HIDDEN(blake3_compress_in_place_sse2)
+HIDDEN(_blake3_compress_in_place_sse2)
+HIDDEN(blake3_compress_xof_sse2)
+HIDDEN(_blake3_compress_xof_sse2)
 .global blake3_hash_many_sse2
 .global _blake3_hash_many_sse2
 .global blake3_compress_in_place_sse2
diff --git a/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S
index c5b103af61c4f..5866bfb7ae461 100644
--- a/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S
@@ -16,19 +16,21 @@
 #define _CET_ENDBR
 #endif
 
-#ifdef __APPLE__
-#define HIDDEN .private_extern
+#if defined(__APPLE__)
+#define HIDDEN(symbol) .private_extern symbol
+#elif defined(__ELF__)
+#define HIDDEN(symbol) .hidden symbol
 #else
-#define HIDDEN .hidden
+#define HIDDEN(symbol)
 #endif
 
 .intel_syntax noprefix
-HIDDEN blake3_hash_many_sse41
-HIDDEN _blake3_hash_many_sse41
-HIDDEN blake3_compress_in_place_sse41
-HIDDEN _blake3_compress_in_place_sse41
-HIDDEN blake3_compress_xof_sse41
-HIDDEN _blake3_compress_xof_sse41
+HIDDEN(blake3_hash_many_sse41)
+HIDDEN(_blake3_hash_many_sse41)
+HIDDEN(blake3_compress_in_place_sse41)
+HIDDEN(_blake3_compress_in_place_sse41)
+HIDDEN(blake3_compress_xof_sse41)
+HIDDEN(_blake3_compress_xof_sse41)
 .global blake3_hash_many_sse41
 .global _blake3_hash_many_sse41
 .global blake3_compress_in_place_sse41
diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index d3bade579f003..0d1c4a800c9df 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -4,6 +4,8 @@
 
 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
 load("@rules_python//python:defs.bzl", "py_binary")
+load("@bazel_lib//lib:run_binary.bzl", "run_binary")
+load("@bazel_skylib//rules:write_file.bzl", "write_file")
 load(
     "//:vars.bzl",
     "LLVM_VERSION_MAJOR",
@@ -577,23 +579,18 @@ exports_files(
     glob(["include/**/*.td"]),
 )
 
-genrule(
+write_file(
     name = "basic_version_gen",
-    outs = ["include/clang/Basic/Version.inc"],
-    cmd = (
-        "echo '#define CLANG_VERSION {vers}' >> $@\n" +
-        "echo '#define CLANG_VERSION_MAJOR {major}' >> $@\n" +
-        "echo '#define CLANG_VERSION_MAJOR_STRING \"{major}\"' >> $@\n" +
-        "echo '#define CLANG_VERSION_MINOR {minor}' >> $@\n" +
-        "echo '#define CLANG_VERSION_PATCHLEVEL {patch}' >> $@\n" +
-        "echo '#define MAX_CLANG_ABI_COMPAT_VERSION {major}' >> $@\n" +
-        "echo '#define CLANG_VERSION_STRING \"{vers}\"' >> $@\n"
-    ).format(
-        major = LLVM_VERSION_MAJOR,
-        minor = LLVM_VERSION_MINOR,
-        patch = LLVM_VERSION_PATCH,
-        vers = PACKAGE_VERSION,
-    ),
+    out = "include/clang/Basic/Version.inc",
+    content = [
+        "#define CLANG_VERSION {}".format(PACKAGE_VERSION),
+        "#define CLANG_VERSION_MAJOR {}".format(LLVM_VERSION_MAJOR),
+        "#define CLANG_VERSION_MAJOR_STRING \"{}\"".format(LLVM_VERSION_MAJOR),
+        "#define CLANG_VERSION_MINOR {}".format(LLVM_VERSION_MINOR),
+        "#define CLANG_VERSION_PATCHLEVEL {}".format(LLVM_VERSION_PATCH),
+        "#define MAX_CLANG_ABI_COMPAT_VERSION {}".format(LLVM_VERSION_MAJOR),
+        "#define CLANG_VERSION_STRING \"{}\"".format(PACKAGE_VERSION),
+    ],
 )
 
 cc_library(
@@ -611,13 +608,15 @@ cc_library(
 
 # TODO: This should get replaced with something that actually generates the
 # correct version number.
-genrule(
+write_file(
     name = "vcs_version_gen",
     # This should be under lib/Basic, but because of how the include paths
     # are passed through bazel, it's easier to drop generated files next to
     # the other includes.
-    outs = ["include/VCSVersion.inc"],
-    cmd = "echo '#undef CLANG_REVISION' > $@",
+    out = "include/VCSVersion.inc",
+    content = [
+        "#undef CLANG_REVISION",
+    ],
 )
 
 py_binary(
@@ -979,16 +978,21 @@ cc_library(
     ],
 )
 
-genrule(
+run_binary(
     name = "analysis_htmllogger_gen",
+    tool = ":bundle_resources",
     srcs = [
         "lib/Analysis/FlowSensitive/HTMLLogger.html",
         "lib/Analysis/FlowSensitive/HTMLLogger.css",
         "lib/Analysis/FlowSensitive/HTMLLogger.js",
     ],
     outs = ["lib/Analysis/FlowSensitive/HTMLLogger.inc"],
-    cmd = "$(location :bundle_resources) $@ $(SRCS)",
-    tools = [":bundle_resources"],
+    args = [
+        "$@",
+        "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.html)",
+        "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.css)",
+        "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.js)",
+    ],
 )
 
 cc_library(
@@ -1549,7 +1553,9 @@ cc_library(
         "lib/Driver",
     ],
     linkopts = select({
-        "@platforms//os:windows": ["version.lib"],
+        "//llvm:is_windows_clang_mingw": ["-lversion"],
+        "//llvm:is_windows_clang_cl": ["version.lib"],
+        "//llvm:is_windows_msvc": ["version.lib"],
         "//conditions:default": [],
     }),
     textual_hdrs = glob([
diff --git a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel
index 1118f5e81f7ed..6f830ba72a0a2 100644
--- a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel
@@ -10,6 +10,7 @@ load(
 load("//llvm:binary_alias.bzl", "binary_alias")
 load("//llvm:driver.bzl", "llvm_driver_cc_binary")
 load("//mlir:tblgen.bzl", "gentbl_cc_library")
+load("@bazel_skylib//rules:write_file.bzl", "write_file")
 
 package(
     default_visibility = ["//visibility:public"],
@@ -19,17 +20,21 @@ package(
 licenses(["notice"])
 
 # TODO: Actually compute version info
-genrule(
+write_file(
     name = "config_version_gen",
-    outs = ["include/lld/Common/Version.inc"],
-    cmd = "echo '#define LLD_VERSION_STRING \"{}\"' > $@".format(LLVM_VERSION),
+    out = "include/lld/Common/Version.inc",
+    content = [
+        "#define LLD_VERSION_STRING \"{}\"".format(LLVM_VERSION),
+    ],
 )
 
-genrule(
+write_file(
     name = "vcs_version_gen",
-    outs = ["Common/VCSVersion.inc"],
-    cmd = "echo '#undef LLD_REVISION' >> $@\n" +
-          "echo '#undef LLD_REPOSITORY' >> $@\n",
+    out = "Common/VCSVersion.inc",
+    content = [
+        "#undef LLD_REVISION",
+        "#undef LLD_REPOSITORY",
+    ],
 )
 
 # See https://github.com/bazelbuild/bazel/issues/13803
diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
index 127bd7968ef0a..98d0cfe55f44e 100644
--- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -4,6 +4,7 @@
 
 load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
 load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
+load("@bazel_skylib//rules:write_file.bzl", "write_file")
 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
 load("@rules_python//python:defs.bzl", "py_binary")
 load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
@@ -208,18 +209,40 @@ cc_library(
     deps = [":config"],
 )
 
-genrule(
+write_file(
     name = "generate_vcs_revision",
-    outs = ["include/llvm/Support/VCSRevision.h"],
-    cmd = "echo '#undef LLVM_REVISION' >> $@\n" +
-          "echo '#undef LLVM_REPOSITORY' >> $@\n",
+    out = "include/llvm/Support/VCSRevision.h",
+    content = [
+        "#undef LLVM_REVISION",
+        "#undef LLVM_REPOSITORY",
+    ],
 )
 
-genrule(
+write_file(
     name = "generate_static_extension_registry",
-    outs = ["include/llvm/Support/Extension.def"],
-    cmd = "echo -e '// extension handlers' >> $@\n" +
-          "echo -e '#undef HANDLE_EXTENSION' >> $@\n",
+    out = "include/llvm/Support/Extension.def",
+    content = [
+        "// extension handlers",
+        "#undef HANDLE_EXTENSION",
+    ],
+)
+
+config_setting(
+    name = "is_windows_clang_mingw",
+    constraint_values = ["@platforms//os:windows"],
+    flag_values = {"@rules_cc//cc/compiler:compiler": "clang"},
+)
+
+config_setting(
+    name = "is_windows_clang_cl",
+    constraint_values = ["@platforms//os:windows"],
+    flag_values = {"@rules_cc//cc/compiler:compiler": "clang-cl"},
+)
+
+config_setting(
+    name = "is_windows_msvc",
+    constraint_values = ["@platforms//os:windows"],
+    flag_values = {"@rules_cc//cc/compiler:compiler": "msvc-cl"},
 )
 
 cc_library(
@@ -297,7 +320,15 @@ cc_library(
     }),
     includes = ["include"],
     linkopts = select({
-        "@platforms//os:windows": [
+        ":is_windows_clang_mingw": [
+            "-lws2_32",
+            "-lntdll",
+        ],
+        ":is_windows_clang_cl": [
+            "ws2_32.lib",
+            "ntdll.lib",
+        ],
+        ":is_windows_msvc": [
             "ws2_32.lib",
             "ntdll.lib",
         ],

``````````

</details>


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


More information about the llvm-commits mailing list