Revert "refactor(cc): remove user_link_flags for Windows and simplify current_py_cc_libs" This reverts commit 71df3253c88c638bfc46bcc8131a9789caf9d261.
diff --git a/python/private/cc/py_extension_macro.bzl b/python/private/cc/py_extension_macro.bzl index 9938697..7514b3f 100644 --- a/python/private/cc/py_extension_macro.bzl +++ b/python/private/cc/py_extension_macro.bzl
@@ -140,18 +140,25 @@ # - On macOS, Apple's ld64 linker requires '-undefined dynamic_lookup' so CPython # C-API symbols (e.g. PyModule_Create) remain unresolved at link time and are # dynamically resolved at runtime when CPython loads the shared library (.so). - # - On Windows, link.exe automatically receives the CPython import library (.lib) - # via CcInfo in deps. + # - On Windows, link.exe receives the CPython import library (.lib) passed via + # current_py_cc_libs. effective_user_link_flags = (user_link_flags or linkopts or []) + select({ "@platforms//os:macos": ["-undefined", "dynamic_lookup"], "@platforms//os:osx": ["-undefined", "dynamic_lookup"], + "@platforms//os:windows": ["$(locations @rules_python//python/cc:current_py_cc_libs)"], "//conditions:default": [], }) csl_kwargs["user_link_flags"] = effective_user_link_flags + csl_additional_linker_inputs = select({ + "@platforms//os:windows": ["@rules_python//python/cc:current_py_cc_libs"], + "//conditions:default": [], + }) + cc_shared_library( name = csl_name, deps = final_csl_deps, + additional_linker_inputs = csl_additional_linker_inputs, dynamic_deps = dynamic_deps, visibility = ["//visibility:private"], **csl_kwargs
diff --git a/python/private/current_py_cc_libs.bzl b/python/private/current_py_cc_libs.bzl index 485e1d2..58ab4b1 100644 --- a/python/private/current_py_cc_libs.bzl +++ b/python/private/current_py_cc_libs.bzl
@@ -18,7 +18,39 @@ def _current_py_cc_libs_impl(ctx): py_cc_toolchain = ctx.toolchains["//python/cc:toolchain_type"].py_cc_toolchain - return list(py_cc_toolchain.libs.providers_map.values()) + providers = [p for p in py_cc_toolchain.libs.providers_map.values() if not hasattr(p, "data_runfiles")] + default_runfiles = None + data_runfiles = None + files = [] + for p in py_cc_toolchain.libs.providers_map.values(): + if hasattr(p, "data_runfiles"): + default_runfiles = p.default_runfiles + data_runfiles = p.data_runfiles + + cc_infos = [p for p in py_cc_toolchain.libs.providers_map.values() if hasattr(p, "linking_context")] + if cc_infos: + cc_info = cc_infos[0] + for input in cc_info.linking_context.linker_inputs.to_list(): + for lib in input.libraries: + if lib.static_library: + files.append(lib.static_library) + if lib.interface_library: + files.append(lib.interface_library) + elif lib.dynamic_library: + files.append(lib.dynamic_library) + + # On Windows MSVC, user_link_flags passes $(locations @rules_python//python/cc:current_py_cc_libs) + # to link.exe. MSVC link.exe accepts import libraries (.lib) but fails with + # LNK1107 if passed raw DLL binaries (.dll). We filter out .dll files so + # DefaultInfo.files only contains linkable library files (.lib / .a). + link_files = [f for f in files if not f.path.endswith(".dll")] + + providers.append(DefaultInfo( + files = depset(link_files), + default_runfiles = default_runfiles, + data_runfiles = data_runfiles, + )) + return providers current_py_cc_libs = rule( implementation = _current_py_cc_libs_impl,