Automated rollback of commit f7946d0107dd75b2f45bcc79b91c016d075a756d.

*** Reason for rollback ***

17.k breakages because of too many combinations
[]

*** Original change description ***

Propagate graph_node_aspect over a computed default attribute

Set computed default to deps only when we need to propagate graph_node aspect.

This change makes it possible to collapse cc_binary and cc_test into a single rule.

PiperOrigin-RevId: 574422599
Change-Id: I5245d28fd6778591410d8027a6edfe742c764426
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
index e5168d4..ac47fdc 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
@@ -14,12 +14,11 @@
 
 """cc_binary Starlark implementation replacing native"""
 
-load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs")
-load(":common/cc/cc_common.bzl", "cc_common")
+load(":common/cc/semantics.bzl", "semantics")
+load(":common/cc/cc_shared_library.bzl", "GraphNodeInfo", "add_unused_dynamic_deps", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "sort_linker_inputs", "throw_linked_but_not_exported_errors")
 load(":common/cc/cc_helper.bzl", "cc_helper", "linker_mode")
 load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/cc/cc_shared_library.bzl", "GraphNodeInfo", "add_unused_dynamic_deps", "build_exports_map_from_only_dynamic_deps", "build_link_once_static_libs_map", "merge_cc_shared_library_infos", "separate_static_and_dynamic_link_libraries", "sort_linker_inputs", "throw_linked_but_not_exported_errors")
-load(":common/cc/semantics.bzl", "semantics")
+load(":common/cc/cc_common.bzl", "cc_common")
 
 DebugPackageInfo = _builtins.toplevel.DebugPackageInfo
 cc_internal = _builtins.internal.cc_internal
@@ -341,7 +340,7 @@
     static_linker_inputs = []
     linker_inputs = cc_linking_context.linker_inputs.to_list()
 
-    all_deps = ctx.attr._deps_analyzed_by_graph_structure_aspect
+    all_deps = ctx.attr.deps + semantics.get_cc_runtimes(ctx, _is_link_shared(ctx))
     graph_structure_aspect_nodes = [dep[GraphNodeInfo] for dep in all_deps if GraphNodeInfo in dep]
 
     can_be_linked_dynamically = {}
@@ -911,18 +910,20 @@
 
     return providers
 
-cc_binary = rule(
-    implementation = _impl,
-    attrs = cc_binary_attrs,
-    outputs = {
-        "stripped_binary": "%{name}.stripped",
-        "dwp_file": "%{name}.dwp",
-    },
-    fragments = ["cpp"] + semantics.additional_fragments(),
-    exec_groups = {
-        "cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
-    },
-    toolchains = cc_helper.use_cpp_toolchain() +
-                 semantics.get_runtimes_toolchain(),
-    executable = True,
-)
+def make_cc_binary(cc_binary_attrs, **kwargs):
+    return rule(
+        implementation = _impl,
+        attrs = cc_binary_attrs,
+        outputs = {
+            "stripped_binary": "%{name}.stripped",
+            "dwp_file": "%{name}.dwp",
+        },
+        fragments = ["cpp"] + semantics.additional_fragments(),
+        exec_groups = {
+            "cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
+        },
+        toolchains = cc_helper.use_cpp_toolchain() +
+                     semantics.get_runtimes_toolchain(),
+        executable = True,
+        **kwargs
+    )
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl
index e2eb792..5b1f6d1 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary_attrs.bzl
@@ -15,13 +15,13 @@
 """Attributes for cc_binary.
 """
 
-load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/cc/cc_shared_library.bzl", "dynamic_deps_attrs")
 load(":common/cc/semantics.bzl", "semantics")
+load(":common/cc/cc_shared_library.bzl", "CcSharedLibraryInfo", "graph_structure_aspect")
+load(":common/cc/cc_info.bzl", "CcInfo")
 
 cc_internal = _builtins.internal.cc_internal
 
