Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,38 @@ def __init__(
self._meson_cross_file.write_text(cross_file_data, encoding='utf-8')
self._meson_args['setup'].extend(('--cross-file', os.fspath(self._meson_cross_file)))

# Android may be native-compiled (e.g. with Termux), or cross-compiled (e.g.
# with cibuildwheel), in which case we should generate a cross file.
# sys.cross_compiling isn't an official Python API, but it originated from
# crossenv and has been followed by other cross-compiling tools.
Comment on lines +748 to +750
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence I suggest to change the comment along these lines:

Suggested change
# with cibuildwheel), in which case we should generate a cross file.
# sys.cross_compiling isn't an official Python API, but it originated from
# crossenv and has been followed by other cross-compiling tools.
Cross compilation can be done the regular Meson way with separate build and host envs,
or with `cibuildwheel` which is `crossenv`-based. Here we try to detect whether
cibuildwheel/crossenv are used, through the `sys.cross_compiling` variable which they
define, and we synthesize a cross file in that case to make that work out of the box.

I think the idea and code are fine.

elif (
sysconfig.get_platform().startswith('android-')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply

Suggested change
sysconfig.get_platform().startswith('android-')
sys.platform == 'android'

?

and getattr(sys, 'cross_compiling', False)
):
Comment on lines +751 to +754
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif (
sysconfig.get_platform().startswith('android-')
and getattr(sys, 'cross_compiling', False)
):
elif sys.platform == 'android' and getattr(sys, 'cross_compiling', False):

Please don't use the black code uglifier.

cpu = platform.machine()
cpu_family = (
'arm' if cpu.startswith('arm') else 'x86' if cpu.endswith('86') else cpu
)
Comment on lines +756 to +758
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cpu_family = (
'arm' if cpu.startswith('arm') else 'x86' if cpu.endswith('86') else cpu
)
cpu_family = 'arm' if cpu.startswith('arm') else 'x86' if cpu.endswith('86') else cpu

This is ugly, but please don't use the black code uglifier to make it uglier.


cross_file_data = textwrap.dedent(f'''
# Binaries are controlled by environment variables, so they don't need
# to be repeated here.
Comment on lines +761 to +762
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any value in adding this comment to the generated file.

[host_machine]
system = 'android'
subsystem = 'android'
kernel = 'linux'
cpu_family = {cpu_family!r}
cpu = {cpu!r}
endian = {sys.byteorder!r}

[properties]
# cibuildwheel's cross virtual environment will make Meson believe it's
# running on Android when it's actually running on Linux or macOS.
needs_exe_wrapper = true
Comment on lines +772 to +774
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get how the comment (which I don't think should be in the generated file) goes with the implemented setting. How does cibuildwheel makes Meson believe it is running on Android? And why does it do so? It is clearly not the case. Also, what it the exe wrapper that is needed? This seems incomplete at best.

''')
self._meson_cross_file.write_text(cross_file_data, encoding='utf-8')
self._meson_args['setup'].extend(('--cross-file', os.fspath(self._meson_cross_file)))

# Support iOS targets. iOS does not have native build tools and always
# requires cross compilation: synthesize the appropriate cross file.
elif sysconfig.get_platform().startswith('ios-'):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,48 @@ def test_archflags_envvar_parsing_invalid(package_purelib_and_platlib, monkeypat
os.environ.pop('_PYTHON_HOST_PLATFORM', None)


@pytest.mark.parametrize(
('cpu', 'cpu_family'),
[
('aarch64', 'aarch64'),
('armv7l', 'arm'),
('armv8l', 'arm'),
('i686', 'x86'),
('x86_64', 'x86_64'),
],
)
@pytest.mark.parametrize('cross', [True, False])
def test_android_project(package_simple, monkeypatch, tmp_path, cpu, cpu_family, cross):
# Mock being on Android
monkeypatch.setattr(sys, 'platform', 'android')
monkeypatch.setattr(sys, 'byteorder', 'little')
monkeypatch.setattr(platform, 'machine', Mock(return_value=cpu))
monkeypatch.setattr(sysconfig, 'get_platform', Mock(return_value='android-24'))
if cross:
monkeypatch.setattr(sys, 'cross_compiling', True, raising=False)
monkeypatch.setenv('STRIP', '/path/to/strip')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is setting this environment variable needed?


# Create a project.
project = mesonpy.Project(source_dir=package_simple, build_dir=tmp_path)

# When cross-compiling, a cross file should be generated and used.
setup_args = project._meson_args['setup']
cross_path = tmp_path / 'meson-python-cross-file.ini'
if cross:
assert setup_args[-2:] == ['--cross-file', str(cross_path)]
cross_config = cross_path.read_text().splitlines()
assert "system = 'android'" in cross_config
assert "subsystem = 'android'" in cross_config
assert "kernel = 'linux'" in cross_config
assert f"cpu_family = '{cpu_family}'" in cross_config
assert f"cpu = '{cpu}'" in cross_config
assert "endian = 'little'" in cross_config
assert 'needs_exe_wrapper = true' in cross_config
else:
assert '--cross-file' not in setup_args
assert not cross_path.exists()


@pytest.mark.skipif(sys.version_info < (3, 13), reason='requires Python 3.13 or higher')
@pytest.mark.parametrize('multiarch', [
'arm64-iphoneos',
Expand Down
Loading