From 0fec4385555f08418817057c945a74c687a200cb Mon Sep 17 00:00:00 2001 From: opacam Date: Sun, 18 Nov 2018 10:25:33 +0100 Subject: [PATCH] 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. --- doc/source/buildoptions.rst | 4 +++ .../java/org/kivy/android/PythonUtil.java | 1 + pythonforandroid/recipes/python3/__init__.py | 26 ++++++++++++++++++- testapps/setup_testapp_python3.py | 2 +- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/doc/source/buildoptions.rst b/doc/source/buildoptions.rst index a9c2b472b2..9af0554b51 100644 --- a/doc/source/buildoptions.rst +++ b/doc/source/buildoptions.rst @@ -27,6 +27,10 @@ and works with any recent version of the Android NDK. Select Python 3 by adding it to your requirements, e.g. ``--requirements=python3``. +.. note:: ctypes is not included automatically, if you would like to use it + then add libffi to your requirements, + e.g. ``--requirements=kivy,libffi,python3``. + CrystaX python3 ############### diff --git a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonUtil.java b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonUtil.java index 978a843fe6..3b4429c9a9 100644 --- a/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonUtil.java +++ b/pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonUtil.java @@ -37,6 +37,7 @@ protected static ArrayList getLibraries(File filesDir) { ArrayList libsList = new ArrayList(); addLibraryIfExists(libsList, "crystax", libsDir); addLibraryIfExists(libsList, "sqlite3", libsDir); + addLibraryIfExists(libsList, "ffi", libsDir); libsList.add("SDL2"); libsList.add("SDL2_image"); libsList.add("SDL2_mixer"); diff --git a/pythonforandroid/recipes/python3/__init__.py b/pythonforandroid/recipes/python3/__init__.py index 2de84a942b..0118efe4be 100644 --- a/pythonforandroid/recipes/python3/__init__.py +++ b/pythonforandroid/recipes/python3/__init__.py @@ -1,4 +1,4 @@ -from pythonforandroid.recipe import TargetPythonRecipe +from pythonforandroid.recipe import TargetPythonRecipe, Recipe from pythonforandroid.toolchain import shprint, current_directory from pythonforandroid.logger import logger, info, error from pythonforandroid.util import ensure_dir, walk_valid_filens @@ -33,16 +33,38 @@ class Python3Recipe(TargetPythonRecipe): + ''' + .. note:: + In order to build certain python modules, we need to add some extra + recipes to our build requirements: + + - ctypes: you must add the recipe for ``libffi``. + ''' version = '3.7.1' url = 'https://www.python.org/ftp/python/{version}/Python-{version}.tgz' name = 'python3' depends = ['hostpython3'] conflicts = ['python3crystax', 'python2'] + opt_depends = ['libffi'] # This recipe can be built only against API 21+ MIN_NDK_API = 21 + def set_libs_flags(self, env, arch): + '''Takes care to properly link libraries with python depending on our + requirements and the attribute :attr:`opt_depends`. + ''' + if 'libffi' in self.ctx.recipe_build_order: + info('Activating flags for libffi') + recipe = Recipe.get_recipe('libffi', self.ctx) + include = ' -I' + ' -I'.join(recipe.get_include_dirs(arch)) + ldflag = ' -L' + join(recipe.get_build_dir(arch.arch), + recipe.get_host(arch), '.libs') + ' -lffi' + env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include + env['LDFLAGS'] = env.get('LDFLAGS', '') + ldflag + return env + def build_arch(self, arch): if self.ctx.ndk_api < self.MIN_NDK_API: error('Target ndk-api is {}, but the python3 recipe supports only {}+'.format( @@ -119,6 +141,8 @@ def build_arch(self, arch): env['SYSROOT'] = sysroot + env = self.set_libs_flags(env, arch) + if not exists('config.status'): shprint(sh.Command(join(recipe_build_dir, 'configure')), *(' '.join(('--host={android_host}', diff --git a/testapps/setup_testapp_python3.py b/testapps/setup_testapp_python3.py index 891db01209..34393e4214 100644 --- a/testapps/setup_testapp_python3.py +++ b/testapps/setup_testapp_python3.py @@ -2,7 +2,7 @@ from distutils.core import setup from setuptools import find_packages -options = {'apk': {'requirements': 'sdl2,pyjnius,kivy,python3', +options = {'apk': {'requirements': 'libffi,sdl2,pyjnius,kivy,python3', 'android-api': 27, 'ndk-api': 21, 'dist-name': 'bdisttest_python3_googlendk',