33from pythonforandroid .util import ensure_dir
44from os .path import join , exists , curdir , abspath
55from os import walk
6+ import os
67import glob
78import sh
89
1112
1213
1314class 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 ('\n sqlite3/*\n lib-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
0 commit comments