-cc_binary_attrs = {
+cc_binary_attrs_with_aspects = {
     "srcs": attr.label_list(
         flags = ["DIRECT_COMPILE_TIME_INPUT"],
         allow_files = True,
@@ -54,19 +54,26 @@
         allow_rules = semantics.ALLOWED_RULES_IN_DEPS + semantics.ALLOWED_RULES_WITH_WARNINGS_IN_DEPS,
         flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
         providers = [CcInfo],
+        aspects = [graph_structure_aspect],
+    ),
+    "dynamic_deps": attr.label_list(
+        allow_files = False,
+        providers = [CcSharedLibraryInfo],
     ),
     "malloc": attr.label(
         default = Label("@" + semantics.get_repo() + "//tools/cpp:malloc"),
         allow_files = False,
         providers = [CcInfo],
-        allow_rules = ["cc_library"],
+        aspects = [graph_structure_aspect],
     ),
     "_default_malloc": attr.label(
         default = configuration_field(fragment = "cpp", name = "custom_malloc"),
+        aspects = [graph_structure_aspect],
     ),
     "link_extra_lib": attr.label(
         default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
         providers = [CcInfo],
+        aspects = [graph_structure_aspect],
     ),
     "stamp": attr.int(
         values = [-1, 0, 1],
@@ -91,6 +98,25 @@
     "_use_auto_exec_groups": attr.bool(default = True),
 }
 
-cc_binary_attrs.update(dynamic_deps_attrs)
-cc_binary_attrs.update(semantics.get_distribs_attr())
-cc_binary_attrs.update(semantics.get_loose_mode_in_hdrs_check_allowed_attr())
+cc_binary_attrs_with_aspects.update(semantics.get_distribs_attr())
+cc_binary_attrs_with_aspects.update(semantics.get_loose_mode_in_hdrs_check_allowed_attr())
+
+# Update attributes to contain no aspect implementation.
+cc_binary_attrs_without_aspects = dict(cc_binary_attrs_with_aspects)
+cc_binary_attrs_without_aspects["deps"] = attr.label_list(
+    allow_files = semantics.ALLOWED_FILES_IN_DEPS,
+    allow_rules = semantics.ALLOWED_RULES_IN_DEPS + semantics.ALLOWED_RULES_WITH_WARNINGS_IN_DEPS,
+    flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
+    providers = [CcInfo],
+)
+cc_binary_attrs_without_aspects["malloc"] = attr.label(
+    default = Label("@" + semantics.get_repo() + "//tools/cpp:malloc"),
+    allow_files = False,
+    allow_rules = ["cc_library"],
+)
+cc_binary_attrs_without_aspects["_default_malloc"] = attr.label(
+    default = configuration_field(fragment = "cpp", name = "custom_malloc"),
+)
+cc_binary_attrs_without_aspects["link_extra_lib"] = attr.label(
+    default = Label("@" + semantics.get_repo() + "//tools/cpp:link_extra_lib"),
+)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary_with_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary_with_aspects.bzl
new file mode 100644
index 0000000..bb172f7
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary_with_aspects.bzl
@@ -0,0 +1,24 @@
+# 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.
+
+"""Exports cc_binary variant with aspects.
+
+If dynamic_deps attribute is specified we need to propagate
+aspects.
+"""
+
+load(":common/cc/cc_binary.bzl", "make_cc_binary")
+load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs_with_aspects")
+
+cc_binary = make_cc_binary(cc_binary_attrs_with_aspects)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary_without_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary_without_aspects.bzl
new file mode 100644
index 0000000..5470e32
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary_without_aspects.bzl
@@ -0,0 +1,24 @@
+# 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.
+
+"""Exports cc_binary variant without aspects.
+
+If dynamic_deps attribute is not specified we do not propagate
+aspects.
+"""
+
+load(":common/cc/cc_binary.bzl", "make_cc_binary")
+load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs_without_aspects")
+
+cc_binary = make_cc_binary(cc_binary_attrs_without_aspects)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary_wrapper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary_wrapper.bzl
new file mode 100644
index 0000000..35c1005
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary_wrapper.bzl
@@ -0,0 +1,30 @@
+# 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.
+
+"""Macro encapsulating cc_binary rule implementation.
+
+This is to avoid propagating aspect on certain attributes in case
+dynamic_deps attribute is not specified.
+"""
+
+load(":common/cc/cc_binary_with_aspects.bzl", cc_binary_with_aspects = "cc_binary")
+load(":common/cc/cc_binary_without_aspects.bzl", cc_binary_without_aspects = "cc_binary")
+load(":common/cc/cc_helper.bzl", "cc_helper")
+
+def cc_binary(**kwargs):
+    # Propagate an aspect if dynamic_deps attribute is specified.
+    if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
+        cc_binary_with_aspects(**kwargs)
+    else:
+        cc_binary_without_aspects(**kwargs)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
index c838a76..405c33b 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
@@ -355,6 +355,14 @@
     )
     return def_file
 
+def _is_non_empty_list_or_select(value, attr):
+    if type(value) == "list":
+        return len(value) > 0
+    elif type(value) == "select":
+        return True
+    else:
+        fail("Only select or list is valid for {} attr".format(attr))
+
 CC_SOURCE = [".cc", ".cpp", ".cxx", ".c++", ".C", ".cu", ".cl"]
 C_SOURCE = [".c"]
 OBJC_SOURCE = [".m"]
@@ -1233,6 +1241,7 @@
     get_copts = _get_copts,
     get_expanded_env = _get_expanded_env,
     has_target_constraints = _has_target_constraints,
+    is_non_empty_list_or_select = _is_non_empty_list_or_select,
     expand_make_variables_for_copts = _expand_make_variables_for_copts,
     build_linking_context_from_libraries = _build_linking_context_from_libraries,
     is_stamping_enabled = _is_stamping_enabled,
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
index 65e1e00..3661767 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
@@ -840,31 +840,6 @@
     fragments = ["cpp"] + semantics.additional_fragments(),
 )
 
