Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions news/4426.fixed.md
Original file line number Diff line number Diff line change
@@ -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)).
1 change: 1 addition & 0 deletions python/private/cc/py_extension_macro.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
9 changes: 9 additions & 0 deletions python/private/cc/py_extension_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +132 to +134

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_limited_api_versioned_only and test_no_limited_api_abi3_only, using new single-library fake Windows toolchains in tests/support/cc_toolchains. Each checks that the sole library is kept.

Assisted by Claude.

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.
Expand Down
12 changes: 12 additions & 0 deletions tests/cc/py_extension/win_libs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
111 changes: 111 additions & 0 deletions tests/cc/py_extension/win_libs/win_libs_tests.bzl
Original file line number Diff line number Diff line change
@@ -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,
],
)
82 changes: 82 additions & 0 deletions tests/support/cc_toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"],
Expand Down
Loading