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