Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ single_version_override(
"//patches/llvm:stage0_runtime_features.patch",
"//patches/llvm:aarch64_outline_atomics.patch",
"//patches/llvm:macos_sdk_resolve_symlinks.patch",
"//patches/llvm:msvc_link_lib_env.patch",
"//patches/llvm:toolchains_gate.patch",
],
version = "0.8.26",
Expand Down Expand Up @@ -568,6 +567,8 @@ use_repo(apple_cc_configure, "local_config_apple_cc_toolchains")

register_toolchains("@local_config_apple_cc_toolchains//:all")

register_toolchains("//cc/private/toolchain/clang_cl:all")

register_toolchains("@llvm//toolchain:all")

# A consumer-declared GCC distribution, selected only through --extra_toolchains in tests.
Expand Down
129 changes: 129 additions & 0 deletions cc/private/toolchain/clang_cl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains/impl:documented_api.bzl", "cc_args_list")
load(":search_path.bzl", "search_path")
load(":toolchains.bzl", "clang_cl_toolchains")

package(default_visibility = ["//visibility:public"])


search_path(
name = "dynamic_crt_link_lib_path",
directories = [
"@llvm//runtimes/msvc:winsdk_ucrt_lib",
"@llvm//runtimes/msvc:winsdk_um_lib",
"@llvm//runtimes/msvc:msvc_common_support_libraries",
"@llvm//runtimes/msvc:msvc_com_support_libraries",
"@llvm//runtimes/msvc:msvc_dynamic_runtime_libraries",
"@llvm//runtimes/cxxstdlib:library_search_directory",
],
target_compatible_with = ["@platforms//os:windows"],
)

cc_args(
name = "dynamic_crt_link_lib_env",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
data = [":dynamic_crt_link_lib_path"],
env = {"LIB": "{lib}"},
format = {"lib": ":dynamic_crt_link_lib_path"},
)

search_path(
name = "dynamic_crt_hosted_link_lib_path",
directories = [
"@llvm//runtimes/msvc:winsdk_ucrt_lib",
"@llvm//runtimes/msvc:winsdk_um_lib",
"@llvm//runtimes/msvc:msvc_common_support_libraries",
"@llvm//runtimes/msvc:msvc_com_support_libraries",
"@llvm//runtimes/msvc:msvc_dynamic_runtime_libraries",
],
target_compatible_with = ["@platforms//os:windows"],
)

cc_args(
name = "dynamic_crt_hosted_link_lib_env",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
data = [":dynamic_crt_hosted_link_lib_path"],
env = {"LIB": "{lib}"},
format = {"lib": ":dynamic_crt_hosted_link_lib_path"},
)

search_path(
name = "static_crt_link_lib_path",
directories = [
"@llvm//runtimes/msvc:winsdk_ucrt_lib",
"@llvm//runtimes/msvc:winsdk_um_lib",
"@llvm//runtimes/msvc:msvc_common_support_libraries",
"@llvm//runtimes/msvc:msvc_com_support_libraries",
"@llvm//runtimes/msvc:msvc_static_runtime_libraries",
"@llvm//runtimes/cxxstdlib:library_search_directory",
],
target_compatible_with = ["@platforms//os:windows"],
)

cc_args(
name = "static_crt_link_lib_env",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
data = [":static_crt_link_lib_path"],
env = {"LIB": "{lib}"},
format = {"lib": ":static_crt_link_lib_path"},
)

search_path(
name = "static_crt_hosted_link_lib_path",
directories = [
"@llvm//runtimes/msvc:winsdk_ucrt_lib",
"@llvm//runtimes/msvc:winsdk_um_lib",
"@llvm//runtimes/msvc:msvc_common_support_libraries",
"@llvm//runtimes/msvc:msvc_com_support_libraries",
"@llvm//runtimes/msvc:msvc_static_runtime_libraries",
],
target_compatible_with = ["@platforms//os:windows"],
)

