1#!/bin/bash
2# Copyright 2017 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
17set -e
18
19SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20cd "$SCRIPT_DIR/../../../.."
21
22DOWNLOADS_DIR=tensorflow/lite/tools/make/downloads
23BZL_FILE_PATH=tensorflow/workspace.bzl
24
25if [[ "${OSTYPE}" == "darwin"* ]]; then
26  function sha256sum() { shasum -a 256 "$@" ; }
27fi
28
29# Ensure it is being run from repo root
30if [ ! -f $BZL_FILE_PATH ]; then
31  echo "Could not find ${BZL_FILE_PATH}":
32  echo "Likely you are not running this from the root directory of the repository.";
33  exit 1;
34fi
35
36EIGEN_URL="$(grep -o 'https.*gitlab.com/libeigen/eigen/-/archive/.*tar\.gz' "${BZL_FILE_PATH}" | grep -v mirror.tensorflow | head -n1)"
37EIGEN_SHA="$(eval echo $(grep '# SHARED_EIGEN_SHA' "${BZL_FILE_PATH}" | grep -o '\".*\"'))"
38GEMMLOWP_URL="$(grep -o 'https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/gemmlowp/.*zip' "${BZL_FILE_PATH}" | head -n1)"
39GEMMLOWP_SHA="$(eval echo $(grep '# SHARED_GEMMLOWP_SHA' "${BZL_FILE_PATH}" | grep -o '\".*\"'))"
40RUY_URL="https://github.com/google/ruy/archive/4790797d11a81f96baf24f3731fd3ca44c2c5f8b.zip"
41RUY_SHA="28331222625e677be004e96da5e9a1cc9d65187d04d70d1ab2ca58445461ecbc"
42GOOGLETEST_URL="https://github.com/google/googletest/archive/release-1.8.0.tar.gz"
43GOOGLETEST_SHA="58a6f4277ca2bc8565222b3bbd58a177609e9c488e8a72649359ba51450db7d8"
44ABSL_URL="$(grep -o 'https://github.com/abseil/abseil-cpp/.*tar.gz' "${BZL_FILE_PATH}" | head -n1)"
45ABSL_SHA="$(eval echo $(grep '# SHARED_ABSL_SHA' "${BZL_FILE_PATH}" | grep -o '\".*\"'))"
46NEON_2_SSE_URL="https://github.com/intel/ARM_NEON_2_x86_SSE/archive/master.zip"
47FARMHASH_URL="https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/farmhash/archive/816a4ae622e964763ca0862d9dbd19324a1eaf45.tar.gz"
48FARMHASH_SHA="$(eval echo $(grep '# SHARED_FARMHASH_SHA' "${BZL_FILE_PATH}" | grep -o '\".*\"'))"
49FLATBUFFERS_URL="https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz"
50FLATBUFFERS_SHA="62f2223fb9181d1d6338451375628975775f7522185266cd5296571ac152bc45"
51FFT2D_URL="https://storage.googleapis.com/mirror.tensorflow.org/www.kurims.kyoto-u.ac.jp/~ooura/fft2d.tgz"
52FP16_URL="https://github.com/Maratyszcza/FP16/archive/febbb1c163726b5db24bed55cc9dc42529068997.zip"
53FFT2D_SHA="ada7e99087c4ed477bfdf11413f2ba8db8a840ba9bbf8ac94f4f3972e2a7cec9"
54CPUINFO_URL="https://github.com/pytorch/cpuinfo/archive/c2092219e7c874783a00a62edb94ddc672f57ab3.zip"
55CPUINFO_SHA="ea56c399a4f6ca5f749e71acb6a7bfdc653eb65d8f658cb2e414a2fcdca1fe8b"
56# TODO(petewarden): Some new code in Eigen triggers a clang bug with iOS arm64,
57#                   so work around it by patching the source.
58replace_by_sed() {
59  local regex="${1}"
60  shift
61  # Detect the version of sed by the return value of "--version" flag. GNU-sed
62  # supports "--version" while BSD-sed doesn't.
63  if ! sed --version >/dev/null 2>&1; then
64    # BSD-sed.
65    sed -i '' -e "${regex}" "$@"
66  else
67    # GNU-sed.
68    sed -i -e "${regex}" "$@"
69  fi
70}
71
72download_and_extract() {
73  local usage="Usage: download_and_extract URL DIR [SHA256]"
74  local url="${1:?${usage}}"
75  local dir="${2:?${usage}}"
76  local sha256="${3}"
77  echo "downloading ${url}" >&2
78  mkdir -p "${dir}"
79  rm -rf ${dir}/*  # Delete existing files.
80  tempdir=$(mktemp -d)
81  filepath="${tempdir}/$(basename ${url})"
82  curl -Lo ${filepath} ${url}
83  if [ -n "${sha256}" ]; then
84    echo "checking sha256 of ${dir}"
85    echo "${sha256}  ${filepath}" | sha256sum -c
86  fi
87  if [[ "${url}" == *gz ]]; then
88    tar -C "${dir}" --strip-components=1 -xzf ${filepath}
89  elif [[ "${url}" == *zip ]]; then
90    tempdir2=$(mktemp -d)
91    unzip ${filepath} -d ${tempdir2}
92
93    # If the zip file contains nested directories, extract the files from the
94    # inner directory.
95    if ls ${tempdir2}/*/* 1> /dev/null 2>&1; then
96      # unzip has no strip components, so unzip to a temp dir, and move the
97      # files we want from the tempdir to destination.
98      cp -R ${tempdir2}/*/* ${dir}/
99    else
100      cp -R ${tempdir2}/* ${dir}/
101    fi
102    rm -rf ${tempdir2}
103  fi
104  rm -rf ${tempdir}
105
106  # Delete any potential BUILD files, which would interfere with Bazel builds.
107  find "${dir}" -type f -name '*BUILD' -delete
108}
109
110download_and_extract "${EIGEN_URL}" "${DOWNLOADS_DIR}/eigen" "${EIGEN_SHA}"
111download_and_extract "${GEMMLOWP_URL}" "${DOWNLOADS_DIR}/gemmlowp" "${GEMMLOWP_SHA}"
112download_and_extract "${RUY_URL}" "${DOWNLOADS_DIR}/ruy" "${RUY_SHA}"
113download_and_extract "${GOOGLETEST_URL}" "${DOWNLOADS_DIR}/googletest" "${GOOGLETEST_SHA}"
114download_and_extract "${ABSL_URL}" "${DOWNLOADS_DIR}/absl" "${ABSL_SHA}"
115download_and_extract "${NEON_2_SSE_URL}" "${DOWNLOADS_DIR}/neon_2_sse"
116download_and_extract "${FARMHASH_URL}" "${DOWNLOADS_DIR}/farmhash" "${FARMHASH_SHA}"
117download_and_extract "${FLATBUFFERS_URL}" "${DOWNLOADS_DIR}/flatbuffers" "${FLATBUFFERS_SHA}"
118download_and_extract "${FFT2D_URL}" "${DOWNLOADS_DIR}/fft2d" "${FFT2D_SHA}"
119download_and_extract "${FP16_URL}" "${DOWNLOADS_DIR}/fp16"
120download_and_extract "${CPUINFO_URL}" "${DOWNLOADS_DIR}/cpuinfo"
121
122replace_by_sed 's#static uint32x4_t p4ui_CONJ_XOR = vld1q_u32( conj_XOR_DATA );#static uint32x4_t p4ui_CONJ_XOR; // = vld1q_u32( conj_XOR_DATA ); - Removed by script#' \
123  "${DOWNLOADS_DIR}/eigen/Eigen/src/Core/arch/NEON/Complex.h"
124replace_by_sed 's#static uint32x2_t p2ui_CONJ_XOR = vld1_u32( conj_XOR_DATA );#static uint32x2_t p2ui_CONJ_XOR;// = vld1_u32( conj_XOR_DATA ); - Removed by scripts#' \
125  "${DOWNLOADS_DIR}/eigen/Eigen/src/Core/arch/NEON/Complex.h"
126replace_by_sed 's#static uint64x2_t p2ul_CONJ_XOR = vld1q_u64( p2ul_conj_XOR_DATA );#static uint64x2_t p2ul_CONJ_XOR;// = vld1q_u64( p2ul_conj_XOR_DATA ); - Removed by script#' \
127  "${DOWNLOADS_DIR}/eigen/Eigen/src/Core/arch/NEON/Complex.h"
128
129echo "download_dependencies.sh completed successfully." >&2
130