blob: 156bb2fb953f361552650369024a199c40a2616a [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" \
James Conroy2e950f42022-11-01 15:01:06 +0000116 cmake -DFLATBUFFERS_BUILD_FLATC="$native_build" \
James Conroy919ec712022-07-13 12:57:53 +0100117 -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"
James Conroy2e950f42022-11-01 15:01:06 +0000148 target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
149 -DCMAKE_SYSTEM_NAME=Linux "
James Conroy919ec712022-07-13 12:57:53 +0100150
151 if [ "$NATIVE_BUILD" -eq 0 ]; then
152 cmake_flags+="ARMCC_FLAGS='-funsafe-math-optimizations' "
153 fi
154 ;;
James Conroy919ec712022-07-13 12:57:53 +0100155 esac
156
157 echo -e "\n***** Building TF Lite for $TARGET_ARCH *****"
158
159 # Cleanup any previous cmake files, except actual builds which we keep
160 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
161
162 eval "$cmake_flags" \
163 cmake -DTFLITE_ENABLE_XNNPACK=OFF \
Colm Donelan3aad6f62023-04-20 21:56:11 +0100164 -DFLATBUFFERS_BUILD_FLATC=OFF \
165 -DBUILD_SHARED_LIBS=OFF \
166 -DBUILD_TESTING=OFF \
James Conroy919ec712022-07-13 12:57:53 +0100167 "$target_arch_cmd" \
168 "$TFLITE_SRC"
169 cmake --build . -j "$NUM_THREADS"
170
171 echo -e "\n***** Built TF Lite for $TARGET_ARCH *****"
172}
173
174generate_tflite_schema()
175{
176 echo -e "\n***** Generating TF Lite Schema *****"
177 mkdir -p "$TFLITE_BUILD_ROOT"
178 cd "$TFLITE_BUILD_ROOT"
179
180 cp "$SCHEMA_SRC" .
181
182 $FLATC -c --gen-object-api --reflect-types --reflect-names schema.fbs
183
184 echo -e "\n***** Generated TF Lite Schema *****"
185}
186
187download_onnx()
188{
189 download_and_extract \
190 "ONNX" \
191 "https://github.com/onnx/onnx/releases/download/v$ONNX_VERSION/onnx-$ONNX_VERSION.tar.gz" \
192 "onnx-$ONNX_VERSION.tar.gz"
193}
194
195generate_onnx_sources()
196{
197 mkdir -p "$ONNX_BUILD_TARGET"
198 cd "$ONNX_SRC"
199
200 echo -e "\n***** Generating ONNX sources for $TARGET_ARCH *****"
201
202 export LD_LIBRARY_PATH="$PROTOBUF_BUILD_HOST"/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
203
204 eval "$PROTOCOL_COMPILER_HOST" onnx/onnx.proto \
205 --proto_path=. \
206 --proto_path="$ONNX_SRC" \
207 --proto_path="$PROTOBUF_BUILD_HOST"/include \
208 --cpp_out "$ONNX_BUILD_TARGET"
209
210 echo -e "\n***** Generated ONNX sources for $TARGET_ARCH *****"
211}
212
James Conroy919ec712022-07-13 12:57:53 +0100213usage()
214{
215 cat <<EOF
216setup-armnn.sh - Download and build Arm NN dependencies in the current directory (ROOT_DIR)
217setup-armnn.sh [OPTION]...
Nikhil Raj542c8482023-04-26 16:22:10 +0100218 --tflite-classic-delegate
219 setup dependencies for the existing Arm NN TF Lite Delegate
220 --tflite-opaque-delegate
221 setup dependencies for the new Opaque Delegate
James Conroy919ec712022-07-13 12:57:53 +0100222 --tflite-parser
223 setup dependencies for the Arm NN TF Lite Parser
224 --onnx-parser
225 setup dependencies for the Arm NN ONNX parser
226 --all
227 setup dependencies for all Arm NN components listed above
James Conroye6f30ad2022-09-08 12:04:26 +0100228 --target-arch=[aarch64|x86_64]
James Conroy919ec712022-07-13 12:57:53 +0100229 specify a target architecture (mandatory)
230 --num-threads=<INTEGER>
231 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
232 -h, --help
233 print brief usage information and exit
234 -x
235 enable shell tracing in this script
236
Nikhil Raj542c8482023-04-26 16:22:10 +0100237At least one dependency flag (e.g. --tflite-classic-delegate) must be provided or else provide --all to setup all dependencies.
James Conroy919ec712022-07-13 12:57:53 +0100238Directories called "source" and "build" will be generated in the current directory (ROOT_DIR) from which this script is called.
239It's recommended to call this script in a directory outside of this Arm NN source repo, to avoid nested repositories.
240
James Conroy919ec712022-07-13 12:57:53 +0100241Examples:
242Setup for aarch64 with all Arm NN dependencies:
243 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --all
Nikhil Raj542c8482023-04-26 16:22:10 +0100244Setup for aarch64 with the existing TF Lite Delegate and TF Lite Parser dependencies only:
245 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --tflite-classic-delegate --tflite-parser
James Conroy919ec712022-07-13 12:57:53 +0100246EOF
247}
248
249# This will catch in validation.sh if not set
250target_arch=""
251
252# Default flag values
Nikhil Raj542c8482023-04-26 16:22:10 +0100253flag_tflite_classic_delegate=0
254flag_tflite_opaque_delegate=0
James Conroy919ec712022-07-13 12:57:53 +0100255flag_tflite_parser=0
256flag_onnx_parser=0
257
258# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
259num_threads=0
260
261name=$(basename "$0")
262
263# If no options provided, show help
264if [ $# -eq 0 ]; then
265 usage
266 exit 1
267fi
268
Nikhil Raj542c8482023-04-26 16:22:10 +0100269args=$(getopt -ohx -l tflite-classic-delegate,tflite-opaque-delegate,tflite-parser,onnx-parser,all,target-arch:,num-threads:,help -n "$name" -- "$@")
James Conroy919ec712022-07-13 12:57:53 +0100270eval set -- "$args"
271while [ $# -gt 0 ]; do
272 if [ -n "${opt_prev:-}" ]; then
273 eval "$opt_prev=\$1"
274 opt_prev=
275 shift 1
276 continue
277 elif [ -n "${opt_append:-}" ]; then
278 if [ -n "$1" ]; then
279 eval "$opt_append=\"\${$opt_append:-} \$1\""
280 fi
281 opt_append=
282 shift 1
283 continue
284 fi
285 case $1 in
286 --tflite-parser)
287 flag_tflite_parser=1
288 ;;
289
Nikhil Raj542c8482023-04-26 16:22:10 +0100290 --tflite-classic-delegate)
291 flag_tflite_classic_delegate=1
292 ;;
293
294 --tflite-opaque-delegate)
295 flag_tflite_opaque_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100296 ;;
297
298 --onnx-parser)
299 flag_onnx_parser=1
300 ;;
301
302 --all)
Nikhil Raj542c8482023-04-26 16:22:10 +0100303 flag_tflite_classic_delegate=1
304 flag_tflite_opaque_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100305 flag_tflite_parser=1
306 flag_onnx_parser=1
307 ;;
308
309 --target-arch)
310 opt_prev=target_arch
311 ;;
312
313 --num-threads)
314 opt_prev=num_threads
315 ;;
316
317 -h | --help)
318 usage
319 exit 0
320 ;;
321
322 -x)
323 set -x
324 ;;
325
326 --)
327 shift
328 break 2
329 ;;
330
331 esac
332 shift 1
333done
334
335# shellcheck source=common.sh
336source "$rel_path"/common.sh
337
338echo -e "\nINFO: Displaying configuration information before execution of $name"
Nikhil Raj542c8482023-04-26 16:22:10 +0100339echo " target-arch: $TARGET_ARCH"
340echo " host-arch: $HOST_ARCH"
341echo "tflite-classic-delegate: $flag_tflite_classic_delegate"
342echo "tflite-opaque-delegate : $flag_tflite_opaque_delegate"
343echo " tflite-parser: $flag_tflite_parser"
344echo " onnx-parser: $flag_onnx_parser"
345echo " num-threads: $NUM_THREADS"
346echo " root directory: $ROOT_DIR"
347echo " source directory: $SOURCE_DIR"
348echo " build directory: $BUILD_DIR"
James Conroy919ec712022-07-13 12:57:53 +0100349
James Conroy210897d2022-08-04 16:55:05 +0100350if check_if_repository .; then
James Conroy919ec712022-07-13 12:57:53 +0100351 echo -e "\n***** WARNING: Running script inside a git repository. To avoid nested repos, call this script from outside of this repo. *****"
352fi
353
354echo -e "\nScript execution will begin in 10 seconds..."
355
356sleep 10
357
358mkdir -p "$SOURCE_DIR"
359mkdir -p "$BUILD_DIR"
360
Nikhil Raj542c8482023-04-26 16:22:10 +0100361if [ "$flag_tflite_classic_delegate" -eq 1 ] || [ "$flag_tflite_opaque_delegate" -eq 1 ] || [ "$flag_tflite_parser" -eq 1 ]; then
James Conroy919ec712022-07-13 12:57:53 +0100362 download_flatbuffers
363
364 # Host build
365 build_flatbuffers 1
366
367 # Target build for cross compile
368 if [ "$NATIVE_BUILD" -eq 0 ]; then
369 build_flatbuffers 0
370 fi
371
372 download_tensorflow
373fi
374
375if [ "$flag_tflite_parser" -eq 1 ]; then
376 generate_tflite_schema
377fi
378
Nikhil Raj542c8482023-04-26 16:22:10 +0100379if [ "$flag_tflite_classic_delegate" -eq 1 ] || [ "$flag_tflite_opaque_delegate" -eq 1 ]; then
James Conroy919ec712022-07-13 12:57:53 +0100380 build_tflite
381fi
382
383if [ "$flag_onnx_parser" -eq 1 ]; then
384 download_protobuf
385
386 # Host build
387 build_protobuf 1
388
389 # Target build for cross compile
390 if [ "$NATIVE_BUILD" -eq 0 ]; then
391 build_protobuf 0
392 fi
393
394 download_onnx
395 generate_onnx_sources
396fi
397
398echo -e "\n***** Arm NN setup complete. Now build with build-armnn.sh. *****\n"
399
400exit 0