-def _deps_analyzed_by_graph_structure_aspect(dynamic_deps, linkshared, deps, malloc, link_extra_lib):
-    if not dynamic_deps:
-        return []
-
-    # Propagate an aspect if dynamic_deps attribute is specified.
-    all_deps = []
-    all_deps.extend(deps)
-
-    if not linkshared:
-        all_deps.append(link_extra_lib)
-        all_deps.append(malloc)
-    return all_deps
-
-dynamic_deps_attrs = {
-    "dynamic_deps": attr.label_list(
-        allow_files = False,
-        providers = [CcSharedLibraryInfo],
-    ),
-    "_deps_analyzed_by_graph_structure_aspect": attr.label_list(
-        providers = [CcInfo],
-        aspects = [graph_structure_aspect],
-        default = _deps_analyzed_by_graph_structure_aspect,
-    ),
-}
-
 for_testing_dont_use_check_if_target_under_path = _check_if_target_under_path
 merge_cc_shared_library_infos = _merge_cc_shared_library_infos
 build_link_once_static_libs_map = _build_link_once_static_libs_map
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
index 4c3b413..22c6fdaf 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
@@ -15,7 +15,7 @@
 """cc_test Starlark implementation."""
 
 load(":common/cc/cc_binary.bzl", "cc_binary_impl")
-load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs")
+load(":common/cc/cc_binary_attrs.bzl", "cc_binary_attrs_with_aspects", "cc_binary_attrs_without_aspects")
 load(":common/cc/cc_helper.bzl", "cc_helper")
 load(":common/cc/semantics.bzl", "semantics")
 load(":common/paths.bzl", "paths")
@@ -77,43 +77,58 @@
     providers.extend(test_providers)
     return providers
 
