1#!/usr/bin/env bash 2# Copyright 2020 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# ============================================================================== 16set -ex 17 18SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 19PYTHON="${CI_BUILD_PYTHON:-python3}" 20VERSION_SUFFIX=${VERSION_SUFFIX:-} 21export TENSORFLOW_DIR="${SCRIPT_DIR}/../../../.." 22TENSORFLOW_LITE_DIR="${TENSORFLOW_DIR}/tensorflow/lite" 23TENSORFLOW_VERSION=$(grep "_VERSION = " "${TENSORFLOW_DIR}/tensorflow/tools/pip_package/setup.py" | cut -d= -f2 | sed "s/[ '-]//g") 24export PACKAGE_VERSION="${TENSORFLOW_VERSION}${VERSION_SUFFIX}" 25export PROJECT_NAME=${WHEEL_PROJECT_NAME:-tflite_runtime} 26BUILD_DIR="${SCRIPT_DIR}/gen/tflite_pip/${PYTHON}" 27TENSORFLOW_TARGET=${TENSORFLOW_TARGET:-$1} 28if [ "${TENSORFLOW_TARGET}" = "rpi" ]; then 29 export TENSORFLOW_TARGET="armhf" 30fi 31export CROSSTOOL_PYTHON_INCLUDE_PATH=$(${PYTHON} -c "from sysconfig import get_paths as gp; print(gp()['include'])") 32 33# Fix container image for cross build. 34if [ ! -z "${CI_BUILD_HOME}" ] && [ `pwd` = "/workspace" ]; then 35 # Fix for curl build problem in 32-bit, see https://stackoverflow.com/questions/35181744/size-of-array-curl-rule-01-is-negative 36 if [ "${TENSORFLOW_TARGET}" = "armhf" ] && [ -f /usr/include/curl/curlbuild.h ]; then 37 sudo sed -i 's/define CURL_SIZEOF_LONG 8/define CURL_SIZEOF_LONG 4/g' /usr/include/curl/curlbuild.h 38 sudo sed -i 's/define CURL_SIZEOF_CURL_OFF_T 8/define CURL_SIZEOF_CURL_OFF_T 4/g' /usr/include/curl/curlbuild.h 39 fi 40 41 # The system-installed OpenSSL headers get pulled in by the latest BoringSSL 42 # release on this configuration, so move them before we build: 43 if [ -d /usr/include/openssl ]; then 44 sudo mv /usr/include/openssl /usr/include/openssl.original 45 fi 46fi 47 48# Build source tree. 49rm -rf "${BUILD_DIR}" && mkdir -p "${BUILD_DIR}/tflite_runtime" 50cp -r "${TENSORFLOW_LITE_DIR}/tools/pip_package/debian" \ 51 "${TENSORFLOW_LITE_DIR}/tools/pip_package/MANIFEST.in" \ 52 "${TENSORFLOW_LITE_DIR}/python/interpreter_wrapper" \ 53 "${BUILD_DIR}" 54cp "${TENSORFLOW_LITE_DIR}/tools/pip_package/setup_with_binary.py" "${BUILD_DIR}/setup.py" 55cp "${TENSORFLOW_LITE_DIR}/python/interpreter.py" \ 56 "${TENSORFLOW_LITE_DIR}/python/metrics/metrics_interface.py" \ 57 "${TENSORFLOW_LITE_DIR}/python/metrics/metrics_portable.py" \ 58 "${BUILD_DIR}/tflite_runtime" 59echo "__version__ = '${PACKAGE_VERSION}'" >> "${BUILD_DIR}/tflite_runtime/__init__.py" 60echo "__git_version__ = '$(git -C "${TENSORFLOW_DIR}" describe)'" >> "${BUILD_DIR}/tflite_runtime/__init__.py" 61 62# Build python interpreter_wrapper. 63cd "${BUILD_DIR}" 64case "${TENSORFLOW_TARGET}" in 65 armhf) 66 BAZEL_FLAGS="--config=elinux_armhf 67 --copt=-march=armv7-a --copt=-mfpu=neon-vfpv4 68 --copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive 69 --define tensorflow_mkldnn_contraction_kernel=0 70 --define=raspberry_pi_with_neon=true" 71 ;; 72 aarch64) 73 BAZEL_FLAGS="--config=elinux_aarch64 74 --define tensorflow_mkldnn_contraction_kernel=0 75 --copt=-O3" 76 ;; 77 native) 78 BAZEL_FLAGS="--copt=-O3 --copt=-march=native" 79 ;; 80 *) 81 BAZEL_FLAGS="--copt=-O3" 82 ;; 83esac 84 85# We need to pass down the environment variable with a possible alternate Python 86# include path for Python 3.x builds to work. 87export CROSSTOOL_PYTHON_INCLUDE_PATH 88 89case "${TENSORFLOW_TARGET}" in 90 windows) 91 LIBRARY_EXTENSION=".pyd" 92 ;; 93 *) 94 LIBRARY_EXTENSION=".so" 95 ;; 96esac 97 98bazel ${BAZEL_STARTUP_OPTIONS} build -c opt -s --config=monolithic --config=noaws --config=nogcp --config=nohdfs --config=nonccl \ 99 ${BAZEL_FLAGS} ${CUSTOM_BAZEL_FLAGS} //tensorflow/lite/python/interpreter_wrapper:_pywrap_tensorflow_interpreter_wrapper 100cp "${TENSORFLOW_DIR}/bazel-bin/tensorflow/lite/python/interpreter_wrapper/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}" \ 101 "${BUILD_DIR}/tflite_runtime" 102# Bazel generates the wrapper library with r-x permissions for user. 103# At least on Windows, we need write permissions to delete the file. 104# Without this, setuptools fails to clean the build directory. 105chmod u+w "${BUILD_DIR}/tflite_runtime/_pywrap_tensorflow_interpreter_wrapper${LIBRARY_EXTENSION}" 106 107# Build python wheel. 108cd "${BUILD_DIR}" 109case "${TENSORFLOW_TARGET}" in 110 armhf) 111 WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-armv7l}" 112 ${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \ 113 bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME} 114 ;; 115 aarch64) 116 WHEEL_PLATFORM_NAME="${WHEEL_PLATFORM_NAME:-linux-aarch64}" 117 ${PYTHON} setup.py bdist --plat-name=${WHEEL_PLATFORM_NAME} \ 118 bdist_wheel --plat-name=${WHEEL_PLATFORM_NAME} 119 ;; 120 *) 121 if [[ -n "${TENSORFLOW_TARGET}" ]] && [[ -n "${TENSORFLOW_TARGET_ARCH}" ]]; then 122 ${PYTHON} setup.py bdist --plat-name=${TENSORFLOW_TARGET}-${TENSORFLOW_TARGET_ARCH} \ 123 bdist_wheel --plat-name=${TENSORFLOW_TARGET}-${TENSORFLOW_TARGET_ARCH} 124 else 125 ${PYTHON} setup.py bdist bdist_wheel 126 fi 127 ;; 128esac 129 130echo "Output can be found here:" 131find "${BUILD_DIR}" 132 133# Build debian package. 134if [[ "${BUILD_DEB}" != "y" ]]; then 135 exit 0 136fi 137 138PYTHON_VERSION=$(${PYTHON} -c "import sys;print(sys.version_info.major)") 139if [[ ${PYTHON_VERSION} != 3 ]]; then 140 echo "Debian package can only be generated for python3." >&2 141 exit 1 142fi 143 144DEB_VERSION=$(dpkg-parsechangelog --show-field Version | cut -d- -f1) 145if [[ "${DEB_VERSION}" != "${PACKAGE_VERSION}" ]]; then 146 cat << EOF > "${BUILD_DIR}/debian/changelog" 147tflite-runtime (${PACKAGE_VERSION}-1) unstable; urgency=low 148 149 * Bump version to ${PACKAGE_VERSION}. 150 151 -- TensorFlow team <packages@tensorflow.org> $(date -R) 152 153$(<"${BUILD_DIR}/debian/changelog") 154EOF 155fi 156 157case "${TENSORFLOW_TARGET}" in 158 armhf) 159 dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a armhf 160 ;; 161 aarch64) 162 dpkg-buildpackage -b -rfakeroot -us -uc -tc -d -a arm64 163 ;; 164 *) 165 dpkg-buildpackage -b -rfakeroot -us -uc -tc -d 166 ;; 167esac 168 169cat "${BUILD_DIR}/debian/changelog" 170