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 -e 33shopt -s inherit_errexit 34 35LIBWEBM_ROOT="$(realpath "$(dirname "$0")/..")" 36readonly LIBWEBM_ROOT 37readonly WORKSPACE=${WORKSPACE:-"$(mktemp -d -t webm.XXX)"} 38 39# shellcheck source=infra/common.sh 40source "${LIBWEBM_ROOT}/infra/common.sh" 41 42usage() { 43 cat << EOF 44Usage: compile.sh BUILD_TYPE TARGET 45Options: 46BUILD_TYPE supported build type (static, static-debug) 47TARGET supported target platform compilation: (native, native-clang, 48 clang-i686, i686-w64-mingw32, x86_64-w64-mingw32, 49 native-Makefile.unix) 50Environment variables: 51WORKSPACE directory where the build is done 52EOF 53} 54 55####################################### 56# Setup ccache for toolchain. 57####################################### 58setup_ccache() { 59 if command -v ccache 2> /dev/null; then 60 export CCACHE_CPP2=yes 61 export PATH="/usr/lib/ccache:${PATH}" 62 fi 63} 64 65################################################################################ 66echo "Building libwebm in ${WORKSPACE}" 67 68if [[ ! -d "${WORKSPACE}" ]]; then 69 log_err "${WORKSPACE} directory does not exist" 70 exit 1 71fi 72 73BUILD_TYPE=${1:?"Build type not defined.$( 74 echo 75 usage 76)"} 77TARGET=${2:?"Target not defined.$( 78 echo 79 usage 80)"} 81BUILD_DIR="${WORKSPACE}/build-${BUILD_TYPE}" 82 83trap cleanup EXIT 84setup_ccache 85make_build_dir "${BUILD_DIR}" 86 87case "${TARGET}" in 88 native-Makefile.unix) 89 make -C "${LIBWEBM_ROOT}" -f Makefile.unix 90 ;; 91 *) 92 opts=() 93 case "${BUILD_TYPE}" in 94 static) opts+=("-DCMAKE_BUILD_TYPE=Release") ;; 95 *debug) opts+=("-DCMAKE_BUILD_TYPE=Debug") ;; 96 *) 97 log_err "${BUILD_TYPE} build type not supported" 98 usage 99 exit 1 100 ;; 101 esac 102 103 TOOLCHAIN_FILE_FLAG="-DCMAKE_TOOLCHAIN_FILE=${LIBWEBM_ROOT}/build" 104 case "${TARGET}" in 105 native-clang) opts+=("-DCMAKE_CXX_COMPILER=clang++") ;; 106 clang-i686) 107 opts+=("-DCMAKE_CXX_COMPILER=clang++") 108 opts+=("-DCMAKE_CXX_FLAGS=-m32") 109 ;; 110 native) ;; # No additional flags needed. 111 i686-w64-mingw32) 112 opts+=("${TOOLCHAIN_FILE_FLAG}/x86-mingw-gcc.cmake") 113 ;; 114 x86_64-w64-mingw32) 115 opts+=("${TOOLCHAIN_FILE_FLAG}/x86_64-mingw-gcc.cmake") 116 ;; 117 *) 118 log_err "${TARGET} TARGET not supported" 119 usage 120 exit 1 121 ;; 122 esac 123 pushd "${BUILD_DIR}" 124 cmake "${LIBWEBM_ROOT}" "${opts[@]}" 125 make -j4 VERBOSE=1 126 popd 127 ;; 128esac 129