|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +script_name=$0 |
| 4 | +compiler_arch="gnu" |
| 5 | +build_threads="8" |
| 6 | + |
| 7 | +print_help() { |
| 8 | + cat <<EOF |
| 9 | +Usage: ${script_name} [OPTION]... |
| 10 | +Build third party library. |
| 11 | +
|
| 12 | +Mandatory arguments to long options are mandatory for short options too. |
| 13 | + -h, --help display this help and exit. |
| 14 | + -c, --compiler <llvm|gnu> use to set compiler(default: gnu). |
| 15 | + -t, --threads use parallel build(default: 8). |
| 16 | +EOF |
| 17 | + exit 1; |
| 18 | +} |
| 19 | + |
| 20 | +TEMP=`getopt -o c:ht: --long compiler:help,threads: \ |
| 21 | + -n ${script_name} -- "$@"` |
| 22 | +if [ $? != 0 ] ; then echo "[ERROR] terminating..." >&2 ; exit 1 ; fi |
| 23 | +eval set -- "$TEMP" |
| 24 | +while true ; do |
| 25 | + case "$1" in |
| 26 | + -c|--compiler) |
| 27 | + compiler_arch=$2 |
| 28 | + echo "[INFO] build library for '${compiler_arch}'" ; |
| 29 | + shift 2 ;; |
| 30 | + -t|--threads) |
| 31 | + build_threads=$2 |
| 32 | + echo "[INFO] '${build_threads}' threads parallel to build" ; |
| 33 | + shift 2 ;; |
| 34 | + -h|--help) |
| 35 | + print_help ; |
| 36 | + shift ;; |
| 37 | + --) shift ; |
| 38 | + break ;; |
| 39 | + *) echo "[ERROR]" ; exit 1 ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +exeIsValid(){ |
| 44 | + if type $1 2>/dev/null; |
| 45 | + then |
| 46 | + return 1 |
| 47 | + else |
| 48 | + return 0 |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +exeIsValid wget |
| 53 | +if [ $? == 0 ] ; then |
| 54 | + echo "[ERROR] please install wget tools and set shell environment PATH to find it" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +exeIsValid git |
| 59 | +if [ $? == 0 ] ; then |
| 60 | + echo "[ERROR] please install git tools and set shell environment PATH to find it" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +exeIsValid unzip |
| 65 | +if [ $? == 0 ] ; then |
| 66 | + echo "[ERROR] please install unzip tools and set shell environment PATH to find it" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +exeIsValid tar |
| 71 | +if [ $? == 0 ] ; then |
| 72 | + echo "[ERROR] please install tar tools and set shell environment PATH to find it" |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +if [ "${compiler_arch}" == "llvm" ] ; then |
| 77 | + exeIsValid aarch64-linux-android21-clang |
| 78 | + if [ $? == 0 ] ; then |
| 79 | + echo "[ERROR] please install android ndk aarch64-linux-android21-clang compiler and set shell environment PATH to find it" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + exeIsValid aarch64-linux-android21-clang++ |
| 83 | + if [ $? == 0 ] ; then |
| 84 | + echo "[ERROR] please install android ndk aarch64-linux-android21-clang++ compiler and set shell environment PATH to find it" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + export CC=aarch64-linux-android21-clang |
| 88 | + export CXX=aarch64-linux-android21-clang++ |
| 89 | +else |
| 90 | + exeIsValid aarch64-linux-gnu-gcc |
| 91 | + if [ $? == 0 ] ; then |
| 92 | + echo "[ERROR] please install GNU gcc ARM compiler and set shell environment PATH to find it" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + exeIsValid aarch64-linux-gnu-g++ |
| 96 | + if [ $? == 0 ] ; then |
| 97 | + echo "[ERROR] please install GNU gcc ARM compiler and set shell environment PATH to find it" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + export CC=aarch64-linux-gnu-gcc |
| 101 | + export CXX=aarch64-linux-gnu-g++ |
| 102 | +fi |
| 103 | + |
| 104 | +script_abs=$(readlink -f "$0") |
| 105 | +script_dir=$(dirname $script_abs) |
| 106 | +current_dir=${PWD} |
| 107 | + |
| 108 | +if [ ! -d "${script_dir}/sources" ]; then |
| 109 | + mkdir ${script_dir}/sources |
| 110 | +fi |
| 111 | + |
| 112 | +rm -rf ${script_dir}/${compiler_arch} |
| 113 | +mkdir ${script_dir}/${compiler_arch} |
| 114 | +env_file="${script_dir}/${compiler_arch}.sh" |
| 115 | +PROTOC_ROOT=${script_dir}/${compiler_arch}/protoc |
| 116 | +Protobuf_ROOT=${script_dir}/${compiler_arch}/protobuf |
| 117 | +FlatBuffers_ROOT=${script_dir}/${compiler_arch}/flatbuffers |
| 118 | +TFLite_ROOT=${script_dir}/${compiler_arch}/tflite |
| 119 | +OpenCL_ROOT=${script_dir}/${compiler_arch}/opencl |
| 120 | +JPEG_ROOT=${script_dir}/${compiler_arch}/jpeg |
| 121 | + |
| 122 | + |
| 123 | +# download prebuilt protoc |
| 124 | +echo "[INFO] install protoc in ${script_dir}..." |
| 125 | +rm -rf ${PROTOC_ROOT} |
| 126 | +mkdir ${PROTOC_ROOT} |
| 127 | +cd ${PROTOC_ROOT} |
| 128 | +if [ ! -f "${script_dir}/sources/protoc-3.1.0-linux-x86_64.zip" ]; then |
| 129 | + wget https://github.com/protocolbuffers/protobuf/releases/download/v3.1.0/protoc-3.1.0-linux-x86_64.zip || exit 1 |
| 130 | + cp protoc-3.1.0-linux-x86_64.zip ${script_dir}/sources/ |
| 131 | +else |
| 132 | + cp ${script_dir}/sources/protoc-3.1.0-linux-x86_64.zip . |
| 133 | +fi |
| 134 | +unzip protoc-3.1.0-linux-x86_64.zip |
| 135 | +rm protoc-3.1.0-linux-x86_64.zip |
| 136 | +export PATH=${PROTOC_ROOT}/bin:$PATH |
| 137 | + |
| 138 | + |
| 139 | +# download and build protobuf |
| 140 | +echo "[INFO] install protobuf in ${script_dir}..." |
| 141 | +rm -rf ${Protobuf_ROOT} |
| 142 | +mkdir ${Protobuf_ROOT} |
| 143 | +cd ${Protobuf_ROOT} |
| 144 | +if [ ! -f "${script_dir}/sources/v3.1.0.tar.gz" ]; then |
| 145 | + wget https://github.com/protocolbuffers/protobuf/archive/v3.1.0.tar.gz || exit 1 |
| 146 | + cp v3.1.0.tar.gz ${script_dir}/sources/ |
| 147 | +else |
| 148 | + cp ${script_dir}/sources/v3.1.0.tar.gz . |
| 149 | +fi |
| 150 | +tar xzf v3.1.0.tar.gz |
| 151 | +cd protobuf-3.1.0 |
| 152 | +if [ ! -f "./configure" ]; then |
| 153 | + ./autogen.sh |
| 154 | +fi |
| 155 | +./configure --host=arm-linux --with-protoc=${PROTOC_ROOT}/bin/protoc\ |
| 156 | + --prefix=${Protobuf_ROOT} |
| 157 | +make -j${build_threads} || exit 1 |
| 158 | +make install -j${build_threads} || exit 1 |
| 159 | +cp ${PROTOC_ROOT}/bin/protoc ${Protobuf_ROOT}/bin |
| 160 | +cd .. |
| 161 | +rm -rf v3.1.0.tar.gz protobuf-3.1.0 |
| 162 | + |
| 163 | + |
| 164 | +# download flatbuffers header file |
| 165 | +echo "[INFO] install flatbuffers in ${script_dir}..." |
| 166 | +rm -rf ${FlatBuffers_ROOT} |
| 167 | +mkdir ${FlatBuffers_ROOT} |
| 168 | +cd ${FlatBuffers_ROOT} |
| 169 | +if [ ! -d "${script_dir}/sources/flatbuffers" ]; then |
| 170 | + git init |
| 171 | + git remote add -f origin https://github.com/google/flatbuffers || exit 1 |
| 172 | + git config core.sparsecheckout true |
| 173 | + echo "include" >> .git/info/sparse-checkout |
| 174 | + git pull origin master || exit 1 |
| 175 | + rm -rf .git* |
| 176 | + cp -r ../flatbuffers ${script_dir}/sources/ |
| 177 | +else |
| 178 | + cp -r ${script_dir}/sources/flatbuffers/* . |
| 179 | +fi |
| 180 | + |
| 181 | + |
| 182 | +# download tensorflow-lite header file |
| 183 | +echo "[INFO] install TFLite in ${script_dir}..." |
| 184 | +rm -rf ${TFLite_ROOT} |
| 185 | +mkdir ${TFLite_ROOT} |
| 186 | +cd ${TFLite_ROOT} |
| 187 | +if [ ! -d "${script_dir}/sources/tflite" ]; then |
| 188 | + mkdir include |
| 189 | + cd include |
| 190 | + git init |
| 191 | + git remote add -f origin https://github.com/tensorflow/tensorflow || exit 1 |
| 192 | + git config core.sparsecheckout true |
| 193 | + echo "lite/schema/schema_generated.h" >> .git/info/sparse-checkout |
| 194 | + git pull origin master || exit 1 |
| 195 | + rm -rf .git* |
| 196 | + cp -r ../../tflite ${script_dir}/sources/ |
| 197 | +else |
| 198 | + cp -r ${script_dir}/sources/tflite/* . |
| 199 | +fi |
| 200 | + |
| 201 | + |
| 202 | +# download and install OpenCL |
| 203 | +echo "[INFO] install opencl in ${script_dir}..." |
| 204 | +rm -rf ${OpenCL_ROOT} |
| 205 | +mkdir ${OpenCL_ROOT} |
| 206 | +cd ${OpenCL_ROOT} |
| 207 | +if [ ! -d "${script_dir}/sources/opencl" ]; then |
| 208 | + mkdir include |
| 209 | + cd include |
| 210 | + git init |
| 211 | + git remote add -f origin https://github.com/KhronosGroup/OpenCL-Headers || exit 1 |
| 212 | + git config core.sparsecheckout true |
| 213 | + echo "CL" >> .git/info/sparse-checkout |
| 214 | + git pull origin master || exit 1 |
| 215 | + rm -rf .git* |
| 216 | + cd .. |
| 217 | + |
| 218 | + mkdir lib64 |
| 219 | + android_device=`adb devices | head -n 2 | tail -n 1 | awk '{print $1}'` |
| 220 | + adb -s ${android_device} pull /vendor/lib64/libOpenCL.so lib64/ |
| 221 | + adb -s ${android_device} pull /vendor/lib64/egl/libGLES_mali.so lib64/ |
| 222 | + cp -r ../opencl ${script_dir}/sources/ |
| 223 | +else |
| 224 | + cp -r ${script_dir}/sources/opencl/* . |
| 225 | +fi |
| 226 | + |
| 227 | + |
| 228 | +# download and build jpeg |
| 229 | +echo "[INFO] install jpeg in ${script_dir}..." |
| 230 | +rm -rf ${JPEG_ROOT} |
| 231 | +mkdir ${JPEG_ROOT} |
| 232 | +cd ${JPEG_ROOT} |
| 233 | +if [ ! -f "${script_dir}/sources/jpegsrc.v9c.tar.gz" ]; then |
| 234 | + wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz || exit 1 |
| 235 | + cp jpegsrc.v9c.tar.gz ${script_dir}/sources/ |
| 236 | +else |
| 237 | + cp ${script_dir}/sources/jpegsrc.v9c.tar.gz . |
| 238 | +fi |
| 239 | +tar xzf jpegsrc.v9c.tar.gz |
| 240 | +cd jpeg-9c |
| 241 | +if [ ! -f "./configure" ]; then |
| 242 | + ./autogen.sh |
| 243 | +fi |
| 244 | +./configure --host=arm-linux --prefix=${JPEG_ROOT} |
| 245 | +make -j${build_threads} || exit 1 |
| 246 | +make install -j${build_threads} || exit 1 |
| 247 | +cd .. |
| 248 | +rm -rf jpeg-9c jpegsrc.v9c.tar.gz |
| 249 | + |
| 250 | + |
| 251 | +echo "[INFO] generate environment file to ${env_file}..." |
| 252 | +echo "#!/bin/bash |
| 253 | +export Protobuf_ROOT=${script_dir}/${compiler_arch}/protobuf |
| 254 | +export FlatBuffers_ROOT=${script_dir}/${compiler_arch}/flatbuffers |
| 255 | +export TFLite_ROOT=${script_dir}/${compiler_arch}/tflite |
| 256 | +export OpenCL_ROOT=${script_dir}/${compiler_arch}/opencl |
| 257 | +export JPEG_ROOT=${script_dir}/${compiler_arch}/jpeg |
| 258 | +export PATH=\${Protobuf_ROOT}/bin:\$PATH |
| 259 | +export LD_LIBRARY_PATH=\${Protobuf_ROOT}/lib:\${OpenCL_ROOT}/lib64:\${JPEG_ROOT}/lib:\$LD_LIBRARY_PATH |
| 260 | +" > ${env_file} |
| 261 | +chmod a+x ${env_file} |
| 262 | +echo "[INFO] please source ${env_file} before use..." |
| 263 | + |
| 264 | +cd ${current_dir} |
0 commit comments