Skip to content

Commit

Permalink
BUG: set _PYTHON_HOST_PLATFOMR env var for the current process too
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnicolodi committed Apr 22, 2023
1 parent 533cc7d commit 7f440df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 4 additions & 5 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,29 +647,28 @@ def __init__(
self._meson_native_file = self._build_dir / 'meson-python-native-file.ini'
self._meson_cross_file = self._build_dir / 'meson-python-cross-file.ini'
self._meson_args: MesonArgs = collections.defaultdict(list)
self._env = os.environ.copy()

_check_meson_version()

self._ninja = _env_ninja_command()
if self._ninja is None:
raise ConfigError(f'Could not find ninja version {_NINJA_REQUIRED_VERSION} or newer.')
self._env.setdefault('NINJA', self._ninja)
os.environ.setdefault('NINJA', self._ninja)

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

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

Expand Down
9 changes: 5 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,18 @@ def disable_pip_version_check():

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


@pytest.fixture
Expand Down
9 changes: 9 additions & 0 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,12 @@ def test_purelib_platlib_split(package_purelib_platlib_split, tmp_path):
with pytest.raises(mesonpy.BuildError, match='The purelib-platlib-split package is split'):
with mesonpy.Project.with_temp_working_dir() as project:
project.wheel(tmp_path)


@pytest.mark.skipif(platform.system() != 'Darwin', reason='macOS specific test')
@pytest.mark.parametrize(('arch'), ['x86_64', 'arm64'])
def test_archflags_envvar(package_purelib_and_platlib, monkeypatch, tmp_path, arch):
monkeypatch.setenv('ARCHFLAGS', f'-arch {arch}')
filename = mesonpy.build_wheel(tmp_path)
name = wheel.wheelfile.WheelFile(tmp_path / filename).parsed_filename
assert name.group('plat').endswith(arch)

0 comments on commit 7f440df

Please sign in to comment.