Skip to content

Commit

Permalink
Add ctypes support for python3's recipe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
opacam committed Nov 19, 2018
1 parent 5fc5241 commit 0fec438
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/source/buildoptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
###############
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected static ArrayList<String> getLibraries(File filesDir) {
ArrayList<String> libsList = new ArrayList<String>();
addLibraryIfExists(libsList, "crystax", libsDir);
addLibraryIfExists(libsList, "sqlite3", libsDir);
addLibraryIfExists(libsList, "ffi", libsDir);
libsList.add("SDL2");
libsList.add("SDL2_image");
libsList.add("SDL2_mixer");
Expand Down
26 changes: 25 additions & 1 deletion pythonforandroid/recipes/python3/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}',
Expand Down
2 changes: 1 addition & 1 deletion testapps/setup_testapp_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0fec438

Please sign in to comment.