-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
190 lines (172 loc) · 7.28 KB
/
Makefile
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
ifndef MIX_APP_PATH
MIX_APP_PATH=$(shell pwd)/_build/default/lib/tflite_beam
PRIV_DIR=$(shell pwd)/priv
LIBUSB_INSTALL_DIR=$(shell pwd)/libusb
else
PRIV_DIR = $(MIX_APP_PATH)/priv
LIBUSB_INSTALL_DIR = $(MIX_APP_PATH)/libusb
endif
NATIVE_BINDINGS_SO = $(PRIV_DIR)/tflite_beam.so
LIBEDGETPU_RUNTIME_PRIV = $(PRIV_DIR)/libedgetpu
TFLITE_BEAM_CORAL_SUPPORT ?= "true"
TFLITE_BEAM_CORAL_LIBEDGETPU_RUNTIME ?= "native"
TFLITE_BEAM_PREFER_PRECOMPILED ?= "true"
TFLITE_BEAM_CORAL_LIBEDGETPU_URL ?= "native"
SCRIPTS_DIR = $(shell pwd)/scripts
C_SRC = $(shell pwd)/c_src
LIB_SRC = $(shell pwd)/lib
LIBCORAL_SRC = $(shell pwd)/c_src/libcoral
PRECOMPILED_ERL_HELPER = $(shell pwd)/tflite_beam_precompiled.erl
UNICODEDATA = $(shell pwd)/unicodedata
UNICODE_DATA = $(PRIV_DIR)/unicode_data.txt
ifdef CMAKE_TOOLCHAIN_FILE
CMAKE_CONFIGURE_FLAGS=-D CMAKE_TOOLCHAIN_FILE="$(CMAKE_TOOLCHAIN_FILE)"
endif
# Tensorflow
TFLITE_USE_GIT_HEAD ?= false
TFLITE_GIT_REPO ?= "https://github.com/tensorflow/tensorflow.git"
TFLITE_VER ?= 2.18.0
TFLITE_VER_V = v$(TFLITE_VER)
ifneq ($(TFLITE_USE_GIT_HEAD), false)
TFLITE_VER_V=$(TFLITE_USE_GIT_BRANCH)
endif
THIRD_PARTY_DIR = $(shell pwd)/3rd_party
TFLITE_BEAM_CACHE_DIR = $(THIRD_PARTY_DIR)/cache
TFLITE_SOURCE_URL = "https://github.com/tensorflow/tensorflow/archive/refs/tags/$(TFLITE_VER_V).zip"
TFLITE_SOURCE_ZIP = $(TFLITE_BEAM_CACHE_DIR)/tensorflow-$(TFLITE_VER_V).zip
UNZIP_TARGET_DIR = $(THIRD_PARTY_DIR)/tensorflow
TENSORFLOW_ROOT_DIR = $(UNZIP_TARGET_DIR)/tensorflow-$(TFLITE_VER)
TFLITE_ROOT_DIR = $(TENSORFLOW_ROOT_DIR)/tensorflow/lite
GFLAGS_ROOT_DIR = $(THIRD_PARTY_DIR)/gflags
GLOG_ROOT_DIR = $(THIRD_PARTY_DIR)/glog
TFLITE_CMAKELISTS_TXT = $(TFLITE_ROOT_DIR)/CMakeLists.txt
CMAKE_TFLITE_BUILD_DIR = $(MIX_APP_PATH)/cmake_tflite_$(TFLITE_VER)
LIBUSB_VERSION = 1.0.27
LIBUSB_SOURCE_URL = https://github.com/libusb/libusb/releases/download/v$(LIBUSB_VERSION)/libusb-$(LIBUSB_VERSION).tar.bz2
LIBUSB_SOURCE_ARCHIVE = $(TFLITE_BEAM_CACHE_DIR)/libusb-$(LIBUSB_VERSION).tar.bz2
LIBUSB_SOURCE_DIR = $(THIRD_PARTY_DIR)/libusb-$(LIBUSB_VERSION)
LIBUSB_SHARED_LIBRARY = $(PRIV_DIR)/libedgetpu/libusb-1.0.0.dylib
UNAME_S := $(shell uname -s)
ifneq ($(UNAME_S),Darwin)
LIBUSB_SHARED_LIBRARY = $(PRIV_DIR)/libedgetpu/libusb-1.0.so.0.4.0
endif
ifeq ($(TARGET_OS),linux)
LIBUSB_SHARED_LIBRARY = $(PRIV_DIR)/libedgetpu/libusb-1.0.so.0.4.0
endif
TFLITE_BEAM_CORAL_USB_THROTTLE ?= true
TFLITE_BEAM_CORAL_LIBEDGETPU_TRIPLET ?= native
TFLITE_BEAM_CORAL_LIBEDGETPU_UNZIPPED_DIR = $(TFLITE_BEAM_CACHE_DIR)/$(TFLITE_BEAM_CORAL_LIBEDGETPU_RUNTIME)
CMAKE_TFLITE_OPTIONS ?= ""
CMAKE_OPTIONS ?= $(CMAKE_TFLITE_OPTIONS)
CMAKE_OPTIONS += $(CMAKE_CONFIGURE_FLAGS)
# bindings
CMAKE_BINDINGS_BUILD_DIR = $(MIX_APP_PATH)/cmake_tflite_beam
MAKE_BUILD_FLAGS ?= auto
.DEFAULT_GLOBAL := build
build: $(NATIVE_BINDINGS_SO) fix_libusb
$(PRIV_DIR):
@ if [ ! -d "$(PRIV_DIR)" ]; then \
rm -rf "$(PRIV_DIR)" ; \
mkdir -p "$(PRIV_DIR)" ; \
fi
create_cache_dir:
@ mkdir -p "$(TFLITE_BEAM_CACHE_DIR)"
$(TFLITE_SOURCE_ZIP): create_cache_dir
@ if [ "$(TFLITE_BEAM_PREFER_PRECOMPILED)" != "true" ]; then \
if [ "$(TFLITE_USE_GIT_HEAD)" = "false" ] && [ ! -e "$(TFLITE_SOURCE_ZIP)" ]; then \
if [ -e "$(shell which curl)" ]; then \
curl -fSL "$(TFLITE_SOURCE_URL)" -o $(TFLITE_SOURCE_ZIP) ; \
elif [ -e "$(shell which wget)" ]; then \
wget "$(TFLITE_SOURCE_URL)" -O $(TFLITE_SOURCE_ZIP) ; \
else \
echo "cannot find curl or wget, cannot download tensorflow source code" ; \
exit 1 ; \
fi \
fi \
fi
unarchive_source_code: $(TFLITE_SOURCE_ZIP)
@ if [ "$(TFLITE_BEAM_PREFER_PRECOMPILED)" != "true" ]; then \
if [ ! -d "$(TENSORFLOW_ROOT_DIR)" ]; then \
rm -rf "$(TENSORFLOW_ROOT_DIR)" ; \
mkdir -p "$(UNZIP_TARGET_DIR)" ; \
if [ "$(TFLITE_USE_GIT_HEAD)" = "false" ]; then \
unzip -qq -o "$(TFLITE_SOURCE_ZIP)" -d "$(UNZIP_TARGET_DIR)" ; \
else \
git clone --branch=$(TFLITE_USE_GIT_BRANCH) --depth=1 $(TFLITE_GIT_REPO) "$(TENSORFLOW_ROOT_DIR)" ; \
fi \
fi \
fi
install_libedgetpu_runtime:
@ if [ "$(TFLITE_BEAM_CORAL_SUPPORT)" = "true" ]; then \
bash scripts/copy_libedgetpu_runtime.sh "$(LIBEDGETPU_RUNTIME_PRIV)" "$(TFLITE_BEAM_CORAL_LIBEDGETPU_UNZIPPED_DIR)" "$(TFLITE_BEAM_CORAL_LIBEDGETPU_TRIPLET)" "$(TFLITE_BEAM_CORAL_USB_THROTTLE)" "$(TFLITE_BEAM_CORAL_LIBEDGETPU_URL)" "$(TFLITE_BEAM_CORAL_LIBEDGETPU_RUNTIME)" "$(TFLITE_BEAM_CACHE_DIR)" && \
if [ "$(TFLITE_BEAM_PREFER_PRECOMPILED)" != "true" ]; then \
if [ ! -e "$(LIBCORAL_SRC)/Makefile" ]; then \
rm -rf "$(LIBCORAL_SRC)" && \
git clone --depth 1 https://github.com/cocoa-xu/libcoral.git "$(LIBCORAL_SRC)" ; \
fi && \
cd "$(LIBCORAL_SRC)" && git submodule update --init libedgetpu && cd ../.. ; \
fi \
fi
libusb: create_cache_dir
@ if [ "$(TFLITE_BEAM_PREFER_PRECOMPILED)" != "true" ]; then \
if [ "$(TFLITE_BEAM_CORAL_SUPPORT)" = "true" ]; then \
echo "LIBUSB_SHARED_LIBRARY: $(LIBUSB_SHARED_LIBRARY)" ; \
if [ ! -e "$(LIBUSB_SHARED_LIBRARY)" ]; then \
mkdir -p "$(LIBUSB_INSTALL_DIR)" ; \
bash scripts/build_libusb.sh "$(LIBUSB_SOURCE_URL)" "$(LIBUSB_SOURCE_ARCHIVE)" "$(THIRD_PARTY_DIR)" "$(LIBUSB_SOURCE_DIR)" "$(LIBUSB_INSTALL_DIR)" "$(PRIV_DIR)" ; \
fi \
fi \
fi
$(UNICODE_DATA): $(PRIV_DIR)
@ if [ ! -e "$(UNICODE_DATA)" ]; then \
cp -f "$(UNICODEDATA)/unicode_data.txt" "$(UNICODE_DATA)" ; \
fi
$(NATIVE_BINDINGS_SO): $(UNICODE_DATA) unarchive_source_code install_libedgetpu_runtime libusb
@ if [ "$(TFLITE_BEAM_PREFER_PRECOMPILED)" = "true" ]; then \
{ \
cd "$(shell pwd)" && \
erlc "$(PRECOMPILED_ERL_HELPER)" && \
erl -noshell -s tflite_beam_precompiled install_precompiled_binary_if_available -s init stop ; } || \
{ \
$(TFLITE_BEAM_MAKE) TFLITE_BEAM_PREFER_PRECOMPILED=false ; } ; \
fi && \
if [ ! -e "$(NATIVE_BINDINGS_SO)" ]; then \
echo "CORAL SUPPORT: $(TFLITE_BEAM_CORAL_SUPPORT)" ; \
echo "LIBEDGETPU runtime: $(TFLITE_BEAM_CORAL_LIBEDGETPU_RUNTIME)" ; \
echo "Throttle USB Coral Devices: $(TFLITE_BEAM_CORAL_USB_THROTTLE)" ; \
if [ ! -e "3rd_party/gflags/CMakeLists.txt" ]; then \
rm -rf 3rd_party/gflags && \
git clone --depth 1 https://github.com/gflags/gflags.git 3rd_party/gflags ; \
fi && \
if [ ! -e "3rd_party/glog/CMakeLists.txt" ]; then \
rm -rf 3rd_party/glog && \
git clone --depth 1 https://github.com/google/glog.git 3rd_party/glog ; \
fi && \
mkdir -p $(CMAKE_BINDINGS_BUILD_DIR) && \
python3 "$(shell pwd)/patches/apply_patch.py" "$(TENSORFLOW_ROOT_DIR)" "$(TFLITE_VER)" && \
cd "$(CMAKE_BINDINGS_BUILD_DIR)" && \
cmake -D C_SRC="$(C_SRC)" \
-D PRIV_DIR="$(PRIV_DIR)" \
-D ERTS_INCLUDE_DIR="$(ERTS_INCLUDE_DIR)" \
-D TFLITE_ROOT_DIR="$(TFLITE_ROOT_DIR)" \
-D GFLAGS_ROOT_DIR="$(GFLAGS_ROOT_DIR)" \
-D GLOG_ROOT_DIR="$(GLOG_ROOT_DIR)" \
-D TFLITE_BEAM_CORAL_SUPPORT="$(TFLITE_BEAM_CORAL_SUPPORT)" \
-D LIBUSB_INSTALL_DIR="$(LIBUSB_INSTALL_DIR)" \
-D MIX_APP_PATH="$(MIX_APP_PATH)" \
$(CMAKE_OPTIONS) \
"$(shell pwd)" && \
if [ "$(MAKE_BUILD_FLAGS)" = "auto" ]; then \
if [ "$(UNAME_S)" = Darwin ]; then \
make "-j$(shell sysctl -n hw.ncpu)" ; \
else \
make "-j$(shell nproc)" ; \
fi; \
else \
make "$(MAKE_BUILD_FLAGS)" ; \
fi && \
mkdir -p "$(PRIV_DIR)" && \
cp "$(CMAKE_BINDINGS_BUILD_DIR)/tflite_beam.so" "$(NATIVE_BINDINGS_SO)" ; \
fi
fix_libusb:
@ bash scripts/macos_libusb_rpath.sh "$(NATIVE_BINDINGS_SO)"