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_ciif [[ $@ == *debug* ]]; then
29cb93a386Sopenharmony_ci  echo "Building a Debug build"
30cb93a386Sopenharmony_ci  DEBUG=true
31cb93a386Sopenharmony_ci  EXTRA_CFLAGS="\"-DSK_DEBUG\", \"-DGR_TEST_UTILS\", "
32cb93a386Sopenharmony_ci  RELEASE_CONF="-O1 --js-opts 0 -s DEMANGLE_SUPPORT=1 -frtti -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g \
33cb93a386Sopenharmony_ci                -DSK_DEBUG --pre-js $BASE_DIR/debug.js"
34cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/wasm_gm_tests_debug"}
35cb93a386Sopenharmony_cielse
36cb93a386Sopenharmony_ci  echo "Building a release build"
37cb93a386Sopenharmony_ci  DEBUG=false
38cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/wasm_gm_tests"}
39cb93a386Sopenharmony_ci  RELEASE_CONF="-O3 -DSK_RELEASE --pre-js $BASE_DIR/release.js \
40cb93a386Sopenharmony_ci              -DGR_TEST_UTILS"
41cb93a386Sopenharmony_ci  EXTRA_CFLAGS="\"-DSK_RELEASE\", \"-DGR_TEST_UTILS\", "
42cb93a386Sopenharmony_cifi
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ciIS_OFFICIAL_BUILD="false"
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_cimkdir -p $BUILD_DIR
47cb93a386Sopenharmony_ci# sometimes the .a files keep old symbols around - cleaning them out makes sure
48cb93a386Sopenharmony_ci# we get a fresh build.
49cb93a386Sopenharmony_cirm -f $BUILD_DIR/*.a
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ciGN_GPU="skia_enable_gpu=true skia_gl_standard = \"webgl\""
52cb93a386Sopenharmony_ciGN_GPU_FLAGS="\"-DSK_DISABLE_LEGACY_SHADERCONTEXT\","
53cb93a386Sopenharmony_ciWASM_GPU="-lGL -DSK_SUPPORT_GPU=1 -DSK_GL \
54cb93a386Sopenharmony_ci          -DSK_DISABLE_LEGACY_SHADERCONTEXT --pre-js $BASE_DIR/cpu.js --pre-js $BASE_DIR/gpu.js\
55cb93a386Sopenharmony_ci          -s USE_WEBGL2=1"
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ciGM_LIB="$BUILD_DIR/libgm_wasm.a"
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ciGN_FONT="skia_enable_fontmgr_custom_directory=false "
60cb93a386Sopenharmony_ciBUILTIN_FONT="$BASE_DIR/fonts/NotoMono-Regular.ttf.cpp"
61cb93a386Sopenharmony_ci# Generate the font's binary file (which is covered by .gitignore)
62cb93a386Sopenharmony_cipython tools/embed_resources.py \
63cb93a386Sopenharmony_ci      --name SK_EMBEDDED_FONTS \
64cb93a386Sopenharmony_ci      --input $BASE_DIR/fonts/NotoMono-Regular.ttf \
65cb93a386Sopenharmony_ci      --output $BASE_DIR/fonts/NotoMono-Regular.ttf.cpp \
66cb93a386Sopenharmony_ci      --align 4
67cb93a386Sopenharmony_ciGN_FONT+="skia_enable_fontmgr_custom_embedded=true skia_enable_fontmgr_custom_empty=false"
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ciGN_SHAPER="skia_use_icu=true skia_use_system_icu=false skia_use_harfbuzz=true skia_use_system_harfbuzz=false"
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_ci# Turn off exiting while we check for ninja (which may not be on PATH)
73cb93a386Sopenharmony_ciset +e
74cb93a386Sopenharmony_ciNINJA=`which ninja`
75cb93a386Sopenharmony_ciif [[ -z $NINJA ]]; then
76cb93a386Sopenharmony_ci  git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
77cb93a386Sopenharmony_ci  NINJA=$BUILD_DIR/depot_tools/ninja
78cb93a386Sopenharmony_cifi
79cb93a386Sopenharmony_ci# Re-enable error checking
80cb93a386Sopenharmony_ciset -e
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci./bin/fetch-gn
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ciecho "Compiling bitcode"
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
87cb93a386Sopenharmony_ci./bin/gn gen ${BUILD_DIR} \
88cb93a386Sopenharmony_ci  --args="cc=\"${EMCC}\" \
89cb93a386Sopenharmony_ci  cxx=\"${EMCXX}\" \
90cb93a386Sopenharmony_ci  ar=\"${EMAR}\" \
91cb93a386Sopenharmony_ci  extra_cflags_cc=[\"-frtti\"] \
92cb93a386Sopenharmony_ci  extra_cflags=[\"-s\", \"WARN_UNALIGNED=1\", \"-s\", \"MAIN_MODULE=1\",
93cb93a386Sopenharmony_ci    \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\",
94cb93a386Sopenharmony_ci    \"-DSK_FORCE_8_BYTE_ALIGNMENT\",
95cb93a386Sopenharmony_ci    ${GN_GPU_FLAGS}
96cb93a386Sopenharmony_ci    ${EXTRA_CFLAGS}
97cb93a386Sopenharmony_ci  ] \
98cb93a386Sopenharmony_ci  is_debug=${DEBUG} \
99cb93a386Sopenharmony_ci  is_official_build=${IS_OFFICIAL_BUILD} \
100cb93a386Sopenharmony_ci  is_component_build=false \
101cb93a386Sopenharmony_ci  werror=true \
102cb93a386Sopenharmony_ci  target_cpu=\"wasm\" \
103cb93a386Sopenharmony_ci  \
104cb93a386Sopenharmony_ci  skia_use_angle=false \
105cb93a386Sopenharmony_ci  skia_use_dng_sdk=false \
106cb93a386Sopenharmony_ci  skia_use_webgl=true \
107cb93a386Sopenharmony_ci  skia_use_fontconfig=false \
108cb93a386Sopenharmony_ci  skia_use_freetype=true \
109cb93a386Sopenharmony_ci  skia_use_libheif=true \
110cb93a386Sopenharmony_ci  skia_use_libjpeg_turbo_decode=true \
111cb93a386Sopenharmony_ci  skia_use_libjpeg_turbo_encode=true \
112cb93a386Sopenharmony_ci  skia_use_libpng_decode=true \
113cb93a386Sopenharmony_ci  skia_use_libpng_encode=true \
114cb93a386Sopenharmony_ci  skia_use_libwebp_decode=true \
115cb93a386Sopenharmony_ci  skia_use_libwebp_encode=true \
116cb93a386Sopenharmony_ci  skia_use_lua=false \
117cb93a386Sopenharmony_ci  skia_use_piex=true \
118cb93a386Sopenharmony_ci  skia_use_system_freetype2=false \
119cb93a386Sopenharmony_ci  skia_use_system_libjpeg_turbo=false \
120cb93a386Sopenharmony_ci  skia_use_system_libpng=false \
121cb93a386Sopenharmony_ci  skia_use_system_libwebp=false \
122cb93a386Sopenharmony_ci  skia_use_system_zlib=false\
123cb93a386Sopenharmony_ci  skia_use_vulkan=false \
124cb93a386Sopenharmony_ci  skia_use_wuffs=true \
125cb93a386Sopenharmony_ci  skia_use_zlib=true \
126cb93a386Sopenharmony_ci  \
127cb93a386Sopenharmony_ci  ${GN_SHAPER} \
128cb93a386Sopenharmony_ci  ${GN_GPU} \
129cb93a386Sopenharmony_ci  ${GN_FONT} \
130cb93a386Sopenharmony_ci  skia_use_expat=true \
131cb93a386Sopenharmony_ci  skia_enable_svg=true \
132cb93a386Sopenharmony_ci  skia_enable_skshaper=true \
133cb93a386Sopenharmony_ci  skia_enable_skparagraph=true \
134cb93a386Sopenharmony_ci  skia_enable_pdf=false"
135cb93a386Sopenharmony_ci
136cb93a386Sopenharmony_ci# Build all the libs we will need below
137cb93a386Sopenharmony_ciparse_targets() {
138cb93a386Sopenharmony_ci  for LIBPATH in $@; do
139cb93a386Sopenharmony_ci    basename $LIBPATH
140cb93a386Sopenharmony_ci  done
141cb93a386Sopenharmony_ci}
142cb93a386Sopenharmony_ci${NINJA} -C ${BUILD_DIR} libskia.a libskshaper.a libskunicode.a \
143cb93a386Sopenharmony_ci  $(parse_targets $GM_LIB)
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ciecho "Generating final wasm"
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ci# Defines for the emscripten compilation step, which builds the tests
148cb93a386Sopenharmony_ci# Aim to match the defines that would be set by gn for the skia compilation step.
149cb93a386Sopenharmony_ciSKIA_DEFINES="
150cb93a386Sopenharmony_ci-DSK_DISABLE_AAA \
151cb93a386Sopenharmony_ci-DSK_FORCE_8_BYTE_ALIGNMENT \
152cb93a386Sopenharmony_ci-DSK_HAS_WUFFS_LIBRARY \
153cb93a386Sopenharmony_ci-DSK_HAS_HEIF_LIBRARY \
154cb93a386Sopenharmony_ci-DSK_ENCODE_WEBP \
155cb93a386Sopenharmony_ci-DSK_CODEC_DECODES_WEBP \
156cb93a386Sopenharmony_ci-DSK_ENCODE_PNG \
157cb93a386Sopenharmony_ci-DSK_CODEC_DECODES_PNG \
158cb93a386Sopenharmony_ci-DSK_ENCODE_JPEG \
159cb93a386Sopenharmony_ci-DSK_CODEC_DECODES_JPEG \
160cb93a386Sopenharmony_ci-DSK_SHAPER_HARFBUZZ_AVAILABLE \
161cb93a386Sopenharmony_ci-DSK_UNICODE_AVAILABLE \
162cb93a386Sopenharmony_ci-DSK_ENABLE_SVG"
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ci# Disable '-s STRICT=1' outside of Linux until
165cb93a386Sopenharmony_ci# https://github.com/emscripten-core/emscripten/issues/12118 is resovled.
166cb93a386Sopenharmony_ciSTRICTNESS="-s STRICT=1"
167cb93a386Sopenharmony_ciif [[ `uname` != "Linux" ]]; then
168cb93a386Sopenharmony_ci  echo "Disabling '-s STRICT=1'. See: https://github.com/emscripten-core/emscripten/issues/12118"
169cb93a386Sopenharmony_ci  STRICTNESS=""
170cb93a386Sopenharmony_cifi
171cb93a386Sopenharmony_ci
172cb93a386Sopenharmony_ciGMS_TO_BUILD="gm/*.cpp"
173cb93a386Sopenharmony_ciTESTS_TO_BUILD="tests/*.cpp"
174cb93a386Sopenharmony_ci
175cb93a386Sopenharmony_ci# When developing locally, it can be faster to focus only on the gms or tests you care about
176cb93a386Sopenharmony_ci# (since they all have to be recompiled/relinked) every time. To do so, mark the following as true
177cb93a386Sopenharmony_ciif false; then
178cb93a386Sopenharmony_ci   GMS_TO_BUILD="gm/gm.cpp"
179cb93a386Sopenharmony_ci   TESTS_TO_BUILD="tests/BulkRectTest.cpp tests/Test.cpp"
180cb93a386Sopenharmony_cifi
181cb93a386Sopenharmony_ci
182cb93a386Sopenharmony_ci# These gms do not compile or link with the WASM code. Thus, we omit them.
183cb93a386Sopenharmony_ciGLOBIGNORE="gm/cgms.cpp:"\
184cb93a386Sopenharmony_ci"gm/compressed_textures.cpp:"\
185cb93a386Sopenharmony_ci"gm/fiddle.cpp:"\
186cb93a386Sopenharmony_ci"gm/particles.cpp:"\
187cb93a386Sopenharmony_ci"gm/xform.cpp:"\
188cb93a386Sopenharmony_ci"gm/video_decoder.cpp:"
189cb93a386Sopenharmony_ci
190cb93a386Sopenharmony_ci# These tests do not compile with the WASM code (require other deps).
191cb93a386Sopenharmony_ciGLOBIGNORE+="tests/CodecTest.cpp:"\
192cb93a386Sopenharmony_ci"tests/ColorSpaceTest.cpp:"\
193cb93a386Sopenharmony_ci"tests/DrawOpAtlasTest.cpp:"\
194cb93a386Sopenharmony_ci"tests/EncodeTest.cpp:"\
195cb93a386Sopenharmony_ci"tests/FontMgrAndroidParserTest.cpp:"\
196cb93a386Sopenharmony_ci"tests/FontMgrFontConfigTest.cpp:"\
197cb93a386Sopenharmony_ci"tests/TypefaceMacTest.cpp:"\
198cb93a386Sopenharmony_ci"tests/SkVMTest.cpp:"
199cb93a386Sopenharmony_ci
200cb93a386Sopenharmony_ci# These tests do complex things with TestContexts, which is not easily supported for the WASM
201cb93a386Sopenharmony_ci# test harness. Thus we omit them.
202cb93a386Sopenharmony_ciGLOBIGNORE+="tests/BackendAllocationTest.cpp:"\
203cb93a386Sopenharmony_ci"tests/EGLImageTest.cpp:"\
204cb93a386Sopenharmony_ci"tests/ImageTest.cpp:"\
205cb93a386Sopenharmony_ci"tests/SurfaceSemaphoreTest.cpp:"\
206cb93a386Sopenharmony_ci"tests/TextureBindingsResetTest.cpp:"\
207cb93a386Sopenharmony_ci"tests/VkHardwareBufferTest.cpp:"
208cb93a386Sopenharmony_ci
209cb93a386Sopenharmony_ci# All the tests in these files crash.
210cb93a386Sopenharmony_ciGLOBIGNORE+="tests/GrThreadSafeCacheTest.cpp"
211cb93a386Sopenharmony_ci
212cb93a386Sopenharmony_ci# Emscripten prefers that the .a files go last in order, otherwise, it
213cb93a386Sopenharmony_ci# may drop symbols that it incorrectly thinks aren't used. One day,
214cb93a386Sopenharmony_ci# Emscripten will use LLD, which may relax this requirement.
215cb93a386Sopenharmony_ciEMCC_DEBUG=1 ${EMCXX} \
216cb93a386Sopenharmony_ci    $RELEASE_CONF \
217cb93a386Sopenharmony_ci    -I. \
218cb93a386Sopenharmony_ci    -DGR_TEST_UTILS \
219cb93a386Sopenharmony_ci    $SKIA_DEFINES \
220cb93a386Sopenharmony_ci    $WASM_GPU \
221cb93a386Sopenharmony_ci    -std=c++17 \
222cb93a386Sopenharmony_ci    --profiling-funcs \
223cb93a386Sopenharmony_ci    --profiling \
224cb93a386Sopenharmony_ci    --bind \
225cb93a386Sopenharmony_ci    --no-entry \
226cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/gm.js \
227cb93a386Sopenharmony_ci    tools/Resources.cpp \
228cb93a386Sopenharmony_ci    $BASE_DIR/gm_bindings.cpp \
229cb93a386Sopenharmony_ci    $GMS_TO_BUILD \
230cb93a386Sopenharmony_ci    $TESTS_TO_BUILD \
231cb93a386Sopenharmony_ci    $GM_LIB \
232cb93a386Sopenharmony_ci    $BUILD_DIR/libskshaper.a \
233cb93a386Sopenharmony_ci    $BUILD_DIR/libskunicode.a \
234cb93a386Sopenharmony_ci    $BUILD_DIR/libsvg.a \
235cb93a386Sopenharmony_ci    $BUILD_DIR/libskia.a \
236cb93a386Sopenharmony_ci    $BUILTIN_FONT \
237cb93a386Sopenharmony_ci    -s LLD_REPORT_UNDEFINED \
238cb93a386Sopenharmony_ci    -s ALLOW_MEMORY_GROWTH=1 \
239cb93a386Sopenharmony_ci    -s EXPORT_NAME="InitWasmGMTests" \
240cb93a386Sopenharmony_ci    -s EXPORTED_FUNCTIONS=['_malloc','_free'] \
241cb93a386Sopenharmony_ci    -s FORCE_FILESYSTEM=1 \
242cb93a386Sopenharmony_ci    -s FILESYSTEM=1 \
243cb93a386Sopenharmony_ci    -s MODULARIZE=1 \
244cb93a386Sopenharmony_ci    -s NO_EXIT_RUNTIME=1 \
245cb93a386Sopenharmony_ci    -s INITIAL_MEMORY=256MB \
246cb93a386Sopenharmony_ci    -s WASM=1 \
247cb93a386Sopenharmony_ci    $STRICTNESS \
248cb93a386Sopenharmony_ci    -o $BUILD_DIR/wasm_gm_tests.js
249