1#!/bin/bash
2# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16#
17# Downloads necessary to build with OPTIMIZED_KERNEL_DIR=xtensa.
18#
19# Called with four arguments:
20# 1 - Path to the downloads folder which is typically
21#     tensorflow/lite/micro/tools/make/downloads
22# 2 - Xtensa variant to download for (e.g. hifi4)
23#
24# This script is called from the Makefile and uses the following convention to
25# enable determination of sucess/failure:
26#
27#   - If the script is successful, the only output on stdout should be SUCCESS.
28#     The makefile checks for this particular string.
29#
30#   - Any string on stdout that is not SUCCESS will be shown in the makefile as
31#     the cause for the script to have failed.
32#
33#   - Any other informational prints should be on stderr.
34
35set -e
36
37SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
38ROOT_DIR=${SCRIPT_DIR}/../../../../../..
39cd "${ROOT_DIR}"
40
41source tensorflow/lite/micro/tools/make/bash_helpers.sh
42
43DOWNLOADS_DIR=${1}
44if [ ! -d ${DOWNLOADS_DIR} ]; then
45  echo "The top-level downloads directory: ${DOWNLOADS_DIR} does not exist."
46  exit 1
47fi
48
49if [[ ${2} == "hifi4" ]]; then
50  LIBRARY_URL="http://github.com/foss-xtensa/nnlib-hifi4/raw/master/archive/xa_nnlib_hifi4_07_27_2021.zip"
51  LIBRARY_DIRNAME="xa_nnlib_hifi4"
52  LIBRARY_MD5="24b8844f8e0c53c1ed8561b09968bb98"
53elif [[ ${2} == "hifi5" ]]; then
54  LIBRARY_URL="http://github.com/foss-xtensa/nnlib-hifi5/raw/master/archive/xa_nnlib_hifi5_06_30.zip"
55  LIBRARY_DIRNAME="xa_nnlib_hifi5"
56  LIBRARY_MD5="0c832b15d27ac557fa5453c902c5662a"
57else
58  echo "Attempting to download an unsupported xtensa variant: ${2}"
59  exit 1
60fi
61
62LIBRARY_INSTALL_PATH=${DOWNLOADS_DIR}/${LIBRARY_DIRNAME}
63
64if [ -d ${LIBRARY_INSTALL_PATH} ]; then
65  echo >&2 "${LIBRARY_INSTALL_PATH} already exists, skipping the download."
66else
67  TMP_ZIP_ARCHIVE_NAME="${LIBRARY_DIRNAME}.zip"
68  wget ${LIBRARY_URL} -O /tmp/${TMP_ZIP_ARCHIVE_NAME} >&2
69  MD5=`md5sum /tmp/${TMP_ZIP_ARCHIVE_NAME} | awk '{print $1}'`
70
71  if [[ ${MD5} != ${LIBRARY_MD5} ]]
72  then
73    echo "Bad checksum. Expected: ${LIBRARY_MD5}, Got: ${MD5}"
74    exit 1
75  fi
76
77  unzip -qo /tmp/${TMP_ZIP_ARCHIVE_NAME} -d ${DOWNLOADS_DIR} >&2
78
79fi
80
81echo "SUCCESS"
82