Skip to content

Commit 201d8f9

Browse files
dnicolodirgommers
authored andcommitted
ENH: improve parsing of the $ARCHFLAGS environment variable
This allows to correctly handle repeated -arch flags.
1 parent 43bb0d7 commit 201d8f9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

mesonpy/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,17 @@ def __init__(
646646
if sysconfig.get_platform().startswith('macosx-'):
647647
archflags = os.environ.get('ARCHFLAGS', '').strip()
648648
if archflags:
649-
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))
649+
650+
# parse the ARCHFLAGS environment variable
651+
parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
652+
parser.add_argument('-arch', action='append')
653+
args, unknown = parser.parse_known_args(archflags.split())
654+
if unknown:
655+
raise ConfigError(f'Unknown flag specified in $ARCHFLAGS={archflags!r}')
656+
arch, *other = set(args.arch)
650657
if other:
651658
raise ConfigError(f'Multi-architecture builds are not supported but $ARCHFLAGS={archflags!r}')
659+
652660
macver, _, nativearch = platform.mac_ver()
653661
if arch != nativearch:
654662
x = os.environ.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')

tests/test_project.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,36 @@ def test_compiler(venv, package_detect_compiler, tmp_path):
211211
venv.pip('install', os.fspath(tmp_path / wheel))
212212
compiler = venv.python('-c', 'import detect_compiler; print(detect_compiler.compiler())').strip()
213213
assert compiler == 'msvc'
214+
215+
216+
@pytest.mark.skipif(sys.platform != 'darwin', reason='macOS specific test')
217+
@pytest.mark.parametrize('archflags', [
218+
'-arch x86_64',
219+
'-arch arm64',
220+
'-arch arm64 -arch arm64',
221+
])
222+
def test_archflags_envvar_parsing(package_purelib_and_platlib, monkeypatch, archflags):
223+
try:
224+
monkeypatch.setenv('ARCHFLAGS', archflags)
225+
arch = archflags.split()[-1]
226+
with mesonpy._project():
227+
assert mesonpy._tags.Tag().platform.endswith(arch)
228+
finally:
229+
# revert environment variable setting done by the in-process build
230+
os.environ.pop('_PYTHON_HOST_PLATFORM', None)
231+
232+
233+
@pytest.mark.skipif(sys.platform != 'darwin', reason='macOS specific test')
234+
@pytest.mark.parametrize('archflags', [
235+
'-arch arm64 -arch x86_64',
236+
'-arch arm64 -DFOO=1',
237+
])
238+
def test_archflags_envvar_parsing_invalid(package_purelib_and_platlib, monkeypatch, archflags):
239+
try:
240+
monkeypatch.setenv('ARCHFLAGS', archflags)
241+
with pytest.raises(mesonpy.ConfigError):
242+
with mesonpy._project():
243+
pass
244+
finally:
245+
# revert environment variable setting done by the in-process build
246+
os.environ.pop('_PYTHON_HOST_PLATFORM', None)

0 commit comments

Comments
 (0)