• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_svg=true \
132  skia_enable_skshaper=true \
133  skia_enable_skparagraph=true \
134  skia_enable_pdf=false"
135
136# Build all the libs we will need below
137parse_targets() {
138  for LIBPATH in $@; do
139    basename $LIBPATH
140  done
141}
142${NINJA} -C ${BUILD_DIR} libskia.a libskshaper.a libskunicode.a \
143  $(parse_targets $GM_LIB)
144
145echo "Generating final wasm"
146
147# Defines for the emscripten compilation step, which builds the tests
148# Aim to match the defines that would be set by gn for the skia compilation step.
149SKIA_DEFINES="
150-DSK_DISABLE_AAA \
151-DSK_FORCE_8_BYTE_ALIGNMENT \
152-DSK_HAS_WUFFS_LIBRARY \
153-DSK_HAS_HEIF_LIBRARY \
154-DSK_ENCODE_WEBP \
155-DSK_CODEC_DECODES_WEBP \
156-DSK_ENCODE_PNG \
157-DSK_CODEC_DECODES_PNG \
158-DSK_ENCODE_JPEG \
159-DSK_CODEC_DECODES_JPEG \
160-DSK_SHAPER_HARFBUZZ_AVAILABLE \
161-DSK_UNICODE_AVAILABLE \
162-DSK_ENABLE_SVG"
163
164# Disable '-s STRICT=1' outside of Linux until
165# https://github.com/emscripten-core/emscripten/issues/12118 is resovled.
166STRICTNESS="-s STRICT=1"
167if [[ `uname` != "Linux" ]]; then
168  echo "Disabling '-s STRICT=1'. See: https://github.com/emscripten-core/emscripten/issues/12118"
169  STRICTNESS=""
170fi
171
172GMS_TO_BUILD="gm/*.cpp"
173TESTS_TO_BUILD="tests/*.cpp"
174
175# When developing locally, it can be faster to focus only on the gms or tests you care about
176# (since they all have to be recompiled/relinked) every time. To do so, mark the following as true
177if false; then
178   GMS_TO_BUILD="gm/gm.cpp"
179   TESTS_TO_BUILD="tests/BulkRectTest.cpp tests/Test.cpp"
180fi
181
182# These gms do not compile or link with the WASM code. Thus, we omit them.
183GLOBIGNORE="gm/cgms.cpp:"\
184"gm/compressed_textures.cpp:"\
185"gm/fiddle.cpp:"\
186"gm/particles.cpp:"\
187"gm/xform.cpp:"\
188"gm/video_decoder.cpp:"
189
190# These tests do not compile with the WASM code (require other deps).
191GLOBIGNORE+="tests/CodecTest.cpp:"\
192"tests/ColorSpaceTest.cpp:"\
193"tests/DrawOpAtlasTest.cpp:"\
194"tests/EncodeTest.cpp:"\
195"tests/FontMgrAndroidParserTest.cpp:"\
196"tests/FontMgrFontConfigTest.cpp:"\
197"tests/TypefaceMacTest.cpp:"\
198"tests/SkVMTest.cpp:"
199
200# These tests do complex things with TestContexts, which is not easily supported for the WASM
201# test harness. Thus we omit them.
202GLOBIGNORE+="tests/BackendAllocationTest.cpp:"\
203"tests/EGLImageTest.cpp:"\
204"tests/ImageTest.cpp:"\
205"tests/SurfaceSemaphoreTest.cpp:"\
206"tests/TextureBindingsResetTest.cpp:"\
207"tests/VkHardwareBufferTest.cpp:"
208
209# All the tests in these files crash.
210GLOBIGNORE+="tests/GrThreadSafeCacheTest.cpp"
211
212# Emscripten prefers that the .a files go last in order, otherwise, it
213# may drop symbols that it incorrectly thinks aren't used. One day,
214# Emscripten will use LLD, which may relax this requirement.
215EMCC_DEBUG=1 ${EMCXX} \
216    $RELEASE_CONF \
217    -I. \
218    -DGR_TEST_UTILS \
219    $SKIA_DEFINES \
220    $WASM_GPU \
221    -std=c++17 \
222    --profiling-funcs \
223    --profiling \
224    --bind \
225    --no-entry \
226    --pre-js $BASE_DIR/gm.js \
227    tools/Resources.cpp \
228    $BASE_DIR/gm_bindings.cpp \
229    $GMS_TO_BUILD \
230    $TESTS_TO_BUILD \
231    $GM_LIB \
232    $BUILD_DIR/libskshaper.a \
233    $BUILD_DIR/libskunicode.a \
234    $BUILD_DIR/libsvg.a \
235    $BUILD_DIR/libskia.a \
236    $BUILTIN_FONT \
237    -s LLD_REPORT_UNDEFINED \
238    -s ALLOW_MEMORY_GROWTH=1 \
239    -s EXPORT_NAME="InitWasmGMTests" \
240    -s EXPORTED_FUNCTIONS=['_malloc','_free'] \
241    -s FORCE_FILESYSTEM=1 \
242    -s FILESYSTEM=1 \
243    -s MODULARIZE=1 \
244    -s NO_EXIT_RUNTIME=1 \
245    -s INITIAL_MEMORY=256MB \
246    -s WASM=1 \
247    $STRICTNESS \
248    -o $BUILD_DIR/wasm_gm_tests.js
249