• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2018 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  echo "Be sure to set the EMSDK environment variable."
13  exit 1
14fi
15
16# Navigate to SKIA_HOME from where this file is located.
17pushd $BASE_DIR/../..
18
19source $EMSDK/emsdk_env.sh
20EMCC=`which emcc`
21EMCXX=`which em++`
22
23RELEASE_CONF="-Oz --closure 1 --llvm-lto 3 -DSK_RELEASE --pre-js $BASE_DIR/release.js \
24              -DGR_GL_CHECK_ALLOC_WITH_GET_ERROR=0"
25EXTRA_CFLAGS="\"-DSK_RELEASE\", \"-DGR_GL_CHECK_ALLOC_WITH_GET_ERROR=0\","
26if [[ $@ == *debug* ]]; then
27  echo "Building a Debug build"
28  EXTRA_CFLAGS="\"-DSK_DEBUG\""
29  RELEASE_CONF="-O0 --js-opts 0 -s DEMANGLE_SUPPORT=1 -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g4 \
30                --source-map-base /node_modules/canvaskit/bin/ -DSK_DEBUG --pre-js $BASE_DIR/debug.js"
31  BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_debug"}
32elif [[ $@ == *profiling* ]]; then
33  echo "Building a build for profiling"
34  RELEASE_CONF="-O3 --source-map-base /node_modules/canvaskit/bin/ --profiling -g4 -DSK_RELEASE \
35                --pre-js $BASE_DIR/release.js -DGR_GL_CHECK_ALLOC_WITH_GET_ERROR=0"
36  BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_profile"}
37else
38  BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm"}
39fi
40
41mkdir -p $BUILD_DIR
42
43GN_GPU="skia_enable_gpu=true skia_gl_standard = \"webgl\""
44GN_GPU_FLAGS="\"-DSK_DISABLE_LEGACY_SHADERCONTEXT\","
45WASM_GPU="-lEGL -lGLESv2 -DSK_SUPPORT_GPU=1 \
46          -DSK_DISABLE_LEGACY_SHADERCONTEXT --pre-js $BASE_DIR/cpu.js --pre-js $BASE_DIR/gpu.js"
47if [[ $@ == *cpu* ]]; then
48  echo "Using the CPU backend instead of the GPU backend"
49  GN_GPU="skia_enable_gpu=false"
50  GN_GPU_FLAGS=""
51  WASM_GPU="-DSK_SUPPORT_GPU=0 --pre-js $BASE_DIR/cpu.js"
52fi
53
54SKOTTIE_JS="--pre-js $BASE_DIR/skottie.js"
55SKOTTIE_BINDINGS="$BASE_DIR/skottie_bindings.cpp"
56
57SKOTTIE_LIB="$BUILD_DIR/libskottie.a \
58             $BUILD_DIR/libsksg.a"
59
60if [[ $@ == *no_skottie* ]]; then
61  echo "Omitting Skottie"
62  SKOTTIE_JS=""
63  SKOTTIE_LIB=""
64  SKOTTIE_BINDINGS=""
65fi
66
67MANAGED_SKOTTIE_BINDINGS="\
68  -DSK_INCLUDE_MANAGED_SKOTTIE=1 \
69  modules/skottie/utils/SkottieUtils.cpp"
70if [[ $@ == *no_managed_skottie* ]]; then
71  echo "Omitting managed Skottie"
72  MANAGED_SKOTTIE_BINDINGS="-DSK_INCLUDE_MANAGED_SKOTTIE=0"
73fi
74
75PARTICLES_BINDINGS="$BASE_DIR/particles_bindings.cpp"
76PARTICLES_LIB="$BUILD_DIR/libparticles.a"
77
78if [[ $@ == *no_particles* ]]; then
79  echo "Omitting Particles"
80  PARTICLES_BINDINGS=""
81  PARTICLES_LIB=""
82fi
83
84HTML_CANVAS_API="--pre-js $BASE_DIR/htmlcanvas/preamble.js \
85--pre-js $BASE_DIR/htmlcanvas/util.js \
86--pre-js $BASE_DIR/htmlcanvas/color.js \
87--pre-js $BASE_DIR/htmlcanvas/font.js \
88--pre-js $BASE_DIR/htmlcanvas/canvas2dcontext.js \
89--pre-js $BASE_DIR/htmlcanvas/htmlcanvas.js \
90--pre-js $BASE_DIR/htmlcanvas/imagedata.js \
91--pre-js $BASE_DIR/htmlcanvas/lineargradient.js \
92--pre-js $BASE_DIR/htmlcanvas/path2d.js \
93--pre-js $BASE_DIR/htmlcanvas/pattern.js \
94--pre-js $BASE_DIR/htmlcanvas/radialgradient.js \
95--pre-js $BASE_DIR/htmlcanvas/postamble.js "
96if [[ $@ == *no_canvas* ]]; then
97  echo "Omitting bindings for HTML Canvas API"
98  HTML_CANVAS_API=""
99fi
100
101GN_FONT="skia_enable_fontmgr_empty=false"
102BUILTIN_FONT="$BASE_DIR/fonts/NotoMono-Regular.ttf.cpp"
103if [[ $@ == *no_font* ]]; then
104  echo "Omitting the built-in font(s) and font manager"
105  BUILTIN_FONT=""
106  GN_FONT="skia_enable_fontmgr_empty=true"
107elif [[ $@ == *no_embedded_font* ]]; then
108  echo "Omitting the built-in font(s)"
109  BUILTIN_FONT=""
110  GN_FONT="skia_enable_fontmgr_custom_empty=true skia_enable_fontmgr_empty=false"
111else
112  # Generate the font's binary file (which is covered by .gitignore)
113  python tools/embed_resources.py \
114      --name SK_EMBEDDED_FONTS \
115      --input $BASE_DIR/fonts/NotoMono-Regular.ttf \
116      --output $BASE_DIR/fonts/NotoMono-Regular.ttf.cpp \
117      --align 4
118fi
119
120GN_SHAPER="skia_use_icu=true skia_use_system_icu=false skia_use_system_harfbuzz=false"
121SHAPER_LIB="$BUILD_DIR/libharfbuzz.a \
122            $BUILD_DIR/libicu.a"
123SHAPER_TARGETS="libharfbuzz.a libicu.a"
124if [[ $@ == *primitive_shaper* ]]; then
125  echo "Using the primitive shaper instead of the harfbuzz/icu one"
126  GN_SHAPER="skia_use_icu=false skia_use_harfbuzz=false"
127  SHAPER_LIB=""
128  SHAPER_TARGETS=""
129fi
130
131# Turn off exiting while we check for ninja (which may not be on PATH)
132set +e
133NINJA=`which ninja`
134if [[ -z $NINJA ]]; then
135  git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
136  NINJA=$BUILD_DIR/depot_tools/ninja
137fi
138# Re-enable error checking
139set -e
140
141./bin/fetch-gn
142
143echo "Compiling bitcode"
144
145# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
146./bin/gn gen ${BUILD_DIR} \
147  --args="cc=\"${EMCC}\" \
148  cxx=\"${EMCXX}\" \
149  extra_cflags_cc=[\"-frtti\"] \
150  extra_cflags=[\"-s\",\"USE_FREETYPE=1\",\"-s\",\"USE_LIBPNG=1\", \"-s\", \"WARN_UNALIGNED=1\",
151    \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\", \"-DSK_DISABLE_READBUFFER\",
152    \"-DSK_DISABLE_EFFECT_DESERIALIZATION\",
153    ${GN_GPU_FLAGS}
154    ${EXTRA_CFLAGS}
155  ] \
156  is_debug=false \
157  is_official_build=true \
158  is_component_build=false \
159  werror=true \
160  target_cpu=\"wasm\" \
161  \
162  skia_use_angle = false \
163  skia_use_dng_sdk=false \
164  skia_use_egl=true \
165  skia_use_expat=false \
166  skia_use_fontconfig=false \
167  skia_use_freetype=true \
168  skia_use_libheif=false \
169  skia_use_libjpeg_turbo=true \
170  skia_use_libpng=true \
171  skia_use_libwebp=false \
172  skia_use_lua=false \
173  skia_use_piex=false \
174  skia_use_system_libpng=true \
175  skia_use_system_freetype2=true \
176  skia_use_system_libjpeg_turbo = false \
177  skia_use_vulkan=false \
178  skia_use_wuffs = true \
179  skia_use_zlib=true \
180  \
181  ${GN_SHAPER} \
182  ${GN_GPU} \
183  ${GN_FONT} \
184  \
185  skia_enable_skshaper=true \
186  skia_enable_ccpr=false \
187  skia_enable_nvpr=false \
188  skia_enable_pdf=false"
189
190# Build all the libs, we'll link the appropriate ones down below
191${NINJA} -C ${BUILD_DIR} libskia.a libskottie.a libsksg.a libskshaper.a libparticles.a $SHAPER_TARGETS
192
193export EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js "
194
195echo "Generating final wasm"
196
197# Emscripten prefers that the .a files go last in order, otherwise, it
198# may drop symbols that it incorrectly thinks aren't used. One day,
199# Emscripten will use LLD, which may relax this requirement.
200${EMCXX} \
201    $RELEASE_CONF \
202    -I. \
203    -Ithird_party/icu \
204    -Ithird_party/skcms \
205    -DSK_DISABLE_READBUFFER \
206    -DSK_DISABLE_AAA \
207    $WASM_GPU \
208    -std=c++14 \
209    --bind \
210    --pre-js $BASE_DIR/preamble.js \
211    --pre-js $BASE_DIR/helper.js \
212    --pre-js $BASE_DIR/interface.js \
213    $SKOTTIE_JS \
214    $HTML_CANVAS_API \
215    --pre-js $BASE_DIR/postamble.js \
216    --post-js $BASE_DIR/ready.js \
217    $BUILTIN_FONT \
218    $BASE_DIR/canvaskit_bindings.cpp \
219    $PARTICLES_BINDINGS \
220    $SKOTTIE_BINDINGS \
221    $MANAGED_SKOTTIE_BINDINGS \
222    $SKOTTIE_LIB \
223    $PARTICLES_LIB \
224    $BUILD_DIR/libskshaper.a \
225    $SHAPER_LIB \
226    $BUILD_DIR/libskia.a \
227    -s ALLOW_MEMORY_GROWTH=1 \
228    -s EXPORT_NAME="CanvasKitInit" \
229    -s FORCE_FILESYSTEM=0 \
230    -s MODULARIZE=1 \
231    -s NO_EXIT_RUNTIME=1 \
232    -s STRICT=1 \
233    -s TOTAL_MEMORY=128MB \
234    -s USE_FREETYPE=1 \
235    -s USE_LIBPNG=1 \
236    -s WARN_UNALIGNED=1 \
237    -s USE_WEBGL2=0 \
238    -s WASM=1 \
239    -o $BUILD_DIR/canvaskit.js
240