compile_android.sh (6357B)
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 32 set -xe 33 LIBWEBP_ROOT="$(realpath "$(dirname "$0")/..")" 34 readonly LIBWEBP_ROOT 35 readonly WORKSPACE=${WORKSPACE:-"$(mktemp -d -t webp.android.XXX)"} 36 # shellcheck source=infra/common.sh 37 source "${LIBWEBP_ROOT}/infra/common.sh" 38 39 usage() { 40 cat << EOF 41 Usage: $(basename "$0") BUILD_TYPE APP_ABI 42 Options: 43 BUILD_TYPE supported build types: 44 static 45 static-debug 46 shared 47 shared-debug 48 APP_ABI supported application binary interfaces: 49 armeabi-v7a 50 arm64-v8a 51 x86 52 x86_64 53 Environment variables: 54 WORKSPACE directory where the build is done. 55 ANDROID_NDK_DIR directory where the android ndk tools are. 56 EOF 57 } 58 59 ################################################################################ 60 echo "Building libwebp for Android in ${WORKSPACE}" 61 62 if [[ ! -d "${WORKSPACE}" ]]; then 63 log_err "${WORKSPACE} directory does not exist." 64 exit 1 65 fi 66 67 readonly BUILD_TYPE=${1:?"BUILD_TYPE is not defined.$( 68 echo 69 usage 70 )"} 71 readonly APP_ABI=${2:?"APP_ABI not defined.$( 72 echo 73 usage 74 )"} 75 readonly ANDROID_NDK_DIR=${ANDROID_NDK_DIR:?"ANDROID_NDK_DIR is not defined.$( 76 echo 77 usage 78 )"} 79 readonly BUILD_DIR="${WORKSPACE}/build-${BUILD_TYPE}" 80 readonly STANDALONE_ANDROID_DIR="${WORKSPACE}/android" 81 82 if [[ ! -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 85 fi 86 87 CFLAGS= 88 LDFLAGS= 89 opts=() 90 case "${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 ;; 104 esac 105 106 case "${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 ;; 114 esac 115 116 # Create a fresh build directory 117 make_build_dir "${BUILD_DIR}" 118 cd "${BUILD_DIR}" 119 ln -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 126 cd "${LIBWEBP_ROOT}" 127 ./autogen.sh 128 129 case "${APP_ABI}" in 130 armeabi*) arch="arm" ;; 131 arm64*) arch="arm64" ;; 132 *) arch="${APP_ABI}" ;; 133 esac 134 # TODO(b/185520507): remove this and use the binaries from 135 # toolchains/llvm/prebuilt/ directly. 136 rm -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}" 140 export PATH="${STANDALONE_ANDROID_DIR}/bin:${PATH}" 141 142 rm -rf "${BUILD_DIR}" 143 make_build_dir "${BUILD_DIR}" 144 cd "${BUILD_DIR}" 145 146 case "${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 167 esac 168 169 setup_ccache 170 CC="clang" 171 172 "${LIBWEBP_ROOT}/configure" --host "${host}" --build \ 173 "$("${LIBWEBP_ROOT}/config.guess")" CC="${CC}" CFLAGS="${CFLAGS}" \ 174 LDFLAGS="${LDFLAGS}" "${opts[@]}" 175 make -j 176 177 if [[ "${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 224 fi