fix(cc): don't drop Windows import libs from cc_binary with dynamic_deps (#4487)

On Windows, `current_py_cc_headers` forwards a `CcInfo` that includes
the Python import libraries. A `cc_binary` with `dynamic_deps` only
links libraries whose owners it can find in its dependency graph, and it
can't find these through the toolchain. The libraries get dropped and
the link fails with `LNK1104: cannot open file 'python313.lib'`.

Return `CcSharedLibraryHintInfo` with the owners, the same fix #4166
made for `current_py_cc_libs`. Apply it to `current_py_cc_headers_abi3`
too.
diff --git a/news/current-py-cc-headers-dynamic-deps.fixed.md b/news/current-py-cc-headers-dynamic-deps.fixed.md
new file mode 100644
index 0000000..7cab53d
--- /dev/null
+++ b/news/current-py-cc-headers-dynamic-deps.fixed.md
@@ -0,0 +1,3 @@
+(cc) {obj}`current_py_cc_headers` and {obj}`current_py_cc_headers_abi3` no
+longer drop the Windows Python import libraries when used by a `cc_binary` with
+`dynamic_deps`.
diff --git a/python/private/current_py_cc_headers.bzl b/python/private/current_py_cc_headers.bzl
index f7fcd8d..38bd43c 100644
--- a/python/private/current_py_cc_headers.bzl
+++ b/python/private/current_py_cc_headers.bzl
@@ -16,16 +16,30 @@
 """
 
 load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
+load("@rules_cc//cc/common:cc_shared_library_hint_info.bzl", "CcSharedLibraryHintInfo")
 load("//python/private:toolchain_types.bzl", "PY_CC_TOOLCHAIN_TYPE")
 
+def _forward_headers(ctx, headers):
+    # Report the owners of forwarded libraries (e.g. the Windows import
+    # libraries) so cc_binary with dynamic_deps doesn't drop them.
+    providers = headers.providers_map
+    owners = {
+        linker_input.owner: None
+        for linker_input in providers["CcInfo"].linking_context.linker_inputs.to_list()
+    }
+    return providers.values() + [CcSharedLibraryHintInfo(
+        attributes = [],
+        owners = owners.keys() or [ctx.label],
+    )]
+
 def _current_py_cc_headers_impl(ctx):
     py_cc_toolchain = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE].py_cc_toolchain
-    return py_cc_toolchain.headers.providers_map.values()
+    return _forward_headers(ctx, py_cc_toolchain.headers)
 
 current_py_cc_headers = rule(
     implementation = _current_py_cc_headers_impl,
     toolchains = [PY_CC_TOOLCHAIN_TYPE],
-    provides = [CcInfo],
+    provides = [CcInfo, CcSharedLibraryHintInfo],
     doc = """
 Provides the currently active Python toolchain's C headers.
 