cc_args(
name = "static_crt_hosted_link_lib_env",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
data = [":static_crt_hosted_link_lib_path"],
env = {"LIB": "{lib}"},
format = {"lib": ":static_crt_hosted_link_lib_path"},
)

cc_args(
name = "empty_link_lib_env",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
env = {"LIB": "__hermetic_llvm_empty_lib__"},
)

cc_args_list(
name = "hosted_link_lib_env",
args = select({
"@llvm//toolchain/features/msvc:static_crt_config": [":static_crt_hosted_link_lib_env"],
"//conditions:default": [":dynamic_crt_hosted_link_lib_env"],
}),
)

cc_args_list(
name = "full_link_lib_env",
args = select({
"@llvm//toolchain/features/msvc:static_crt_config": [":static_crt_link_lib_env"],
"//conditions:default": [":dynamic_crt_link_lib_env"],
}),
)

cc_args_list(
name = "runtime_link_lib_env",
args = select({
"@llvm//toolchain:runtimes_stage1_hosted": [":hosted_link_lib_env"],
"//conditions:default": [":full_link_lib_env"],
}),
)

cc_args_list(
name = "link_lib_env",
args = select({
"@llvm//toolchain:runtimes_none": [":empty_link_lib_env"],
"//conditions:default": [":runtime_link_lib_env"],
}),
)

