From 26ed7133f84bb8326e2aaf6d3000049bcfe06837 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Sun, 16 Jun 2024 23:30:20 -0400 Subject: [PATCH 1/6] feat: windows tools --- toolchain/internal/common.bzl | 6 +++--- toolchain/internal/configure.bzl | 13 +++++++++---- toolchain/internal/release_name.bzl | 10 ++++++++++ toolchain/internal/repo.bzl | 19 +++++++++++-------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/toolchain/internal/common.bzl b/toolchain/internal/common.bzl index ff94bfb22..d3a7d631c 100644 --- a/toolchain/internal/common.bzl +++ b/toolchain/internal/common.bzl @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64")] +SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64"), ("windows", "x86_64")] # Map of tool name to its symlinked name in the tools directory. # See tool_paths in toolchain/cc_toolchain_config.bzl. @@ -104,7 +104,7 @@ def os(rctx): name = rctx.attr.exec_os if name: - if name in ("linux", "darwin"): + if name in ("linux", "darwin", "windows"): return name else: fail("Unsupported value for exec_os: %s" % name) @@ -120,7 +120,7 @@ def os(rctx): def os_bzl(os): # Return the OS string as used in bazel platform constraints. - return {"darwin": "osx", "linux": "linux"}[os] + return {"darwin": "osx", "linux": "linux", "windows": "windows"}[os] def arch(rctx): arch = rctx.attr.exec_arch diff --git a/toolchain/internal/configure.bzl b/toolchain/internal/configure.bzl index 6e40059cf..21b726526 100644 --- a/toolchain/internal/configure.bzl +++ b/toolchain/internal/configure.bzl @@ -61,9 +61,6 @@ def llvm_config_impl(rctx): _check_os_arch_keys(rctx.attr.cxx_builtin_include_directories) os = _os(rctx) - if os == "windows": - _empty_repository(rctx) - return arch = _arch(rctx) if not rctx.attr.toolchain_roots: @@ -311,6 +308,7 @@ def _cc_toolchain_str( "darwin-aarch64": "aarch64-apple-macosx", "linux-aarch64": "aarch64-unknown-linux-gnu", "linux-x86_64": "x86_64-unknown-linux-gnu", + "windows-x86_64": "x86_64-pc-windows-msvc", }[target_pair] cxx_builtin_include_directories = [ toolchain_path_prefix + "include/c++/v1", @@ -545,7 +543,13 @@ cc_toolchain( major_llvm_version = major_llvm_version, ) +def _extension(os): + if (os == "windows"): + return ".exe" + return "" + def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, exec_dl_ext): + ext = _extension(_os(rctx)) if use_absolute_paths: llvm_dist_label_prefix = ":" filenames = [] @@ -553,7 +557,7 @@ def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_ filename = "lib/{}.{}".format(libname, exec_dl_ext) filenames.append(filename) for toolname in _aliased_tools: - filename = "bin/{}".format(toolname) + filename = "bin/{}{}".format(toolname, ext) filenames.append(filename) for filename in filenames: @@ -570,6 +574,7 @@ cc_import( tool_target_strs = [] for name in _aliased_tools: + name = name+ext template = """ native_binary( name = "{name}", diff --git a/toolchain/internal/release_name.bzl b/toolchain/internal/release_name.bzl index e3d4a540d..70a01a955 100755 --- a/toolchain/internal/release_name.bzl +++ b/toolchain/internal/release_name.bzl @@ -46,6 +46,16 @@ def _darwin(llvm_version, arch): ) def _windows(llvm_version, arch): + major_llvm_version = _major_llvm_version(llvm_version) + if (major_llvm_version >= 18 and arch.endswith("64")): + arch = "x86_64" + suffix = "pc-windows-msvc" + return "clang+llvm-{llvm_version}-{arch}-{suffix}.tar.xz".format( + llvm_version = llvm_version, + arch = arch, + suffix = suffix, + ) + if arch.endswith("64"): win_arch = "win64" else: diff --git a/toolchain/internal/repo.bzl b/toolchain/internal/repo.bzl index 27012db6f..3b2899d02 100644 --- a/toolchain/internal/repo.bzl +++ b/toolchain/internal/repo.bzl @@ -291,14 +291,17 @@ llvm_config_attrs.update({ def llvm_repo_impl(rctx): os = _os(rctx) if os == "windows": - rctx.file("BUILD.bazel", executable = False) - return None - - rctx.file( - "BUILD.bazel", - content = rctx.read(Label("//toolchain:BUILD.llvm_repo")), - executable = False, - ) + rctx.file( + "BUILD.bazel", + content = rctx.read(Label("//toolchain:BUILD.llvm_repo_windows")), + executable = False, + ) + else: + rctx.file( + "BUILD.bazel", + content = rctx.read(Label("//toolchain:BUILD.llvm_repo")), + executable = False, + ) updated_attrs = _download_llvm(rctx) From d8b3b71943486cea06b2d421419ddc0df945daed Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Sun, 16 Jun 2024 23:36:56 -0400 Subject: [PATCH 2/6] feat: windows build file --- toolchain/BUILD.llvm_repo_windows | 140 ++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 toolchain/BUILD.llvm_repo_windows diff --git a/toolchain/BUILD.llvm_repo_windows b/toolchain/BUILD.llvm_repo_windows new file mode 100644 index 000000000..442ffdbb0 --- /dev/null +++ b/toolchain/BUILD.llvm_repo_windows @@ -0,0 +1,140 @@ +# Copyright 2021 The Bazel Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +package(default_visibility = ["//visibility:public"]) + +# Some targets may need to directly depend on these files. +exports_files(glob([ + "bin/*", + "lib/*", + "include/*", +])) + +## LLVM toolchain files + +filegroup( + name = "clang", + srcs = [ + "bin/clang.exe", + "bin/clang++.exe", + "bin/clang-cpp.exe", + ], +) + +filegroup( + name = "ld", + srcs = glob(["bin/ld.lld.exe", "bin/ld64.lld.exe"]), +) + +filegroup( + name = "include", + srcs = glob([ + "include/**/c++/**", + "lib/clang/*/include/**", + ]), +) + +filegroup( + name = "all_includes", + srcs = glob(["include/**"]), +) + +filegroup( + name = "bin", + srcs = glob(["bin/**"]), +) + +filegroup( + name = "lib", + srcs = glob( + [ + "lib/**/lib*.a", + "lib/clang/*/lib/**/*.a", + "lib/clang/*/lib/**/*.dylib", + # clang_rt.*.o supply crtbegin and crtend sections. + "lib/**/clang_rt.*.o", + ], + exclude = [ + "lib/libLLVM*.a", + "lib/libclang*.a", + "lib/liblld*.a", + ], + ), + # Include the .dylib files in the linker sandbox even though they will + # not be available at runtime to allow sanitizers to work locally. + # Any library linked from the toolchain to be released should be linked statically. +) + +filegroup( + name = "ar", + srcs = ["bin/llvm-ar.exe"], +) + +filegroup( + name = "as", + srcs = [ + "bin/clang.exe", + "bin/llvm-as.exe", + ], +) + +filegroup( + name = "nm", + srcs = ["bin/llvm-nm.exe"], +) + +filegroup( + name = "objcopy", + srcs = ["bin/llvm-objcopy.exe"], +) + +filegroup( + name = "objdump", + srcs = ["bin/llvm-objdump.exe"], +) + +filegroup( + name = "profdata", + srcs = ["bin/llvm-profdata.exe"], +) + +filegroup( + name = "dwp", + srcs = ["bin/llvm-dwp.exe"], +) + +filegroup( + name = "ranlib", + srcs = ["bin/llvm-ranlib.exe"], +) + +filegroup( + name = "readelf", + srcs = ["bin/llvm-readelf.exe"], +) + +filegroup( + name = "strip", + srcs = ["bin/llvm-strip.exe"], +) + +filegroup( + name = "symbolizer", + srcs = ["bin/llvm-symbolizer.exe"], +) + +filegroup( + name = "clang-tidy", + srcs = ["bin/clang-tidy.exe"], +) From 0c19342b0e7dd8c58163744ed98971e2fdcfe250 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Sun, 16 Jun 2024 23:52:19 -0400 Subject: [PATCH 3/6] continue --- toolchain/cc_toolchain_config.bzl | 8 ++++++++ toolchain/internal/configure.bzl | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index c1b0d9226..f5cc8b51e 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -89,6 +89,14 @@ def cc_toolchain_config( "clang", "glibc_unknown", ), + "windows-x86_64": ( + "clang-x86_64-windows", + "k8", + "msvc", + "clang", + "windows_x86_64", + "windows_x86_64", + ), }[target_os_arch_key] # Unfiltered compiler flags; these are placed at the end of the command diff --git a/toolchain/internal/configure.bzl b/toolchain/internal/configure.bzl index 21b726526..01512089a 100644 --- a/toolchain/internal/configure.bzl +++ b/toolchain/internal/configure.bzl @@ -336,7 +336,7 @@ def _cc_toolchain_str( _join(sysroot_prefix, "/System/Library/Frameworks"), ]) else: - fail("Unreachable") + pass cxx_builtin_include_directories.extend(toolchain_info.additional_include_dirs_dict.get(target_pair, [])) From 95dfb67754a81fe8d66d2e07a447a7d89250b1e6 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 17 Jun 2024 06:50:38 -0400 Subject: [PATCH 4/6] fix: address comments --- toolchain/BUILD.llvm_repo_windows | 19 +++++++------------ toolchain/internal/configure.bzl | 4 ++-- toolchain/internal/release_name.bzl | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/toolchain/BUILD.llvm_repo_windows b/toolchain/BUILD.llvm_repo_windows index 442ffdbb0..80b0cff62 100644 --- a/toolchain/BUILD.llvm_repo_windows +++ b/toolchain/BUILD.llvm_repo_windows @@ -29,12 +29,14 @@ filegroup( "bin/clang.exe", "bin/clang++.exe", "bin/clang-cpp.exe", + "bin/clang-cl.exe", ], ) filegroup( name = "ld", - srcs = glob(["bin/ld.lld.exe", "bin/ld64.lld.exe"]), + # a set of 4 identical binaries? + srcs = glob(["bin/ld.lld.exe", "bin/ld64.lld.exe", "lld-link.exe", "lld.exe"]), ) filegroup( @@ -59,21 +61,14 @@ filegroup( name = "lib", srcs = glob( [ - "lib/**/lib*.a", - "lib/clang/*/lib/**/*.a", - "lib/clang/*/lib/**/*.dylib", - # clang_rt.*.o supply crtbegin and crtend sections. - "lib/**/clang_rt.*.o", + "lib/**/*.lib", ], exclude = [ - "lib/libLLVM*.a", - "lib/libclang*.a", - "lib/liblld*.a", + "lib/LLVM*.lib", + "lib/clang*.lib", + "lib/lld*.lib", ], ), - # Include the .dylib files in the linker sandbox even though they will - # not be available at runtime to allow sanitizers to work locally. - # Any library linked from the toolchain to be released should be linked statically. ) filegroup( diff --git a/toolchain/internal/configure.bzl b/toolchain/internal/configure.bzl index 01512089a..6840d318a 100644 --- a/toolchain/internal/configure.bzl +++ b/toolchain/internal/configure.bzl @@ -544,7 +544,7 @@ cc_toolchain( ) def _extension(os): - if (os == "windows"): + if os == "windows": return ".exe" return "" @@ -574,7 +574,7 @@ cc_import( tool_target_strs = [] for name in _aliased_tools: - name = name+ext + name = name + ext template = """ native_binary( name = "{name}", diff --git a/toolchain/internal/release_name.bzl b/toolchain/internal/release_name.bzl index 70a01a955..a56adabcc 100755 --- a/toolchain/internal/release_name.bzl +++ b/toolchain/internal/release_name.bzl @@ -47,7 +47,7 @@ def _darwin(llvm_version, arch): def _windows(llvm_version, arch): major_llvm_version = _major_llvm_version(llvm_version) - if (major_llvm_version >= 18 and arch.endswith("64")): + if major_llvm_version >= 18 and arch.endswith("64"): arch = "x86_64" suffix = "pc-windows-msvc" return "clang+llvm-{llvm_version}-{arch}-{suffix}.tar.xz".format( From e0275438450ac0ce61887830f97273659695a464 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 17 Jun 2024 07:29:58 -0400 Subject: [PATCH 5/6] fix: address comments --- platforms/BUILD.bazel | 8 ++++ toolchain/BUILD.llvm_repo_windows | 64 ++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/platforms/BUILD.bazel b/platforms/BUILD.bazel index d9c7140dc..3d78b56a8 100644 --- a/platforms/BUILD.bazel +++ b/platforms/BUILD.bazel @@ -45,3 +45,11 @@ platform( "@platforms//cpu:aarch64", ], ) + +platform( + name = "windows-x86_64", + constraint_values = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], +) diff --git a/toolchain/BUILD.llvm_repo_windows b/toolchain/BUILD.llvm_repo_windows index 80b0cff62..1a85d38b0 100644 --- a/toolchain/BUILD.llvm_repo_windows +++ b/toolchain/BUILD.llvm_repo_windows @@ -23,6 +23,8 @@ exports_files(glob([ ## LLVM toolchain files +ALL_DLLS = glob(["bin/*.dll"]) + filegroup( name = "clang", srcs = [ @@ -30,13 +32,41 @@ filegroup( "bin/clang++.exe", "bin/clang-cpp.exe", "bin/clang-cl.exe", - ], + ] + ALL_DLLS, +) + +filegroup( + name = "clang-cl", + srcs = [ + "bin/clang-cl.exe", + ] + ALL_DLLS, +) + +filegroup( + name = "llvm-ml", + srcs = [ + "bin/llvm-ml.exe", + ] + ALL_DLLS, ) filegroup( name = "ld", # a set of 4 identical binaries? - srcs = glob(["bin/ld.lld.exe", "bin/ld64.lld.exe", "lld-link.exe", "lld.exe"]), + srcs = ["bin/ld.lld.exe", "bin/ld64.lld.exe", "lld-link.exe", "lld.exe"] + ALL_DLLS, +) + +filegroup( + name = "lld-link", + srcs = [ + "bin/lld-link.exe", + ] + ALL_DLLS, +) + +filegroup( + name = "llvm-lib", + srcs = [ + "bin/llvm-lib.exe", + ] + ALL_DLLS, ) filegroup( @@ -71,9 +101,14 @@ filegroup( ), ) +filegroup( + name = "dll", + srcs = ALL_DLLS, +) + filegroup( name = "ar", - srcs = ["bin/llvm-ar.exe"], + srcs = ["bin/llvm-ar.exe"] + ALL_DLLS, ) filegroup( @@ -81,55 +116,56 @@ filegroup( srcs = [ "bin/clang.exe", "bin/llvm-as.exe", - ], + ] + ALL_DLLS, ) filegroup( name = "nm", - srcs = ["bin/llvm-nm.exe"], + srcs = ["bin/llvm-nm.exe"] + ALL_DLLS, ) filegroup( name = "objcopy", - srcs = ["bin/llvm-objcopy.exe"], + srcs = ["bin/llvm-objcopy.exe"] + ALL_DLLS, + deps = [":dll"], ) filegroup( name = "objdump", - srcs = ["bin/llvm-objdump.exe"], + srcs = ["bin/llvm-objdump.exe"] + ALL_DLLS, ) filegroup( name = "profdata", - srcs = ["bin/llvm-profdata.exe"], + srcs = ["bin/llvm-profdata.exe"] + ALL_DLLS, ) filegroup( name = "dwp", - srcs = ["bin/llvm-dwp.exe"], + srcs = ["bin/llvm-dwp.exe"] + ALL_DLLS, ) filegroup( name = "ranlib", - srcs = ["bin/llvm-ranlib.exe"], + srcs = ["bin/llvm-ranlib.exe"] + ALL_DLLS, ) filegroup( name = "readelf", - srcs = ["bin/llvm-readelf.exe"], + srcs = ["bin/llvm-readelf.exe"] + ALL_DLLS, ) filegroup( name = "strip", - srcs = ["bin/llvm-strip.exe"], + srcs = ["bin/llvm-strip.exe"] + ALL_DLLS, ) filegroup( name = "symbolizer", - srcs = ["bin/llvm-symbolizer.exe"], + srcs = ["bin/llvm-symbolizer.exe"] + ALL_DLLS, ) filegroup( name = "clang-tidy", - srcs = ["bin/clang-tidy.exe"], + srcs = ["bin/clang-tidy.exe"] + ALL_DLLS, ) From f2de4fc207ad0ad46da25969300e17b3ac7a2536 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 17 Jun 2024 15:24:24 -0400 Subject: [PATCH 6/6] fix: typo --- toolchain/BUILD.llvm_repo_windows | 1 - 1 file changed, 1 deletion(-) diff --git a/toolchain/BUILD.llvm_repo_windows b/toolchain/BUILD.llvm_repo_windows index 1a85d38b0..baca19e4f 100644 --- a/toolchain/BUILD.llvm_repo_windows +++ b/toolchain/BUILD.llvm_repo_windows @@ -127,7 +127,6 @@ filegroup( filegroup( name = "objcopy", srcs = ["bin/llvm-objcopy.exe"] + ALL_DLLS, - deps = [":dll"], ) filegroup(