Skip to content

Commit 0fec438

Browse files
committed
Add ctypes support for python3's recipe
Should be mentioned that the current test app for python3 has been modified by adding libffi to the requirements because the ui for the app has some button to test the ctypes module.
1 parent 5fc5241 commit 0fec438

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

doc/source/buildoptions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ and works with any recent version of the Android NDK.
2727
Select Python 3 by adding it to your requirements,
2828
e.g. ``--requirements=python3``.
2929

30+
.. note:: ctypes is not included automatically, if you would like to use it
31+
then add libffi to your requirements,
32+
e.g. ``--requirements=kivy,libffi,python3``.
33+
3034

3135
CrystaX python3
3236
###############

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected static ArrayList<String> getLibraries(File filesDir) {
3737
ArrayList<String> libsList = new ArrayList<String>();
3838
addLibraryIfExists(libsList, "crystax", libsDir);
3939
addLibraryIfExists(libsList, "sqlite3", libsDir);
40+
addLibraryIfExists(libsList, "ffi", libsDir);
4041
libsList.add("SDL2");
4142
libsList.add("SDL2_image");
4243
libsList.add("SDL2_mixer");

pythonforandroid/recipes/python3/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pythonforandroid.recipe import TargetPythonRecipe
1+
from pythonforandroid.recipe import TargetPythonRecipe, Recipe
22
from pythonforandroid.toolchain import shprint, current_directory
33
from pythonforandroid.logger import logger, info, error
44
from pythonforandroid.util import ensure_dir, walk_valid_filens
@@ -33,16 +33,38 @@
3333

3434

3535
class Python3Recipe(TargetPythonRecipe):
36+
'''
37+
.. note::
38+
In order to build certain python modules, we need to add some extra
39+
recipes to our build requirements:
40+
41+
- ctypes: you must add the recipe for ``libffi``.
42+
'''
3643
version = '3.7.1'
3744
url = 'https://www.python.org/ftp/python/{version}/Python-{version}.tgz'
3845
name = 'python3'
3946

4047
depends = ['hostpython3']
4148
conflicts = ['python3crystax', 'python2']
49+
opt_depends = ['libffi']
4250

4351
# This recipe can be built only against API 21+
4452
MIN_NDK_API = 21
4553

54+
def set_libs_flags(self, env, arch):
55+
'''Takes care to properly link libraries with python depending on our
56+
requirements and the attribute :attr:`opt_depends`.
57+
'''
58+
if 'libffi' in self.ctx.recipe_build_order:
59+
info('Activating flags for libffi')
60+
recipe = Recipe.get_recipe('libffi', self.ctx)
61+
include = ' -I' + ' -I'.join(recipe.get_include_dirs(arch))
62+
ldflag = ' -L' + join(recipe.get_build_dir(arch.arch),
63+
recipe.get_host(arch), '.libs') + ' -lffi'
64+
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include
65+
env['LDFLAGS'] = env.get('LDFLAGS', '') + ldflag
66+
return env
67+
4668
def build_arch(self, arch):
4769
if self.ctx.ndk_api < self.MIN_NDK_API:
4870
error('Target ndk-api is {}, but the python3 recipe supports only {}+'.format(
@@ -119,6 +141,8 @@ def build_arch(self, arch):
119141

120142
env['SYSROOT'] = sysroot
121143

144+
env = self.set_libs_flags(env, arch)
145+
122146
if not exists('config.status'):
123147
shprint(sh.Command(join(recipe_build_dir, 'configure')),
124148
*(' '.join(('--host={android_host}',

testapps/setup_testapp_python3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from distutils.core import setup
33
from setuptools import find_packages
44

5-
options = {'apk': {'requirements': 'sdl2,pyjnius,kivy,python3',
5+
options = {'apk': {'requirements': 'libffi,sdl2,pyjnius,kivy,python3',
66
'android-api': 27,
77
'ndk-api': 21,
88
'dist-name': 'bdisttest_python3_googlendk',

0 commit comments

Comments
 (0)