diff --git a/news/4426.fixed.md b/news/4426.fixed.md new file mode 100644 index 0000000000..9a8fc92976 --- /dev/null +++ b/news/4426.fixed.md @@ -0,0 +1,4 @@ +(cc) {obj}`py_extension` on Windows now links one CPython import library: the +stable ABI one (`python3.lib`) for limited API modules, else the versioned one. +Before, it passed both to the linker as a single argument +([#4426](https://github.com/bazel-contrib/rules_python/issues/4426)). diff --git a/python/private/cc/py_extension_macro.bzl b/python/private/cc/py_extension_macro.bzl index b76685113a..b9ab6960a0 100644 --- a/python/private/cc/py_extension_macro.bzl +++ b/python/private/cc/py_extension_macro.bzl @@ -154,6 +154,7 @@ def py_extension( # from CcInfo in py_cc_toolchain because system_provided=True in cc_import leaves DefaultInfo empty. py_extension_libs( name = win_libs_name, + py_limited_api = py_limited_api, tags = ["manual"], visibility = ["//visibility:private"], ) diff --git a/python/private/cc/py_extension_rule.bzl b/python/private/cc/py_extension_rule.bzl index e167969d5b..0669aba6ea 100644 --- a/python/private/cc/py_extension_rule.bzl +++ b/python/private/cc/py_extension_rule.bzl @@ -124,10 +124,19 @@ def _py_extension_libs_impl(ctx): elif lib.dynamic_library: files.append(lib.dynamic_library) link_files = [f for f in files if not f.path.endswith(".dll")] + + # link.exe and lld-link get all the files as one argument, so keep one: + # the stable ABI library for limited API modules, else the versioned one. + abi3_files = [f for f in link_files if f.basename in ("python3.lib", "python3t.lib")] + if ctx.attr.py_limited_api: + link_files = abi3_files or link_files + else: + link_files = [f for f in link_files if f not in abi3_files] or link_files return [DefaultInfo(files = depset(link_files))] py_extension_libs = rule( implementation = _py_extension_libs_impl, + attrs = {"py_limited_api": attr.string()}, toolchains = [PY_CC_TOOLCHAIN_TYPE], doc = """\ Private internal helper rule for extracting Windows C/C++ library files from toolchain. diff --git a/tests/cc/py_extension/win_libs/BUILD.bazel b/tests/cc/py_extension/win_libs/BUILD.bazel new file mode 100644 index 0000000000..709086132b --- /dev/null +++ b/tests/cc/py_extension/win_libs/BUILD.bazel @@ -0,0 +1,12 @@ +load(":win_libs_tests.bzl", "win_libs_test_suite") + +package( + default_testonly = True, + default_visibility = ["//visibility:private"], +) + +licenses(["notice"]) + +win_libs_test_suite( + name = "win_libs_tests", +) diff --git a/tests/cc/py_extension/win_libs/win_libs_tests.bzl b/tests/cc/py_extension/win_libs/win_libs_tests.bzl new file mode 100644 index 0000000000..e848b20ec1 --- /dev/null +++ b/tests/cc/py_extension/win_libs/win_libs_tests.bzl @@ -0,0 +1,111 @@ +# 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. + +"""Tests for the CPython import library that py_extension links on Windows.""" + +load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") +load("//python/cc:py_extension.bzl", "py_extension") +load("//tests/support:support.bzl", "CC_TOOLCHAIN") + +# The Windows py_cc_toolchain goes last, since later extra_toolchains win: +# the one in CC_TOOLCHAIN has no target constraints. +def _config_settings(py_cc_toolchain): + return { + "//command_line_option:extra_toolchains": [ + CC_TOOLCHAIN, + str(Label("//tests/support/cc_toolchains:" + py_cc_toolchain)), + ], + "//command_line_option:platforms": [str(Label("//tests/support/platforms:windows_x86_64"))], + } + +_CONFIG_SETTINGS = _config_settings("fake_py_cc_toolchain_windows") + +def _test_limited_api(name): + py_extension( + name = name + "_pyext", + py_limited_api = "3.8", + tags = ["manual"], + ) + analysis_test( + name = name, + target = "_" + name + "_pyext_win_libs", + impl = _test_limited_api_impl, + config_settings = _CONFIG_SETTINGS, + ) + +def _test_limited_api_impl(env, target): + env.expect.that_collection( + [f.basename for f in target[DefaultInfo].files.to_list()], + ).contains_exactly(["python3.lib"]) + +def _test_no_limited_api(name): + py_extension( + name = name + "_pyext", + tags = ["manual"], + ) + analysis_test( + name = name, + target = "_" + name + "_pyext_win_libs", + impl = _test_no_limited_api_impl, + config_settings = _CONFIG_SETTINGS, + ) + +def _test_no_limited_api_impl(env, target): + env.expect.that_collection( + [f.basename for f in target[DefaultInfo].files.to_list()], + ).contains_exactly(["python312.lib"]) + +def _test_limited_api_versioned_only(name): + py_extension( + name = name + "_pyext", + py_limited_api = "3.8", + tags = ["manual"], + ) + analysis_test( + name = name, + target = "_" + name + "_pyext_win_libs", + impl = _test_limited_api_versioned_only_impl, + config_settings = _config_settings("fake_py_cc_toolchain_windows_versioned_only"), + ) + +def _test_limited_api_versioned_only_impl(env, target): + env.expect.that_collection( + [f.basename for f in target[DefaultInfo].files.to_list()], + ).contains_exactly(["python312.lib"]) + +def _test_no_limited_api_abi3_only(name): + py_extension( + name = name + "_pyext", + tags = ["manual"], + ) + analysis_test( + name = name, + target = "_" + name + "_pyext_win_libs", + impl = _test_no_limited_api_abi3_only_impl, + config_settings = _config_settings("fake_py_cc_toolchain_windows_abi3_only"), + ) + +def _test_no_limited_api_abi3_only_impl(env, target): + env.expect.that_collection( + [f.basename for f in target[DefaultInfo].files.to_list()], + ).contains_exactly(["python3.lib"]) + +def win_libs_test_suite(name): + test_suite( + name = name, + tests = [ + _test_limited_api, + _test_no_limited_api, + _test_limited_api_versioned_only, + _test_no_limited_api_abi3_only, + ], + ) diff --git a/tests/support/cc_toolchains/BUILD.bazel b/tests/support/cc_toolchains/BUILD.bazel index a74a8016fc..81f74595a6 100644 --- a/tests/support/cc_toolchains/BUILD.bazel +++ b/tests/support/cc_toolchains/BUILD.bazel @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@rules_cc//cc:cc_import.bzl", "cc_import") load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain") load("@rules_cc//cc/toolchains:cc_toolchain_suite.bzl", "cc_toolchain_suite") @@ -85,6 +86,87 @@ cc_library( tags = PREVENT_IMPLICIT_BUILDING_TAGS, ) +toolchain( + name = "fake_py_cc_toolchain_windows", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, + target_compatible_with = ["@platforms//os:windows"], + toolchain = ":fake_py_cc_toolchain_windows_impl", + toolchain_type = "@rules_python//python/cc:toolchain_type", +) + +py_cc_toolchain( + name = "fake_py_cc_toolchain_windows_impl", + headers = ":py_headers", + headers_abi3 = ":py_headers_abi3", + libs = ":fake_libs_windows", + platform_machine = "x86_64", + python_version = "3.12", + sys_platform = "win32", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, +) + +# Windows installs ship both the versioned and the stable ABI import library. +cc_library( + name = "fake_libs_windows", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, + deps = [ + ":fake_python312_lib", + ":fake_python3_lib", + ], +) + +toolchain( + name = "fake_py_cc_toolchain_windows_versioned_only", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, + target_compatible_with = ["@platforms//os:windows"], + toolchain = ":fake_py_cc_toolchain_windows_versioned_only_impl", + toolchain_type = "@rules_python//python/cc:toolchain_type", +) + +py_cc_toolchain( + name = "fake_py_cc_toolchain_windows_versioned_only_impl", + headers = ":py_headers", + headers_abi3 = ":py_headers_abi3", + libs = ":fake_python312_lib", + platform_machine = "x86_64", + python_version = "3.12", + sys_platform = "win32", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, +) + +toolchain( + name = "fake_py_cc_toolchain_windows_abi3_only", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, + target_compatible_with = ["@platforms//os:windows"], + toolchain = ":fake_py_cc_toolchain_windows_abi3_only_impl", + toolchain_type = "@rules_python//python/cc:toolchain_type", +) + +py_cc_toolchain( + name = "fake_py_cc_toolchain_windows_abi3_only_impl", + headers = ":py_headers", + headers_abi3 = ":py_headers_abi3", + libs = ":fake_python3_lib", + platform_machine = "x86_64", + python_version = "3.12", + sys_platform = "win32", + tags = PREVENT_IMPLICIT_BUILDING_TAGS, +) + +cc_import( + name = "fake_python3_lib", + interface_library = "python3.lib", + system_provided = True, + tags = PREVENT_IMPLICIT_BUILDING_TAGS, +) + +cc_import( + name = "fake_python312_lib", + interface_library = "python312.lib", + system_provided = True, + tags = PREVENT_IMPLICIT_BUILDING_TAGS, +) + cc_toolchain_suite( name = "cc_toolchain_suite", tags = ["manual"],