-_cc_test_attrs = dict(cc_binary_attrs)
+def make_cc_test(with_aspects = False):
+    """Makes one of the cc_test rule variants.
 
-# Update cc_test defaults:
-_cc_test_attrs.update(
-    _is_test = attr.bool(default = True),
-    _apple_constraints = attr.label_list(
-        default = [
-            "@" + paths.join(semantics.get_platforms_root(), "os:ios"),
-            "@" + paths.join(semantics.get_platforms_root(), "os:macos"),
-            "@" + paths.join(semantics.get_platforms_root(), "os:tvos"),
-            "@" + paths.join(semantics.get_platforms_root(), "os:watchos"),
-        ],
-    ),
-    # Starlark tests don't get `env_inherit` by default.
-    env_inherit = attr.string_list(),
-    stamp = attr.int(values = [-1, 0, 1], default = 0),
-    linkstatic = attr.bool(default = False),
-)
-_cc_test_attrs.update(semantics.get_test_malloc_attr())
-_cc_test_attrs.update(semantics.get_coverage_attrs())
+    This function shall only be used internally in CC ruleset.
 
-cc_test = rule(
-    implementation = _impl,
-    attrs = _cc_test_attrs,
-    outputs = {
-        # TODO(b/198254254): Handle case for windows.
-        "stripped_binary": "%{name}.stripped",
-        "dwp_file": "%{name}.dwp",
-    },
-    fragments = ["cpp", "coverage"] + semantics.additional_fragments(),
-    exec_groups = {
-        "cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
-        # testing.ExecutionInfo defaults to an exec_group of "test".
-        "test": exec_group(toolchains = [config_common.toolchain_type(_CC_TEST_TOOLCHAIN_TYPE, mandatory = False)]),
-    },
-    toolchains = [] +
-                 cc_helper.use_cpp_toolchain() +
-                 semantics.get_runtimes_toolchain(),
-    test = True,
-)
+    Args:
+      with_aspects: Attaches graph_structure_aspect to `deps` attribute and
+        implicit deps.
+    Returns:
+      A cc_test rule class.
+    """
+    _cc_test_attrs = None
+    if with_aspects:
+        _cc_test_attrs = dict(cc_binary_attrs_with_aspects)
+    else:
+        _cc_test_attrs = dict(cc_binary_attrs_without_aspects)
+
+    # Update cc_test defaults:
+    _cc_test_attrs.update(
+        _is_test = attr.bool(default = True),
+        _apple_constraints = attr.label_list(
+            default = [
+                "@" + paths.join(semantics.get_platforms_root(), "os:ios"),
+                "@" + paths.join(semantics.get_platforms_root(), "os:macos"),
+                "@" + paths.join(semantics.get_platforms_root(), "os:tvos"),
+                "@" + paths.join(semantics.get_platforms_root(), "os:watchos"),
+            ],
+        ),
+        # Starlark tests don't get `env_inherit` by default.
+        env_inherit = attr.string_list(),
+        stamp = attr.int(values = [-1, 0, 1], default = 0),
+        linkstatic = attr.bool(default = False),
+    )
+    _cc_test_attrs.update(semantics.get_test_malloc_attr())
+    _cc_test_attrs.update(semantics.get_coverage_attrs())
+
+    return rule(
+        implementation = _impl,
+        attrs = _cc_test_attrs,
+        outputs = {
+            # TODO(b/198254254): Handle case for windows.
+            "stripped_binary": "%{name}.stripped",
+            "dwp_file": "%{name}.dwp",
+        },
+        fragments = ["cpp", "coverage"] + semantics.additional_fragments(),
+        exec_groups = {
+            "cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
+            # testing.ExecutionInfo defaults to an exec_group of "test".
+            "test": exec_group(toolchains = [config_common.toolchain_type(_CC_TEST_TOOLCHAIN_TYPE, mandatory = False)]),
+        },
+        toolchains = [] +
+                     cc_helper.use_cpp_toolchain() +
+                     semantics.get_runtimes_toolchain(),
+        test = True,
+    )
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_with_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_aspects.bzl
new file mode 100644
index 0000000..074519b
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_with_aspects.bzl
@@ -0,0 +1,19 @@
+# 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.
+
+"""cc_test Starlark implementation."""
+
+load(":common/cc/cc_test.bzl", "make_cc_test")
+
+cc_test = make_cc_test(with_aspects = True)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_without_aspects.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_without_aspects.bzl
new file mode 100644
index 0000000..c63ddd3
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_without_aspects.bzl
@@ -0,0 +1,19 @@
+# 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.
+
+"""cc_test Starlark implementation."""
+
+load(":common/cc/cc_test.bzl", "make_cc_test")
+
+cc_test = make_cc_test(with_aspects = False)
diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl
index 3dda190..d8f35bb 100644
--- a/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_test_wrapper.bzl
@@ -14,13 +14,17 @@
 
 """cc_test Starlark implementation."""
 
