blob: e5b4c2160bed2bcc1a85c5834669e308ad21fbf1 [file] [log] [blame]
James Conroy919ec712022-07-13 12:57:53 +01001#!/bin/bash
2#
John Mcloughlin35bae832023-07-24 11:55:13 +01003# Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
James Conroy919ec712022-07-13 12:57:53 +01004# 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
John Mcloughlin35bae832023-07-24 11:55:13 +010037download_androidndk()
38{
39 cd "$SOURCE_DIR"
40 echo -e "\n***** Downloading Android NDK *****\n"
Nikhil Raj58a49232023-12-21 12:08:42 +000041 wget https://dl.google.com/android/repository/android-ndk-r26b-linux.zip
John Mcloughlin35bae832023-07-24 11:55:13 +010042 echo -e "\n***** Extracting archive *****"
Nikhil Raj58a49232023-12-21 12:08:42 +000043 unzip android-ndk-r26b-linux.zip
John Mcloughlin35bae832023-07-24 11:55:13 +010044 echo -e "\n***** Removing archive *****"
Nikhil Raj58a49232023-12-21 12:08:42 +000045 rm android-ndk-r26b-linux.zip
John Mcloughlin35bae832023-07-24 11:55:13 +010046 echo -e "\n***** Android NDK downloaded *****"
47}
48
James Conroy919ec712022-07-13 12:57:53 +010049download_protobuf()
50{
51 download_and_extract \
52 "Protobuf" \
53 "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz" \
54 "protobuf-all-$PROTOBUF_VERSION.tar.gz"
55}
56
57build_protobuf()
58{
59 local native_build=$1
60 local build_dir="$PROTOBUF_BUILD_TARGET"
61 local cmake_flags=""
62 local target_arch="$TARGET_ARCH"
63 local additional_cmds=""
64
65 if [ "$native_build" -eq 0 ]; then
66 mkdir -p "$PROTOBUF_BUILD_TARGET"
67 additional_cmds+="--with-protoc=$PROTOCOL_COMPILER_HOST "
68 if [ "$TARGET_ARCH" == "aarch64" ]; then
69 cmake_flags+="$AARCH64_COMPILER_FLAGS"
70 additional_cmds+="--host=aarch64-linux "
James Conroy919ec712022-07-13 12:57:53 +010071 fi
John Mcloughlin35bae832023-07-24 11:55:13 +010072 if [ "$TARGET_ARCH" == "android64" ]; then
73 additional_cmds+="--host=aarch64-linux-android "
74 cmake_flags+="$ANDROID64_COMPILER_FLAGS"
75 fi
James Conroy919ec712022-07-13 12:57:53 +010076 else
77 target_arch="$HOST_ARCH"
78 mkdir -p "$PROTOBUF_BUILD_HOST"
79 build_dir="$PROTOBUF_BUILD_HOST"
80 fi
81
82 echo -e "\n***** Building Protobuf for $target_arch ***** "
83
84 cd "$PROTOBUF_BUILD_ROOT"
85
86 # Cleanup any previous cmake files, except actual builds which we keep
87 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
88
John Mcloughlin35bae832023-07-24 11:55:13 +010089 if [ "$native_build" -eq 0 ] && [ "$TARGET_ARCH" == "android64" ]; then
90 eval "$cmake_flags"
91 cmake -DCMAKE_ANDROID_NDK="$NDK_SRC" \
92 -DCMAKE_SYSTEM_NAME=Android \
93 -DCMAKE_SYSTEM_VERSION="$ANDROID_API_VERSION" \
94 -DCMAKE_ANDROID_ARCH_ABI="$ANDROID_ARM_ARCH" \
95 -DCMAKE_CXX_FLAGS=--std=c++14 \
96 -Dprotobuf_BUILD_TESTS=OFF \
97 -Dprotobuf_BUILD_SHARED_LIBS=ON \
98 -Dprotobuf_WITH_ZLIB=OFF \
99 -DCMAKE_BUILD_TYPE=Release \
100 $PROTOBUF_SRC/cmake/
101 make libprotobuf -j "$NUM_THREADS"
102 cmake -DCMAKE_INSTALL_PREFIX=$PROTOBUF_BUILD_TARGET -DCOMPONENT=libprotobuf -P cmake_install.cmake
103 cmake -DCMAKE_INSTALL_PREFIX=$PROTOBUF_BUILD_TARGET -DCOMPONENT=protobuf-headers -P cmake_install.cmake
104 else
105 eval "$cmake_flags" \
106 "$PROTOBUF_SRC"/configure --prefix="$build_dir" "$additional_cmds"
107 make install -j "$NUM_THREADS"
108 fi
James Conroy919ec712022-07-13 12:57:53 +0100109
110 echo -e "\n***** Protobuf built for $target_arch ***** "
111}
112
113download_flatbuffers()
114{
115 download_and_extract \
116 "Flatbuffers" \
117 "https://github.com/google/flatbuffers/archive/v$FLATBUFFERS_VERSION.tar.gz" \
118 "flatbuffers-$FLATBUFFERS_VERSION.tar.gz"
119}
120
121build_flatbuffers()
122{
123 local native_build=$1
124 local build_dir="$FLATBUFFERS_BUILD_TARGET"
125 local target_arch="$TARGET_ARCH"
126
127 local cmake_flags="CXXFLAGS=-fPIC "
128
129 if [ "$native_build" -eq 0 ]; then
130 mkdir -p "$FLATBUFFERS_BUILD_TARGET"
Nikhil Rajba6dcb22023-11-24 16:25:10 +0000131 if [ "$TARGET_ARCH" == "aarch64" ]; then
James Conroy919ec712022-07-13 12:57:53 +0100132 cmake_flags+="$AARCH64_COMPILER_FLAGS"
James Conroy919ec712022-07-13 12:57:53 +0100133 fi
Nikhil Rajba6dcb22023-11-24 16:25:10 +0000134 if [ "$TARGET_ARCH" == "android64" ]; then
135 cmake_flags+="$ANDROID64_COMPILER_FLAGS"
136 fi
James Conroy919ec712022-07-13 12:57:53 +0100137 else
138 target_arch="$HOST_ARCH"
139 mkdir -p "$FLATBUFFERS_BUILD_HOST"
140 build_dir="$FLATBUFFERS_BUILD_HOST"
141 fi
142
143 echo -e "\n***** Building flatbuffers for $target_arch *****"
144
145 mkdir -p "$FLATBUFFERS_BUILD_ROOT"
146 cd "$FLATBUFFERS_BUILD_ROOT"
147
148 # Cleanup any previous cmake files, except actual builds which we keep
149 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
150
John Mcloughlin35bae832023-07-24 11:55:13 +0100151 if [ "$native_build" -eq 0 ] && [ "$TARGET_ARCH" == "android64" ]; then
152 eval "$cmake_flags" \
153 cmake -DCMAKE_ANDROID_NDK="$NDK_SRC" \
154 -DCMAKE_SYSTEM_NAME=Android \
155 -DCMAKE_SYSTEM_VERSION="$ANDROID_API_VERSION" \
156 -DCMAKE_ANDROID_ARCH_ABI="$ANDROID_ARM_ARCH" \
157 -DCMAKE_CXX_FLAGS=--std=c++14 \
158 -DFLATBUFFERS_BUILD_FLATC=0 \
159 -DCMAKE_INSTALL_PREFIX:PATH="$build_dir" \
160 -DCMAKE_BUILD_TYPE=Release \
161 -DFLATBUFFERS_BUILD_TESTS=0 \
162 "$FLATBUFFERS_SRC"
163 else
164 eval "$cmake_flags" \
165 cmake -DFLATBUFFERS_BUILD_FLATC="$native_build" \
166 -DCMAKE_INSTALL_PREFIX:PATH="$build_dir" \
167 -DFLATBUFFERS_BUILD_TESTS=0 \
168 "$FLATBUFFERS_SRC"
169 fi
James Conroy919ec712022-07-13 12:57:53 +0100170 make all install -j "$NUM_THREADS"
171
172 echo -e "\n***** Built flatbuffers for $target_arch *****"
173}
174
175download_tensorflow()
176{
177 cd "$SOURCE_DIR"
178
179 echo -e "\n***** Downloading TensorFlow *****"
180 git clone https://github.com/tensorflow/tensorflow.git
181 cd "$TENSORFLOW_SRC"
182
James Conroy210897d2022-08-04 16:55:05 +0100183 git checkout "$TENSORFLOW_VERSION"
James Conroy919ec712022-07-13 12:57:53 +0100184 echo -e "\n***** TensorFlow downloaded *****"
185}
186
187build_tflite()
188{
189 mkdir -p "$TFLITE_BUILD_TARGET"
190 cd "$TFLITE_BUILD_TARGET"
191
192 local target_arch_cmd="" # default is native, no command needed
193 local cmake_flags=""
194
195 case "$TARGET_ARCH" in
196 "aarch64")
197 cmake_flags+="$AARCH64_COMPILER_FLAGS"
James Conroy2e950f42022-11-01 15:01:06 +0000198 target_arch_cmd="-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
199 -DCMAKE_SYSTEM_NAME=Linux "
James Conroy919ec712022-07-13 12:57:53 +0100200
201 if [ "$NATIVE_BUILD" -eq 0 ]; then
202 cmake_flags+="ARMCC_FLAGS='-funsafe-math-optimizations' "
203 fi
204 ;;
John Mcloughlin35bae832023-07-24 11:55:13 +0100205 "android64")
Nikhil Rajba6dcb22023-11-24 16:25:10 +0000206 cmake_flags+="$ANDROID64_COMPILER_FLAGS"
John Mcloughlin35bae832023-07-24 11:55:13 +0100207 if [ "$NATIVE_BUILD" -eq 0 ]; then
208 target_arch_cmd="-DCMAKE_TOOLCHAIN_FILE=$NDK_SRC/build/cmake/android.toolchain.cmake \
209 -DANDROID_ABI=$ANDROID_ARM_ARCH \
210 -DANDROID_PLATFORM=$ANDROID_API_VERSION"
211 fi
212 ;;
James Conroy919ec712022-07-13 12:57:53 +0100213 esac
214
215 echo -e "\n***** Building TF Lite for $TARGET_ARCH *****"
216
217 # Cleanup any previous cmake files, except actual builds which we keep
218 find . -mindepth 1 -name "*_build" -prune -o -exec rm -rf {} +
219
220 eval "$cmake_flags" \
221 cmake -DTFLITE_ENABLE_XNNPACK=OFF \
Colm Donelan3aad6f62023-04-20 21:56:11 +0100222 -DFLATBUFFERS_BUILD_FLATC=OFF \
223 -DBUILD_SHARED_LIBS=OFF \
224 -DBUILD_TESTING=OFF \
James Conroy919ec712022-07-13 12:57:53 +0100225 "$target_arch_cmd" \
226 "$TFLITE_SRC"
227 cmake --build . -j "$NUM_THREADS"
228
229 echo -e "\n***** Built TF Lite for $TARGET_ARCH *****"
230}
231
232generate_tflite_schema()
233{
234 echo -e "\n***** Generating TF Lite Schema *****"
235 mkdir -p "$TFLITE_BUILD_ROOT"
236 cd "$TFLITE_BUILD_ROOT"
237
238 cp "$SCHEMA_SRC" .
239
240 $FLATC -c --gen-object-api --reflect-types --reflect-names schema.fbs
241
242 echo -e "\n***** Generated TF Lite Schema *****"
243}
244
245download_onnx()
246{
247 download_and_extract \
248 "ONNX" \
249 "https://github.com/onnx/onnx/releases/download/v$ONNX_VERSION/onnx-$ONNX_VERSION.tar.gz" \
250 "onnx-$ONNX_VERSION.tar.gz"
251}
252
253generate_onnx_sources()
254{
255 mkdir -p "$ONNX_BUILD_TARGET"
256 cd "$ONNX_SRC"
257
258 echo -e "\n***** Generating ONNX sources for $TARGET_ARCH *****"
259
260 export LD_LIBRARY_PATH="$PROTOBUF_BUILD_HOST"/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
261
262 eval "$PROTOCOL_COMPILER_HOST" onnx/onnx.proto \
263 --proto_path=. \
264 --proto_path="$ONNX_SRC" \
265 --proto_path="$PROTOBUF_BUILD_HOST"/include \
266 --cpp_out "$ONNX_BUILD_TARGET"
267
268 echo -e "\n***** Generated ONNX sources for $TARGET_ARCH *****"
269}
270
James Conroy919ec712022-07-13 12:57:53 +0100271usage()
272{
273 cat <<EOF
274setup-armnn.sh - Download and build Arm NN dependencies in the current directory (ROOT_DIR)
275setup-armnn.sh [OPTION]...
Nikhil Raj542c8482023-04-26 16:22:10 +0100276 --tflite-classic-delegate
277 setup dependencies for the existing Arm NN TF Lite Delegate
278 --tflite-opaque-delegate
279 setup dependencies for the new Opaque Delegate
James Conroy919ec712022-07-13 12:57:53 +0100280 --tflite-parser
281 setup dependencies for the Arm NN TF Lite Parser
282 --onnx-parser
283 setup dependencies for the Arm NN ONNX parser
284 --all
285 setup dependencies for all Arm NN components listed above
John Mcloughlin35bae832023-07-24 11:55:13 +0100286 --target-arch=[aarch64|android64|x86_64]
James Conroy919ec712022-07-13 12:57:53 +0100287 specify a target architecture (mandatory)
288 --num-threads=<INTEGER>
289 specify number of threads/cores to build dependencies with (optional: defaults to number of online CPU cores on host)
290 -h, --help
291 print brief usage information and exit
292 -x
293 enable shell tracing in this script
294
Nikhil Raj542c8482023-04-26 16:22:10 +0100295At 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 +0100296Directories called "source" and "build" will be generated in the current directory (ROOT_DIR) from which this script is called.
297It's recommended to call this script in a directory outside of this Arm NN source repo, to avoid nested repositories.
298
James Conroy919ec712022-07-13 12:57:53 +0100299Examples:
300Setup for aarch64 with all Arm NN dependencies:
301 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --all
Nikhil Raj542c8482023-04-26 16:22:10 +0100302Setup for aarch64 with the existing TF Lite Delegate and TF Lite Parser dependencies only:
303 <PATH_TO>/setup-armnn.sh --target-arch=aarch64 --tflite-classic-delegate --tflite-parser
James Conroy919ec712022-07-13 12:57:53 +0100304EOF
305}
306
307# This will catch in validation.sh if not set
308target_arch=""
309
310# Default flag values
Nikhil Raj542c8482023-04-26 16:22:10 +0100311flag_tflite_classic_delegate=0
312flag_tflite_opaque_delegate=0
James Conroy919ec712022-07-13 12:57:53 +0100313flag_tflite_parser=0
314flag_onnx_parser=0
315
316# If --num-threads is not set, the default NUM_THREADS value in common.sh will be used
317num_threads=0
318
319name=$(basename "$0")
320
321# If no options provided, show help
322if [ $# -eq 0 ]; then
323 usage
324 exit 1
325fi
326
Nikhil Raj542c8482023-04-26 16:22:10 +0100327args=$(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 +0100328eval set -- "$args"
329while [ $# -gt 0 ]; do
330 if [ -n "${opt_prev:-}" ]; then
331 eval "$opt_prev=\$1"
332 opt_prev=
333 shift 1
334 continue
335 elif [ -n "${opt_append:-}" ]; then
336 if [ -n "$1" ]; then
337 eval "$opt_append=\"\${$opt_append:-} \$1\""
338 fi
339 opt_append=
340 shift 1
341 continue
342 fi
343 case $1 in
344 --tflite-parser)
345 flag_tflite_parser=1
346 ;;
347
Nikhil Raj542c8482023-04-26 16:22:10 +0100348 --tflite-classic-delegate)
349 flag_tflite_classic_delegate=1
350 ;;
351
352 --tflite-opaque-delegate)
353 flag_tflite_opaque_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100354 ;;
355
356 --onnx-parser)
357 flag_onnx_parser=1
358 ;;
359
360 --all)
Nikhil Raj542c8482023-04-26 16:22:10 +0100361 flag_tflite_classic_delegate=1
362 flag_tflite_opaque_delegate=1
James Conroy919ec712022-07-13 12:57:53 +0100363 flag_tflite_parser=1
364 flag_onnx_parser=1
365 ;;
366
367 --target-arch)
368 opt_prev=target_arch
369 ;;
370
371 --num-threads)
372 opt_prev=num_threads
373 ;;
374
375 -h | --help)
376 usage
377 exit 0
378 ;;
379
380 -x)
381 set -x
382 ;;
383
384 --)
385 shift
386 break 2
387 ;;
388
389 esac
390 shift 1
391done
392
393# shellcheck source=common.sh
394source "$rel_path"/common.sh
395
396echo -e "\nINFO: Displaying configuration information before execution of $name"
Nikhil Raj542c8482023-04-26 16:22:10 +0100397echo " target-arch: $TARGET_ARCH"
398echo " host-arch: $HOST_ARCH"
399echo "tflite-classic-delegate: $flag_tflite_classic_delegate"
400echo "tflite-opaque-delegate : $flag_tflite_opaque_delegate"
401echo " tflite-parser: $flag_tflite_parser"
402echo " onnx-parser: $flag_onnx_parser"
403echo " num-threads: $NUM_THREADS"
404echo " root directory: $ROOT_DIR"
405echo " source directory: $SOURCE_DIR"
406echo " build directory: $BUILD_DIR"
James Conroy919ec712022-07-13 12:57:53 +0100407
James Conroy210897d2022-08-04 16:55:05 +0100408if check_if_repository .; then
James Conroy919ec712022-07-13 12:57:53 +0100409 echo -e "\n***** WARNING: Running script inside a git repository. To avoid nested repos, call this script from outside of this repo. *****"
410fi
411
412echo -e "\nScript execution will begin in 10 seconds..."
413
414sleep 10
415
416mkdir -p "$SOURCE_DIR"
417mkdir -p "$BUILD_DIR"
418
John Mcloughlin35bae832023-07-24 11:55:13 +0100419if [ "$TARGET_ARCH" == "android64" ]; then
420 download_androidndk
421fi
422
Nikhil Raj542c8482023-04-26 16:22:10 +0100423if [ "$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 +0100424 download_flatbuffers
425
426 # Host build
427 build_flatbuffers 1
428
429 # Target build for cross compile
430 if [ "$NATIVE_BUILD" -eq 0 ]; then
431 build_flatbuffers 0
432 fi
433
434 download_tensorflow
435fi
436
437if [ "$flag_tflite_parser" -eq 1 ]; then
438 generate_tflite_schema
439fi
440
Nikhil Raj542c8482023-04-26 16:22:10 +0100441if [ "$flag_tflite_classic_delegate" -eq 1 ] || [ "$flag_tflite_opaque_delegate" -eq 1 ]; then
James Conroy919ec712022-07-13 12:57:53 +0100442 build_tflite
443fi
444
445if [ "$flag_onnx_parser" -eq 1 ]; then
446 download_protobuf
447
448 # Host build
449 build_protobuf 1
450
451 # Target build for cross compile
452 if [ "$NATIVE_BUILD" -eq 0 ]; then
453 build_protobuf 0
454 fi
455
456 download_onnx
457 generate_onnx_sources
458fi
459
460echo -e "\n***** Arm NN setup complete. Now build with build-armnn.sh. *****\n"
461
Nikhil Rajba6dcb22023-11-24 16:25:10 +0000462exit 0