Skip to content

Commit 0a8d96a

Browse files
committed
Add support for ctypes to 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. References: This commit partially fixes some of the points mentioned in issue number #1455
1 parent 819cecd commit 0a8d96a

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

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: 49 additions & 16 deletions
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
44
from pythonforandroid.util import ensure_dir, walk_valid_filens
@@ -33,13 +33,53 @@
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']
42-
# opt_depends = ['openssl', 'sqlite3']
49+
opt_depends = ['libffi'] # 'openssl', 'sqlite3'
50+
51+
configure_args = (
52+
'--host={android_host}',
53+
'--build={android_build}',
54+
'--enable-shared',
55+
'--disable-ipv6',
56+
'ac_cv_file__dev_ptmx=yes',
57+
'ac_cv_file__dev_ptc=no',
58+
'--without-ensurepip',
59+
'ac_cv_little_endian_double=yes',
60+
'--prefix={prefix}',
61+
'--exec-prefix={exec_prefix}')
62+
63+
def set_libs_flags(self, env, arch):
64+
'''Takes care to properly link libraries with python depending on our
65+
requirements and the attribute :attr:`opt_depends`.
66+
'''
67+
def add_flags(include_flags, link_flags):
68+
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include_flags
69+
env['LDFLAGS'] = env.get('LDFLAGS', '') + link_flags
70+
71+
if 'libffi' in self.ctx.recipe_build_order:
72+
info('Activating flags for libffi')
73+
recipe = Recipe.get_recipe('libffi', self.ctx)
74+
include = ' -I' + ' -I'.join(recipe.get_include_dirs(arch))
75+
ldflag = ' -L' + join(recipe.get_build_dir(arch.arch),
76+
recipe.get_host(arch), '.libs') + ' -lffi'
77+
add_flags(include, ldflag)
78+
# libffi needs some extra configurations
79+
env['LIBFFI_CFLAGS'] = env.get('CFLAGS', '') + include
80+
env['LIBFFI_LIBS'] = ldflag
81+
self.configure_args += ('--with-system-ffi',)
82+
return env
4383

4484
def build_arch(self, arch):
4585
recipe_build_dir = self.get_build_dir(arch.arch)
@@ -112,22 +152,15 @@ def build_arch(self, arch):
112152

113153
env['SYSROOT'] = sysroot
114154

155+
env = self.set_libs_flags(env, arch)
156+
115157
if not exists('config.status'):
116158
shprint(sh.Command(join(recipe_build_dir, 'configure')),
117-
*(' '.join(('--host={android_host}',
118-
'--build={android_build}',
119-
'--enable-shared',
120-
'--disable-ipv6',
121-
'ac_cv_file__dev_ptmx=yes',
122-
'ac_cv_file__dev_ptc=no',
123-
'--without-ensurepip',
124-
'ac_cv_little_endian_double=yes',
125-
'--prefix={prefix}',
126-
'--exec-prefix={exec_prefix}')).format(
127-
android_host=android_host,
128-
android_build=android_build,
129-
prefix=sys_prefix,
130-
exec_prefix=sys_exec_prefix)).split(' '), _env=env)
159+
*(' '.join(self.configure_args).format(
160+
android_host=android_host,
161+
android_build=android_build,
162+
prefix=sys_prefix,
163+
exec_prefix=sys_exec_prefix)).split(' '), _env=env)
131164

132165
if not exists('python'):
133166
shprint(sh.make, 'all', _env=env)

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)