clang_cl_toolchains(link_lib_env = ":link_lib_env")
26 changes: 26 additions & 0 deletions cc/private/toolchain/clang_cl/search_path.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Joins library directories into one search path value for cc_args format."""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")

def _directory_path(target):
if DirectoryInfo in target:
return target[DirectoryInfo].path
files = target[DefaultInfo].files.to_list()
if len(files) != 1:
fail("%s should be either a directory or a single file." % target.label)
return files[0].path

def _search_path_impl(ctx):
return [
DefaultInfo(files = depset(transitive = [d[DefaultInfo].files for d in ctx.attr.directories])),
BuildSettingInfo(value = ctx.attr.separator.join([_directory_path(d) for d in ctx.attr.directories])),
]

search_path = rule(
implementation = _search_path_impl,
attrs = {
"directories": attr.label_list(mandatory = True),
"separator": attr.string(default = ";"),
},
)
186 changes: 186 additions & 0 deletions cc/private/toolchain/clang_cl/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
"""clang-cl cc_toolchains for MSVC targets whose link actions carry the hermetic LIB search path."""

load("@bazel_features//:features.bzl", "bazel_features")
load("@llvm//platforms:common.bzl", "MSVC_TARGET_STAGE0_SUPPORTED_EXECS", "WINDOWS_TARGETS")
load("@llvm//toolchain:cc_toolchain.bzl", "cc_toolchain")
load("@llvm//toolchain:merged_resource_directory.bzl", "merged_resource_directory")
load("@llvm//toolchain:selects.bzl", "platform_cc_tool_map", "platform_module_map", "platform_resource_dir")
load("@llvm//toolchain/args:resource_directory_args.bzl", "resource_directory_args")
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")

def _tool_map(prefix, exec_os, exec_cpu):
suffix = ".exe" if exec_os == "windows" else ""
resource_dir = platform_resource_dir(exec_os, exec_cpu)

def binary(name):
return resource_dir.same_package_label("bin/" + name + suffix)

cc_tool(
name = prefix + "_clang_cl",
src = binary("clang-cl"),
data = [
resource_dir,
binary("lld-link"),
],
capabilities = [
"@rules_cc//cc/toolchains/capabilities:has_configured_linker_path",
"@rules_cc//cc/toolchains/capabilities:supports_dynamic_linker",
"@rules_cc//cc/toolchains/capabilities:supports_interface_shared_libraries",
],
allowlist_include_directories = [
resource_dir,
resource_dir.same_package_label("builtin_resource_include_dir"),
],
)

cc_tool(
name = prefix + "_llvm_lib",
src = binary("llvm-lib"),
env = {"LIB": "__hermetic_llvm_empty_lib__"},
)

cc_tool(
name = prefix + "_llvm_strip",
src = binary("llvm-strip"),
data = select({
"@llvm//toolchain:runtimes_all": ["@llvm//tools/internal:static-library-validator"],
"//conditions:default": [],
}) + [binary(tool) for tool in ["c++filt", "llvm-nm", "gcov", "llvm-cov", "llvm-profdata"]],
)

cc_tool(
name = prefix + "_def_file_generator",
src = "@llvm//tools/def_file_generator",
data = [binary("llvm-nm")],
env = {"LLVM_NM": "{llvm_nm}"},
format = {"llvm_nm": binary("llvm-nm")},
)

cc_tool(
name = prefix + "_static_library_validator",
src = "@llvm//tools/internal:static-library-validator",
data = [binary("c++filt"), binary("llvm-nm")],
env = {
"LLVM_CXXFILT": "{cxxfilt}",
"LLVM_NM": "{llvm_nm}",
},
format = {
"cxxfilt": binary("c++filt"),
"llvm_nm": binary("llvm-nm"),
},
)

clang_cl = ":" + prefix + "_clang_cl"
construction_tools = {
"@rules_cc//cc/toolchains/actions:" + action: clang_cl
for action in [
"assemble",
"c_compile",
"cpp_compile",
"linkstamp_compile",
"lto_backend",
"preprocess_assemble",
"cpp_header_parsing",
"cpp_link_executable",
"cpp_link_dynamic_library",
"cpp_link_nodeps_dynamic_library",
"objc_executable",
"lto_index_for_executable",
"lto_index_for_dynamic_library",
"lto_index_for_nodeps_dynamic_library",
]
} | {
"@rules_cc//cc/toolchains/actions:ar_actions": ":" + prefix + "_llvm_lib",
"@rules_cc//cc/toolchains/actions:strip": ":" + prefix + "_llvm_strip",
}
validator = {
"@rules_cc//cc/toolchains/actions:validate_static_library": ":" + prefix + "_static_library_validator",
} if bazel_features.cc.supports_starlarkified_toolchains else {}

cc_tool_map(
name = prefix + "_complete_tools",
tools = construction_tools | validator | {
"@rules_cc//cc/toolchains/actions:generate_def_file": ":" + prefix + "_def_file_generator",
},
)

cc_tool_map(
name = prefix + "_construction_tools",
tools = construction_tools,
)

native.alias(
name = prefix + "_tools",
actual = select({
"@llvm//toolchain:runtimes_all": ":" + prefix + "_complete_tools",
"//conditions:default": ":" + prefix + "_construction_tools",
}),
)
return ":" + prefix + "_tools"

def clang_cl_toolchains(link_lib_env):
"""Declares one clang-cl cc_toolchain per LLVM execution platform for MSVC targets.

