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# Called with following arguments: 18# 1 - Path to the downloads folder which is typically 19# tensorflow/lite/micro/tools/make/downloads 20# 21# This script is called from the Makefile and uses the following convention to 22# enable determination of sucess/failure: 23# 24# - If the script is successful, the only output on stdout should be SUCCESS. 25# The makefile checks for this particular string. 26# 27# - Any string on stdout that is not SUCCESS will be shown in the makefile as 28# the cause for the script to have failed. 29# 30# - Any other informational prints should be on stderr. 31 32set -e 33 34SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 35ROOT_DIR=${SCRIPT_DIR}/../../../../.. 36cd "${ROOT_DIR}" 37 38source tensorflow/lite/micro/tools/make/bash_helpers.sh 39 40DOWNLOADS_DIR=${1} 41if [ ! -d ${DOWNLOADS_DIR} ]; then 42 echo "The top-level downloads directory: ${DOWNLOADS_DIR} does not exist." 43 exit 1 44fi 45 46# TODO(b/173239141): Patch flatbuffers to avoid pulling in extra symbols from 47# strtod that are not used at runtime but are still problematic on the 48# Bluepill platform. 49# 50# Parameter(s): 51# $1 - full path to the downloaded flexbuffers.h that will be patched in-place. 52function patch_to_avoid_strtod() { 53 local input_flexbuffers_path="$1" 54 local temp_flexbuffers_path="/tmp/flexbuffers_patched.h" 55 local string_to_num_line=`awk '/StringToNumber/{ print NR; }' ${input_flexbuffers_path}` 56 local case_string_line=$((${string_to_num_line} - 2)) 57 58 head -n ${case_string_line} ${input_flexbuffers_path} > ${temp_flexbuffers_path} 59 60 echo "#if 1" >> ${temp_flexbuffers_path} 61 echo "#pragma GCC diagnostic push" >> ${temp_flexbuffers_path} 62 echo "#pragma GCC diagnostic ignored \"-Wnull-dereference\"" >> ${temp_flexbuffers_path} 63 echo " // TODO(b/173239141): Patched via micro/tools/make/flexbuffers_download.sh" >> ${temp_flexbuffers_path} 64 echo " // Introduce a segfault for an unsupported code path for TFLM." >> ${temp_flexbuffers_path} 65 echo " return *(static_cast<double*>(nullptr));" >> ${temp_flexbuffers_path} 66 echo "#pragma GCC diagnostic pop" >> ${temp_flexbuffers_path} 67 echo "#else" >> ${temp_flexbuffers_path} 68 echo " // This is the original code" >> ${temp_flexbuffers_path} 69 sed -n -e $((${string_to_num_line} - 1)),$((${string_to_num_line} + 1))p ${input_flexbuffers_path} >> ${temp_flexbuffers_path} 70 echo "#endif" >> ${temp_flexbuffers_path} 71 72 local total_num_lines=`wc -l ${input_flexbuffers_path} | awk '{print $1}'` 73 sed -n -e $((${string_to_num_line} + 2)),${total_num_lines}p ${input_flexbuffers_path} >> ${temp_flexbuffers_path} 74 mv ${input_flexbuffers_path} ${input_flexbuffers_path}.orig 75 mv ${temp_flexbuffers_path} ${input_flexbuffers_path} 76} 77 78# The BUILD files in the downloaded folder result in an error with: 79# bazel build tensorflow/lite/micro/... 80# 81# Parameters: 82# $1 - path to the downloaded flatbuffers code. 83function delete_build_files() { 84 rm -f `find ${1} -name BUILD` 85 rm -f `find ${1} -name BUILD.bazel` 86} 87 88DOWNLOADED_FLATBUFFERS_PATH=${DOWNLOADS_DIR}/flatbuffers 89 90if [ -d ${DOWNLOADED_FLATBUFFERS_PATH} ]; then 91 echo >&2 "${DOWNLOADED_FLATBUFFERS_PATH} already exists, skipping the download." 92else 93 ZIP_PREFIX="dca12522a9f9e37f126ab925fd385c807ab4f84e" 94 FLATBUFFERS_URL="http://mirror.tensorflow.org/github.com/google/flatbuffers/archive/${ZIP_PREFIX}.zip" 95 FLATBUFFERS_MD5="aa9adc93eb9b33fa1a2a90969e48baee" 96 97 wget ${FLATBUFFERS_URL} -O /tmp/${ZIP_PREFIX}.zip >&2 98 check_md5 /tmp/${ZIP_PREFIX}.zip ${FLATBUFFERS_MD5} 99 100 unzip -qo /tmp/${ZIP_PREFIX}.zip -d /tmp >&2 101 mv /tmp/flatbuffers-${ZIP_PREFIX} ${DOWNLOADED_FLATBUFFERS_PATH} 102 103 patch_to_avoid_strtod ${DOWNLOADED_FLATBUFFERS_PATH}/include/flatbuffers/flexbuffers.h 104 delete_build_files ${DOWNLOADED_FLATBUFFERS_PATH} 105fi 106 107echo "SUCCESS" 108