Skip to content

Commit

Permalink
ENH: allow to overwrite macOS platform tag via _PYTHON_HOST_PLATFORM
Browse files Browse the repository at this point in the history
Use sysconfig.get_platform() instead of platform.mac_ver() to base the
platform tag computation.  The ability to overwrite the value returned
by former via the _PYTHON_HOST_PLATFORM environment variable is used
in macOS extension modules cross-compilation.

See mesonbuild#222 for background.
  • Loading branch information
dnicolodi committed Dec 11, 2022
1 parent 07a3ce6 commit 7aba25c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
14 changes: 13 additions & 1 deletion mesonpy/_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ def get_abi_tag() -> str:


def _get_macosx_platform_tag() -> str:
ver, x, arch = platform.mac_ver()
ver, _, arch = platform.mac_ver()

# Override the architecture with the one provided in the
# _PYTHON_HOST_PLATFORM environment variable. This environment
# variable affects the sysconfig.get_platform() return value and
# is used to cross-compile python extensions on macOS for a
# different architecture. We base the platform tag computation on
# platform.mac_ver() but respect the content of the environment
# variable.
try:
arch = os.environ.get('_PYTHON_HOST_PLATFORM', '').split('-')[2]
except IndexError:
pass

# Override the macOS version if one is provided via the
# MACOS_DEPLOYMENT_TARGET environment variable.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def test_macos_platform_tag(monkeypatch):
assert next(packaging.tags.mac_platforms((major, minor))) == mesonpy._tags.get_platform_tag()


@pytest.mark.skipif(platform.system() != 'Darwin', reason='macOS specific test')
def test_python_host_platform(monkeypatch):
monkeypatch.setenv('_PYTHON_HOST_PLATFORM', 'macosx-12.0-arm64')
assert mesonpy._tags.get_platform_tag().endswith('arm64')
monkeypatch.setenv('_PYTHON_HOST_PLATFORM', 'macosx-11.1-x86_64')
assert mesonpy._tags.get_platform_tag().endswith('x86_64')


def wheel_builder_test_factory(monkeypatch, content):
files = defaultdict(list)
files.update({key: [(pathlib.Path(x), os.path.join('build', x)) for x in value] for key, value in content.items()})
Expand Down

0 comments on commit 7aba25c

Please sign in to comment.