Skip to content

Commit 40ed06d

Browse files
authored
Python 3.8 support on Android (#2044)
* Did basic changes for Python3.8 support * Added python 3.8.1 patch to git * Added infrastructure for recipe-version-dependent patching * Added --fix-cortex-a8 removal patch for py3.8.1 * Fully set up Python3 patches to work with both 3.7.1 and 3.8.1 * Fixed bugs in is_version_{lt,gt} patching helpers * Replaced func call with pre-existing variable reference * Added some blank lines to make pep8 happy
1 parent 6a7e6ff commit 40ed06d

File tree

10 files changed

+109
-12
lines changed

10 files changed

+109
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected static ArrayList<String> getLibraries(File libsDir) {
4444
libsList.add("python3.5m");
4545
libsList.add("python3.6m");
4646
libsList.add("python3.7m");
47+
libsList.add("python3.8m");
4748
libsList.add("main");
4849
return libsList;
4950
}
@@ -64,7 +65,7 @@ public static void loadLibraries(File filesDir, File libsDir) {
6465
// load, and it has failed, give a more
6566
// general error
6667
Log.v(TAG, "Library loading error: " + e.getMessage());
67-
if (lib.startsWith("python3.7") && !foundPython) {
68+
if (lib.startsWith("python3.8") && !foundPython) {
6869
throw new java.lang.RuntimeException("Could not load any libpythonXXX.so");
6970
} else if (lib.startsWith("python")) {
7071
continue;
@@ -81,7 +82,7 @@ public static void loadLibraries(File filesDir, File libsDir) {
8182
} catch(UnsatisfiedLinkError e) {
8283
Log.v(TAG, "Failed to load _io.so or unicodedata.so...but that's okay.");
8384
}
84-
85+
8586
try {
8687
// System.loadLibrary("ctypes");
8788
System.load(filesDirPath + "/lib/python2.7/lib-dynload/_ctypes.so");
@@ -92,4 +93,3 @@ public static void loadLibraries(File filesDir, File libsDir) {
9293
Log.v(TAG, "Loaded everything!");
9394
}
9495
}
95-

pythonforandroid/patching.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from os import uname
2+
from distutils.version import LooseVersion
23

34

45
def check_all(*callables):
@@ -69,3 +70,20 @@ def is_ndk(ndk):
6970
def is_x(recipe, **kwargs):
7071
return recipe.ctx.ndk == ndk
7172
return is_x
73+
74+
75+
def is_version_gt(version):
76+
def is_x(recipe, **kwargs):
77+
return LooseVersion(recipe.version) > version
78+
79+
80+
def is_version_lt(version):
81+
def is_x(recipe, **kwargs):
82+
return LooseVersion(recipe.version) < version
83+
return is_x
84+
85+
86+
def version_starts_with(version):
87+
def is_x(recipe, **kwargs):
88+
return recipe.version.startswith(version)
89+
return is_x

pythonforandroid/python.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,19 @@ def build_arch(self, arch):
438438
if not exists('config.status'):
439439
shprint(sh.Command(join(recipe_build_dir, 'configure')))
440440

441-
# Create the Setup file. This copying from Setup.dist
442-
# seems to be the normal and expected procedure.
443-
shprint(sh.cp, join('Modules', 'Setup.dist'),
444-
join(build_dir, 'Modules', 'Setup'))
441+
# Create the Setup file. This copying from Setup.dist is
442+
# the normal and expected procedure before Python 3.8, but
443+
# after this the file with default options is already named "Setup"
444+
setup_dist_location = join('Modules', 'Setup.dist')
445+
if exists(setup_dist_location):
446+
shprint(sh.cp, setup_dist_location,
447+
join(build_dir, 'Modules', 'Setup'))
448+
else:
449+
# Check the expected file does exist
450+
setup_location = join('Modules', 'Setup')
451+
if not exists(setup_location):
452+
raise BuildInterruptingException(
453+
"Could not find Setup.dist or Setup in Python build")
445454

446455
shprint(sh.make, '-j', str(cpu_count()), '-C', build_dir)
447456

pythonforandroid/recipes/hostpython3/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Hostpython3Recipe(HostPythonRecipe):
99
Refactored into the new class
1010
:class:`~pythonforandroid.python.HostPythonRecipe`
1111
'''
12-
version = '3.7.1'
12+
version = '3.8.1'
1313
name = 'hostpython3'
1414
conflicts = ['hostpython2']
1515

pythonforandroid/recipes/python3/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sh
22
from pythonforandroid.python import GuestPythonRecipe
33
from pythonforandroid.recipe import Recipe
4+
from pythonforandroid.patching import version_starts_with
45

56

67
class Python3Recipe(GuestPythonRecipe):
@@ -18,15 +19,24 @@ class Python3Recipe(GuestPythonRecipe):
1819
:class:`~pythonforandroid.python.GuestPythonRecipe`
1920
'''
2021

21-
version = '3.7.1'
22+
version = '3.8.1'
2223
url = 'https://www.python.org/ftp/python/{version}/Python-{version}.tgz'
2324
name = 'python3'
2425

25-
patches = ['patches/fix-ctypes-util-find-library.patch',
26-
'patches/fix-zlib-version.patch']
26+
patches = [
27+
# Python 3.7.1
28+
('patches/py3.7.1_fix-ctypes-util-find-library.patch', version_starts_with("3.7")),
29+
('patches/py3.7.1_fix-zlib-version.patch', version_starts_with("3.7")),
30+
31+
# Python 3.8.1
32+
('patches/py3.8.1.patch', version_starts_with("3.8"))
33+
]
2734

2835
if sh.which('lld') is not None:
29-
patches = patches + ["patches/remove-fix-cortex-a8.patch"]
36+
patches = patches + [
37+
("patches/py3.7.1_remove-fix-cortex-a8.patch", version_starts_with("3.7")),
38+
("patches/py3.8.1_remove-fix-cortex-a8.patch", version_starts_with("3.8"))
39+
]
3040

3141
depends = ['hostpython3', 'sqlite3', 'openssl', 'libffi']
3242
conflicts = ['python2']
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
2+
index 97973bc..053c231 100644
3+
--- a/Lib/ctypes/util.py
4+
+++ b/Lib/ctypes/util.py
5+
@@ -67,6 +67,13 @@ if os.name == "nt":
6+
return fname
7+
return None
8+
9+
+# This patch overrides the find_library to look in the right places on
10+
+# Android
11+
+if True:
12+
+ from android._ctypes_library_finder import find_library as _find_lib
13+
+ def find_library(name):
14+
+ return _find_lib(name)
15+
+
16+
elif os.name == "posix" and sys.platform == "darwin":
17+
from ctypes.macholib.dyld import dyld_find as _dyld_find
18+
def find_library(name):
19+
diff --git a/configure b/configure
20+
index 0914e24..dd00812 100755
21+
--- a/configure
22+
+++ b/configure
23+
@@ -18673,4 +18673,3 @@ if test "$Py_OPT" = 'false' -a "$Py_DEBUG" != 'true'; then
24+
echo "" >&6
25+
echo "" >&6
26+
fi
27+
-
28+
diff --git a/setup.py b/setup.py
29+
index 20d7f35..af15cc2 100644
30+
--- a/setup.py
31+
+++ b/setup.py
32+
@@ -1501,7 +1501,9 @@ class PyBuildExt(build_ext):
33+
if zlib_inc is not None:
34+
zlib_h = zlib_inc[0] + '/zlib.h'
35+
version = '"0.0.0"'
36+
- version_req = '"1.1.3"'
37+
+ # version_req = '"1.1.3"'
38+
+ version_req = '"{}"'.format(
39+
+ os.environ.get('ZLIB_VERSION', '1.1.3'))
40+
if MACOS and is_macosx_sdk_path(zlib_h):
41+
zlib_h = os.path.join(macosx_sdk_root(), zlib_h[1:])
42+
with open(zlib_h) as fp:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/configure b/configure
2+
index 0914e24..7517168 100755
3+
--- a/configure
4+
+++ b/configure
5+
@@ -5642,7 +5642,7 @@ $as_echo_n "checking for the Android arm ABI... " >&6; }
6+
$as_echo "$_arm_arch" >&6; }
7+
if test "$_arm_arch" = 7; then
8+
BASECFLAGS="${BASECFLAGS} -mfloat-abi=softfp -mfpu=vfpv3-d16"
9+
- LDFLAGS="${LDFLAGS} -march=armv7-a -Wl,--fix-cortex-a8"
10+
+ LDFLAGS="${LDFLAGS} -march=armv7-a"
11+
fi
12+
else
13+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: not Android" >&5
14+
@@ -18673,4 +18673,3 @@ if test "$Py_OPT" = 'false' -a "$Py_DEBUG" != 'true'; then
15+
echo "" >&6
16+
echo "" >&6
17+
fi
18+
-

0 commit comments

Comments
 (0)