blob: d6f6fce9db32c5e03ed90db5be41a3a38ae4efc3 [file] [log] [blame]
James Conroy919ec712022-07-13 12:57:53 +01001#!/bin/bash
2#
3# Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
4# SPDX-License-Identifier: MIT
5#
6
7# Script which downloads and builds Arm NN dependencies
8# Perquisite to running build-armnn.sh
9
10set -o nounset # Catch references to undefined variables.
11set -o pipefail # Catch non zero exit codes within pipelines.
12set -o errexit # Catch and propagate non zero exit codes.
13
14rel_path=$(dirname "$0") # relative path from where script is executed to script location
15
16# Download an archive using wget and extract using tar
17# Takes three arguments:
18# 1. Name of dependency being downloaded e.g. Flatbuffers
19# 2. Link to archive
20# 3. Filename given to archive upon downloading
21download_and_extract()
22{
23 cd "$SOURCE_DIR"
24
James Conroy210897d2022-08-04 16:55:05 +010025 echo -e "\n***** Downloading $1 *****\n"
James Conroy919ec712022-07-13 12:57:53 +010026 wget -O "$3" "$2"
27
28 echo -e "\n***** Extracting archive *****"
29 tar -xzf "$3"
30
31 echo -e "\n***** Removing archive *****"
32 rm "$3"
33
34 echo -e "\n***** $1 downloaded *****"
35}
36
37download_protobuf()
38{
39 download_and_extract \
40 "Protobuf" \
41 "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz" \
42 "protobuf-all-$PROTOBUF_VERSION.tar.gz"
43}
44
45build_protobuf()
46{
47 local native_build=$1
48 local build_dir="$PROTOBUF_BUILD_TARGET"
49 local cmake_flags=""
50 local target_arch="$TARGET_ARCH"
51 local additional_cmds=""
52
53 if [ "$native_build" -eq 0 ]; then
54 mkdir -p "$PROTOBUF_BUILD_TARGET"
55 additional_cmds+="--with-protoc=$PROTOCOL_COMPILER_HOST "
56 if [ "$TARGET_ARCH" == "aarch64" ]; then
57 cmake_flags+="$AARCH64_COMPILER_FLAGS"
58 additional_cmds+="--host=aarch64-linux "
59 elif [ "$TARGET_ARCH" == "aarch32" ]; then
60 cmake_flags+="$AARCH32_COMPILER_FLAGS"
61 additional_cmds+="--host=arm-linux "
62 fi
63 else
64 target_arch="$HOST_ARCH"
65 mkdir -p "$PROTOBUF_BUILD_HOST"
66 build_dir="$PROTOBUF_BUILD_HOST"
67 fi
68
69 echo -e "\n***** Building Protobuf for $target_arch ***** "
70
71 cd "$PROTOBUF_BUILD_ROOT"
72
73 # Cleanup any previous cmake files, except actual builds which we keep
74 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
75
76 eval "$cmake_flags" \
77 "$PROTOBUF_SRC"/configure --prefix="$build_dir" "$additional_cmds"
78 make install -j "$NUM_THREADS"
79
80 echo -e "\n***** Protobuf built for $target_arch ***** "
81}
82
83download_flatbuffers()
84{
85 download_and_extract \
86 "Flatbuffers" \
87 "https://github.com/google/flatbuffers/archive/v$FLATBUFFERS_VERSION.tar.gz" \
88 "flatbuffers-$FLATBUFFERS_VERSION.tar.gz"
89}
90
91build_flatbuffers()
92{
93 local native_build=$1
94 local build_dir="$FLATBUFFERS_BUILD_TARGET"
95 local target_arch="$TARGET_ARCH"
96
97 local cmake_flags="CXXFLAGS=-fPIC "
98
99 if [ "$native_build" -eq 0 ]; then
100 mkdir -p "$FLATBUFFERS_BUILD_TARGET"
101 if [ "$TARGET_ARCH" == "aarch64" ]; then
102 cmake_flags+="$AARCH64_COMPILER_FLAGS"
103 elif [ "$TARGET_ARCH" == "aarch32" ]; then
104 cmake_flags+="$AARCH32_COMPILER_FLAGS"
105 fi
106 else
107 target_arch="$HOST_ARCH"
108 mkdir -p "$FLATBUFFERS_BUILD_HOST"
109 build_dir="$FLATBUFFERS_BUILD_HOST"
110 fi
111
112 echo -e "\n***** Building flatbuffers for $target_arch *****"
113
114 mkdir -p "$FLATBUFFERS_BUILD_ROOT"
115 cd "$FLATBUFFERS_BUILD_ROOT"
116
117 # Cleanup any previous cmake files, except actual builds which we keep
118 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
119
120 eval "$cmake_flags" \
121 cmake -DFLATBUFFERS_BUILD_FLATC=1 \
122 -DCMAKE_INSTALL_PREFIX:PATH="$build_dir" \
123 -DFLATBUFFERS_BUILD_TESTS=0 \
124 "$FLATBUFFERS_SRC"
125 make all install -j "$NUM_THREADS"
126
127 echo -e "\n***** Built flatbuffers for $target_arch *****"
128}
129
130download_tensorflow()
131{
132 cd "$SOURCE_DIR"
133
134 echo -e "\n***** Downloading TensorFlow *****"
135 git clone https://github.com/tensorflow/tensorflow.git
136 cd "$TENSORFLOW_SRC"
137
James Conroy210897d2022-08-04 16:55:05 +0100138 git checkout "$TENSORFLOW_VERSION"
James Conroy919ec712022-07-13 12:57:53 +0100139 echo -e "\n***** TensorFlow downloaded *****"
140}
141
142build_tflite()
143{
144 mkdir -p "$TFLITE_BUILD_TARGET"
145 cd "$TFLITE_BUILD_TARGET"
146
147 local target_arch_cmd="" # default is native, no command needed
148 local cmake_flags=""
149
150 case "$TARGET_ARCH" in
151 "aarch64")
152 cmake_flags+="$AARCH64_COMPILER_FLAGS"
153 target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=aarch64 "
154
155 if [ "$NATIVE_BUILD" -eq 0 ]; then
156 cmake_flags+="ARMCC_FLAGS='-funsafe-math-optimizations' "
157 fi
158 ;;
159
160 "aarch32")
161 cmake_flags+="$AARCH32_COMPILER_FLAGS"
162 target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=armv7 "
163
164 if [ "$NATIVE_BUILD" -eq 0 ]; then
165 cmake_flags+="ARMCC_FLAGS='-march=armv7-a -mfpu=neon-vfpv4 -funsafe-math-optimizations -mfp16-format=ieee' "
166 fi
167 ;;
168 esac
169
170 echo -e "\n***** Building TF Lite for $TARGET_ARCH *****"
171
172 # Cleanup any previous cmake files, except actual builds which we keep
173 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
174
175 eval "$cmake_flags" \
176 cmake -DTFLITE_ENABLE_XNNPACK=OFF \
177 "$target_arch_cmd" \
178 "$TFLITE_SRC"
179 cmake --build . -j "$NUM_THREADS"
180
181 echo -e "\n***** Built TF Lite for $TARGET_ARCH *****"
182}
183
184generate_tflite_schema()
185{
186 echo -e "\n***** Generating TF Lite Schema *****"
187 mkdir -p "$TFLITE_BUILD_ROOT"
188 cd "$TFLITE_BUILD_ROOT"
189
190 cp "$SCHEMA_SRC" .
191
192 $FLATC -c --gen-object-api --reflect-types --reflect-names schema.fbs
193
194 echo -e "\n***** Generated TF Lite Schema *****"
195}
196
197download_onnx()
198{
199 download_and_extract \
200 "ONNX" \
201 "https://github.com/onnx/onnx/releases/download/v$ONNX_VERSION/onnx-$ONNX_VERSION.tar.gz" \
202 "onnx-$ONNX_VERSION.tar.gz"
203}
204
205generate_onnx_sources()
206{
207 mkdir -p "$ONNX_BUILD_TARGET"
208 cd "$ONNX_SRC"
209
210 echo -e "\n***** Generating ONNX sources for $TARGET_ARCH *****"
211
212 export LD_LIBRARY_PATH="$PROTOBUF_BUILD_HOST"/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
213
214 eval "$PROTOCOL_COMPILER_HOST" onnx/onnx.proto \
215 --proto_path=. \
216 --proto_path="$ONNX_SRC" \
217 --proto_path="$PROTOBUF_BUILD_HOST"/include \
218 --cpp_out "$ONNX_BUILD_TARGET"
219
220 echo -e "\n***** Generated ONNX sources for $TARGET_ARCH *****"
221}
222
James Conroy919ec712022-07-13 12:57:53 +0100223usage()
224{
225 cat <<EOF
226setup-armnn.sh - Download and build Arm NN dependencies in the current directory (ROOT_DIR)
227setup-armnn.sh [OPTION]...
228 --tflite-delegate
229 setup dependencies for the Arm NN TF Lite Delegate
230 --tflite-parser
231 setup dependencies for the Arm NN TF Lite Parser
232 --onnx-parser
233 setup dependencies for the Arm NN ONNX parser
234 --all
235 setup dependencies for all Arm NN components listed above
236 --target-arch=[aarch64|aarch32|x86_64]
237 specify a target architecture (mandatory)
238 --num-threads=<INTEGER>
239 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
240 -h, --help
241 print brief usage information and exit
242 -x
243 enable shell tracing in this script
244
245At least one dependency flag (e.g. --tflite-delegate) must be provided or else provide --all to setup all dependencies.
246Directories called "source" and "build" will be generated in the current directory (ROOT_DIR) from which this script is called.
247It's recommended to call this script in a directory outside of this Arm NN source repo, to avoid nested repositories.
248
James Conroy919ec712022-07-13 12:57:53 +0100249Examples:
250Setup for aarch64 with all Arm NN dependencies:
251 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --all
252Setup for aarch64 with TF Lite Delegate and TF Lite Parser dependencies only:
253 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --tflite-delegate --tflite-parser
254Setup for aarch32 with all Arm NN dependencies:
255 <PATH_TO>/setup-armnn.sh --target-arch=aarch32 --all
256EOF
257}
258
259# This will catch in validation.sh if not set
260target_arch=""
261
262# Default flag values
263flag_tflite_delegate=0
264flag_tflite_parser=0
265flag_onnx_parser=0
266
267# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
268num_threads=0
269
270name=$(basename "$0")
271
272# If no options provided, show help
273if [ $# -eq 0 ]; then
274 usage
275 exit 1
276fi
277
278args=$(getopt -ohx -l tflite-delegate,tflite-parser,onnx-parser,all,target-arch:,num-threads:,help -n "$name" -- "$@")
279eval set -- "$args"
280while [ $# -gt 0 ]; do
281 if [ -n "${opt_prev:-}" ]; then
282 eval "$opt_prev=\$1"
283 opt_prev=
284 shift 1
285 continue
286 elif [ -n "${opt_append:-}" ]; then
287 if [ -n "$1" ]; then
288 eval "$opt_append=\"\${$opt_append:-} \$1\""
289 fi
290 opt_append=
291 shift 1
292 continue
293 fi
294 case $1 in
295 --tflite-parser)
296 flag_tflite_parser=1
297 ;;
298
299 --tflite-delegate)
300 flag_tflite_delegate=1
301 ;;
302
303 --onnx-parser)
304 flag_onnx_parser=1
305 ;;
306
307 --all)
308 flag_tflite_delegate=1
309 flag_tflite_parser=1
310 flag_onnx_parser=1
311 ;;
312
313 --target-arch)
314 opt_prev=target_arch
315 ;;
316
317 --num-threads)
318 opt_prev=num_threads
319 ;;
320
321 -h | --help)
322 usage
323 exit 0
324 ;;
325
326 -x)
327 set -x
328 ;;
329
330 --)
331 shift
332 break 2
333 ;;
334
335 esac
336 shift 1
337done
338
339# shellcheck source=common.sh
340source "$rel_path"/common.sh
341
342echo -e "\nINFO: Displaying configuration information before execution of $name"
343echo " target-arch: $TARGET_ARCH"
344echo " host-arch: $HOST_ARCH"
345echo " tflite-delegate: $flag_tflite_delegate"
346echo " tflite-parser: $flag_tflite_parser"
347echo " onnx-parser: $flag_onnx_parser"
348echo " num-threads: $NUM_THREADS"
349echo " root directory: $ROOT_DIR"
350echo "source directory: $SOURCE_DIR"
351echo " build directory: $BUILD_DIR"
352
James Conroy210897d2022-08-04 16:55:05 +0100353if check_if_repository .; then
James Conroy919ec712022-07-13 12:57:53 +0100354 echo -e "\n***** WARNING: Running script inside a git repository. To avoid nested repos, call this script from outside of this repo. *****"
355fi
356
357echo -e "\nScript execution will begin in 10 seconds..."
358
359sleep 10
360
361mkdir -p "$SOURCE_DIR"
362mkdir -p "$BUILD_DIR"
363
James Conroy919ec712022-07-13 12:57:53 +0100364if [ "$flag_tflite_delegate" -eq 1 ] || [ "$flag_tflite_parser" -eq 1 ]; then
365 download_flatbuffers
366
367 # Host build
368 build_flatbuffers 1
369
370 # Target build for cross compile
371 if [ "$NATIVE_BUILD" -eq 0 ]; then
372 build_flatbuffers 0
373 fi
374
375 download_tensorflow
376fi
377
378if [ "$flag_tflite_parser" -eq 1 ]; then
379 generate_tflite_schema
380fi
381
382if [ "$flag_tflite_delegate" -eq 1 ]; then
383 build_tflite
384fi
385
386if [ "$flag_onnx_parser" -eq 1 ]; then
387 download_protobuf
388
389 # Host build
390 build_protobuf 1
391
392 # Target build for cross compile
393 if [ "$NATIVE_BUILD" -eq 0 ]; then
394 build_protobuf 0
395 fi
396
397 download_onnx
398 generate_onnx_sources
399fi
400
401echo -e "\n***** Arm NN setup complete. Now build with build-armnn.sh. *****\n"
402
403exit 0