xref: /aosp_15_r20/external/webp/infra/compile_js.sh (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li#!/bin/bash
2*b2055c35SXin Li# Copyright (c) 2021, Google Inc. All rights reserved.
3*b2055c35SXin Li#
4*b2055c35SXin Li# Redistribution and use in source and binary forms, with or without
5*b2055c35SXin Li# modification, are permitted provided that the following conditions are
6*b2055c35SXin Li# met:
7*b2055c35SXin Li#
8*b2055c35SXin Li#   * Redistributions of source code must retain the above copyright
9*b2055c35SXin Li#     notice, this list of conditions and the following disclaimer.
10*b2055c35SXin Li#
11*b2055c35SXin Li#   * Redistributions in binary form must reproduce the above copyright
12*b2055c35SXin Li#     notice, this list of conditions and the following disclaimer in
13*b2055c35SXin Li#     the documentation and/or other materials provided with the
14*b2055c35SXin Li#     distribution.
15*b2055c35SXin Li#
16*b2055c35SXin Li#   * Neither the name of Google nor the names of its contributors may
17*b2055c35SXin Li#     be used to endorse or promote products derived from this software
18*b2055c35SXin Li#     without specific prior written permission.
19*b2055c35SXin Li#
20*b2055c35SXin Li# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*b2055c35SXin Li# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*b2055c35SXin Li# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*b2055c35SXin Li# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*b2055c35SXin Li# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*b2055c35SXin Li# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*b2055c35SXin Li# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*b2055c35SXin Li# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*b2055c35SXin Li# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*b2055c35SXin Li# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*b2055c35SXin Li# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*b2055c35SXin Li
32*b2055c35SXin Liset -ex
33*b2055c35SXin Li
34*b2055c35SXin Lireadonly WORKSPACE="${WORKSPACE:-"$(mktemp -d -t webp.js.XXX)"}"
35*b2055c35SXin Lireadonly BUILD_DIR="${WORKSPACE}/webp_js/"
36*b2055c35SXin Lireadonly LIBWEBP_ROOT="$(realpath "$(dirname "$0")/..")"
37*b2055c35SXin Li
38*b2055c35SXin Li# shellcheck source=infra/common.sh
39*b2055c35SXin Lisource "${LIBWEBP_ROOT}/infra/common.sh"
40*b2055c35SXin Li
41*b2055c35SXin Liusage() {
42*b2055c35SXin Li  cat << EOF
43*b2055c35SXin LiUsage: $(basename "$0")
44*b2055c35SXin LiEnvironment variables:
45*b2055c35SXin LiWORKSPACE directory where the build is done
46*b2055c35SXin LiEMSDK_DIR directory where emsdk is installed
47*b2055c35SXin LiEOF
48*b2055c35SXin Li}
49*b2055c35SXin Li
50*b2055c35SXin Li[[ -d "${EMSDK_DIR:?Not defined}" ]] \
51*b2055c35SXin Li  || (log_err "${EMSDK_DIR} is not a valid directory." && exit 1)
52*b2055c35SXin Li
53*b2055c35SXin Li# shellcheck source=/opt/emsdk/emsdk_env.sh
54*b2055c35SXin Lisource "${EMSDK_DIR}/emsdk_env.sh"
55*b2055c35SXin Li
56*b2055c35SXin Lireadonly EMSCRIPTEN=${EMSCRIPTEN:-"${EMSDK}/upstream/emscripten"}
57*b2055c35SXin Lireadonly \
58*b2055c35SXin Li  EMSCRIPTEN_CMAKE_FILE="${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake"
59*b2055c35SXin Limake_build_dir "${BUILD_DIR}"
60*b2055c35SXin Li
61*b2055c35SXin Lipushd "${BUILD_DIR}"
62*b2055c35SXin Liopts=("-GUnix Makefiles" "-DWEBP_BUILD_WEBP_JS=ON")
63*b2055c35SXin Liif [[ -z "$(command -v emcmake)" ]]; then
64*b2055c35SXin Li  opts+=("-DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN_CMAKE_FILE}")
65*b2055c35SXin Li  cmake \
66*b2055c35SXin Li    "${opts[@]}" \
67*b2055c35SXin Li    "${LIBWEBP_ROOT}"
68*b2055c35SXin Li  make -j
69*b2055c35SXin Lielse
70*b2055c35SXin Li  emcmake cmake \
71*b2055c35SXin Li    "${opts[@]}" \
72*b2055c35SXin Li    "${LIBWEBP_ROOT}"
73*b2055c35SXin Li  emmake make -j
74*b2055c35SXin Lifi
75*b2055c35SXin Lipopd
76