Fork `cc_toolchain_alias` rule

PiperOrigin-RevId: 806179753
Change-Id: I40d7472877b7791d6636b7c933c170b1dd1cda48
diff --git a/cc/BUILD b/cc/BUILD
index 730133a..b0a349b 100644
--- a/cc/BUILD
+++ b/cc/BUILD
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+load("//cc/toolchains:cc_toolchain_alias.bzl", "cc_toolchain_alias")
 load(":starlark_doc_extract_helper.bzl", "starlark_doc_extract_helper")
 
 package(default_visibility = ["//visibility:public"])
diff --git a/cc/extensions.bzl b/cc/extensions.bzl
index 6252622..c95b775 100644
--- a/cc/extensions.bzl
+++ b/cc/extensions.bzl
@@ -60,6 +60,7 @@
 load("@rules_cc//cc/private/rules_impl:fdo/memprof_profile.bzl", _memprof_profile = "memprof_profile")
 load("@rules_cc//cc/private/rules_impl:fdo/propeller_optimize.bzl", _propeller_optimize = "propeller_optimize")
 load("@rules_cc//cc/private/rules_impl:cc_toolchain.bzl", _cc_toolchain = "cc_toolchain")
+load("@rules_cc//cc/private/rules_impl:cc_toolchain_alias.bzl", _cc_toolchain_alias = "cc_toolchain_alias")
 
 cc_binary = _cc_binary
 cc_import = _cc_import
@@ -74,6 +75,7 @@
 memprof_profile = _memprof_profile
 propeller_optimize = _propeller_optimize
 cc_toolchain = _cc_toolchain
+cc_toolchain_alias = _cc_toolchain_alias
 
 CcSharedLibraryInfo = _CcSharedLibraryInfo
             """,
@@ -108,6 +110,7 @@
 memprof_profile = getattr(native, "memprof_profile", None) # only in Bazel 7+
 propeller_optimize = native.propeller_optimize
 cc_toolchain = native.cc_toolchain
+cc_toolchain_alias = native.cc_toolchain_alias
 
 CcSharedLibraryInfo = NativeCcSharedLibraryInfo
             """,
diff --git a/cc/private/bazel7/BUILD b/cc/private/bazel7/BUILD
index ebf88d4..494e01b 100644
--- a/cc/private/bazel7/BUILD
+++ b/cc/private/bazel7/BUILD
@@ -11,6 +11,7 @@
 # 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.
+load("//cc/toolchains:cc_toolchain_alias.bzl", "cc_toolchain_alias")
 
 package(default_visibility = ["//visibility:public"])
 
diff --git a/cc/private/rules_impl/BUILD b/cc/private/rules_impl/BUILD
index a817d68..c38189e 100644
--- a/cc/private/rules_impl/BUILD
+++ b/cc/private/rules_impl/BUILD
@@ -64,6 +64,7 @@
     name = "toolchain_rules",
     srcs = [
         "cc_toolchain.bzl",
+        "cc_toolchain_alias.bzl",
         "cc_toolchain_info.bzl",
         "cc_toolchain_provider_helper.bzl",
         "fdo/fdo_context.bzl",
diff --git a/cc/private/rules_impl/cc_toolchain_alias.bzl b/cc/private/rules_impl/cc_toolchain_alias.bzl
new file mode 100644
index 0000000..630451b
--- /dev/null
+++ b/cc/private/rules_impl/cc_toolchain_alias.bzl
@@ -0,0 +1,53 @@
+# Copyright 2022 The Bazel Authors. All rights reserved.
+#
+# 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.
+
+"""Starlark implementation of cc_toolchain_alias rule."""
+
+load("//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
+load("//cc/common:cc_common.bzl", "cc_common")
+load("//cc/common:cc_helper.bzl", "cc_helper")
+load("//cc/common:semantics.bzl", "semantics")
+
+CcToolchainInfo = cc_common.CcToolchainInfo
+TemplateVariableInfo = platform_common.TemplateVariableInfo
+ToolchainInfo = platform_common.ToolchainInfo
+
+def _impl(ctx):
+    cc_toolchain = find_cc_toolchain(ctx, mandatory = ctx.attr.mandatory)
+    if not cc_toolchain:
+        return []
+    make_variables = cc_toolchain._additional_make_variables
+    cc_provider_make_variables = cc_helper.get_toolchain_global_make_variables(cc_toolchain)
+    template_variable_info = TemplateVariableInfo(make_variables | cc_provider_make_variables)
+    toolchain = ToolchainInfo(
+        cc = cc_toolchain,
+        cc_provider_in_toolchain = True,
+    )
+    return [
+        cc_toolchain,
+        toolchain,
+        template_variable_info,
+        DefaultInfo(
+            files = cc_toolchain._all_files_including_libc,
+        ),
+    ]
+
+cc_toolchain_alias = rule(
+    implementation = _impl,
+    fragments = ["cpp", "platform"],
+    attrs = {
+        "mandatory": attr.bool(default = True),
+    },
+    toolchains = use_cc_toolchain() + semantics.get_runtimes_toolchain(),
+)
diff --git a/cc/toolchains/cc_toolchain_alias.bzl b/cc/toolchains/cc_toolchain_alias.bzl
new file mode 100644
index 0000000..b0aa1b1
--- /dev/null
+++ b/cc/toolchains/cc_toolchain_alias.bzl
@@ -0,0 +1,16 @@
+# Copyright 2024 The Bazel Authors. All rights reserved.
+#
+# 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.
+"""cc_toolchain_alias rule"""
+
+cc_toolchain_alias = native.cc_toolchain_alias
diff --git a/cc/toolchains/cc_toolchain_alias.bzl.oss b/cc/toolchains/cc_toolchain_alias.bzl.oss
new file mode 100644
index 0000000..2728213
--- /dev/null
+++ b/cc/toolchains/cc_toolchain_alias.bzl.oss
@@ -0,0 +1,18 @@
+# Copyright 2024 The Bazel Authors. All rights reserved.
+#
+# 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.
+"""cc_toolchain rule"""
+
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_toolchain_alias = "cc_toolchain_alias")
+
+cc_toolchain_alias = _cc_toolchain_alias