From 7aba25ceb9a8eb38cf20e003ee42e27bdcbdcd6e Mon Sep 17 00:00:00 2001 From: Daniele Nicolodi Date: Tue, 22 Nov 2022 22:51:41 +0100 Subject: [PATCH] ENH: allow to overwrite macOS platform tag via _PYTHON_HOST_PLATFORM 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 #222 for background. --- mesonpy/_tags.py | 14 +++++++++++++- tests/test_tags.py | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mesonpy/_tags.py b/mesonpy/_tags.py index 538ceabc0..cdfa68094 100644 --- a/mesonpy/_tags.py +++ b/mesonpy/_tags.py @@ -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. diff --git a/tests/test_tags.py b/tests/test_tags.py index 07ba555ec..c1571db01 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -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()})