Args:
link_lib_env: cc_args setting LIB on link actions.
"""
for (exec_os, exec_cpu) in MSVC_TARGET_STAGE0_SUPPORTED_EXECS:
prefix = exec_os + "_" + exec_cpu
case_overlays = [] if exec_os == "windows" else ["@llvm//toolchain/args/windows/msvc:case_overlays"]
msvc_args = [
"@llvm//toolchain/args/windows/msvc:normalized_default_libs_for_runtime",
"@llvm//toolchain/args/windows/msvc:normalized_sdk_compile_args",
link_lib_env,
] + case_overlays

merged_resource_directory(
name = prefix + "_resource_directory",
parent = platform_resource_dir(exec_os, exec_cpu),
srcs = ["@llvm//runtimes:resource_directory"],
)
resource_directory_args(
name = prefix + "_resource_directory_args",
compile_directory = platform_resource_dir(exec_os, exec_cpu),
link_directory = ":" + prefix + "_resource_directory",
)

native.alias(
name = prefix + "_llvm_tools",
actual = platform_cc_tool_map(exec_os, exec_cpu),
)

cc_toolchain(
name = prefix + "_cc_toolchain",
tool_map = select({
"@llvm//constraints/windows/abi:msvc": _tool_map(prefix, exec_os, exec_cpu),
"//conditions:default": ":" + prefix + "_llvm_tools",
}),
module_map = platform_module_map(exec_os, exec_cpu),
supports_gnu_param_file_quoting = exec_os in ["linux", "macos"],
extra_args = [":" + prefix + "_resource_directory_args"] + select({
"@llvm//platforms/config:windows_x86_64_msvc": msvc_args,
"@llvm//platforms/config:windows_aarch64_msvc": msvc_args,
"//conditions:default": [],
}),
)

for (target_os, target_cpu) in WINDOWS_TARGETS:
native.toolchain(
name = prefix + "_to_" + target_os + "_" + target_cpu,
exec_compatible_with = [
"@platforms//cpu:" + exec_cpu,
"@platforms//os:" + exec_os,
],
target_compatible_with = [
"@platforms//cpu:" + target_cpu,
"@platforms//os:" + target_os,
],
target_settings = [
"//config:toolchains_on",
"@llvm//toolchain:bootstrap_stage0_prebuilt_seed",
"@llvm//platforms/config:windows_" + target_cpu + "_msvc",
],
toolchain = ":" + prefix + "_cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
1 change: 0 additions & 1 deletion patches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ The Version column is generated from `MODULE.bazel`, and every patch it applies
| `llvm` | 0.8.26 | `stage0_runtime_features.patch` | Stage0 runtime objects reset the same features as the bootstrap transition, so they share one configuration: `//cli/process/...` configures about 71k targets on Linux instead of 84k (upstream: hermeticbuild/hermetic-llvm#925; drop when an llvm release has it). Consumer fix: #11416 |
| `llvm` | 0.8.26 | `aarch64_outline_atomics.patch` | The aarch64 builtins skip the `__aarch64_*` outline atomic helpers on macOS and Windows, where clang never calls them (upstream: hermeticbuild/hermetic-llvm#923; drop when an llvm release has it). Consumer fix: #11416 |
| `llvm` | 0.8.26 | `macos_sdk_resolve_symlinks.patch` | The macOS SDK repo copies file symlinks, drops dangling ones, and flattens each `.framework` to its `Versions/Current` contents when fetched, so the sysroot has no symlinks and each header has one path; each framework keeps its `.tbd` at `Versions/<version>/` so ld64.lld resolves re-exports by install name. Consumer fix: #11416 |
| `llvm` | 0.8.26 | `msvc_link_lib_env.patch` | MSVC link actions set `LIB` to the hermetic Windows SDK, MSVC CRT and libc++ lib dirs instead of an empty marker, so rules_rust passes each as `-LIBPATH:` and rust-lld finds `kernel32.lib` without a system Visual Studio. Consumer fix: #11967 |
| `windows_support` | 0.4.1 | `host_tools.patch` | Opt-in `host_tools` tag keeps the MSVC compiler tools and Windows SDK `bin` (hermeticbuild/windows_support#19). Consumer fix: #11416 |
| `windows_support` | 0.4.1 | `rules_dx_hosts.patch` | `host_tools` is on by default under `rules_dx`, so `@dx_msvc` has `cl.exe`; child names match case-insensitively, since payload directory case differs from `Hostx64`. Consumer fix: #11416 |
| `windows_support` | 0.4.1 | `dx_msvc_layout.patch` | `@msvc_runtime` writes the MSVC header and library subsets that llvm's clang-cl toolchain searches and exposes them as `subdirectory` targets (upstream: hermeticbuild/windows_support#20 and PR #21; drop when a windows_support release has it). Consumer fix: #11416 |
Expand Down
1 change: 0 additions & 1 deletion patches/llvm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ exports_files([
"stage0_runtime_features.patch",
"aarch64_outline_atomics.patch",
"macos_sdk_resolve_symlinks.patch",
"msvc_link_lib_env.patch",
"toolchains_gate.patch",
])

Expand Down
Loading
Loading