xref: /aosp_15_r20/external/webp/infra/compile.sh (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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")/..")"
34WORKSPACE=${WORKSPACE:-"$(mktemp -d -t webp.XXX)"}
35
36# shellcheck source=infra/common.sh
37source "${LIBWEBP_ROOT}/infra/common.sh"
38
39usage() {
40  cat << EOF
41Usage: compile.sh BUILD_TYPE TARGET
42Options:
43BUILD_TYPE  supported build type: (shared, static, static-debug)
44TARGET      supported target platforms:
45              aarch64-linux-clang
46              aarch64-linux-gnu
47              arm-linux-gnueabi
48              arm-neon-linux-gnueabi
49              cmake
50              cmake-aarch64
51              cmake-arm
52              cmake-clang
53              disable-near-lossless
54              disable-sse4.1
55              disable-stats
56              force-aligned-32
57              force-aligned-64
58              gradle
59              i686-linux-asan
60              i686-linux-clang
61              i686-linux-gnu
62              i686-w64-mingw32
63              mips2el-linux-gnu
64              mips32dspr2el-linux-gnu
65              mips32eb-linux-gnu
66              mips32el-linux-gnu
67              mips32r2el-linux-gnu
68              mips32r5el-linux-gnu
69              mips64r2el-linux-gnu
70              mips64r6el-linux-gnu
71              native
72              reduce-csp
73              reduce-size
74              reduce-size-disable-stats
75              visibility-default-gnu
76              visibility-hidden-clang
77              visibility-hidden-gnu
78              wasm
79              x86_64-linux-clang
80              x86_64-linux-gnu
81              x86_64-linux-msan
82              x86_64-w64-mingw32
83Environment variables:
84WORKSPACE   directory where the build is done
85EOF
86}
87
88################################################################################
89echo "Building libwebp in ${WORKSPACE}"
90
91if [[ ! -d "${WORKSPACE}" ]]; then
92  log_err "${WORKSPACE} directory does not exist"
93  exit 1
94fi
95
96BUILD_TYPE=${1:?"Build type not defined.$(
97  echo
98  usage
99)"}
100TARGET=${2:?"Target not defined.$(
101  echo
102  usage
103)"}
104readonly BUILD_DIR="${WORKSPACE}/build-${BUILD_TYPE}"
105
106trap 'cleanup ${BUILD_DIR}' EXIT
107make_build_dir "${BUILD_DIR}"
108
109config_flags=()
110case "${BUILD_TYPE}" in
111  shared*) ;;  # Valid BUILD_TYPE but no setup required
112  static*) config_flags+=("--disable-shared") ;;
113  experimental) config_flags+=("--enable-experimental") ;;
114  *)
115    log_err "Invalid BUILD_TYPE"
116    usage
117    exit 1
118    ;;
119esac
120
121if grep -m 1 -q "enable-asserts" "${LIBWEBP_ROOT}/configure.ac"; then
122  config_flags+=("--enable-asserts")
123fi
124
125case "${TARGET}" in
126  aarch64-linux-clang)
127    TARGET="aarch64-linux-gnu"
128    CC="clang"
129    CC="${CC} --target=aarch64-linux-gnu"
130    export CC
131    export CFLAGS="-isystem /usr/aarch64-linux-gnu/include"
132    ;;
133  arm-linux-gnueabi)
134    export CFLAGS="-O3 -march=armv7-a -mfloat-abi=softfp -ftree-vectorize"
135    ;;
136  arm-neon-linux-gnueabi)
137    TARGET="arm-linux-gnueabi"
138    CFLAGS="-O3 -march=armv7-a -mfpu=neon -mfloat-abi=softfp -ftree-vectorize"
139    export CFLAGS
140    ;;
141  mips2el-linux-gnu)
142    export CFLAGS="-EL -O2 -mips2"
143    TARGET="mipsel-linux-gnu"
144    ;;
145  mips32el-linux-gnu)
146    export CFLAGS="-EL -O2 -mips32"
147    TARGET="mipsel-linux-gnu"
148    ;;
149  mips32r2el-linux-gnu)
150    export CFLAGS="-EL -O2 -mips32r2"
151    TARGET="mipsel-linux-gnu"
152    ;;
153  mips32dspr2el-linux-gnu)
154    export CFLAGS="-EL -O2 -mdspr2"
155    TARGET="mipsel-linux-gnu"
156    ;;
157  mips32r5el-linux-gnu)
158    export CFLAGS="-EL -O2 -mips32r5 -mmsa"
159    TARGET="mipsel-linux-gnu"
160    ;;
161  mips32eb-linux-gnu)
162    export CFLAGS="-EB -O2 -mips32"
163    TARGET="mips-linux-gnu"
164    ;;
165  mips64r2el-linux-gnu)
166    export CFLAGS="-EL -O2 -mips64r2 -mabi=64"
167    TARGET="mips64el-linux-gnuabi64"
168    ;;
169  mips64r6el-linux-gnu)
170    export CFLAGS="-EL -O2 -mips64r6 -mabi=64 -mmsa"
171    TARGET="mips-img-linux-gnu"
172    ;;
173  i686-linux-gnu)
174    export CC="gcc -m32"
175    ;;
176  i686-linux-clang)
177    TARGET="i686-linux-gnu"
178    export CC="clang -m32"
179    ;;
180  i686-linux-asan)
181    TARGET="i686-linux-gnu"
182    export CC="clang -m32 -fsanitize=address"
183    ;;
184  i686-linux-msan)
185    TARGET="i686-linux-gnu"
186    export CC="clang -m32 -fsanitize=memory"
187    ;;
188  x86_64-linux-clang)
189    TARGET="x86_64-linux-gnu"
190    export CC=clang
191    ;;
192  x86_64-linux-msan)
193    TARGET="x86_64-linux-gnu"
194    export CC="clang -fsanitize=memory"
195    ;;
196  force-aligned-32)
197    config_flags+=("--enable-aligned")
198    TARGET="i686-linux-gnu"
199    export CC="gcc -m32"
200    ;;
201  force-aligned-64)
202    config_flags+=("--enable-aligned")
203    TARGET="x86_64-linux-gnu"
204    ;;
205  visibility-default-*)
206    export CFLAGS="-O2 -g -fvisibility=default"
207    TARGET="x86_64-linux-gnu"
208    ;;
209  visibility-hidden-*)
210    export CFLAGS="-O2 -g -fvisibility=hidden"
211    if [[ "${TARGET}" = "visibility-hidden-clang" ]]; then
212      export CC=clang
213    fi
214    TARGET="x86_64-linux-gnu"
215    ;;
216  disable-sse4.1)
217    grep "${TARGET}" "${LIBWEBP_ROOT}/configure.ac" || exit 0
218    config_flags+=("--${TARGET}")
219    TARGET="x86_64-linux-gnu"
220    ;;
221  disable-near-lossless)
222    grep "${TARGET}" "${LIBWEBP_ROOT}/configure.ac" || exit 0
223    config_flags+=("--${TARGET}")
224    TARGET="x86_64-linux-gnu"
225    ;;
226  disable-stats)
227    git -C "${LIBWEBP_ROOT}" grep WEBP_DISABLE_STATS || exit 0
228    export CFLAGS="-O2 -g -DWEBP_DISABLE_STATS"
229    TARGET="x86_64-linux-gnu"
230    ;;
231  reduce-size)
232    git -C "${LIBWEBP_ROOT}" grep WEBP_REDUCE_SIZE || exit 0
233    export CFLAGS="-O2 -g -DWEBP_REDUCE_SIZE"
234    TARGET="x86_64-linux-gnu"
235    ;;
236  reduce-size-disable-stats)
237    git -C "${LIBWEBP_ROOT}" grep -e WEBP_DISABLE_STATS -e WEBP_REDUCE_SIZE \
238      || exit 0
239    export CFLAGS="-O2 -g -DWEBP_DISABLE_STATS -DWEBP_REDUCE_SIZE"
240    TARGET="x86_64-linux-gnu"
241    ;;
242  reduce-csp)
243    git -C "${LIBWEBP_ROOT}" grep WEBP_REDUCE_CSP || exit 0
244    export CFLAGS="-O2 -g -DWEBP_REDUCE_CSP"
245    TARGET="x86_64-linux-gnu"
246    ;;
247  x86_64-linux-gnu | *mingw32 | aarch64*) ;;  # Default target configuration
248  # non-configure based builds
249  native)
250    setup_ccache
251    # exercise makefile.unix then quit
252    make -C "${LIBWEBP_ROOT}" -f makefile.unix -j all
253    for tgt in extras examples/anim_diff; do
254      grep -q -m 1 "${tgt}" "${LIBWEBP_ROOT}/makefile.unix" \
255        && make -C "${LIBWEBP_ROOT}" -f makefile.unix -j "${tgt}"
256    done
257    [[ -d "${LIBWEBP_ROOT}/tests/fuzzer" ]] \
258      && make -j -C "${LIBWEBP_ROOT}/tests/fuzzer" -f makefile.unix
259    exit 0
260    ;;
261  cmake*)
262    setup_ccache
263    # exercise cmake then quit
264    opts=()
265    case "${TARGET}" in
266      cmake-clang)
267        opts+=("-DCMAKE_C_COMPILER=clang")
268        ;;
269      cmake-arm)
270        opts+=("-DCMAKE_C_COMPILER=arm-linux-gnueabi-gcc")
271        case "${GERRIT_BRANCH:-}" in
272          portable-intrinsics | 0.6.1) exit 0 ;;
273          *) ;;  # Skip configuration
274        esac
275        ;;
276      cmake-aarch64)
277        opts+=("-DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc")
278        case "${GERRIT_BRANCH:-}" in
279          portable-intrinsics | 0.6.1) exit 0 ;;
280          *) ;;  # Skip configuration
281        esac
282        ;;
283      *) ;;  # Skip configuration
284    esac
285    case "${BUILD_TYPE}" in
286      static*)
287        opts+=("-DBUILD_SHARED_LIBS=OFF")
288        ;;
289      experimental)
290        opts+=("-DWEBP_EXPERIMENTAL_FEATURES=ON" "-DBUILD_SHARED_LIBS=ON")
291        ;;
292      *)
293        opts+=("-DBUILD_SHARED_LIBS=ON")
294        ;;
295    esac
296    case "${BUILD_TYPE}" in
297      *debug) opts+=("-DCMAKE_BUILD_TYPE=Debug") ;;
298      *) opts+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo") ;;
299    esac
300    cd "${BUILD_DIR}"
301    opts+=("-DWEBP_BUILD_CWEBP=ON" "-DWEBP_BUILD_DWEBP=ON")
302    grep -m 1 -q WEBP_BUILD_GIF2WEBP "${LIBWEBP_ROOT}/CMakeLists.txt" \
303      && opts+=("-DWEBP_BUILD_GIF2WEBP=ON")
304    grep -m 1 -q WEBP_BUILD_IMG2WEBP "${LIBWEBP_ROOT}/CMakeLists.txt" \
305      && opts+=("-DWEBP_BUILD_IMG2WEBP=ON")
306    cmake "${opts[@]}" "${LIBWEBP_ROOT}"
307    make VERBOSE=1 -j
308    case "${BUILD_TYPE}" in
309      static)
310        mkdir -p examples
311        cp [cd]webp examples
312        ;;
313      *) ;;  # Skip configuration.
314    esac
315
316    grep "install" "${LIBWEBP_ROOT}/CMakeLists.txt" || exit 0
317
318    make DESTDIR="${BUILD_DIR}/webp-install" install/strip
319    mkdir tmp
320    cd tmp
321    cat > CMakeLists.txt << EOF
322cmake_minimum_required(VERSION 2.8.7)
323
324project(libwebp C)
325
326find_package(WebP)
327if (NOT WebP_FOUND)
328  message(FATAL_ERROR "WebP package not found")
329endif ()
330message("WebP_FOUND: \${WebP_FOUND}")
331message("WebP_INCLUDE_DIRS: \${WebP_INCLUDE_DIRS}")
332message("WebP_LIBRARIES: \${WebP_LIBRARIES}")
333message("WEBP_INCLUDE_DIRS: \${WEBP_INCLUDE_DIRS}")
334message("WEBP_LIBRARIES: \${WEBP_LIBRARIES}")
335EOF
336    cmake . "${opts[@]}" \
337      "-DCMAKE_PREFIX_PATH=${BUILD_DIR}/webp-install/usr/local"
338    exit 0
339    ;;
340  gradle)
341    setup_ccache
342    # exercise gradle then quit
343    [[ -f "${LIBWEBP_ROOT}/gradlew" ]] || exit 0
344
345    cd "${BUILD_DIR}"
346    # TODO -g / --gradle-user-home could be used if there's a race between jobs
347    "${LIBWEBP_ROOT}/gradlew" -p "${LIBWEBP_ROOT}" buildAllExecutables
348    exit 0
349    ;;
350  wasm)
351    grep -m 1 -q WEBP_ENABLE_WASM "${LIBWEBP_ROOT}/CMakeLists.txt" || exit 0
352    opts+=("-DCMAKE_C_COMPILER=clang" "-DWEBP_ENABLE_WASM=ON")
353    opts+=("-DWEBP_BUILD_CWEBP=ON" "-DWEBP_BUILD_DWEBP=ON")
354    case "${BUILD_TYPE}" in
355      *debug) opts+=("-DCMAKE_BUILD_TYPE=Debug") ;;
356      *) opts+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo") ;;
357    esac
358    cd "${BUILD_DIR}"
359    cmake "${opts[@]}" "${LIBWEBP_ROOT}"
360    make VERBOSE=1 -j
361    mkdir examples
362    case "${BUILD_TYPE}" in
363      static)
364        mkdir -p examples
365        cp [cd]webp examples
366        ;;
367      *) ;;  # Skip configuration
368    esac
369    exit 0
370    ;;
371  *)
372    log_err "Invalid TARGET"
373    usage
374    exit 1
375    ;;
376esac
377
378case "${TARGET}" in
379  *mingw32) ;;  # Skip configuration
380  *)
381    case "${TARGET}-${CC}" in
382      static-debug-gcc* | static-debug-)
383        CFLAGS="${CFLAGS} -fprofile-arcs -ftest-coverage -O0 -g"
384        CXXFLAGS="${CXXFLAGS} -fprofile-arcs -ftest-coverage -O0 -g"
385        export CFLAGS CXXFLAGS
386        ;;
387      *) ;;  # This case should not be reached.
388    esac
389    ;;
390esac
391
392setup_ccache
393
394cd "${LIBWEBP_ROOT}"
395./autogen.sh
396
397cd "${BUILD_DIR}"
398"${LIBWEBP_ROOT}/configure" \
399  --host "${TARGET}" --build "$("${LIBWEBP_ROOT}/config.guess")" \
400  --enable-everything "${config_flags[@]}"
401make -j V=1
402