|
| 1 | +from pythonforandroid.recipe import PythonRecipe, current_directory,\ |
| 2 | + shprint, info_main, warning |
| 3 | +from pythonforandroid.logger import error |
| 4 | +from os.path import join |
| 5 | +import sh |
| 6 | + |
| 7 | + |
| 8 | +class TFLiteRuntimeRecipe(PythonRecipe): |
| 9 | + |
| 10 | + ############################################################### |
| 11 | + # |
| 12 | + # tflite-runtime README: |
| 13 | + # https://github.com/Android-for-Python/c4k_tflite_example/blob/main/README.md |
| 14 | + # |
| 15 | + # Recipe build references: |
| 16 | + # https://developer.android.com/ndk/guides/cmake |
| 17 | + # https://developer.android.com/ndk/guides/cpu-arm-neon#cmake |
| 18 | + # https://www.tensorflow.org/lite/guide/build_cmake |
| 19 | + # https://www.tensorflow.org/lite/guide/build_cmake_arm |
| 20 | + # |
| 21 | + # Tested using cmake 3.16.3 probably requires cmake >= 3.13 |
| 22 | + # |
| 23 | + # THIS RECIPE DOES NOT BUILD x86_64, USE X86 FOR AN EMULATOR |
| 24 | + # |
| 25 | + ############################################################### |
| 26 | + |
| 27 | + version = '2.8.0' |
| 28 | + url = 'https://github.com/tensorflow/tensorflow/archive/refs/tags/v{version}.zip' |
| 29 | + depends = ['pybind11', 'numpy'] |
| 30 | + patches = ['CMakeLists.patch', 'build_with_cmake.patch'] |
| 31 | + site_packages_name = 'tflite-runtime' |
| 32 | + call_hostpython_via_targetpython = False |
| 33 | + |
| 34 | + def build_arch(self, arch): |
| 35 | + if arch.arch == 'x86_64': |
| 36 | + warning("******** tflite-runtime x86_64 will not be built *******") |
| 37 | + warning("Expect one of these app run time error messages:") |
| 38 | + warning("ModuleNotFoundError: No module named 'tensorflow'") |
| 39 | + warning("ModuleNotFoundError: No module named 'tflite_runtime'") |
| 40 | + warning("Use x86 not x86_64") |
| 41 | + return |
| 42 | + |
| 43 | + env = self.get_recipe_env(arch) |
| 44 | + |
| 45 | + # Directories |
| 46 | + root_dir = self.get_build_dir(arch.arch) |
| 47 | + script_dir = join(root_dir, |
| 48 | + 'tensorflow', 'lite', 'tools', 'pip_package') |
| 49 | + build_dir = join(script_dir, 'gen', 'tflite_pip', 'python3') |
| 50 | + |
| 51 | + # Includes |
| 52 | + python_include_dir = self.ctx.python_recipe.include_root(arch.arch) |
| 53 | + pybind11_recipe = self.get_recipe('pybind11', self.ctx) |
| 54 | + pybind11_include_dir = pybind11_recipe.get_include_dir(arch) |
| 55 | + numpy_include_dir = join(self.ctx.get_site_packages_dir(arch), |
| 56 | + 'numpy', 'core', 'include') |
| 57 | + includes = ' -I' + python_include_dir +\ |
| 58 | + ' -I' + numpy_include_dir +\ |
| 59 | + ' -I' + pybind11_include_dir |
| 60 | + |
| 61 | + # Scripts |
| 62 | + build_script = join(script_dir, 'build_pip_package_with_cmake.sh') |
| 63 | + toolchain = join(self.ctx.ndk_dir, |
| 64 | + 'build', 'cmake', 'android.toolchain.cmake') |
| 65 | + |
| 66 | + # Build |
| 67 | + ######## |
| 68 | + with current_directory(root_dir): |
| 69 | + env.update({ |
| 70 | + 'TENSORFLOW_TARGET': 'android', |
| 71 | + 'CMAKE_TOOLCHAIN_FILE': toolchain, |
| 72 | + 'ANDROID_PLATFORM': str(self.ctx.ndk_api), |
| 73 | + 'ANDROID_ABI': arch.arch, |
| 74 | + 'WRAPPER_INCLUDES': includes, |
| 75 | + 'CMAKE_SHARED_LINKER_FLAGS': env['LDFLAGS'], |
| 76 | + }) |
| 77 | + |
| 78 | + try: |
| 79 | + info_main('tflite-runtime is building...') |
| 80 | + info_main('Expect this to take at least 5 minutes...') |
| 81 | + cmd = sh.Command(build_script) |
| 82 | + cmd(_env=env) |
| 83 | + except sh.ErrorReturnCode as e: |
| 84 | + error(str(e.stderr)) |
| 85 | + exit(1) |
| 86 | + |
| 87 | + # Install |
| 88 | + ########## |
| 89 | + info_main('Installing tflite-runtime into site-packages') |
| 90 | + with current_directory(build_dir): |
| 91 | + hostpython = sh.Command(self.hostpython_location) |
| 92 | + install_dir = self.ctx.get_python_install_dir(arch.arch) |
| 93 | + env['PACKAGE_VERSION'] = self.version |
| 94 | + shprint(hostpython, 'setup.py', 'install', '-O2', |
| 95 | + '--root={}'.format(install_dir), |
| 96 | + '--install-lib=.', |
| 97 | + _env=env) |
| 98 | + |
| 99 | + |
| 100 | +recipe = TFLiteRuntimeRecipe() |
0 commit comments