Skip to content

Commit

Permalink
ENH: improve parsing of the $ARCHFLAGS environment variable
Browse files Browse the repository at this point in the history
This allows to correctly handle repeated -arch flags.
  • Loading branch information
dnicolodi committed Nov 11, 2023
1 parent d70fd9e commit 470b516
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,17 @@ def __init__(
if sysconfig.get_platform().startswith('macosx-'):
archflags = os.environ.get('ARCHFLAGS', '').strip()
if archflags:
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))

# parse the ARCHFLAGS environment variable
parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
parser.add_argument('-arch', action='append')
args, unknown = parser.parse_known_args(archflags.split())
if unknown:
raise ConfigError(f'Unknown flag specified in $ARCHFLAGS={archflags!r}')
arch, *other = set(args.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 = os.environ.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
Expand Down
33 changes: 33 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,36 @@ def test_compiler(venv, package_detect_compiler, tmp_path):
venv.pip('install', os.fspath(tmp_path / wheel))
compiler = venv.python('-c', 'import detect_compiler; print(detect_compiler.compiler())').strip()
assert compiler == 'msvc'


@pytest.mark.skipif(sys.platform != 'darwin', reason='macOS specific test')
@pytest.mark.parametrize('archflags', [
'-arch x86_64',
'-arch arm64',
'-arch arm64 -arch arm64',
])
def test_archflags_envvar_parsing(package_purelib_and_platlib, monkeypatch, archflags):
try:
monkeypatch.setenv('ARCHFLAGS', archflags)
arch = archflags.split()[-1]
with mesonpy._project():
assert mesonpy._tags.Tag().platform.endswith(arch)
finally:
# revert environment variable setting done by the in-process build
os.environ.pop('_PYTHON_HOST_PLATFORM', None)


@pytest.mark.skipif(sys.platform != 'darwin', reason='macOS specific test')
@pytest.mark.parametrize('archflags', [
'-arch arm64 -arch x86_64',
'-arch arm64 -DFOO=1',
])
def test_archflags_envvar_parsing_invalid(package_purelib_and_platlib, monkeypatch, archflags):
try:
monkeypatch.setenv('ARCHFLAGS', archflags)
with pytest.raises(mesonpy.ConfigError):
with mesonpy._project():
pass
finally:
# revert environment variable setting done by the in-process build
os.environ.pop('_PYTHON_HOST_PLATFORM', None)

0 comments on commit 470b516

Please sign in to comment.