Skip to content

Commit 2bb7038

Browse files
rgommersdnicolodi
authored andcommitted
ENH: implement support for build-details.json (PEP 739)
1 parent e33fe06 commit 2bb7038

4 files changed

Lines changed: 78 additions & 19 deletions

File tree

‎mesonpy/__init__.py‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ class _WheelBuilder():
340340
_manifest: Dict[str, List[_Entry]]
341341
_limited_api: bool
342342
_allow_windows_shared_libs: bool
343+
_build_details: mesonpy._tags.BuildDetails | None
343344

344345
@property
345346
def _has_internal_libs(self) -> bool:
@@ -370,8 +371,8 @@ def tag(self) -> mesonpy._tags.Tag:
370371
# does not contain any extension module (does not
371372
# distribute any file in {platlib}) thus use generic
372373
# implementation and ABI tags.
373-
return mesonpy._tags.Tag('py3', 'none', None)
374-
return mesonpy._tags.Tag(None, self._stable_abi, None)
374+
return mesonpy._tags.Tag('py3', 'none', None, self._build_details)
375+
return mesonpy._tags.Tag(None, self._stable_abi, None, self._build_details)
375376

376377
@property
377378
def name(self) -> str:
@@ -834,6 +835,25 @@ def __init__(
834835
''')
835836
self._meson_native_file.write_text(native_file_data, encoding='utf-8')
836837

838+
self._build_details: mesonpy._tags.BuildDetails | None = None
839+
# Starting with version 1.10, Meson can consume a `build-detail.json`
840+
# file following the specification is PEP 739 to obtain required
841+
# information to build extension modules without having to run the
842+
# interpreter. The path to the `build-details.json` can be specified
843+
# passing the with the `-Dpython.build_config=` option to `meson
844+
# setup`. Extract the value passed to this option and use the details
845+
# in the `build-details.json` file to compute the wheel tag.
846+
self._build_details: mesonpy._tags.BuildDetails | None = None
847+
parser = argparse.ArgumentParser(add_help=False)
848+
parser.add_argument('-D', action='append', default=[])
849+
args, _ = parser.parse_known_args(self._meson_args['setup'])
850+
for arg in reversed(args.D):
851+
name, value = arg.split('=', 1)
852+
if name == 'python.build_config':
853+
with open(value, 'r', encoding='utf8') as f:
854+
self._build_details = json.load(f)
855+
break
856+
837857
# reconfigure if we have a valid Meson build directory. Meson
838858
# uses the presence of the 'meson-private/coredata.dat' file
839859
# in the build directory as indication that the build
@@ -1151,13 +1171,15 @@ def sdist(self, directory: Path) -> pathlib.Path:
11511171
def wheel(self, directory: Path) -> pathlib.Path:
11521172
"""Generates a wheel in the specified directory."""
11531173
self.build()
1154-
builder = _WheelBuilder(self._metadata, self._manifest, self._limited_api, self._allow_windows_shared_libs)
1174+
builder = _WheelBuilder(
1175+
self._metadata, self._manifest, self._limited_api, self._allow_windows_shared_libs, self._build_details)
11551176
return builder.build(directory)
11561177

11571178
def editable(self, directory: Path) -> pathlib.Path:
11581179
"""Generates an editable wheel in the specified directory."""
11591180
self.build()
1160-
builder = _EditableWheelBuilder(self._metadata, self._manifest, self._limited_api, self._allow_windows_shared_libs)
1181+
builder = _EditableWheelBuilder(
1182+
self._metadata, self._manifest, self._limited_api, self._allow_windows_shared_libs, self._build_details)
11611183
return builder.build(directory, self._source_dir, self._build_dir, self._build_command, self._editable_verbose)
11621184

11631185

‎mesonpy/_tags.py‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@
1313

1414

1515
if typing.TYPE_CHECKING: # pragma: no cover
16-
from typing import Optional, Union
16+
from typing import Optional, TypedDict, Union
17+
18+
class _Abi(TypedDict):
19+
extension_suffix: str
20+
21+
class _ImplementationVersion(TypedDict):
22+
major: int
23+
minor: int
24+
25+
class _Implementation(TypedDict):
26+
name: str
27+
version: _ImplementationVersion
28+
29+
class BuildDetails(TypedDict):
30+
abi: _Abi
31+
implementation: _Implementation
32+
platform: str
1733

1834

1935
# https://peps.python.org/pep-0425/#python-tag
@@ -29,11 +45,17 @@
2945
_32_BIT_INTERPRETER = struct.calcsize('P') == 4
3046

3147

32-
def get_interpreter_tag() -> str:
33-
name = sys.implementation.name
48+
def get_interpreter_tag(build_details: BuildDetails | None = None) -> str:
49+
if build_details is None:
50+
name = sys.implementation.name
51+
major, minor = sys.version_info[:2]
52+
else:
53+
name = build_details['implementation']['name']
54+
_v = build_details['implementation']['version']
55+
major = _v['major']
56+
minor = _v['minor']
3457
name = INTERPRETERS.get(name, name)
35-
version = sys.version_info
36-
return f'{name}{version[0]}{version[1]}'
58+
return f'{name}{major}{minor}'
3759

3860

3961
def _get_config_var(name: str, default: Union[str, int, None] = None) -> Union[str, int, None]:
@@ -43,15 +65,19 @@ def _get_config_var(name: str, default: Union[str, int, None] = None) -> Union[s
4365
return value
4466

4567

46-
def get_abi_tag() -> str:
68+
def get_abi_tag(build_details: BuildDetails | None = None) -> str:
4769
# The best solution to obtain the Python ABI is to parse the
4870
# $SOABI or $EXT_SUFFIX sysconfig variables as defined in PEP-314.
4971

5072
# PyPy reports a $SOABI that does not agree with $EXT_SUFFIX.
5173
# Using $EXT_SUFFIX will not break when PyPy will fix this.
5274
# See https://foss.heptapod.net/pypy/pypy/-/issues/3816 and
5375
# https://github.com/pypa/packaging/pull/607.
54-
empty, abi, ext = str(sysconfig.get_config_var('EXT_SUFFIX')).split('.')
76+
if build_details is not None:
77+
ext_suffix = build_details['abi']['extension_suffix']
78+
else:
79+
ext_suffix = str(sysconfig.get_config_var('EXT_SUFFIX'))
80+
empty, abi, ext = ext_suffix.split('.')
5581

5682
# The packaging module initially based his understanding of the
5783
# $SOABI variable on the inconsistent value reported by PyPy, and
@@ -159,8 +185,8 @@ def _get_ios_platform_tag() -> str:
159185
return f'ios_{version[0]}_{version[1]}_{multiarch}'
160186

161187

162-
def get_platform_tag() -> str:
163-
platform = sysconfig.get_platform()
188+
def get_platform_tag(build_details: BuildDetails | None = None) -> str:
189+
platform = build_details['platform'] if build_details is not None else sysconfig.get_platform()
164190
if platform.startswith('macosx'):
165191
return _get_macosx_platform_tag()
166192
if platform.startswith('ios'):
@@ -175,10 +201,11 @@ def get_platform_tag() -> str:
175201

176202

177203
class Tag:
178-
def __init__(self, interpreter: Optional[str] = None, abi: Optional[str] = None, platform: Optional[str] = None):
179-
self.interpreter = interpreter or get_interpreter_tag()
180-
self.abi = abi or get_abi_tag()
181-
self.platform = platform or get_platform_tag()
204+
def __init__(self, interpreter: Optional[str] = None, abi: Optional[str] = None, platform: Optional[str] = None,
205+
build_details: BuildDetails | None = None):
206+
self.interpreter = interpreter or get_interpreter_tag(build_details)
207+
self.abi = abi or get_abi_tag(build_details)
208+
self.platform = platform or get_platform_tag(build_details)
182209

183210
def __str__(self) -> str:
184211
return f'{self.interpreter}-{self.abi}-{self.platform}'

‎tests/test_tags.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
import importlib.machinery
6+
import json
67
import os
78
import pathlib
89
import platform
@@ -106,7 +107,7 @@ def wheel_builder_test_factory(content, pure=True, limited_api=False):
106107
manifest = defaultdict(list)
107108
for key, value in content.items():
108109
manifest[key] = [mesonpy._Entry(pathlib.Path(x), os.path.join('build', x)) for x in value]
109-
return mesonpy._WheelBuilder(None, manifest, limited_api, False)
110+
return mesonpy._WheelBuilder(None, manifest, limited_api, False, None)
110111

111112

112113
def test_tag_empty_wheel():
@@ -149,3 +150,12 @@ def test_tag_mixed_abi():
149150
}, pure=False, limited_api=True)
150151
with pytest.raises(mesonpy.BuildError, match='The package declares compatibility with Python limited API but '):
151152
assert str(builder.tag) == f'{INTERPRETER}-abi3-{PLATFORM}'
153+
154+
155+
def test_build_details():
156+
try:
157+
with open(os.path.join(sysconfig.get_path('stdlib'), 'build-details.json'), encoding='utf8') as f:
158+
build_details = json.load(f)
159+
except FileNotFoundError:
160+
return pytest.skip('build-details.json not found')
161+
assert str(mesonpy._tags.Tag()) == str(mesonpy._tags.Tag(build_details=build_details))

‎tests/test_wheel.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_entrypoints(wheel_full_metadata):
255255
def test_top_level_modules(package_module_types):
256256
with mesonpy._project() as project:
257257
builder = mesonpy._EditableWheelBuilder(
258-
project._metadata, project._manifest, project._limited_api, project._allow_windows_shared_libs)
258+
project._metadata, project._manifest, project._limited_api, project._allow_windows_shared_libs, None)
259259
assert set(builder._top_level_modules) == {
260260
'file',
261261
'package',

0 commit comments

Comments
 (0)