3
3
from pythonforandroid .util import ensure_dir
4
4
from os .path import join , exists , curdir , abspath
5
5
from os import walk
6
+ import os
6
7
import glob
7
8
import sh
8
9
11
12
12
13
13
14
class SDL2GradleBootstrap (Bootstrap ):
14
- name = 'sdl2_gradle '
15
+ name = 'sdl2 '
15
16
16
17
recipe_depends = ['sdl2' , ('python2' , 'python3' , 'python3crystax' )]
17
18
@@ -23,44 +24,49 @@ def run_distribute(self):
23
24
from_crystax = self .ctx .python_recipe .from_crystax
24
25
crystax_python_dir = join ("crystax_python" , "crystax_python" )
25
26
27
+ python_bundle_dir = join ('_python_bundle' , '_python_bundle' )
28
+
26
29
if len (self .ctx .archs ) > 1 :
27
30
raise ValueError ("SDL2/gradle support only one arch" )
28
31
29
32
info ("Copying SDL2/gradle build for {}" .format (arch ))
30
33
shprint (sh .rm , "-rf" , self .dist_dir )
31
34
shprint (sh .cp , "-r" , self .build_dir , self .dist_dir )
32
35
33
- # either the build use environemnt variable (ANDROID_HOME)
36
+ # either the build use environment variable (ANDROID_HOME)
34
37
# or the local.properties if exists
35
38
with current_directory (self .dist_dir ):
36
39
with open ('local.properties' , 'w' ) as fileh :
37
40
fileh .write ('sdk.dir={}' .format (self .ctx .sdk_dir ))
38
41
42
+ # TODO: Move the packaged python building to the python recipes
39
43
with current_directory (self .dist_dir ):
40
44
info ("Copying Python distribution" )
41
45
42
- if not exists ( "private" ) and not from_crystax :
46
+ if 'python2' in self . ctx . recipe_build_order :
43
47
ensure_dir ("private" )
44
- if not exists ("crystax_python" ) and from_crystax :
48
+ elif not exists ("crystax_python" ) and from_crystax :
45
49
ensure_dir (crystax_python_dir )
50
+ elif 'python3' in self .ctx .recipe_build_order :
51
+ ensure_dir (python_bundle_dir )
46
52
47
53
hostpython = sh .Command (self .ctx .hostpython )
48
- if not from_crystax :
54
+ if self . ctx . python_recipe . name == 'python2' :
49
55
try :
50
56
shprint (hostpython , '-OO' , '-m' , 'compileall' ,
51
57
python_install_dir ,
52
58
_tail = 10 , _filterout = "^Listing" )
53
59
except sh .ErrorReturnCode :
54
60
pass
55
- if not exists ('python-install' ):
61
+ if 'python2' in self . ctx . recipe_build_order and not exists ('python-install' ):
56
62
shprint (
57
63
sh .cp , '-a' , python_install_dir , './python-install' )
58
64
59
65
self .distribute_libs (arch , [self .ctx .get_libs_dir (arch .arch )])
60
66
self .distribute_javaclasses (self .ctx .javaclass_dir ,
61
67
dest_dir = join ("src" , "main" , "java" ))
62
68
63
- if not from_crystax :
69
+ if self . ctx . python_recipe . name == 'python2' :
64
70
info ("Filling private directory" )
65
71
if not exists (join ("private" , "lib" )):
66
72
info ("private/lib does not exist, making" )
@@ -100,7 +106,55 @@ def run_distribute(self):
100
106
shprint (sh .rm , '-f' , filename )
101
107
shprint (sh .rm , '-rf' , 'config/python.o' )
102
108
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
104
158
ndk_dir = self .ctx .ndk_dir
105
159
py_recipe = self .ctx .python_recipe
106
160
python_dir = join (ndk_dir , 'sources' , 'python' ,
@@ -129,7 +183,7 @@ def run_distribute(self):
129
183
fileh .write ('\n sqlite3/*\n lib-dynload/_sqlite3.so\n ' )
130
184
131
185
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
133
187
super (SDL2GradleBootstrap , self ).run_distribute ()
134
188
135
189
0 commit comments