@@ -41,6 +55,11 @@
     deps = ["@rules_python//python/cc:current_py_cc_headers"]
 )
 ```
+
+:::{versionchanged} VERSION_NEXT_PATCH
+The Windows import libraries are no longer dropped by `cc_binary` with
+`dynamic_deps`.
+:::
 """,
 )
 
@@ -54,12 +73,12 @@
         ).format(
             PY_CC_TOOLCHAIN_TYPE,
         ))
-    return py_cc_toolchain.headers_abi3.providers_map.values()
+    return _forward_headers(ctx, py_cc_toolchain.headers_abi3)
 
 current_py_cc_headers_abi3 = rule(
     implementation = _current_py_cc_headers_abi3_impl,
     toolchains = [PY_CC_TOOLCHAIN_TYPE],
-    provides = [CcInfo],
+    provides = [CcInfo, CcSharedLibraryHintInfo],
     doc = """
 Provides the currently active Python toolchain's C ABI3 headers.
 
@@ -78,5 +97,10 @@
 
 :::{versionadded} 1.7.0
 :::
+
+:::{versionchanged} VERSION_NEXT_PATCH
+The Windows import libraries are no longer dropped by `cc_binary` with
+`dynamic_deps`.
+:::
 """,
 )
diff --git a/tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl b/tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl
index f52d93e..c8c7877 100644
--- a/tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl
+++ b/tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl
@@ -14,9 +14,14 @@
 
 """Tests for current_py_cc_headers."""
 
+load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
+load("@rules_cc//cc:cc_library.bzl", "cc_library")
+load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library")
 load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
 load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
 load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", "util")
+load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
 load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
 load("//tests/support:support.bzl", "CC_TOOLCHAIN")
 
@@ -110,6 +115,84 @@
 
 _tests.append(_test_current_toolchain_headers_abi3)
 
+# On Windows the headers targets carry the Python import libraries. Check that
+# they are still linked into a cc_binary with dynamic_deps.
+def _setup_binary_with_dynamic_deps(name, headers_target):
+    util.helper_target(
+        cc_library,
+        name = name + ".python_lib",
+        srcs = ["python_lib.c"],
+    )
+    util.helper_target(
+        py_cc_toolchain,
+        name = name + ".py_cc_toolchain",
+        headers = ":" + name + ".python_lib",
+        headers_abi3 = ":" + name + ".python_lib",
+        python_version = "3.999",
+    )
+    util.helper_target(
+        native.toolchain,
+        name = name + ".toolchain",
+        toolchain = ":" + name + ".py_cc_toolchain",
+        toolchain_type = "//python/cc:toolchain_type",
+    )
+    util.helper_target(
+        cc_library,
+        name = name + ".dep",
+        srcs = ["python_lib.c"],
+    )
+    util.helper_target(
+        cc_shared_library,
+        name = name + ".dep_shared",
+        deps = [":" + name + ".dep"],
+    )
+    util.helper_target(
+        cc_binary,
+        name = name + ".bin",
+        linkshared = True,
+        dynamic_deps = [":" + name + ".dep_shared"],
+        deps = [
+            ":" + name + ".dep",
+            headers_target,
+        ],
+    )
+    analysis_test(
+        name = name,
+        impl = _test_binary_with_dynamic_deps_impl,
+        target = name + ".bin",
+        config_settings = {
+            # This transition replaces the C++ toolchain supplied by RBE.
+            "//command_line_option:extra_toolchains": [
+                str(native.package_relative_label(":" + name + ".toolchain")),
+                str(Label("//tests/support/cc_toolchains:linux_toolchain_definition")),
+                str(Label("//tests/support/cc_toolchains:mac_toolchain_definition")),
+                str(Label("//tests/support/cc_toolchains:windows_toolchain_definition")),
+            ],
+        },
+    )
+
+def _test_binary_with_dynamic_deps_impl(env, target):
+    test_name = target.label.name.removesuffix(".bin")
+    link_inputs = env.expect.that_target(target).action_named("CppLink").inputs()
+    link_inputs.contains_predicate(
+        matching.file_basename_contains(test_name + ".python_lib"),
+    )
+
+    # :dep is linked through :dep_shared, not statically.
+    link_inputs.not_contains_predicate(
+        matching.file_basename_contains(test_name + ".dep."),
+    )
+
+def _test_headers_binary_with_dynamic_deps(name):
+    _setup_binary_with_dynamic_deps(name, "//python/cc:current_py_cc_headers")
+
+_tests.append(_test_headers_binary_with_dynamic_deps)
+
+def _test_headers_abi3_binary_with_dynamic_deps(name):
+    _setup_binary_with_dynamic_deps(name, "//python/cc:current_py_cc_headers_abi3")
+
+_tests.append(_test_headers_abi3_binary_with_dynamic_deps)
+
 def current_py_cc_headers_test_suite(name):
     test_suite(
         name = name,
diff --git a/tests/cc/current_py_cc_headers/python_lib.c b/tests/cc/current_py_cc_headers/python_lib.c
new file mode 100644
index 0000000..cea2d11
--- /dev/null
+++ b/tests/cc/current_py_cc_headers/python_lib.c
@@ -0,0 +1 @@
+int python_library_symbol(void) { return 0; }