1#!/bin/bash 2# Copyright (c) 2021, Google Inc. All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: 7# 8# * Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 11# * Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in 13# the documentation and/or other materials provided with the 14# distribution. 15# 16# * Neither the name of Google nor the names of its contributors may 17# be used to endorse or promote products derived from this software 18# without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32set -xe 33LIBWEBP_ROOT="$(realpath "$(dirname "$0")/..")" 34readonly LIBWEBP_ROOT 35readonly WORKSPACE=${WORKSPACE:-"$(mktemp -d -t webp.android.XXX)"} 36# shellcheck source=infra/common.sh 37source "${LIBWEBP_ROOT}/infra/common.sh" 38 39usage() { 40 cat << EOF 41Usage: $(basename "$0") BUILD_TYPE APP_ABI 42Options: 43BUILD_TYPE supported build types: 44 static 45 static-debug 46 shared 47 shared-debug 48APP_ABI supported application binary interfaces: 49 armeabi-v7a 50 arm64-v8a 51 x86 52 x86_64 53Environment variables: 54WORKSPACE directory where the build is done. 55ANDROID_NDK_DIR directory where the android ndk tools are. 56EOF 57} 58 59################################################################################ 60echo "Building libwebp for Android in ${WORKSPACE}" 61 62if [[ ! -d "${WORKSPACE}" ]]; then 63 log_err "${WORKSPACE} directory does not exist." 64 exit 1 65fi 66 67readonly BUILD_TYPE=${1:?"BUILD_TYPE is not defined.$( 68 echo 69 usage 70)"} 71readonly APP_ABI=${2:?"APP_ABI not defined.$( 72 echo 73 usage 74)"} 75readonly ANDROID_NDK_DIR=${ANDROID_NDK_DIR:?"ANDROID_NDK_DIR is not defined.$( 76 echo 77 usage 78)"} 79readonly BUILD_DIR="${WORKSPACE}/build-${BUILD_TYPE}" 80readonly STANDALONE_ANDROID_DIR="${WORKSPACE}/android" 81 82if [[ ! -x "${ANDROID_NDK_DIR}/ndk-build" ]]; then 83 log_err "unable to find ndk-build in ANDROID_NDK_DIR: ${ANDROID_NDK_DIR}." 84 exit 1 85fi 86 87CFLAGS= 88LDFLAGS= 89opts=() 90case "${BUILD_TYPE}" in 91 *debug) 92 readonly APP_OPTIM="debug" 93 CFLAGS="-O0 -g" 94 opts+=("--enable-asserts") 95 ;; 96 static* | shared*) 97 readonly APP_OPTIM="release" 98 CFLAGS="-O2 -g" 99 ;; 100 *) 101 usage 102 exit 1 103 ;; 104esac 105 106case "${BUILD_TYPE}" in 107 shared*) readonly SHARED="1" ;; 108 *) 109 readonly SHARED="0" 110 CFLAGS="${CFLAGS} -fPIE" 111 LDFLAGS="${LDFLAGS} -Wl,-pie" 112 opts+=("--disable-shared") 113 ;; 114esac 115 116# Create a fresh build directory 117make_build_dir "${BUILD_DIR}" 118cd "${BUILD_DIR}" 119ln -s "${LIBWEBP_ROOT}" jni 120 121"${ANDROID_NDK_DIR}/ndk-build" -j2 \ 122 APP_ABI="${APP_ABI}" \ 123 APP_OPTIM="${APP_OPTIM}" \ 124 ENABLE_SHARED="${SHARED}" 125 126cd "${LIBWEBP_ROOT}" 127./autogen.sh 128 129case "${APP_ABI}" in 130 armeabi*) arch="arm" ;; 131 arm64*) arch="arm64" ;; 132 *) arch="${APP_ABI}" ;; 133esac 134# TODO(b/185520507): remove this and use the binaries from 135# toolchains/llvm/prebuilt/ directly. 136rm -rf "${STANDALONE_ANDROID_DIR}" 137"${ANDROID_NDK_DIR}/build/tools/make_standalone_toolchain.py" \ 138 --api 24 --arch "${arch}" --stl gnustl --install-dir \ 139 "${STANDALONE_ANDROID_DIR}" 140export PATH="${STANDALONE_ANDROID_DIR}/bin:${PATH}" 141 142rm -rf "${BUILD_DIR}" 143make_build_dir "${BUILD_DIR}" 144cd "${BUILD_DIR}" 145 146case "${arch}" in 147 arm) 148 host="arm-linux-androideabi" 149 case "${APP_ABI}" in 150 armeabi) ;; 151 armeabi-v7a) 152 CFLAGS="${CFLAGS} -march=armv7-a -mfpu=neon -mfloat-abi=softfp" 153 ;; 154 *) ;; # No configuration needed 155 esac 156 ;; 157 arm64) 158 host="aarch64-linux-android" 159 ;; 160 x86) 161 host="i686-linux-android" 162 ;; 163 x86_64) 164 host="x86_64-linux-android" 165 ;; 166 *) ;; # Skip configuration 167esac 168 169setup_ccache 170CC="clang" 171 172"${LIBWEBP_ROOT}/configure" --host "${host}" --build \ 173 "$("${LIBWEBP_ROOT}/config.guess")" CC="${CC}" CFLAGS="${CFLAGS}" \ 174 LDFLAGS="${LDFLAGS}" "${opts[@]}" 175make -j 176 177if [[ "${GERRIT_REFSPEC:-}" = "refs/heads/portable-intrinsics" ]] \ 178 || [[ "${GERRIT_BRANCH:-}" = "portable-intrinsics" ]]; then 179 cd "${WORKSPACE}" 180 rm -rf build && mkdir build 181 cd build 182 standalone="${WORKSPACE}/android" 183 cmake ../libwebp \ 184 -DWEBP_BUILD_DWEBP=1 \ 185 -DCMAKE_C_COMPILER="${standalone}/bin/clang" \ 186 -DCMAKE_PREFIX_PATH="${standalone}/sysroot/usr/lib" \ 187 -DCMAKE_C_FLAGS=-fPIE \ 188 -DCMAKE_EXE_LINKER_FLAGS=-Wl,-pie \ 189 -DCMAKE_BUILD_TYPE=Release \ 190 -DWEBP_ENABLE_WASM=1 191 make -j2 192 193 cd "${WORKSPACE}" 194 make_build_dir "${BUILD_DIR}" 195 cd "${BUILD_DIR}" 196 case "${APP_ABI}" in 197 armeabi-v7a | arm64*) 198 cmake "${LIBWEBP_ROOT}" \ 199 -DWEBP_BUILD_DWEBP=1 \ 200 -DCMAKE_C_COMPILER="${standalone}/bin/clang" \ 201 -DCMAKE_PREFIX_PATH="${standalone}/sysroot/usr/lib" \ 202 -DCMAKE_C_FLAGS='-fPIE -DENABLE_NEON_BUILTIN_MULHI_INT16X8' \ 203 -DCMAKE_EXE_LINKER_FLAGS=-Wl,-pie \ 204 -DCMAKE_BUILD_TYPE=Release \ 205 -DWEBP_ENABLE_WASM=1 206 make -j2 207 ;; 208 x86*) 209 cmake "${LIBWEBP_ROOT}" \ 210 -DWEBP_BUILD_DWEBP=1 \ 211 -DCMAKE_C_COMPILER="${standalone}/bin/clang" \ 212 -DCMAKE_PREFIX_PATH="${standalone}/sysroot/usr/lib" \ 213 -DCMAKE_C_FLAGS='-fPIE -DENABLE_X86_BUILTIN_MULHI_INT16X8' \ 214 -DCMAKE_EXE_LINKER_FLAGS=-Wl,-pie \ 215 -DCMAKE_BUILD_TYPE=Release \ 216 -DWEBP_ENABLE_WASM=1 217 make -j2 218 ;; 219 *) 220 log_err "APP_ABI not supported." 221 exit 1 222 ;; 223 esac 224fi 225