|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +Bootstrap for SDL2, using gradlew for building |
| 4 | +============================================== |
| 5 | +
|
| 6 | +.. warning:: Experimental |
| 7 | +
|
| 8 | +Good point: |
| 9 | +- automatic dependencies management |
| 10 | +- no need to unpack aar |
| 11 | +
|
| 12 | +TODO: |
| 13 | +- test with crystax |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +from pythonforandroid.toolchain import ( |
| 18 | + Bootstrap, shprint, current_directory, info, warning, ArchARM, info_main) |
| 19 | +from pythonforandroid.utils import ensure_dir |
| 20 | +from os.path import join, exists, curdir, abspath |
| 21 | +from os import walk, makedirs |
| 22 | +import glob |
| 23 | +import sh |
| 24 | + |
| 25 | + |
| 26 | +EXCLUDE_EXTS = (".py", ".pyc", ".so.o", ".so.a", ".so.libs", ".pyx") |
| 27 | + |
| 28 | + |
| 29 | +class SDL2GradleBootstrap(Bootstrap): |
| 30 | + name = 'sdl2_gradle' |
| 31 | + |
| 32 | + recipe_depends = ['sdl2', ('python2', 'python3crystax')] |
| 33 | + |
| 34 | + def run_distribute(self): |
| 35 | + info_main("# Creating Android project ({})".format(self.name)) |
| 36 | + |
| 37 | + arch = self.ctx.archs[0] |
| 38 | + python_install_dir = self.ctx.get_python_install_dir() |
| 39 | + from_crystax = self.ctx.python_recipe.from_crystax |
| 40 | + crystax_python_dir = join("crystax_python", "crystax_python") |
| 41 | + |
| 42 | + if len(self.ctx.archs) > 1: |
| 43 | + raise ValueError("SDL2/gradle support only one arch") |
| 44 | + |
| 45 | + info("Copying SDL2/gradle build for {}".format(arch)) |
| 46 | + shprint(sh.rm, "-rf", self.dist_dir) |
| 47 | + shprint(sh.cp, "-r", self.build_dir, self.dist_dir) |
| 48 | + |
| 49 | + # either the build use environemnt variable (ANDROID_HOME) |
| 50 | + # or the local.properties if exists |
| 51 | + with current_directory(self.dist_dir): |
| 52 | + with open('local.properties', 'w') as fileh: |
| 53 | + fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir)) |
| 54 | + |
| 55 | + with current_directory(self.dist_dir): |
| 56 | + info("Copying Python distribution") |
| 57 | + |
| 58 | + if not exists("private") and not from_crystax: |
| 59 | + ensure_dir("private") |
| 60 | + if not exists("crystax_python") and from_crystax: |
| 61 | + ensure_dir(crystax_python_dir) |
| 62 | + |
| 63 | + hostpython = sh.Command(self.ctx.hostpython) |
| 64 | + if not from_crystax: |
| 65 | + try: |
| 66 | + shprint(hostpython, '-OO', '-m', 'compileall', |
| 67 | + python_install_dir, |
| 68 | + _tail=10, _filterout="^Listing") |
| 69 | + except sh.ErrorReturnCode: |
| 70 | + pass |
| 71 | + if not exists('python-install'): |
| 72 | + shprint( |
| 73 | + sh.cp, '-a', python_install_dir, './python-install') |
| 74 | + |
| 75 | + self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)], |
| 76 | + dest_dir=join("src", "main", "jniLibs")) |
| 77 | + self.distribute_javaclasses(self.ctx.javaclass_dir, |
| 78 | + dest_dir=join("src", "main", "java")) |
| 79 | + |
| 80 | + if not from_crystax: |
| 81 | + info("Filling private directory") |
| 82 | + if not exists(join("private", "lib")): |
| 83 | + info("private/lib does not exist, making") |
| 84 | + shprint(sh.cp, "-a", |
| 85 | + join("python-install", "lib"), "private") |
| 86 | + shprint(sh.mkdir, "-p", |
| 87 | + join("private", "include", "python2.7")) |
| 88 | + |
| 89 | + # AND: Copylibs stuff should go here |
| 90 | + libpymodules_fn = join("libs", arch.arch, "libpymodules.so") |
| 91 | + if exists(libpymodules_fn): |
| 92 | + shprint(sh.mv, libpymodules_fn, 'private/') |
| 93 | + shprint(sh.cp, |
| 94 | + join('python-install', 'include', |
| 95 | + 'python2.7', 'pyconfig.h'), |
| 96 | + join('private', 'include', 'python2.7/')) |
| 97 | + |
| 98 | + info('Removing some unwanted files') |
| 99 | + shprint(sh.rm, '-f', join('private', 'lib', 'libpython2.7.so')) |
| 100 | + shprint(sh.rm, '-rf', join('private', 'lib', 'pkgconfig')) |
| 101 | + |
| 102 | + libdir = join(self.dist_dir, 'private', 'lib', 'python2.7') |
| 103 | + site_packages_dir = join(libdir, 'site-packages') |
| 104 | + with current_directory(libdir): |
| 105 | + removes = [] |
| 106 | + for dirname, root, filenames in walk("."): |
| 107 | + for filename in filenames: |
| 108 | + for suffix in EXCLUDE_EXTS: |
| 109 | + if filename.endswith(suffix): |
| 110 | + removes.append(filename) |
| 111 | + shprint(sh.rm, '-f', *removes) |
| 112 | + |
| 113 | + info('Deleting some other stuff not used on android') |
| 114 | + # To quote the original distribute.sh, 'well...' |
| 115 | + shprint(sh.rm, '-rf', 'lib2to3') |
| 116 | + shprint(sh.rm, '-rf', 'idlelib') |
| 117 | + for filename in glob.glob('config/libpython*.a'): |
| 118 | + shprint(sh.rm, '-f', filename) |
| 119 | + shprint(sh.rm, '-rf', 'config/python.o') |
| 120 | + |
| 121 | + else: # Python *is* loaded from crystax |
| 122 | + ndk_dir = self.ctx.ndk_dir |
| 123 | + py_recipe = self.ctx.python_recipe |
| 124 | + python_dir = join(ndk_dir, 'sources', 'python', |
| 125 | + py_recipe.version, 'libs', arch.arch) |
| 126 | + shprint(sh.cp, '-r', join(python_dir, |
| 127 | + 'stdlib.zip'), crystax_python_dir) |
| 128 | + shprint(sh.cp, '-r', join(python_dir, |
| 129 | + 'modules'), crystax_python_dir) |
| 130 | + shprint(sh.cp, '-r', self.ctx.get_python_install_dir(), |
| 131 | + crystax_python_dir) |
| 132 | + |
| 133 | + info('Renaming .so files to reflect cross-compile') |
| 134 | + site_packages_dir = join(crystax_python_dir, "site-packages") |
| 135 | + find_ret = shprint( |
| 136 | + sh.find, site_packages_dir, '-iname', '*.so') |
| 137 | + filenames = find_ret.stdout.decode('utf-8').split('\n')[:-1] |
| 138 | + for filename in filenames: |
| 139 | + parts = filename.split('.') |
| 140 | + if len(parts) <= 2: |
| 141 | + continue |
| 142 | + shprint(sh.mv, filename, filename.split('.')[0] + '.so') |
| 143 | + site_packages_dir = join(abspath(curdir), |
| 144 | + site_packages_dir) |
| 145 | + if 'sqlite3' not in self.ctx.recipe_build_order: |
| 146 | + with open('blacklist.txt', 'a') as fileh: |
| 147 | + fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n') |
| 148 | + |
| 149 | + self.strip_libraries(arch) |
| 150 | + self.fry_eggs(site_packages_dir) |
| 151 | + super(SDL2GradleBootstrap, self).run_distribute() |
| 152 | + |
| 153 | + |
| 154 | +bootstrap = SDL2GradleBootstrap() |
0 commit comments