Skip to content

Commit b2e5bc3

Browse files
authored
Merge pull request kivy#1011 from inclement/python3.6_support
Added support for Python 3.6
2 parents 3c2825b + e5f23a5 commit b2e5bc3

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonUtil.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,30 @@ protected static String[] getLibraries() {
1616
"SDL2_ttf",
1717
"python2.7",
1818
"python3.5m",
19+
"python3.6m",
1920
"main"
2021
};
2122
}
2223

2324
public static void loadLibraries(File filesDir) {
2425

2526
String filesDirPath = filesDir.getAbsolutePath();
26-
boolean skippedPython = false;
27+
boolean foundPython = false;
2728

2829
for (String lib : getLibraries()) {
2930
try {
3031
System.loadLibrary(lib);
32+
if (lib.startsWith("python")) {
33+
foundPython = true;
34+
}
3135
} catch(UnsatisfiedLinkError e) {
32-
if (lib.startsWith("python") && !skippedPython) {
33-
skippedPython = true;
34-
continue;
36+
// If this is the last possible libpython
37+
// load, and it has failed, give a more
38+
// general error
39+
if (lib.startsWith("python3.6") && !foundPython) {
40+
throw new java.lang.RuntimeException("Could not load any libpythonXXX.so");
3541
}
36-
throw e;
42+
continue;
3743
}
3844
}
3945

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

5460
Log.v(TAG, "Loaded everything!");
55-
}
61+
}
5662
}

pythonforandroid/recipe.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,6 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
818818

819819
with current_directory(self.get_build_dir(arch.arch)):
820820
hostpython = sh.Command(self.hostpython_location)
821-
# hostpython = sh.Command('python3.5')
822821

823822

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

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

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

10991098
return env
11001099

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11

22
from pythonforandroid.recipe import TargetPythonRecipe
33
from pythonforandroid.toolchain import shprint, current_directory, ArchARM
4-
from pythonforandroid.logger import info
5-
from pythonforandroid.util import ensure_dir
4+
from pythonforandroid.logger import info, error
5+
from pythonforandroid.util import ensure_dir, temp_directory
66
from os.path import exists, join
7-
from os import uname
87
import glob
98
import sh
109

10+
prebuilt_download_locations = {
11+
'3.6': ('https://github.com/inclement/crystax_python_builds/'
12+
'releases/download/0.1/crystax_python_3.6_armeabi_armeabi-v7a.tar.gz')}
13+
1114
class Python3Recipe(TargetPythonRecipe):
1215
version = '3.5'
1316
url = ''
@@ -24,19 +27,50 @@ def get_dir_name(self):
2427
return name
2528

2629
def build_arch(self, arch):
27-
info('Extracting CrystaX python3 from NDK package')
30+
# We don't have to actually build anything as CrystaX comes
31+
# with the necessary modules. They are included by modifying
32+
# the Android.mk in the jni folder.
33+
34+
# If the Python version to be used is not prebuilt with the CrystaX
35+
# NDK, we do have to download it.
36+
37+
crystax_python_dir = join(self.ctx.ndk_dir, 'sources', 'python')
38+
if not exists(join(crystax_python_dir, self.version)):
39+
info(('The NDK does not have a prebuilt Python {}, trying '
40+
'to obtain one.').format(self.version))
41+
42+
if self.version not in prebuilt_download_locations:
43+
error(('No prebuilt version for Python {} could be found, '
44+
'the built cannot continue.'))
45+
exit(1)
46+
47+
with temp_directory() as td:
48+
self.download_file(prebuilt_download_locations[self.version],
49+
join(td, 'downloaded_python'))
50+
shprint(sh.tar, 'xf', join(td, 'downloaded_python'),
51+
'--directory', crystax_python_dir)
52+
53+
if not exists(join(crystax_python_dir, self.version)):
54+
error(('Something went wrong, the directory at {} should '
55+
'have been created but does not exist.').format(
56+
join(crystax_python_dir, self.version)))
57+
58+
if not exists(join(
59+
crystax_python_dir, self.version, 'libs', arch.arch)):
60+
error(('The prebuilt Python for version {} does not contain '
61+
'binaries for your chosen architecture "{}".').format(
62+
self.version, arch.arch))
63+
exit(1)
64+
65+
# TODO: We should have an option to build a new Python. This
66+
# would also allow linking to openssl and sqlite from CrystaX.
2867

2968
dirn = self.ctx.get_python_install_dir()
3069
ensure_dir(dirn)
3170

71+
# Instead of using a locally built hostpython, we use the
72+
# user's Python for now. They must have the right version
73+
# available. Using e.g. pyenv makes this easy.
3274
self.ctx.hostpython = 'python{}'.format(self.version)
33-
# ensure_dir(join(dirn, 'lib'))
34-
# ensure_dir(join(dirn, 'lib', 'python{}'.format(self.version),
35-
# 'site-packages'))
36-
37-
# ndk_dir = self.ctx.ndk_dir
38-
# sh.cp('-r', '/home/asandy/kivytest/crystax_stdlib', join(dirn, 'lib', 'python3.5'))
39-
# 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'))
40-
# ensure_dir(join(dirn, 'lib', 'site-packages'))
4175

4276
recipe = Python3Recipe()

0 commit comments

Comments
 (0)