From b467d9f752f923707e1fcafe7593ee503ec0f4b7 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 24 Mar 2026 15:34:27 -0700 Subject: [PATCH] Pass through target_system_name This implements another attribute on toolchain configuration for rules based toolchains. Turns out similar to compiler and cpu many places reference the `target_gnu_system_name` which comes from this attribute. I added support for makefile variable expansion on this since it's potentially derived from a rule, vs a hardcoded string. Examples: - https://github.com/bazelbuild/rules_apple/blob/eed702ea4b42dca3081a63bb6abd030878abcb6f/apple/internal/linking_support.bzl#L108 - https://github.com/GoogleCloudPlatform/google-auth-library-perl/blob/f776c0292561df92358a1f86af172651bdb3d8e5/protobuf/protobuf_release.bzl#L16 - https://github.com/JetBrains/android/blob/46bd55242aa52447b13489147ec37cfdcf7902cb/aswb/aspect/build_dependencies_cc_deps.bzl#L64 - https://github.com/protocolbuffers/protobuf-javascript/blob/291b4604a3f9624e0a6b51a1e100368d13f43822/protobuf_javascript_release.bzl#L19 - https://github.com/bazelbuild/intellij/blob/cef70399b849f5393455d30eb8c98e9a7e542624/aspect/intellij_info_impl.bzl#L459 --- cc/toolchains/impl/toolchain_config.bzl | 3 ++- cc/toolchains/toolchain.bzl | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cc/toolchains/impl/toolchain_config.bzl b/cc/toolchains/impl/toolchain_config.bzl index 1f6efefe5..1a7c916bc 100644 --- a/cc/toolchains/impl/toolchain_config.bzl +++ b/cc/toolchains/impl/toolchain_config.bzl @@ -84,8 +84,8 @@ def _cc_toolchain_config_impl(ctx): # compiler compiler = ctx.attr.compiler, target_cpu = ctx.attr.cpu, + target_system_name = ctx.expand_make_variables("target_system_name", ctx.attr.target_system_name, {}), # These fields are only relevant for legacy toolchain resolution. - target_system_name = "", target_libc = "", abi_version = "", abi_libc_version = "", @@ -105,6 +105,7 @@ cc_toolchain_config = rule( # Attributes new to this rule. "compiler": attr.string(default = ""), "cpu": attr.string(default = ""), + "target_system_name": attr.string(default = ""), "tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True), "args": attr.label_list(providers = [ArgsListInfo]), "known_features": attr.label_list(providers = [FeatureSetInfo]), diff --git a/cc/toolchains/toolchain.bzl b/cc/toolchains/toolchain.bzl index fa41163ab..b804b7e33 100644 --- a/cc/toolchains/toolchain.bzl +++ b/cc/toolchains/toolchain.bzl @@ -70,6 +70,7 @@ def cc_toolchain( supports_header_parsing = False, supports_param_files = False, compiler = "", + target_system_name = None, **kwargs): """A C/C++ toolchain configuration. @@ -158,6 +159,10 @@ def cc_toolchain( compiler: (str) The type of compiler used by this toolchain (e.g. "gcc", "clang"). The current toolchain's compiler is exposed to `@rules_cc//cc/private/toolchain:compiler (compiler_flag)` as a flag value. + target_system_name: (str) The target system name for this toolchain. This is exposed through the + toolchain info providers. This string is commonly the target triple you would pass to + `clang -target` (e.g. "x86_64-unknown-linux-gnu", "aarch64-apple-darwin"). If not + provided, a default is selected based on the target platform.. **kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) that should be applied to all rules created by this macro. """ @@ -177,6 +182,15 @@ def cc_toolchain( known_features = known_features, enabled_features = enabled_features, compiler = compiler, + target_system_name = target_system_name or select({ + Label("//cc/toolchains/impl:darwin_aarch64"): "aarch64-apple-darwin", + Label("//cc/toolchains/impl:darwin_x86_64"): "x86_64-apple-darwin", + Label("//cc/toolchains/impl:linux_aarch64"): "aarch64-unknown-linux-gnu", + Label("//cc/toolchains/impl:linux_x86_64"): "x86_64-unknown-linux-gnu", + Label("//cc/toolchains/impl:windows_x86_32"): "i686-pc-windows-msvc", + Label("//cc/toolchains/impl:windows_x86_64"): "x86_64-pc-windows-msvc", + "//conditions:default": "", + }), cpu = select({ Label("//cc/toolchains/impl:darwin_aarch64"): "darwin_arm64", Label("//cc/toolchains/impl:darwin_x86_64"): "darwin_x86_64",