Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ctypes to python3's recipe #1463

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we need a more automatic way to add library loads here, unless - is it essential to load this library in this way, or does dynamic access work?
That doesn't need to hold up this PR though.

Copy link
Member Author

@opacam opacam Nov 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested without these line and dynamic access seems to work, thanks! :)
I correct myself ( I suppose that I did not update the test app on my device when removed those lines... 😅), another test with a clean branch and without modifying these line lead me to ctypes import error, so...I think these line is required because the ctypes module gets build in python modules...plus seems logical that we need these line, as we do with sqlite3 and openssl (cause are external dependencies for python)...anyway...I'm doing another test re-adding these lines again...which should make it work...

Update:
The test application successfully import ctypes with these line...crash without it.

libsList.add("SDL2");
libsList.add("SDL2_image");
libsList.add("SDL2_mixer");
Expand Down
65 changes: 49 additions & 16 deletions 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
from pythonforandroid.util import ensure_dir, walk_valid_filens
Expand Down Expand Up @@ -33,13 +33,53 @@


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 = ['openssl', 'sqlite3']
opt_depends = ['libffi'] # 'openssl', 'sqlite3'

configure_args = (
'--host={android_host}',
'--build={android_build}',
'--enable-shared',
'--disable-ipv6',
'ac_cv_file__dev_ptmx=yes',
'ac_cv_file__dev_ptc=no',
'--without-ensurepip',
'ac_cv_little_endian_double=yes',
'--prefix={prefix}',
'--exec-prefix={exec_prefix}')

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`.
'''
def add_flags(include_flags, link_flags):
opacam marked this conversation as resolved.
Show resolved Hide resolved
env['CPPFLAGS'] = env.get('CPPFLAGS', '') + include_flags
env['LDFLAGS'] = env.get('LDFLAGS', '') + link_flags

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'
add_flags(include, ldflag)
# libffi needs some extra configurations
env['LIBFFI_CFLAGS'] = env.get('CFLAGS', '') + include
opacam marked this conversation as resolved.
Show resolved Hide resolved
env['LIBFFI_LIBS'] = ldflag
self.configure_args += ('--with-system-ffi',)
opacam marked this conversation as resolved.
Show resolved Hide resolved
return env

def build_arch(self, arch):
recipe_build_dir = self.get_build_dir(arch.arch)
Expand Down Expand Up @@ -112,22 +152,15 @@ 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}',
'--build={android_build}',
'--enable-shared',
'--disable-ipv6',
'ac_cv_file__dev_ptmx=yes',
'ac_cv_file__dev_ptc=no',
'--without-ensurepip',
'ac_cv_little_endian_double=yes',
'--prefix={prefix}',
'--exec-prefix={exec_prefix}')).format(
android_host=android_host,
android_build=android_build,
prefix=sys_prefix,
exec_prefix=sys_exec_prefix)).split(' '), _env=env)
*(' '.join(self.configure_args).format(
android_host=android_host,
android_build=android_build,
prefix=sys_prefix,
exec_prefix=sys_exec_prefix)).split(' '), _env=env)

if not exists('python'):
shprint(sh.make, 'all', _env=env)
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