-load(":common/cc/cc_test.bzl", _cc_test = "cc_test")
+load(":common/cc/cc_helper.bzl", "cc_helper")
+load(":common/cc/cc_test_with_aspects.bzl", _cc_test_with_aspects = "cc_test")
+load(":common/cc/cc_test_without_aspects.bzl", _cc_test_without_aspects = "cc_test")
 load(":common/cc/semantics.bzl", "semantics")
 
 def cc_test(**kwargs):
     """Entry point for cc_test rules.
 
-    It  serves to detect if the `linkstatic` attribute was explicitly set or not.
+    This avoids propagating aspects on certain attributes if dynamic_deps attribute is unset.
+
+    It also serves to detect if the `linkstatic` attribute was explicitly set or not.
     This is to workaround a deficiency in Starlark attributes.
     (See: https://github.com/bazelbuild/bazel/issues/14434)
 
@@ -31,4 +35,8 @@
     if "linkstatic" not in kwargs:
         kwargs["linkstatic"] = semantics.get_linkstatic_default_for_test()
 
-    _cc_test(**kwargs)
+    # Propagate an aspect if dynamic_deps attribute is specified.
+    if "dynamic_deps" in kwargs and cc_helper.is_non_empty_list_or_select(kwargs["dynamic_deps"], "dynamic_deps"):
+        _cc_test_with_aspects(**kwargs)
+    else:
+        _cc_test_without_aspects(**kwargs)
diff --git a/src/main/starlark/builtins_bzl/common/exports.bzl b/src/main/starlark/builtins_bzl/common/exports.bzl
index 4b6fa03..3244105 100755
--- a/src/main/starlark/builtins_bzl/common/exports.bzl
+++ b/src/main/starlark/builtins_bzl/common/exports.bzl
@@ -14,7 +14,7 @@
 
 """Exported builtins symbols that are not specific to OSS Bazel."""
 
-load("@_builtins//:common/cc/cc_binary.bzl", "cc_binary")
+load("@_builtins//:common/cc/cc_binary_wrapper.bzl", "cc_binary")
 load("@_builtins//:common/cc/cc_common.bzl", "cc_common")
 load("@_builtins//:common/cc/cc_compilation_helper.bzl", "cc_compilation_helper")
 load("@_builtins//:common/cc/cc_helper.bzl", "cc_helper")
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
index d1f3826..9d43b4e 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
@@ -224,6 +224,7 @@
 
   @Test
   public void testMacroHasGeneratorAttributes() throws Exception {
+    setBuildLanguageOptions("--experimental_builtins_injection_override=+cc_binary");
     scratch.file(
         "test/starlark/extension.bzl",
         "def _impl(ctx):",
@@ -242,7 +243,9 @@
         "  native_macro_rule = 'native_macro')",
         "macro_rule(name = 'macro_target')",
         "no_macro_rule(name = 'no_macro_target')",
-        "native_macro_rule(name = 'native_macro_target')");
+        "native_macro_rule(name = 'native_macro_target')",
+        "cc_binary(name = 'cc_target', deps = ['cc_dep'])",
+        "cc_library(name = 'cc_dep')");
 
     Rule withMacro = getRuleForTarget("macro_target");
     assertThat(withMacro.getAttr("generator_name")).isEqualTo("macro_target");
@@ -259,6 +262,12 @@
     assertThat(nativeMacro.getAttr("generator_name")).isEqualTo("native_macro_target");
     assertThat(nativeMacro.getAttr("generator_function")).isEqualTo("native_macro");
     assertThat(nativeMacro.getAttr("generator_location")).isEqualTo("test/starlark/BUILD:5:18");
+
+    // Starlark version of cc_binary is created by a wrapper macro.
+    Rule ccTarget = getRuleForTarget("cc_target");
+    assertThat(ccTarget.getAttr("generator_name")).isEqualTo("cc_target");
+    assertThat(ccTarget.getAttr("generator_function")).isEqualTo("cc_binary");
+    assertThat(ccTarget.getAttr("generator_location")).isEqualTo("test/starlark/BUILD:6:10");
   }
 
   @Test