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