Skip to content

Commit 7f440df

Browse files
committed
BUG: set _PYTHON_HOST_PLATFOMR env var for the current process too
The environment variable is used to compute the wheel platform tag. However this happens in process and setting the environment variable only for forked processes does not affect the tag. This fixes the wheel filename produced when cross compiling on macOS via the ARCHFLAGS environment variable. While at it, simplify a bit the handling of environment variables.
1 parent 533cc7d commit 7f440df

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

mesonpy/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,29 +647,28 @@ def __init__(
647647
self._meson_native_file = self._build_dir / 'meson-python-native-file.ini'
648648
self._meson_cross_file = self._build_dir / 'meson-python-cross-file.ini'
649649
self._meson_args: MesonArgs = collections.defaultdict(list)
650-
self._env = os.environ.copy()
651650

652651
_check_meson_version()
653652

654653
self._ninja = _env_ninja_command()
655654
if self._ninja is None:
656655
raise ConfigError(f'Could not find ninja version {_NINJA_REQUIRED_VERSION} or newer.')
657-
self._env.setdefault('NINJA', self._ninja)
656+
os.environ.setdefault('NINJA', self._ninja)
658657

659658
# make sure the build dir exists
660659
self._build_dir.mkdir(exist_ok=True, parents=True)
661660
self._install_dir.mkdir(exist_ok=True, parents=True)
662661

663662
# setuptools-like ARCHFLAGS environment variable support
664663
if sysconfig.get_platform().startswith('macosx-'):
665-
archflags = self._env.get('ARCHFLAGS', '').strip()
664+
archflags = os.environ.get('ARCHFLAGS', '').strip()
666665
if archflags:
667666
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))
668667
if other:
669668
raise ConfigError(f'Multi-architecture builds are not supported but $ARCHFLAGS={archflags!r}')
670669
macver, _, nativearch = platform.mac_ver()
671670
if arch != nativearch:
672-
x = self._env.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
671+
x = os.environ.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
673672
if not x.endswith(arch):
674673
raise ConfigError(f'$ARCHFLAGS={archflags!r} and $_PYTHON_HOST_PLATFORM={x!r} do not agree')
675674
family = 'aarch64' if arch == 'arm64' else arch
@@ -736,7 +735,7 @@ def _run(self, cmd: Sequence[str]) -> None:
736735
# command line appears before the command output. Without it,
737736
# the lines appear in the wrong order in pip output.
738737
print('{cyan}{bold}+ {}{reset}'.format(' '.join(cmd), **_STYLES), flush=True)
739-
r = subprocess.run(cmd, env=self._env, cwd=self._build_dir)
738+
r = subprocess.run(cmd, cwd=self._build_dir)
740739
if r.returncode != 0:
741740
raise SystemExit(r.returncode)
742741

tests/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,18 @@ def disable_pip_version_check():
156156

157157
@pytest.fixture(scope='session')
158158
def pep518_wheelhouse(tmp_path_factory):
159-
wheelhouse = tmp_path_factory.mktemp('wheelhouse')
160-
meson_python = str(package_dir.parent.parent)
159+
wheelhouse = os.fspath(tmp_path_factory.mktemp('wheelhouse'))
160+
meson_python = os.fspath(package_dir.parent.parent)
161161
# Populate wheelhouse with wheel for the following packages and
162162
# their dependencies. Wheels are downloaded from PyPI or built
163163
# from the source distribution as needed. Sources or wheels in
164164
# the pip cache are used when available.
165165
packages = [
166166
meson_python,
167167
]
168-
subprocess.run([sys.executable, '-m', 'pip', 'wheel', '--wheel-dir', str(wheelhouse), *packages], check=True)
169-
return str(wheelhouse)
168+
cmd = [sys.executable, '-m', 'pip', 'wheel', '--no-build-isolation', '--wheel-dir', wheelhouse, *packages]
169+
subprocess.run(cmd, check=True)
170+
return wheelhouse
170171

171172

172173
@pytest.fixture

tests/test_wheel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,12 @@ def test_purelib_platlib_split(package_purelib_platlib_split, tmp_path):
240240
with pytest.raises(mesonpy.BuildError, match='The purelib-platlib-split package is split'):
241241
with mesonpy.Project.with_temp_working_dir() as project:
242242
project.wheel(tmp_path)
243+
244+
245+
@pytest.mark.skipif(platform.system() != 'Darwin', reason='macOS specific test')
246+
@pytest.mark.parametrize(('arch'), ['x86_64', 'arm64'])
247+
def test_archflags_envvar(package_purelib_and_platlib, monkeypatch, tmp_path, arch):
248+
monkeypatch.setenv('ARCHFLAGS', f'-arch {arch}')
249+
filename = mesonpy.build_wheel(tmp_path)
250+
name = wheel.wheelfile.WheelFile(tmp_path / filename).parsed_filename
251+
assert name.group('plat').endswith(arch)

0 commit comments

Comments
 (0)