Skip to content

Commit 6017030

Browse files
committed
Made bootstrap collation work with python3 recipe
1 parent 6237346 commit 6017030

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

pythonforandroid/bootstrap.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,15 @@ def strip_libraries(self, arch):
254254
warning('Can\'t find strip in PATH...')
255255
return
256256
strip = sh.Command(strip)
257-
filens = shprint(sh.find, join(self.dist_dir, 'private'),
258-
join(self.dist_dir, 'libs'),
259-
'-iname', '*.so', _env=env).stdout.decode('utf-8')
257+
258+
if self.ctx.python_recipe.name == 'python2':
259+
filens = shprint(sh.find, join(self.dist_dir, 'private'),
260+
join(self.dist_dir, 'libs'),
261+
'-iname', '*.so', _env=env).stdout.decode('utf-8')
262+
else:
263+
filens = shprint(sh.find, join(self.dist_dir, '_python_bundle', '_python_bundle', 'modules'),
264+
join(self.dist_dir, 'libs'),
265+
'-iname', '*.so', _env=env).stdout.decode('utf-8')
260266
logger.info('Stripping libraries in private dir')
261267
for filen in filens.split('\n'):
262268
try:

pythonforandroid/bootstraps/sdl2/__init__.py

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pythonforandroid.util import ensure_dir
44
from os.path import join, exists, curdir, abspath
55
from os import walk
6+
import os
67
import glob
78
import sh
89

@@ -11,7 +12,7 @@
1112

1213

1314
class SDL2GradleBootstrap(Bootstrap):
14-
name = 'sdl2_gradle'
15+
name = 'sdl2'
1516

1617
recipe_depends = ['sdl2', ('python2', 'python3', 'python3crystax')]
1718

@@ -23,44 +24,49 @@ def run_distribute(self):
2324
from_crystax = self.ctx.python_recipe.from_crystax
2425
crystax_python_dir = join("crystax_python", "crystax_python")
2526

27+
python_bundle_dir = join('_python_bundle', '_python_bundle')
28+
2629
if len(self.ctx.archs) > 1:
2730
raise ValueError("SDL2/gradle support only one arch")
2831

2932
info("Copying SDL2/gradle build for {}".format(arch))
3033
shprint(sh.rm, "-rf", self.dist_dir)
3134
shprint(sh.cp, "-r", self.build_dir, self.dist_dir)
3235

33-
# either the build use environemnt variable (ANDROID_HOME)
36+
# either the build use environment variable (ANDROID_HOME)
3437
# or the local.properties if exists
3538
with current_directory(self.dist_dir):
3639
with open('local.properties', 'w') as fileh:
3740
fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir))
3841

42+
# TODO: Move the packaged python building to the python recipes
3943
with current_directory(self.dist_dir):
4044
info("Copying Python distribution")
4145

42-
if not exists("private") and not from_crystax:
46+
if 'python2' in self.ctx.recipe_build_order:
4347
ensure_dir("private")
44-
if not exists("crystax_python") and from_crystax:
48+
elif not exists("crystax_python") and from_crystax:
4549
ensure_dir(crystax_python_dir)
50+
elif 'python3' in self.ctx.recipe_build_order:
51+
ensure_dir(python_bundle_dir)
4652

4753
hostpython = sh.Command(self.ctx.hostpython)
48-
if not from_crystax:
54+
if self.ctx.python_recipe.name == 'python2':
4955
try:
5056
shprint(hostpython, '-OO', '-m', 'compileall',
5157
python_install_dir,
5258
_tail=10, _filterout="^Listing")
5359
except sh.ErrorReturnCode:
5460
pass
55-
if not exists('python-install'):
61+
if 'python2' in self.ctx.recipe_build_order and not exists('python-install'):
5662
shprint(
5763
sh.cp, '-a', python_install_dir, './python-install')
5864

5965
self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
6066
self.distribute_javaclasses(self.ctx.javaclass_dir,
6167
dest_dir=join("src", "main", "java"))
6268

