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
29 changes: 29 additions & 0 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,35 @@ 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), cross-compiled with manual
# Meson arguments, or cross-compiled with cibuildwheel, which uses a similar
# approach to crossenv. In the latter case, we synthesize a cross file to make
# that work out of the box.
Comment on lines +747 to +750
Copy link
Member

Choose a reason for hiding this comment

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

I think

Suggested change
# Android may be native-compiled (e.g. with Termux), cross-compiled with manual
# Meson arguments, or cross-compiled with cibuildwheel, which uses a similar
# approach to crossenv. In the latter case, we synthesize a cross file to make
# that work out of the box.
# Simplify cross-compilation for Android with cibuildwheel: detect the
# cross-compilation environment set up by cibuildwheel and synthesize an
# appropriate cross file.

is more to the point and describes what the following code block is really about.

elif sysconfig.get_platform().startswith('android-') and 'CIBUILDWHEEL' in os.environ:
cpu = platform.machine()
cpu_family = 'arm' if cpu.startswith('arm') else 'x86' if cpu.endswith('86') else cpu
Copy link
Member

Choose a reason for hiding this comment

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

Can we have a comment that explains what this normalization does? I find the 'x86' if cpu.endswith('86') part particularly puzzling. What does platform.machine() return other than x86? Judging from the test, this simply maps i686 to x86. If this is the case, the more explicit 'x86' if cpu == 'i686' would be better.


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

[properties]
# cibuildwheel monkey-patches platform.system and platform.machine to
# simulate running on Android, when it's actually running on Linux or
# macOS. So the host and build machines will appear to match, but host
# binaries cannot actually be run on the build machine. As described at
# https://mesonbuild.com/Cross-compilation.html, we indicate this by
# setting needs_exe_wrapper = true, but NOT setting exe_wrapper.
Comment on lines +765 to +770
Copy link
Member

Choose a reason for hiding this comment

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

Mostly to make things easier for me to understand when I will go back to read this one year from now:

Suggested change
# cibuildwheel monkey-patches platform.system and platform.machine to
# simulate running on Android, when it's actually running on Linux or
# macOS. So the host and build machines will appear to match, but host
# binaries cannot actually be run on the build machine. As described at
# https://mesonbuild.com/Cross-compilation.html, we indicate this by
# setting needs_exe_wrapper = true, but NOT setting exe_wrapper.
# Due to Python lacking proper cross-compilation support, for the build
# to produce the correct wheel tags when cross-compiling for Android,
# cibuildwheel monkey-patches platform.system() and platform.machine()
# to simulate running on Android. This makes Meson believe that host and
# build machines match and thus that host binaries can be run on the build
# machine, when this is not actually the case. Override the auto-detection.

needs_exe_wrapper = true
''')
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
47 changes: 47 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,53 @@ 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, 'system', Mock(return_value='Android'))
monkeypatch.setattr(platform, 'machine', Mock(return_value=cpu))
monkeypatch.setattr(sysconfig, 'get_platform', Mock(return_value='android-24'))
if cross:
monkeypatch.setenv('CIBUILDWHEEL', '1')

# Meson may require some tools to be configured when fatal warnings are enabled.
# Set the same set of variables as cibuildwheel.
for name in ['ar', 'as', 'cc', 'cxx', 'ld', 'nm', 'ranlib', 'readelf', 'strip']:
monkeypatch.setenv(name.upper(), f'/path/to/{name}')

# 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