blob: b016b53bd0cea544f5d2d31ff372d08a40ddb433 [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 "
James Conroy919ec712022-07-13 12:57:53 +010059 fi
60 else
61 target_arch="$HOST_ARCH"
62 mkdir -p "$PROTOBUF_BUILD_HOST"
63 build_dir="$PROTOBUF_BUILD_HOST"
64 fi
65
66 echo -e "\n***** Building Protobuf for $target_arch ***** "
67
68 cd "$PROTOBUF_BUILD_ROOT"
69
70 # Cleanup any previous cmake files, except actual builds which we keep
71 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
72
73 eval "$cmake_flags" \
74 "$PROTOBUF_SRC"/configure --prefix="$build_dir" "$additional_cmds"
75 make install -j "$NUM_THREADS"
76
77 echo -e "\n***** Protobuf built for $target_arch ***** "
78}
79
80download_flatbuffers()
81{
82 download_and_extract \
83 "Flatbuffers" \
84 "https://github.com/google/flatbuffers/archive/v$FLATBUFFERS_VERSION.tar.gz" \
85 "flatbuffers-$FLATBUFFERS_VERSION.tar.gz"
86}
87
88build_flatbuffers()
89{
90 local native_build=$1
91 local build_dir="$FLATBUFFERS_BUILD_TARGET"
92 local target_arch="$TARGET_ARCH"
93
94 local cmake_flags="CXXFLAGS=-fPIC "
95
96 if [ "$native_build" -eq 0 ]; then
97 mkdir -p "$FLATBUFFERS_BUILD_TARGET"
98 if [ "$TARGET_ARCH" == "aarch64" ]; then
99 cmake_flags+="$AARCH64_COMPILER_FLAGS"
James Conroy919ec712022-07-13 12:57:53 +0100100 fi
101 else
102 target_arch="$HOST_ARCH"
103 mkdir -p "$FLATBUFFERS_BUILD_HOST"
104 build_dir="$FLATBUFFERS_BUILD_HOST"
105 fi
106
107 echo -e "\n***** Building flatbuffers for $target_arch *****"
108
109 mkdir -p "$FLATBUFFERS_BUILD_ROOT"
110 cd "$FLATBUFFERS_BUILD_ROOT"
111
112 # Cleanup any previous cmake files, except actual builds which we keep
113 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
114
115 eval "$cmake_flags" \
116 cmake -DFLATBUFFERS_BUILD_FLATC=1 \
117 -DCMAKE_INSTALL_PREFIX:PATH="$build_dir" \
118 -DFLATBUFFERS_BUILD_TESTS=0 \
119 "$FLATBUFFERS_SRC"
120 make all install -j "$NUM_THREADS"
121
122 echo -e "\n***** Built flatbuffers for $target_arch *****"
123}
124
125download_tensorflow()
126{
127 cd "$SOURCE_DIR"
128
129 echo -e "\n***** Downloading TensorFlow *****"
130 git clone https://github.com/tensorflow/tensorflow.git
131 cd "$TENSORFLOW_SRC"
132
James Conroy210897d2022-08-04 16:55:05 +0100133 git checkout "$TENSORFLOW_VERSION"
James Conroy919ec712022-07-13 12:57:53 +0100134 echo -e "\n***** TensorFlow downloaded *****"
135}
136
137build_tflite()
138{
139 mkdir -p "$TFLITE_BUILD_TARGET"
140 cd "$TFLITE_BUILD_TARGET"
141
142 local target_arch_cmd="" # default is native, no command needed
143 local cmake_flags=""
144
145 case "$TARGET_ARCH" in
146 "aarch64")
147 cmake_flags+="$AARCH64_COMPILER_FLAGS"
148 target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=aarch64 "
149
150 if [ "$NATIVE_BUILD" -eq 0 ]; then
151 cmake_flags+="ARMCC_FLAGS='-funsafe-math-optimizations' "
152 fi
153 ;;
James Conroy919ec712022-07-13 12:57:53 +0100154 esac
155
156 echo -e "\n***** Building TF Lite for $TARGET_ARCH *****"
157
158 # Cleanup any previous cmake files, except actual builds which we keep
159 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
160
161 eval "$cmake_flags" \
162 cmake -DTFLITE_ENABLE_XNNPACK=OFF \
163 "$target_arch_cmd" \
164 "$TFLITE_SRC"
165 cmake --build . -j "$NUM_THREADS"
166
167 echo -e "\n***** Built TF Lite for $TARGET_ARCH *****"
168}
169
170generate_tflite_schema()
171{
172 echo -e "\n***** Generating TF Lite Schema *****"
173 mkdir -p "$TFLITE_BUILD_ROOT"
174 cd "$TFLITE_BUILD_ROOT"
175
176 cp "$SCHEMA_SRC" .
177
178 $FLATC -c --gen-object-api --reflect-types --reflect-names schema.fbs
179
180 echo -e "\n***** Generated TF Lite Schema *****"
181}
182
183download_onnx()
184{
185 download_and_extract \
186 "ONNX" \
187 "https://github.com/onnx/onnx/releases/download/v$ONNX_VERSION/onnx-$ONNX_VERSION.tar.gz" \
188 "onnx-$ONNX_VERSION.tar.gz"
189}
190
191generate_onnx_sources()
192{
193 mkdir -p "$ONNX_BUILD_TARGET"
194 cd "$ONNX_SRC"
195
196 echo -e "\n***** Generating ONNX sources for $TARGET_ARCH *****"
197
198 export LD_LIBRARY_PATH="$PROTOBUF_BUILD_HOST"/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
199
200 eval "$PROTOCOL_COMPILER_HOST" onnx/onnx.proto \
201 --proto_path=. \
202 --proto_path="$ONNX_SRC" \
203 --proto_path="$PROTOBUF_BUILD_HOST"/include \
204 --cpp_out "$ONNX_BUILD_TARGET"
205
206 echo -e "\n***** Generated ONNX sources for $TARGET_ARCH *****"
207}
208
James Conroy919ec712022-07-13 12:57:53 +0100209usage()
210{
211 cat <<EOF
212setup-armnn.sh - Download and build Arm NN dependencies in the current directory (ROOT_DIR)
213setup-armnn.sh [OPTION]...
214 --tflite-delegate
215 setup dependencies for the Arm NN TF Lite Delegate
216 --tflite-parser
217 setup dependencies for the Arm NN TF Lite Parser
218 --onnx-parser
219 setup dependencies for the Arm NN ONNX parser
220 --all
221 setup dependencies for all Arm NN components listed above
James Conroye6f30ad2022-09-08 12:04:26 +0100222 --target-arch=[aarch64|x86_64]
James Conroy919ec712022-07-13 12:57:53 +0100223 specify a target architecture (mandatory)
224 --num-threads=<INTEGER>
225 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
226 -h, --help
227 print brief usage information and exit
228 -x
229 enable shell tracing in this script
230
231At least one dependency flag (e.g. --tflite-delegate) must be provided or else provide --all to setup all dependencies.
232Directories called "source" and "build" will be generated in the current directory (ROOT_DIR) from which this script is called.
233It's recommended to call this script in a directory outside of this Arm NN source repo, to avoid nested repositories.
234
James Conroy919ec712022-07-13 12:57:53 +0100235Examples:
236Setup for aarch64 with all Arm NN dependencies:
237 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --all
238Setup for aarch64 with TF Lite Delegate and TF Lite Parser dependencies only:
239 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --tflite-delegate --tflite-parser
James Conroy919ec712022-07-13 12:57:53 +0100240EOF
241}
242
243# This will catch in validation.sh if not set
244target_arch=""
245
246# Default flag values
247flag_tflite_delegate=0
248flag_tflite_parser=0
249flag_onnx_parser=0
250
251# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
252num_threads=0
253
254name=$(basename "$0")
255
256# If no options provided, show help
257if [ $# -eq 0 ]; then
258 usage
259 exit 1
260fi
261
262args=$(getopt -ohx -l tflite-delegate,tflite-parser,onnx-parser,all,target-arch:,num-threads:,help -n "$name" -- "$@")
263eval set -- "$args"
264while [ $# -gt 0 ]; do
265 if [ -n "${opt_prev:-}" ]; then
266 eval "$opt_prev=\$1"
267 opt_prev=
268 shift 1
269 continue
270 elif [ -n "${opt_append:-}" ]; then
271 if [ -n "$1" ]; then
272 eval "$opt_append=\"\${$opt_append:-} \$1\""
273 fi
274 opt_append=
275 shift 1
276 continue
277 fi
278 case $1 in
279 --tflite-parser)
280 flag_tflite_parser=1
281 ;;
282
283 --tflite-delegate)
284 flag_tflite_delegate=1
285 ;;
286
287 --onnx-parser)
288 flag_onnx_parser=1
289 ;;
290
291 --all)
292 flag_tflite_delegate=1
293 flag_tflite_parser=1
294 flag_onnx_parser=1
295 ;;
296
297 --target-arch)
298 opt_prev=target_arch
299 ;;
300
301 --num-threads)
302 opt_prev=num_threads
303 ;;
304
305 -h | --help)
306 usage
307 exit 0
308 ;;
309
310 -x)
311 set -x
312 ;;
313
314 --)
315 shift
316 break 2
317 ;;
318
319 esac
320 shift 1
321done
322
323# shellcheck source=common.sh
324source "$rel_path"/common.sh
325
326echo -e "\nINFO: Displaying configuration information before execution of $name"
327echo " target-arch: $TARGET_ARCH"
328echo " host-arch: $HOST_ARCH"
329echo " tflite-delegate: $flag_tflite_delegate"
330echo " tflite-parser: $flag_tflite_parser"
331echo " onnx-parser: $flag_onnx_parser"
332echo " num-threads: $NUM_THREADS"
333echo " root directory: $ROOT_DIR"
334echo "source directory: $SOURCE_DIR"
335echo " build directory: $BUILD_DIR"
336
James Conroy210897d2022-08-04 16:55:05 +0100337if check_if_repository .; then
James Conroy919ec712022-07-13 12:57:53 +0100338 echo -e "\n***** WARNING: Running script inside a git repository. To avoid nested repos, call this script from outside of this repo. *****"
339fi
340
341echo -e "\nScript execution will begin in 10 seconds..."
342
343sleep 10
344
345mkdir -p "$SOURCE_DIR"
346mkdir -p "$BUILD_DIR"
347
James Conroy919ec712022-07-13 12:57:53 +0100348if [ "$flag_tflite_delegate" -eq 1 ] || [ "$flag_tflite_parser" -eq 1 ]; then
349 download_flatbuffers
350
351 # Host build
352 build_flatbuffers 1
353
354 # Target build for cross compile
355 if [ "$NATIVE_BUILD" -eq 0 ]; then
356 build_flatbuffers 0
357 fi
358
359 download_tensorflow
360fi
361
362if [ "$flag_tflite_parser" -eq 1 ]; then
363 generate_tflite_schema
364fi
365
366if [ "$flag_tflite_delegate" -eq 1 ]; then
367 build_tflite
368fi
369
370if [ "$flag_onnx_parser" -eq 1 ]; then
371 download_protobuf
372
373 # Host build
374 build_protobuf 1
375
376 # Target build for cross compile
377 if [ "$NATIVE_BUILD" -eq 0 ]; then
378 build_protobuf 0
379 fi
380
381 download_onnx
382 generate_onnx_sources
383fi
384
385echo -e "\n***** Arm NN setup complete. Now build with build-armnn.sh. *****\n"
386
387exit 0