Skip to content

Commit 6f4077d

Browse files
committed
Enable libffi support for python recipes
Seems that the ctypes module cannot be build against the python3's included sources for libffi, so here we solve the problem by linking with our recipe for libffi. 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 and also has been documented the issue mentioned.
1 parent e24ede5 commit 6f4077d

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

doc/source/buildoptions.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ This option builds Python 2.7.2 for your selected Android
1717
architecture. There are no special requirements, all the building is
1818
done locally.
1919

20+
.. note:: if you add libffi to your requirements, ctypes will be linked to it
21+
instead of building the included one in python2 sources. This may be
22+
a good idea, because the libffi included in python sources is an old
23+
version of libffi and may have some security issues.
24+
2025

2126
python3
2227
~~~~~~~
@@ -27,6 +32,10 @@ and works with any recent version of the Android NDK.
2732
Select Python 3 by adding it to your requirements,
2833
e.g. ``--requirements=python3``.
2934

35+
.. note:: ctypes is not included automatically, if you would like to use it
36+
then add libffi to your requirements,
37+
e.g. ``--requirements=kivy,libffi,python3``.
38+
3039

3140
CrystaX python3
3241
###############

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/python.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class GuestPythonRecipe(TargetPythonRecipe):
162162
'''The file extensions from site packages dir that we don't want to be
163163
included in our python bundle.'''
164164

165-
opt_depends = ['sqlite3']
165+
opt_depends = ['sqlite3', 'libffi']
166166
'''The optional libraries which we would like to get our python linked'''
167167

168168
def __init__(self, *args, **kwargs):
@@ -173,11 +173,23 @@ def set_libs_flags(self, env, arch):
173173
'''Takes care to properly link libraries with python depending on our
174174
requirements and the attribute :attr:`opt_depends`.
175175
'''
176+
def add_flags(include_flags, link_flags):
177+
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include_flags
178+
env['LDFLAGS'] = env.get('LDFLAGS', '') + link_flags
179+
176180
if 'sqlite3' in self.ctx.recipe_build_order:
177181
info('Activating flags for sqlite3')
178182
recipe = Recipe.get_recipe('sqlite3', self.ctx)
179-
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + recipe.get_build_dir(arch.arch)
180-
env['LDFLAGS'] = env.get('LDFLAGS', '') + recipe.get_lib_dir(arch) + ' -lsqlite3'
183+
add_flags(' -I' + recipe.get_build_dir(arch.arch),
184+
' -L' + recipe.get_lib_dir(arch) + ' -lsqlite3')
185+
186+
if 'libffi' in self.ctx.recipe_build_order:
187+
info('Activating flags for libffi')
188+
recipe = Recipe.get_recipe('libffi', self.ctx)
189+
add_flags(' -I' + ' -I'.join(recipe.get_include_dirs(arch)),
190+
' -L' + join(recipe.get_build_dir(arch.arch),
191+
recipe.get_host(arch), '.libs') + ' -lffi')
192+
181193
return env
182194

183195
def get_recipe_env(self, arch=None, with_flags_in_cc=True):

pythonforandroid/recipes/python3/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ class Python3Recipe(GuestPythonRecipe):
55
'''
66
The python3's recipe.
77
8-
.. note:: This recipe can be built only against API 21+
8+
.. note:: This recipe can be built only against API 21+. Also, in order to
9+
build certain python modules, we need to add some extra recipes to our
10+
build requirements:
11+
12+
- ctypes: you must add the recipe for ``libffi``.
913
1014
.. warning:: The support for libraries is temporary disabled. It will be
1115
enabled in a near future.

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': 'numpy,sdl2,pyjnius,kivy,python3',
5+
options = {'apk': {'requirements': 'libffi,numpy,sdl2,pyjnius,kivy,python3',
66
'android-api': 27,
77
'ndk-api': 21,
88
'dist-name': 'bdisttest_python3_googlendk',

0 commit comments

Comments
 (0)