refactor(toolchain): rename abi_tag to soabi to eliminate PEP ambiguity (#4008) Rename `abi_tag` to `soabi` in `PyCcToolchainInfo` and associated toolchain implementation and macro parameters. I found the `abi_tag` term a bit ambiguous with PEP 425 wheel ABI tags and PEP 3149 C extension filename SOABI tags. So rename it to `soabi` to directly reflect the SOABI sysconfig var that it is traditionally derived from. This also uncovered a bug where the value was _just_ the Python ABI-ness (`cpython-311`), while it should be the full ABI infix that includes the platform (`cpython-311-x86_64-linux-gnu`). This also helped simplify some of the Windows code paths.
diff --git a/news/3973.added.md b/news/3973.added.md index ef3796a..20d1361 100644 --- a/news/3973.added.md +++ b/news/3973.added.md
@@ -1,6 +1,6 @@ (cc) Added experimental {obj}`py_extension` macro for creating C/C++ Python extension modules ([#3283](https://github.com/bazel-contrib/rules_python/issues/3283)). -(cc) Added `abi_flags`, `abi_tag`, `libc`, `platform_machine`, `platform_tag`, -and `sys_platform` attributes and info fields to {obj}`py_cc_toolchain` / +(cc) Added `libc`, `platform_machine`, `platform_tag`, `soabi`, and +`sys_platform` attributes and info fields to {obj}`py_cc_toolchain` / {obj}`PyCcToolchainInfo`.
diff --git a/python/private/cc/py_extension_rule.bzl b/python/private/cc/py_extension_rule.bzl index dc5a5e0..e167969 100644 --- a/python/private/cc/py_extension_rule.bzl +++ b/python/private/cc/py_extension_rule.bzl
@@ -26,11 +26,9 @@ else: py_toolchain = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE] py_cc_toolchain = py_toolchain.py_cc_toolchain - platform_tag = _get_platform(ctx) - output_filename = "{module_name}.{abi_tag}-{platform}.{ext}".format( + output_filename = "{module_name}.{soabi}.{ext}".format( module_name = module_name, - abi_tag = py_cc_toolchain.abi_tag, - platform = platform_tag, + soabi = py_cc_toolchain.soabi, ext = ext, ) @@ -112,26 +110,6 @@ """ return "pyd" if is_windows_platform(ctx) else "so" -def _get_platform(ctx): - """Derives the PEP 3149 platform tag from the active Python C++ toolchain. - - Args: - ctx: The rule context. - - Returns: - The platform tag, e.g. "x86_64-linux-gnu" or "win_amd64" - """ - py_toolchain = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE] - py_cc_toolchain = py_toolchain.py_cc_toolchain - if not py_cc_toolchain.platform_tag: - fail( - ("ERROR: Unable to resolve platform_tag from Python C++ toolchain for {self}. " + - "Please ensure the active py_cc_toolchain provides a non-empty platform_tag.").format( - self = ctx.label, - ), - ) - return py_cc_toolchain.platform_tag - def _py_extension_libs_impl(ctx): py_toolchain = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE] py_cc_toolchain = py_toolchain.py_cc_toolchain
diff --git a/python/private/get_local_runtime_info.py b/python/private/get_local_runtime_info.py index 340ef93..e0eab96 100644 --- a/python/private/get_local_runtime_info.py +++ b/python/private/get_local_runtime_info.py
@@ -248,7 +248,7 @@ "abi_dynamic_libraries": _unique_basenames(abi_dynamic_libraries), "abi_interface_libraries": _unique_basenames(abi_interface_libraries), "abi_flags": abi_flags, - "abi_tag": config_vars.get("SOABI") or "", + "soabi": config_vars.get("SOABI") or "", "shlib_suffix": ".dylib" if _IS_DARWIN else "", "additional_dlls": dlls, "defines": defines,
diff --git a/python/private/local_runtime_repo.bzl b/python/private/local_runtime_repo.bzl index 48fc576..f5597db 100644 --- a/python/private/local_runtime_repo.bzl +++ b/python/private/local_runtime_repo.bzl
@@ -35,7 +35,7 @@ minor = "{minor}", micro = "{micro}", abi_flags = "{abi_flags}", - abi_tag = "{abi_tag}", + soabi = "{soabi}", os = "{os}", implementation_name = "{implementation_name}", interpreter_path = "{interpreter_path}", @@ -56,7 +56,7 @@ minor = "0", micro = "0", abi_flags = "", - abi_tag = "", + soabi = "", os = "@platforms//:incompatible", implementation_name = "incompatible", interpreter_path = "/incompatible", @@ -216,7 +216,7 @@ minor = info["minor"], micro = info["micro"], abi_flags = info["abi_flags"], - abi_tag = info["abi_tag"], + soabi = info["soabi"], os = "@platforms//os:{}".format(repo_utils.get_platforms_os_name(rctx)), implementation_name = info["implementation_name"], interpreter_path = _norm_path(interpreter_path),
diff --git a/python/private/local_runtime_repo_setup.bzl b/python/private/local_runtime_repo_setup.bzl index 78c8bd0..62e46e4 100644 --- a/python/private/local_runtime_repo_setup.bzl +++ b/python/private/local_runtime_repo_setup.bzl
@@ -40,7 +40,7 @@ additional_dlls, sys_platform = "", platform_machine = "", - abi_tag = ""): + soabi = ""): """Defines a toolchain implementation for a local Python runtime. Generates public targets: @@ -76,7 +76,7 @@ e.g. ["lib/msvcrt123.dll"] sys_platform: `str` The PEP 508 `sys_platform` marker, e.g. 'linux', 'darwin', 'win32'. platform_machine: `str` The PEP 508 `platform_machine` marker, e.g. 'x86_64', 'aarch64'. - abi_tag: `str` The ABI tag for extension modules, e.g. 'cpython-311'. + soabi: `str` The SOABI tag for extension modules, e.g. 'cpython-311-x86_64-linux-gnu'. """ major_minor = "{}.{}".format(major, minor) major_minor_micro = "{}.{}".format(major_minor, micro) @@ -190,7 +190,7 @@ py_cc_toolchain( name = "py_cc_toolchain", abi_flags = abi_flags, - abi_tag = abi_tag, + soabi = soabi, headers = ":python_headers", headers_abi3 = ":python_headers_abi3", libs = ":libpython",
diff --git a/python/private/py_cc_toolchain_info.bzl b/python/private/py_cc_toolchain_info.bzl index 19dc5de..208a958 100644 --- a/python/private/py_cc_toolchain_info.bzl +++ b/python/private/py_cc_toolchain_info.bzl
@@ -25,16 +25,6 @@ :::{versionadded} VERSION_NEXT_FEATURE ::: """, - "abi_tag": """\ -:type: str - -The ABI tag for extension modules, equivalent to the `SOABI` sysconfig var -(see [PEP 3149](https://peps.python.org/pep-3149/)), e.g. 'cpython-311' or -'cpython-313t'. - -:::{versionadded} VERSION_NEXT_FEATURE -::: -""", "headers": """\ :type: struct @@ -133,6 +123,16 @@ The Python Major.Minor version. """, + "soabi": """\ +:type: str + +The SOABI tag for extension modules (see +[PEP 3149](https://peps.python.org/pep-3149/)), e.g. +'cpython-311-x86_64-linux-gnu' or 'cp311'. + +:::{versionadded} VERSION_NEXT_FEATURE +::: +""", "sys_platform": """ :type: str
diff --git a/python/private/py_cc_toolchain_rule.bzl b/python/private/py_cc_toolchain_rule.bzl index a6cb296..02d4222 100644 --- a/python/private/py_cc_toolchain_rule.bzl +++ b/python/private/py_cc_toolchain_rule.bzl
@@ -88,19 +88,6 @@ if ctx.attr._py_freethreaded_flag[BuildSettingInfo].value == FreeThreadedFlag.YES: abi_flags += "t" - abi_tag = ctx.attr.abi_tag - if not abi_tag: - # Derive default ABI tag: - # On POSIX: cpython-XX[t] (PEP 3149 / PEP 703) - # On Windows: cpXX[t] (PEP 3149 / PEP 703, CPython issue & commit): - # - https://peps.python.org/pep-3149/ - # - https://peps.python.org/pep-0703/ - # - https://github.com/python/cpython/issues/67169 - # - https://github.com/python/cpython/commit/03a144bb6ac3d7631a3bdb895e2a1f2d021fb08b - version_parts = ctx.attr.python_version.split(".") - prefix = "cp" if ctx.attr.sys_platform == "win32" else "cpython-" - abi_tag = "{}{}{}{}".format(prefix, version_parts[0], version_parts[1], abi_flags) - libc = ctx.attr.libc or LibcFlag.get_value(ctx) platform_tag = _get_platform_tag( @@ -109,9 +96,19 @@ libc = libc, ) + soabi = ctx.attr.soabi + if not soabi: + # Derive default SOABI tag according to PEP 3149: + # On POSIX: cpython-XX[t]-<platform_tag> + # On Windows: cpXX[t]-<platform_tag> (CPython issue #67169) + version_parts = ctx.attr.python_version.split(".") + prefix = "cp" if ctx.attr.sys_platform == "win32" else "cpython-" + soabi = "{}{}{}{}".format(prefix, version_parts[0], version_parts[1], abi_flags) + if platform_tag: + soabi = "{}-{}".format(soabi, platform_tag) + py_cc_toolchain = PyCcToolchainInfo( abi_flags = abi_flags, - abi_tag = abi_tag, headers = struct( providers_map = { "CcInfo": ctx.attr.headers[CcInfo], @@ -123,6 +120,7 @@ platform_machine = ctx.attr.platform_machine, platform_tag = platform_tag, python_version = ctx.attr.python_version, + soabi = soabi, sys_platform = ctx.attr.sys_platform, ) extra_kwargs = {} @@ -149,17 +147,6 @@ ::: """, ), - "abi_tag": attr.string( - doc = """\ -The ABI tag for extension modules, equivalent to the `SOABI` sysconfig var -(see [PEP 3149](https://peps.python.org/pep-3149/)), e.g. 'cpython-311' or -'cpython-313t'. - -:::{versionadded} VERSION_NEXT_FEATURE -::: -""", - default = "", - ), "headers": attr.label( doc = ("Target that provides the Python headers. Typically this " + "is a cc_library target."), @@ -207,6 +194,16 @@ doc = "The Major.minor Python version, e.g. 3.11", mandatory = True, ), + "soabi": attr.string( + doc = """\ +The SOABI tag for extension modules (see PEP 3149), e.g. +'cpython-311-x86_64-linux-gnu' or 'cp311'. + +:::{versionadded} VERSION_NEXT_FEATURE +::: +""", + default = "", + ), "sys_platform": attr.string( doc = """ Target OS as a PEP 508 `sys_platform` marker, e.g. 'linux', 'darwin', 'win32'.
diff --git a/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl b/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl index 5b57810..5b6f8e1 100644 --- a/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl +++ b/tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl
@@ -119,10 +119,11 @@ matching.str_matches("/libpython3."), ) - # ===== Verify PEP 508 platform markers ===== + # ===== Verify PEP 508 platform markers & SOABI ===== toolchain.sys_platform().equals("linux") toolchain.platform_machine().equals("x86_64") toolchain.platform_tag().equals("x86_64-linux-gnu") + toolchain.soabi().equals("cpython-3999-x86_64-linux-gnu") _tests.append(_test_py_cc_toolchain) @@ -148,6 +149,7 @@ toolchain.sys_platform().equals("darwin") toolchain.platform_machine().equals("arm64") toolchain.platform_tag().equals("darwin") + toolchain.soabi().equals("cpython-311-darwin") _tests.append(_test_custom_pep508_markers)
diff --git a/tests/support/py_cc_toolchain_info_subject.bzl b/tests/support/py_cc_toolchain_info_subject.bzl index fbf5a1a..d6aa258 100644 --- a/tests/support/py_cc_toolchain_info_subject.bzl +++ b/tests/support/py_cc_toolchain_info_subject.bzl
@@ -25,6 +25,7 @@ platform_machine = lambda *a, **k: _py_cc_toolchain_info_subject_platform_machine(self, *a, **k), platform_tag = lambda *a, **k: _py_cc_toolchain_info_subject_platform_tag(self, *a, **k), python_version = lambda *a, **k: _py_cc_toolchain_info_subject_python_version(self, *a, **k), + soabi = lambda *a, **k: _py_cc_toolchain_info_subject_soabi(self, *a, **k), sys_platform = lambda *a, **k: _py_cc_toolchain_info_subject_sys_platform(self, *a, **k), actual = info, ) @@ -82,6 +83,12 @@ meta = self.meta.derive("python_version()"), ) +def _py_cc_toolchain_info_subject_soabi(self): + return subjects.str( + self.actual.soabi, + meta = self.meta.derive("soabi()"), + ) + def _py_cc_toolchain_info_subject_sys_platform(self): return subjects.str( self.actual.sys_platform,