Skip to content

Added support for Python 3.6 #1011

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

Merged
merged 2 commits into from
Mar 3, 2017
Merged
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 @@ -16,24 +16,30 @@ protected static String[] getLibraries() {
"SDL2_ttf",
"python2.7",
"python3.5m",
"python3.6m",
"main"
};
}

public static void loadLibraries(File filesDir) {

String filesDirPath = filesDir.getAbsolutePath();
boolean skippedPython = false;
boolean foundPython = false;

for (String lib : getLibraries()) {
try {
System.loadLibrary(lib);
if (lib.startsWith("python")) {
foundPython = true;
}
} catch(UnsatisfiedLinkError e) {
if (lib.startsWith("python") && !skippedPython) {
skippedPython = true;
continue;
// If this is the last possible libpython
// load, and it has failed, give a more
// general error
if (lib.startsWith("python3.6") && !foundPython) {
throw new java.lang.RuntimeException("Could not load any libpythonXXX.so");
}
throw e;
continue;
}
}

Expand All @@ -52,5 +58,5 @@ public static void loadLibraries(File filesDir) {
}

Log.v(TAG, "Loaded everything!");
}
}
}
9 changes: 4 additions & 5 deletions pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,6 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):

with current_directory(self.get_build_dir(arch.arch)):
hostpython = sh.Command(self.hostpython_location)
# hostpython = sh.Command('python3.5')


if self.ctx.python_recipe.from_crystax:
Expand Down Expand Up @@ -986,15 +985,13 @@ def build_cython_components(self, arch):
site_packages_dirs = command(
'-c', 'import site; print("\\n".join(site.getsitepackages()))')
site_packages_dirs = site_packages_dirs.stdout.decode('utf-8').split('\n')
# env['PYTHONPATH'] = '/usr/lib/python3.5/site-packages/:/usr/lib/python3.5'
if 'PYTHONPATH' in env:
env['PYTHONPATH'] = env + ':{}'.format(':'.join(site_packages_dirs))
else:
env['PYTHONPATH'] = ':'.join(site_packages_dirs)

with current_directory(self.get_build_dir(arch.arch)):
hostpython = sh.Command(self.ctx.hostpython)
# hostpython = sh.Command('python3.5')
shprint(hostpython, '-c', 'import sys; print(sys.path)', _env=env)
print('cwd is', realpath(curdir))
info('Trying first build of {} to get cython files: this is '
Expand Down Expand Up @@ -1089,12 +1086,14 @@ def get_recipe_env(self, arch, with_flags_in_cc=True):
self.ctx.python_recipe.version, 'include',
'python')) + env['CFLAGS']

# Temporarily hardcode the -lpython3.5 as this does not
# Temporarily hardcode the -lpython3.x as this does not
# get applied automatically in some environments. This
# will need generalising, along with the other hardcoded
# py3.5 references, to support other python3 or crystax
# python versions.
env['LDFLAGS'] = env['LDFLAGS'] + ' -lpython3.5m'
python3_version = self.ctx.python_recipe.version
python3_version = '.'.join(python3_version.split('.')[:2])
env['LDFLAGS'] = env['LDFLAGS'] + ' -lpython{}m'.format(python3_version)

return env

Expand Down
58 changes: 46 additions & 12 deletions pythonforandroid/recipes/python3crystax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

from pythonforandroid.recipe import TargetPythonRecipe
from pythonforandroid.toolchain import shprint, current_directory, ArchARM
from pythonforandroid.logger import info
from pythonforandroid.util import ensure_dir
from pythonforandroid.logger import info, error
from pythonforandroid.util import ensure_dir, temp_directory
from os.path import exists, join
from os import uname
import glob
import sh

prebuilt_download_locations = {
'3.6': ('https://github.com/inclement/crystax_python_builds/'
'releases/download/0.1/crystax_python_3.6_armeabi_armeabi-v7a.tar.gz')}

class Python3Recipe(TargetPythonRecipe):
version = '3.5'
url = ''
Expand All @@ -24,19 +27,50 @@ def get_dir_name(self):
return name

def build_arch(self, arch):
info('Extracting CrystaX python3 from NDK package')
# We don't have to actually build anything as CrystaX comes
# with the necessary modules. They are included by modifying
# the Android.mk in the jni folder.

# If the Python version to be used is not prebuilt with the CrystaX
# NDK, we do have to download it.

crystax_python_dir = join(self.ctx.ndk_dir, 'sources', 'python')
if not exists(join(crystax_python_dir, self.version)):
info(('The NDK does not have a prebuilt Python {}, trying '
'to obtain one.').format(self.version))

if self.version not in prebuilt_download_locations:
error(('No prebuilt version for Python {} could be found, '
'the built cannot continue.'))
exit(1)

with temp_directory() as td:
self.download_file(prebuilt_download_locations[self.version],
join(td, 'downloaded_python'))
shprint(sh.tar, 'xf', join(td, 'downloaded_python'),
'--directory', crystax_python_dir)

if not exists(join(crystax_python_dir, self.version)):
error(('Something went wrong, the directory at {} should '
'have been created but does not exist.').format(
join(crystax_python_dir, self.version)))

if not exists(join(
crystax_python_dir, self.version, 'libs', arch.arch)):
error(('The prebuilt Python for version {} does not contain '
'binaries for your chosen architecture "{}".').format(
self.version, arch.arch))
exit(1)

# TODO: We should have an option to build a new Python. This
# would also allow linking to openssl and sqlite from CrystaX.

dirn = self.ctx.get_python_install_dir()
ensure_dir(dirn)

# Instead of using a locally built hostpython, we use the
# user's Python for now. They must have the right version
# available. Using e.g. pyenv makes this easy.
self.ctx.hostpython = 'python{}'.format(self.version)
# ensure_dir(join(dirn, 'lib'))
# ensure_dir(join(dirn, 'lib', 'python{}'.format(self.version),
# 'site-packages'))

# ndk_dir = self.ctx.ndk_dir
# sh.cp('-r', '/home/asandy/kivytest/crystax_stdlib', join(dirn, 'lib', 'python3.5'))
# sh.cp('-r', '/home/asandy/android/crystax-ndk-10.3.0/sources/python/3.5/libs/armeabi/modules', join(dirn, 'lib', 'python3.5', 'lib-dynload'))
# ensure_dir(join(dirn, 'lib', 'site-packages'))

recipe = Python3Recipe()