Skip to content

Commit 06840fb

Browse files
committed
ENH: crude support for ARCHFLAGS
1 parent d7f67d4 commit 06840fb

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

mesonpy/__init__.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,43 @@ def __init__(
567567
self._build_dir = pathlib.Path(build_dir).absolute() if build_dir else (self._working_dir / 'build')
568568
self._install_dir = self._working_dir / 'install'
569569
self._meson_native_file = self._source_dir / '.mesonpy-native-file.ini'
570+
self._meson_cross_file = self._source_dir / '.mesonpy-cross-file.ini'
571+
self._meson_args = collections.defaultdict(list, meson_args or {})
570572
self._env = os.environ.copy()
571573

572574
# prepare environment
573575
ninja_path = _env_ninja_command()
574576
if ninja_path is not None:
575577
self._env.setdefault('NINJA', str(ninja_path))
576578

579+
# setuptools-like ARCHFLAGS environment variable support
580+
if sysconfig.get_platform().startswith('macosx-'):
581+
archflags = self._env.get('ARCHFLAGS')
582+
if archflags is not None:
583+
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))
584+
if other:
585+
raise ConfigError(
586+
f'multi-architecture builds are not supported but $ARCHFLAGS is set to {archflags!r}')
587+
macver, _, nativearch = platform.mac_ver()
588+
if arch != nativearch:
589+
x = self._env.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
590+
if not x.endswith(arch):
591+
raise ConfigError(
592+
f'$ARCHFLAGS and $_PYTHON_HOST_PLATFORM environment variables do not agree: {archflags!r} {x!r}')
593+
family = 'aarch64' if arch == 'arm64' else arch
594+
cross_file_data = textwrap.dedent(f'''
595+
[binaries]
596+
c = ['cc', '-arch', {arch!r}]
597+
cpp = ['cpp', '-arch', {arch!r}]
598+
[host_machine]
599+
system = 'Darwin'
600+
cpu = {arch!r}
601+
cpu_family = {family!r}
602+
endian = 'little'
603+
''')
604+
self._meson_cross_file.write_text(cross_file_data)
605+
self._meson_args['setup'].extend(('--cross-file', os.fspath(self._meson_cross_file)))
606+
577607
# load config -- PEP 621 support is optional
578608
self._config = tomllib.loads(self._source_dir.joinpath('pyproject.toml').read_text())
579609
self._pep621 = 'project' in self._config
@@ -595,10 +625,9 @@ def __init__(
595625
self._validate_metadata()
596626

597627
# load meson args
598-
self._meson_args = collections.defaultdict(tuple, meson_args or {})
599628
for key in self._get_config_key('args'):
600629
args_from_config = tuple(self._get_config_key(f'args.{key}'))
601-
self._meson_args[key] = args_from_config + tuple(self._meson_args[key])
630+
self._meson_args[key].extend(args_from_config)
602631
# XXX: We should validate the user args to make sure they don't conflict with ours.
603632

604633
self._check_for_unknown_config_keys({
@@ -975,7 +1004,7 @@ def _validate_string_collection(key: str) -> None:
9751004
with Project.with_temp_working_dir(
9761005
build_dir=builddir,
9771006
meson_args=typing.cast(MesonArgs, {
978-
key: config_settings.get(f'{key}-args', ())
1007+
key: config_settings.get(f'{key}-args', [])
9791008
for key in meson_args_keys
9801009
}),
9811010
) as project:

0 commit comments

Comments
 (0)