1cb93a386Sopenharmony_ci#!/bin/bash
2cb93a386Sopenharmony_ci# Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci#
4cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci# found in the LICENSE file.
6cb93a386Sopenharmony_ci
7cb93a386Sopenharmony_ciset -ex
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ciBASE_DIR=`cd $(dirname ${BASH_SOURCE[0]}) && pwd`
10cb93a386Sopenharmony_ci# This expects the environment variable EMSDK to be set
11cb93a386Sopenharmony_ciif [[ ! -d $EMSDK ]]; then
12cb93a386Sopenharmony_ci  cat >&2 << "EOF"
13cb93a386Sopenharmony_ciBe sure to set the EMSDK environment variable to the location of Emscripten SDK:
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci    https://emscripten.org/docs/getting_started/downloads.html
16cb93a386Sopenharmony_ciEOF
17cb93a386Sopenharmony_ci  exit 1
18cb93a386Sopenharmony_cifi
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci# Navigate to SKIA_HOME from where this file is located.
21cb93a386Sopenharmony_cipushd $BASE_DIR/../..
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_cisource $EMSDK/emsdk_env.sh
24cb93a386Sopenharmony_ciEMCC=`which emcc`
25cb93a386Sopenharmony_ciEMCXX=`which em++`
26cb93a386Sopenharmony_ciEMAR=`which emar`
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ciRELEASE_CONF="-Oz --closure 1 --llvm-lto 1 -DSK_RELEASE --pre-js $BASE_DIR/release.js \
29cb93a386Sopenharmony_ci              -DGR_GL_CHECK_ALLOC_WITH_GET_ERROR=0 -DSK_DISABLE_TRACING"
30cb93a386Sopenharmony_ciEXTRA_CFLAGS="\"-DSK_RELEASE\", \"-DSK_DISABLE_TRACING\""
31cb93a386Sopenharmony_ciIS_OFFICIAL_BUILD="true"
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ciif [[ $@ == *full-build* ]]; then
34cb93a386Sopenharmony_ci  # Full Skottie with all bells and whistles.
35cb93a386Sopenharmony_ci  BUILD_TYPE="full"
36cb93a386Sopenharmony_ci  BUILD_CFG="\
37cb93a386Sopenharmony_ci    skia_enable_fontmgr_custom_embedded=true \
38cb93a386Sopenharmony_ci    skia_enable_fontmgr_custom_empty=false \
39cb93a386Sopenharmony_ci    skia_use_freetype=true \
40cb93a386Sopenharmony_ci    skia_use_libgifcodec=true \
41cb93a386Sopenharmony_ci    skia_use_harfbuzz=true \
42cb93a386Sopenharmony_ci    skia_use_icu=true \
43cb93a386Sopenharmony_ci    skia_use_libpng_decode=true \
44cb93a386Sopenharmony_ci    skia_use_wuffs=true \
45cb93a386Sopenharmony_ci    skia_use_zlib=true \
46cb93a386Sopenharmony_ci    \
47cb93a386Sopenharmony_ci    skia_use_system_freetype2=false \
48cb93a386Sopenharmony_ci    skia_use_system_harfbuzz=false \
49cb93a386Sopenharmony_ci    skia_use_system_icu=false \
50cb93a386Sopenharmony_ci    skia_use_system_libpng=false \
51cb93a386Sopenharmony_ci    skia_use_system_zlib=false\
52cb93a386Sopenharmony_ci  "
53cb93a386Sopenharmony_cielse
54cb93a386Sopenharmony_ci  # Smallest usable Skottie.
55cb93a386Sopenharmony_ci  BUILD_TYPE="minimal"
56cb93a386Sopenharmony_ci  BUILD_CFG="\
57cb93a386Sopenharmony_ci    skia_enable_fontmgr_custom_embedded=false \
58cb93a386Sopenharmony_ci    skia_enable_fontmgr_custom_empty=true \
59cb93a386Sopenharmony_ci    skia_use_freetype=false \
60cb93a386Sopenharmony_ci    skia_use_libgifcodec=false \
61cb93a386Sopenharmony_ci    skia_use_harfbuzz=false \
62cb93a386Sopenharmony_ci    skia_use_icu=false \
63cb93a386Sopenharmony_ci    skia_use_libpng_decode=false \
64cb93a386Sopenharmony_ci    skia_use_wuffs=false \
65cb93a386Sopenharmony_ci    skia_use_zlib=false \
66cb93a386Sopenharmony_ci  "
67cb93a386Sopenharmony_cifi
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ciif [[ $@ == *debug* ]]; then
70cb93a386Sopenharmony_ci  echo "Building a *${BUILD_TYPE}* Debug build"
71cb93a386Sopenharmony_ci  EXTRA_CFLAGS="\"-DSK_DEBUG\""
72cb93a386Sopenharmony_ci  RELEASE_CONF="-O0 --js-opts 0 -s DEMANGLE_SUPPORT=1 -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g4 \
73cb93a386Sopenharmony_ci                --source-map-base /bin/ -DSK_DEBUG --pre-js $BASE_DIR/debug.js"
74cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/skottiekit_debug"}
75cb93a386Sopenharmony_cielif [[ $@ == *profiling* ]]; then
76cb93a386Sopenharmony_ci  echo "Building a *${BUILD_TYPE}* build for profiling"
77cb93a386Sopenharmony_ci  RELEASE_CONF+=" --profiling-funcs --closure 0"
78cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/skottiekit_profile"}
79cb93a386Sopenharmony_cielse
80cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/skottiekit"}
81cb93a386Sopenharmony_cifi
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_cimkdir -p $BUILD_DIR
84cb93a386Sopenharmony_ci# sometimes the .a files keep old symbols around - cleaning them out makes sure
85cb93a386Sopenharmony_ci# we get a fresh build.
86cb93a386Sopenharmony_cirm -f $BUILD_DIR/*.a
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ciGN_GPU="skia_enable_gpu=true skia_gl_standard = \"webgl\""
89cb93a386Sopenharmony_ciGN_GPU_FLAGS="\"-DSK_DISABLE_LEGACY_SHADERCONTEXT\","
90cb93a386Sopenharmony_ciWASM_GPU="-lEGL -lGL -lGLESv2 -DSK_SUPPORT_GPU=1 -DSK_GL \
91cb93a386Sopenharmony_ci          -DSK_DISABLE_LEGACY_SHADERCONTEXT --pre-js $BASE_DIR/cpu.js --pre-js $BASE_DIR/gpu.js\
92cb93a386Sopenharmony_ci          -s USE_WEBGL2=1"
93cb93a386Sopenharmony_ciif [[ $@ == *cpu* ]]; then
94cb93a386Sopenharmony_ci  echo "Using the CPU backend instead of the GPU backend"
95cb93a386Sopenharmony_ci  GN_GPU="skia_enable_gpu=false"
96cb93a386Sopenharmony_ci  GN_GPU_FLAGS=""
97cb93a386Sopenharmony_ci  WASM_GPU="-DSK_SUPPORT_GPU=0 --pre-js $BASE_DIR/cpu.js -s USE_WEBGL2=0"
98cb93a386Sopenharmony_cifi
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ciSKOTTIE_LIB="$BUILD_DIR/libskottie.a \
101cb93a386Sopenharmony_ci             $BUILD_DIR/libsksg.a"
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_ciMANAGED_SKOTTIE_BINDINGS="\
105cb93a386Sopenharmony_ci  -DSK_INCLUDE_MANAGED_SKOTTIE=1 \
106cb93a386Sopenharmony_ci  modules/skottie/utils/SkottieUtils.cpp"
107cb93a386Sopenharmony_ciif [[ $@ == *no_managed_skottie* ]]; then
108cb93a386Sopenharmony_ci  echo "Omitting managed Skottie"
109cb93a386Sopenharmony_ci  MANAGED_SKOTTIE_BINDINGS="-DSK_INCLUDE_MANAGED_SKOTTIE=0"
110cb93a386Sopenharmony_cifi
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ci# Turn off exiting while we check for ninja (which may not be on PATH)
113cb93a386Sopenharmony_ciset +e
114cb93a386Sopenharmony_ciNINJA=`which ninja`
115cb93a386Sopenharmony_ciif [[ -z $NINJA ]]; then
116cb93a386Sopenharmony_ci  git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
117cb93a386Sopenharmony_ci  NINJA=$BUILD_DIR/depot_tools/ninja
118cb93a386Sopenharmony_cifi
119cb93a386Sopenharmony_ci# Re-enable error checking
120cb93a386Sopenharmony_ciset -e
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ci./bin/fetch-gn
123cb93a386Sopenharmony_ci
124cb93a386Sopenharmony_ciecho "Compiling bitcode"
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
127cb93a386Sopenharmony_ci./bin/gn gen ${BUILD_DIR} \
128cb93a386Sopenharmony_ci  --args="cc=\"${EMCC}\" \
129cb93a386Sopenharmony_ci  cxx=\"${EMCXX}\" \
130cb93a386Sopenharmony_ci  ar=\"${EMAR}\" \
131cb93a386Sopenharmony_ci  extra_cflags_cc=[\"-frtti\"] \
132cb93a386Sopenharmony_ci  extra_cflags=[\"-s\", \"WARN_UNALIGNED=1\", \"-s\", \"MAIN_MODULE=1\",
133cb93a386Sopenharmony_ci    \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\",
134cb93a386Sopenharmony_ci    \"-DSK_DISABLE_EFFECT_DESERIALIZATION\",
135cb93a386Sopenharmony_ci    \"-DSK_FORCE_8_BYTE_ALIGNMENT\",
136cb93a386Sopenharmony_ci    ${GN_GPU_FLAGS}
137cb93a386Sopenharmony_ci    ${EXTRA_CFLAGS}
138cb93a386Sopenharmony_ci  ] \
139cb93a386Sopenharmony_ci  is_debug=false \
140cb93a386Sopenharmony_ci  is_official_build=${IS_OFFICIAL_BUILD} \
141cb93a386Sopenharmony_ci  is_component_build=false \
142cb93a386Sopenharmony_ci  werror=true \
143cb93a386Sopenharmony_ci  target_cpu=\"wasm\" \
144cb93a386Sopenharmony_ci  \
145cb93a386Sopenharmony_ci  ${BUILD_CFG} \
146cb93a386Sopenharmony_ci  skia_use_angle=false \
147cb93a386Sopenharmony_ci  skia_use_dng_sdk=false \
148cb93a386Sopenharmony_ci  skia_use_egl=true \
149cb93a386Sopenharmony_ci  skia_use_expat=false \
150cb93a386Sopenharmony_ci  skia_use_fontconfig=false \
151cb93a386Sopenharmony_ci  skia_use_libheif=false \
152cb93a386Sopenharmony_ci  skia_use_libjpeg_turbo_decode=true \
153cb93a386Sopenharmony_ci  skia_use_libjpeg_turbo_encode=false \
154cb93a386Sopenharmony_ci  skia_use_libpng_encode=false \
155cb93a386Sopenharmony_ci  skia_use_libwebp_decode=false \
156cb93a386Sopenharmony_ci  skia_use_libwebp_encode=false \
157cb93a386Sopenharmony_ci  skia_use_lua=false \
158cb93a386Sopenharmony_ci  skia_use_piex=false \
159cb93a386Sopenharmony_ci  skia_use_system_libjpeg_turbo=false \
160cb93a386Sopenharmony_ci  skia_use_vulkan=false \
161cb93a386Sopenharmony_ci  skia_enable_fontmgr_custom_directory=false \
162cb93a386Sopenharmony_ci  \
163cb93a386Sopenharmony_ci  ${GN_GPU} \
164cb93a386Sopenharmony_ci  \
165cb93a386Sopenharmony_ci  skia_enable_skshaper=true \
166cb93a386Sopenharmony_ci  skia_enable_pdf=false"
167cb93a386Sopenharmony_ci
168cb93a386Sopenharmony_ci# Build all the libs we will need below
169cb93a386Sopenharmony_ci
170cb93a386Sopenharmony_ci${NINJA} -C ${BUILD_DIR} libskia.a libskottie.a libsksg.a libskshaper.a
171cb93a386Sopenharmony_ci
172cb93a386Sopenharmony_ciexport EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js "
173cb93a386Sopenharmony_ci
174cb93a386Sopenharmony_ciecho "Generating final wasm"
175cb93a386Sopenharmony_ci
176cb93a386Sopenharmony_ci# Emscripten prefers that the .a files go last in order, otherwise, it
177cb93a386Sopenharmony_ci# may drop symbols that it incorrectly thinks aren't used. One day,
178cb93a386Sopenharmony_ci# Emscripten will use LLD, which may relax this requirement.
179cb93a386Sopenharmony_ci${EMCXX} \
180cb93a386Sopenharmony_ci    -I. \
181cb93a386Sopenharmony_ci    $RELEASE_CONF \
182cb93a386Sopenharmony_ci    -DSK_DISABLE_AAA \
183cb93a386Sopenharmony_ci    -DSK_FORCE_8_BYTE_ALIGNMENT \
184cb93a386Sopenharmony_ci    $WASM_GPU \
185cb93a386Sopenharmony_ci    -std=c++17 \
186cb93a386Sopenharmony_ci    --bind \
187cb93a386Sopenharmony_ci    --no-entry \
188cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/preamble.js \
189cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/helper.js \
190cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/interface.js \
191cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/postamble.js \
192cb93a386Sopenharmony_ci    $BASE_DIR/skottiekit_bindings.cpp \
193cb93a386Sopenharmony_ci    modules/skresources/src/SkResources.cpp \
194cb93a386Sopenharmony_ci    $MANAGED_SKOTTIE_BINDINGS \
195cb93a386Sopenharmony_ci    $BUILD_DIR/libskottie.a \
196cb93a386Sopenharmony_ci    $BUILD_DIR/libsksg.a \
197cb93a386Sopenharmony_ci    $BUILD_DIR/libskshaper.a \
198cb93a386Sopenharmony_ci    $BUILD_DIR/libskia.a \
199cb93a386Sopenharmony_ci    -s ALLOW_MEMORY_GROWTH=1 \
200cb93a386Sopenharmony_ci    -s EXPORT_NAME="SkottieKitInit" \
201cb93a386Sopenharmony_ci    -s FORCE_FILESYSTEM=0 \
202cb93a386Sopenharmony_ci    -s FILESYSTEM=0 \
203cb93a386Sopenharmony_ci    -s MODULARIZE=1 \
204cb93a386Sopenharmony_ci    -s NO_EXIT_RUNTIME=1 \
205cb93a386Sopenharmony_ci    -s STRICT=1 \
206cb93a386Sopenharmony_ci    -s INITIAL_MEMORY=128MB \
207cb93a386Sopenharmony_ci    -s WARN_UNALIGNED=1 \
208cb93a386Sopenharmony_ci    -s WASM=1 \
209cb93a386Sopenharmony_ci    -o $BUILD_DIR/skottiekit.js
210