63-
if not from_crystax:
69+
if self.ctx.python_recipe.name == 'python2':
6470
info("Filling private directory")
6571
if not exists(join("private", "lib")):
6672
info("private/lib does not exist, making")
@@ -100,7 +106,55 @@ def run_distribute(self):
100106
shprint(sh.rm, '-f', filename)
101107
shprint(sh.rm, '-rf', 'config/python.o')
102108

103-
else: # Python *is* loaded from crystax
109+
elif self.ctx.python_recipe.name == 'python3':
110+
ndk_dir = self.ctx.ndk_dir
111+
py_recipe = self.ctx.python_recipe
112+
113+
## Build the python bundle:
114+
115+
# Bundle compiled python modules to a folder
116+
modules_dir = join(python_bundle_dir, 'modules')
117+
ensure_dir(modules_dir)
118+
119+
modules_build_dir = join(
120+
self.ctx.python_recipe.get_build_dir(arch.arch),
121+
'android-build',
122+
'lib.linux-arm-3.7')
123+
module_filens = (glob.glob(join(modules_build_dir, '*.so')) +
124+
glob.glob(join(modules_build_dir, '*.py')))
125+
for filen in module_filens:
126+
shprint(sh.cp, filen, modules_dir)
127+
128+
# zip up the standard library
129+
stdlib_zip = join(self.dist_dir, python_bundle_dir, 'stdlib.zip')
130+
with current_directory(
131+
join(self.ctx.python_recipe.get_build_dir(arch.arch),
132+
'Lib')):
133+
shprint(sh.zip, '-r', stdlib_zip, *os.listdir())
134+
135+
# copy the site-packages into place
136+
shprint(sh.cp, '-r', self.ctx.get_python_install_dir(),
137+
join(python_bundle_dir, 'site-packages'))
138+
139+
# copy the python .so files into place
140+
python_build_dir = join(py_recipe.get_build_dir(arch.arch),
141+
'android-build')
142+
shprint(sh.cp, join(python_build_dir, 'libpython3.7m.so'),
143+
'libs/{}'.format(arch.arch))
144+
shprint(sh.cp, join(python_build_dir, 'libpython3.7m.so.1.0'),
145+
'libs/{}'.format(arch.arch))
146+
147+
info('Renaming .so files to reflect cross-compile')
148+
site_packages_dir = join(python_bundle_dir, 'site-packages')
149+
py_so_files = shprint(sh.find, site_packages_dir, '-iname', '*.so')
150+
filens = py_so_files.stdout.decode('utf-8').split('\n')[:-1]
151+
for filen in filens:
152+
parts = filen.split('.')
153+
if len(parts) <= 2:
154+
continue
155+
shprint(sh.mv, filen, parts[0] + '.so')
156+
157+
elif self.ctx.python_recipe.from_crystax: # Python *is* loaded from crystax
104158
ndk_dir = self.ctx.ndk_dir
105159
py_recipe = self.ctx.python_recipe
106160
python_dir = join(ndk_dir, 'sources', 'python',
@@ -129,7 +183,7 @@ def run_distribute(self):
129183
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')
130184

131185
self.strip_libraries(arch)
132-
self.fry_eggs(site_packages_dir)
186+
# self.fry_eggs(site_packages_dir) # TODO uncomment this and make it work with python3
133187
super(SDL2GradleBootstrap, self).run_distribute()
134188

135189

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected static ArrayList<String> getLibraries(File filesDir) {
4545
addLibraryIfExists(libsList, "crypto.*", libsDir);
4646
libsList.add("python2.7");
4747
libsList.add("python3.5m");
48+
libsList.add("python3.6m");
49+
libsList.add("python3.7m");
4850
libsList.add("main");
4951
return libsList;
5052
}
@@ -66,7 +68,7 @@ public static void loadLibraries(File filesDir) {
6668
// load, and it has failed, give a more
6769
// general error
6870
Log.v(TAG, "Library loading error: " + e.getMessage());
69-
if (lib.startsWith("python3.6") && !foundPython) {
71+
if (lib.startsWith("python3.7") && !foundPython) {
7072
throw new java.lang.RuntimeException("Could not load any libpythonXXX.so");
7173
} else if (lib.startsWith("python")) {
7274
continue;

0 commit comments

Comments
 (0)