|
| 1 | +'''A dummy recipe to build my custom lib |
| 2 | +''' |
| 3 | +from os.path import basename, dirname, exists, isdir, isfile, join, realpath, split |
| 4 | + |
| 5 | +import fnmatch |
| 6 | +from os import listdir, unlink, environ, curdir, walk |
| 7 | +import sh |
| 8 | +from pythonforandroid.util import ( |
| 9 | + current_directory, ensure_dir, BuildInterruptingException, rmdir, move, |
| 10 | + touch) |
| 11 | +from pythonforandroid.recipe import Recipe, CythonRecipe, IncludedFilesBehaviour, CompiledComponentsPythonRecipe |
| 12 | +from pythonforandroid.logger import ( |
| 13 | + logger, info, warning, debug, shprint, info_main) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +class UtilsRecipe(CythonRecipe): |
| 19 | + version = '0.1.7' |
| 20 | + url = 'https://github.com/ssppkenny/myutils/archive/refs/tags/0.1.7.zip' |
| 21 | + site_packages_name = 'utils' |
| 22 | + depends = ['setuptools', 'cython'] |
| 23 | + install_in_hostpython = False |
| 24 | + call_hostpython_via_targetpython = False |
| 25 | + |
| 26 | + |
| 27 | + def build_arch(self, arch): |
| 28 | + info("build_arch utils") |
| 29 | + '''Build any cython components, then install the Python module by |
| 30 | + calling setup.py install with the target Python dir. |
| 31 | + ''' |
| 32 | + Recipe.build_arch(self, arch) |
| 33 | + self.build_cython_components(arch) |
| 34 | + self.install_python_package(arch) |
| 35 | + |
| 36 | + def build_cython_components(self, arch): |
| 37 | + info("build_cython_components utils") |
| 38 | + info('Cythonizing anything necessary in {}'.format(self.name)) |
| 39 | + |
| 40 | + env = self.get_recipe_env(arch) |
| 41 | + |
| 42 | + with current_directory(self.get_build_dir(arch.arch)): |
| 43 | + hostpython = sh.Command(self.ctx.hostpython) |
| 44 | + shprint(hostpython, '-c', 'import sys; print(sys.path)', _env=env) |
| 45 | + debug('cwd is {}'.format(realpath(curdir))) |
| 46 | + info('Trying first build of {} to get cython files: this is ' |
| 47 | + 'expected to fail'.format(self.name)) |
| 48 | + |
| 49 | + manually_cythonise = False |
| 50 | + try: |
| 51 | + shprint(hostpython, 'setup.py', 'build_ext', '-v', _env=env, |
| 52 | + *self.setup_extra_args) |
| 53 | + except sh.ErrorReturnCode_1: |
| 54 | + print() |
| 55 | + info('{} first build failed (as expected)'.format(self.name)) |
| 56 | + manually_cythonise = True |
| 57 | + |
| 58 | + if manually_cythonise: |
| 59 | + self.cythonize_build(env=env) |
| 60 | + shprint(hostpython, 'setup.py', 'build_ext', '-v', _env=env, |
| 61 | + _tail=20, _critical=True, *self.setup_extra_args) |
| 62 | + else: |
| 63 | + info('First build appeared to complete correctly, skipping manual' |
| 64 | + 'cythonising.') |
| 65 | + |
| 66 | + if not self.ctx.with_debug_symbols: |
| 67 | + self.strip_object_files(arch, env) |
| 68 | + |
| 69 | + def strip_object_files(self, arch, env, build_dir=None): |
| 70 | + if build_dir is None: |
| 71 | + build_dir = self.get_build_dir(arch.arch) |
| 72 | + with current_directory(build_dir): |
| 73 | + info('Stripping object files') |
| 74 | + shprint(sh.find, '.', '-iname', '*.so', '-exec', |
| 75 | + '/usr/bin/echo', '{}', ';', _env=env) |
| 76 | + shprint(sh.find, '.', '-iname', '*.so', '-exec', |
| 77 | + env['STRIP'].split(' ')[0], '--strip-unneeded', |
| 78 | + # '/usr/bin/strip', '--strip-unneeded', |
| 79 | + '{}', ';', _env=env) |
| 80 | + |
| 81 | + def cythonize_file(self, env, build_dir, filename): |
| 82 | + info("cythonize file utils " + filename) |
| 83 | + short_filename = filename |
| 84 | + if filename.startswith(build_dir): |
| 85 | + short_filename = filename[len(build_dir) + 1:] |
| 86 | + info(u"Cythonize {}".format(short_filename)) |
| 87 | + cyenv = env.copy() |
| 88 | + if 'CYTHONPATH' in cyenv: |
| 89 | + cyenv['PYTHONPATH'] = cyenv['CYTHONPATH'] |
| 90 | + elif 'PYTHONPATH' in cyenv: |
| 91 | + del cyenv['PYTHONPATH'] |
| 92 | + if 'PYTHONNOUSERSITE' in cyenv: |
| 93 | + cyenv.pop('PYTHONNOUSERSITE') |
| 94 | + python_command = sh.Command("python{}".format( |
| 95 | + self.ctx.python_recipe.major_minor_version_string.split(".")[0] |
| 96 | + )) |
| 97 | + shprint(python_command, "-c" |
| 98 | + "import sys; from Cython.Compiler.Main import setuptools_main; sys.exit(setuptools_main());", |
| 99 | + filename, *self.cython_args, _env=cyenv) |
| 100 | + |
| 101 | + def cythonize_build(self, env, build_dir="."): |
| 102 | + info("cythonize_build utils") |
| 103 | + if not self.cythonize: |
| 104 | + info('Running cython cancelled per recipe setting') |
| 105 | + return |
| 106 | + info('Running cython where appropriate') |
| 107 | + for root, dirnames, filenames in walk("."): |
| 108 | + for filename in fnmatch.filter(filenames, "*.pyx"): |
| 109 | + self.cythonize_file(env, build_dir, join(root, filename)) |
| 110 | + |
| 111 | + def get_recipe_env(self, arch, with_flags_in_cc=True): |
| 112 | + info("get_recipe_env utils") |
| 113 | + env = super().get_recipe_env(arch, with_flags_in_cc) |
| 114 | + env['LDFLAGS'] = env['LDFLAGS'] + ' -L{} '.format( |
| 115 | + self.ctx.get_libs_dir(arch.arch) + |
| 116 | + ' -L{} '.format(self.ctx.libs_dir) + |
| 117 | + ' -L{}'.format(join(self.ctx.bootstrap.build_dir, 'obj', 'local', |
| 118 | + arch.arch))) |
| 119 | + |
| 120 | + env['LDSHARED'] = env['CC'] + ' -shared' |
| 121 | + # shprint(sh.whereis, env['LDSHARED'], _env=env) |
| 122 | + env['LIBLINK'] = 'NOTNONE' |
| 123 | + if self.ctx.copy_libs: |
| 124 | + env['COPYLIBS'] = '1' |
| 125 | + |
| 126 | + # Every recipe uses its own liblink path, object files are |
| 127 | + # collected and biglinked later |
| 128 | + liblink_path = join(self.get_build_container_dir(arch.arch), |
| 129 | + 'objects_{}'.format(self.name)) |
| 130 | + env['LIBLINK_PATH'] = liblink_path |
| 131 | + ensure_dir(liblink_path) |
| 132 | + ## env['LDFLAGS'] += ' -framework CoreFoundation -framework CoreGraphics' |
| 133 | + |
| 134 | + return env |
| 135 | + |
| 136 | + |
| 137 | +recipe = UtilsRecipe() |
0 commit comments