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