Skip to content

Commit 6d871ce

Browse files
authored
Merge branch 'main' into fix_dependency_resolver_on_windows
2 parents 727379a + d5a595c commit 6d871ce

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Other changes:
9797
hardcoded in Bazel, WORKSPACE mode.
9898
* (pypi): {bzl:obj}`compile_pip_requirements` no longer fails on Windows when `--enable_runfiles` is not enabled.
9999
* (pypi): {bzl:obj}`compile_pip_requirements` now correctly updates files in the source tree on Windows when `--windows_enable_symlinks` is not enabled.
100+
* (repositories): Add libs/python3.lib and pythonXY.dll to the `libpython` target
101+
defined by a repository template. This enables stable ABI builds of Python extensions
102+
on Windows (by defining Py_LIMITED_API).
100103

101104
{#v0-0-0-added}
102105
### Added

python/private/hermetic_runtime_repo_setup.bzl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ def define_hermetic_runtime_toolchain_impl(
8989
}),
9090
system_provided = True,
9191
)
92+
cc_import(
93+
name = "abi3_interface",
94+
interface_library = select({
95+
_IS_FREETHREADED: "libs/python3t.lib",
96+
"//conditions:default": "libs/python3.lib",
97+
}),
98+
system_provided = True,
99+
)
92100

93101
native.filegroup(
94102
name = "includes",
@@ -97,7 +105,7 @@ def define_hermetic_runtime_toolchain_impl(
97105
cc_library(
98106
name = "python_headers",
99107
deps = select({
100-
"@bazel_tools//src/conditions:windows": [":interface"],
108+
"@bazel_tools//src/conditions:windows": [":interface", ":abi3_interface"],
101109
"//conditions:default": None,
102110
}),
103111
hdrs = [":includes"],
@@ -156,15 +164,22 @@ def define_hermetic_runtime_toolchain_impl(
156164
"lib/libpython{major}.{minor}t.dylib".format(**version_dict),
157165
],
158166
":is_freethreaded_windows": [
159-
"python3.dll",
167+
"python3t.dll",
168+
"python{major}{minor}t.dll".format(**version_dict),
160169
"libs/python{major}{minor}t.lib".format(**version_dict),
170+
"libs/python3t.lib",
161171
],
162172
"@platforms//os:linux": [
163173
"lib/libpython{major}.{minor}.so".format(**version_dict),
164174
"lib/libpython{major}.{minor}.so.1.0".format(**version_dict),
165175
],
166176
"@platforms//os:macos": ["lib/libpython{major}.{minor}.dylib".format(**version_dict)],
167-
"@platforms//os:windows": ["python3.dll", "libs/python{major}{minor}.lib".format(**version_dict)],
177+
"@platforms//os:windows": [
178+
"python3.dll",
179+
"python{major}{minor}.dll".format(**version_dict),
180+
"libs/python{major}{minor}.lib".format(**version_dict),
181+
"libs/python3.lib",
182+
],
168183
}),
169184
)
170185

python/private/pypi/pkg_aliases.bzl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,31 @@ load(":whl_target_platforms.bzl", "whl_target_platforms")
3636
# it. It is more of an internal consistency check.
3737
_VERSION_NONE = (0, 0)
3838

39+
_CONFIG_SETTINGS_PKG = str(Label("//python/config_settings:BUILD.bazel")).partition(":")[0]
40+
3941
_NO_MATCH_ERROR_TEMPLATE = """\
4042
No matching wheel for current configuration's Python version.
4143
4244
The current build configuration's Python version doesn't match any of the Python
43-
wheels available for this wheel. This wheel supports the following Python
45+
wheels available for this distribution. This distribution supports the following Python
4446
configuration settings:
4547
{config_settings}
4648
4749
To determine the current configuration's Python version, run:
4850
`bazel config <config id>` (shown further below)
49-
and look for
50-
{rules_python}//python/config_settings:python_version
5151
52-
If the value is missing, then the "default" Python version is being used,
53-
which has a "null" version value and will not match version constraints.
54-
"""
52+
and look for one of:
53+
{settings_pkg}:python_version
54+
{settings_pkg}:pip_whl
55+
{settings_pkg}:pip_whl_glibc_version
56+
{settings_pkg}:pip_whl_muslc_version
57+
{settings_pkg}:pip_whl_osx_arch
58+
{settings_pkg}:pip_whl_osx_version
59+
{settings_pkg}:py_freethreaded
60+
{settings_pkg}:py_linux_libc
61+
62+
If the value is missing, then the default value is being used, see documentation:
63+
{docs_url}/python/config_settings"""
5564

5665
def _no_match_error(actual):
5766
if type(actual) != type({}):
@@ -68,7 +77,8 @@ def _no_match_error(actual):
6877
for value in (key if type(key) == "tuple" else [key])
6978
])),
7079
).lstrip(),
71-
rules_python = "rules_python",
80+
settings_pkg = _CONFIG_SETTINGS_PKG,
81+
docs_url = "https://rules-python.readthedocs.io/en/latest/api/rules_python",
7282
)
7383

7484
def pkg_aliases(

tests/cc/current_py_cc_libs/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,21 @@ cc_test(
3333
"@rules_python//python/cc:current_py_cc_libs",
3434
],
3535
)
36+
37+
# This is technically a headers test, but since the pyconfig.h header
38+
# designates the appropriate lib to link on Win+MSVC, this test verifies that
39+
# the expected Windows libraries are all present in the expected location.
40+
# Since we define the Py_LIMITED_API macro, we expect the linker to go search
41+
# for libs/python3.lib.
42+
# buildifier: disable=native-cc
43+
cc_test(
44+
name = "python_libs_linking_windows_test",
45+
srcs = ["python_libs_linking_test.cc"],
46+
defines = ["Py_LIMITED_API=0x030A0000"],
47+
env = {"HELLO": "world"},
48+
target_compatible_with = ["@platforms//os:windows"],
49+
deps = [
50+
"@rules_python//python/cc:current_py_cc_headers",
51+
"@rules_python//python/cc:current_py_cc_libs",
52+
],
53+
)

tests/cc/current_py_cc_libs/python_libs_linking_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main(int argc, char** argv) {
1212
// To make it actually run, more custom initialization is necessary.
1313
// See https://docs.python.org/3/c-api/intro.html#embedding-python
1414
Py_Initialize();
15-
PyRun_SimpleString("print('Hello, world')\n");
15+
Py_BytesMain(argc, argv);
1616
Py_Finalize();
1717
return 0;
1818
}

0 commit comments

Comments
 (0)