-
Notifications
You must be signed in to change notification settings - Fork 83
Add support for targeting Android #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||||||||
| elif ( | ||||||||||||
| sysconfig.get_platform().startswith('android-') | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not simply
Suggested change
? |
||||||||||||
| and getattr(sys, 'cross_compiling', False) | ||||||||||||
| ): | ||||||||||||
|
Comment on lines
+751
to
+754
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please don't use the |
||||||||||||
| cpu = platform.machine() | ||||||||||||
| cpu_family = ( | ||||||||||||
| 'arm' if cpu.startswith('arm') else 'x86' if cpu.endswith('86') else cpu | ||||||||||||
| ) | ||||||||||||
|
Comment on lines
+756
to
+758
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is ugly, but please don't use the |
||||||||||||
|
|
||||||||||||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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-'): | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
|
||
There was a problem hiding this comment.
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:
I think the idea and code are fine.