blob: 780d5ea1f2f56e001b0ef38f8b13f1a72f000eca [file] [log] [blame]
Kevin Lubick217056c2018-09-20 17:39:31 -04001#!/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
Kevin Lubick217056c2018-09-20 17:39:31 -040016# Navigate to SKIA_HOME from where this file is located.
17pushd $BASE_DIR/../..
18
19source $EMSDK/emsdk_env.sh
Kevin Lubick8e9750d2018-10-09 09:36:35 -040020EMCC=`which emcc`
21EMCXX=`which em++`
Kevin Lubick217056c2018-09-20 17:39:31 -040022
Kevin Lubicka96ec092018-12-17 11:08:36 -050023RELEASE_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\","
Kevin Lubick217056c2018-09-20 17:39:31 -040026if [[ $@ == *debug* ]]; then
27 echo "Building a Debug build"
Kevin Lubickd45c7812018-10-02 11:33:52 -040028 EXTRA_CFLAGS="\"-DSK_DEBUG\""
Kevin Lubicka96ec092018-12-17 11:08:36 -050029 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"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040031 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_debug"}
Kevin Lubickf9910ea2018-12-03 14:49:15 -050032elif [[ $@ == *profiling* ]]; then
33 echo "Building a build for profiling"
Kevin Lubicka96ec092018-12-17 11:08:36 -050034 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"
Kevin Lubickf9910ea2018-12-03 14:49:15 -050036 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_profile"}
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040037else
38 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm"}
Kevin Lubick217056c2018-09-20 17:39:31 -040039fi
40
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040041mkdir -p $BUILD_DIR
42
Kevin Lubick53965c92018-10-11 08:51:55 -040043GN_GPU="skia_enable_gpu=true"
Kevin Lubick2cf29ce2018-10-25 14:46:50 -040044GN_GPU_FLAGS="\"-DIS_WEBGL=1\", \"-DSK_DISABLE_LEGACY_SHADERCONTEXT\","
45WASM_GPU="-lEGL -lGLESv2 -DSK_SUPPORT_GPU=1 \
Kevin Lubickb07204a2018-11-20 14:07:42 -050046 -DSK_DISABLE_LEGACY_SHADERCONTEXT --pre-js $BASE_DIR/cpu.js --pre-js $BASE_DIR/gpu.js"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040047if [[ $@ == *cpu* ]]; then
48 echo "Using the CPU backend instead of the GPU backend"
Kevin Lubick53965c92018-10-11 08:51:55 -040049 GN_GPU="skia_enable_gpu=false"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040050 GN_GPU_FLAGS=""
Kevin Lubick5b90b842018-10-17 07:57:18 -040051 WASM_GPU="-DSK_SUPPORT_GPU=0 --pre-js $BASE_DIR/cpu.js"
Kevin Lubick53965c92018-10-11 08:51:55 -040052fi
53
Kevin Lubick46839422019-01-03 14:27:27 -050054WASM_SKOTTIE=" \
55 $BASE_DIR/skottie_bindings.cpp \
Kevin Lubick53965c92018-10-11 08:51:55 -040056 modules/skottie/src/Skottie.cpp \
57 modules/skottie/src/SkottieAdapter.cpp \
58 modules/skottie/src/SkottieAnimator.cpp \
59 modules/skottie/src/SkottieJson.cpp \
60 modules/skottie/src/SkottieLayer.cpp \
61 modules/skottie/src/SkottieLayerEffect.cpp \
62 modules/skottie/src/SkottiePrecompLayer.cpp \
63 modules/skottie/src/SkottieProperty.cpp \
64 modules/skottie/src/SkottieShapeLayer.cpp \
65 modules/skottie/src/SkottieTextLayer.cpp \
66 modules/skottie/src/SkottieValue.cpp \
67 modules/sksg/src/*.cpp \
68 src/core/SkCubicMap.cpp \
69 src/core/SkTime.cpp \
70 src/pathops/SkOpBuilder.cpp \
71 src/utils/SkJSON.cpp \
72 src/utils/SkParse.cpp "
73if [[ $@ == *no_skottie* ]]; then
74 echo "Omitting Skottie"
Kevin Lubick46839422019-01-03 14:27:27 -050075 WASM_SKOTTIE=""
Kevin Lubick53965c92018-10-11 08:51:55 -040076fi
77
Florin Malitac2e85f92018-11-29 11:38:12 -050078WASM_MANAGED_SKOTTIE="\
79 -DSK_INCLUDE_MANAGED_SKOTTIE=1 \
80 modules/skottie/utils/SkottieUtils.cpp"
81if [[ $@ == *no_managed_skottie* ]]; then
82 echo "Omitting managed Skottie"
83 WASM_MANAGED_SKOTTIE="-DSK_INCLUDE_MANAGED_SKOTTIE=0"
84fi
85
Kevin Lubick53eabf62018-12-10 12:41:26 -050086HTML_CANVAS_API="--pre-js $BASE_DIR/htmlcanvas/preamble.js \
87--pre-js $BASE_DIR/htmlcanvas/util.js \
88--pre-js $BASE_DIR/htmlcanvas/color.js \
89--pre-js $BASE_DIR/htmlcanvas/font.js \
90--pre-js $BASE_DIR/htmlcanvas/canvas2dcontext.js \
91--pre-js $BASE_DIR/htmlcanvas/htmlcanvas.js \
92--pre-js $BASE_DIR/htmlcanvas/imagedata.js \
93--pre-js $BASE_DIR/htmlcanvas/lineargradient.js \
Kevin Lubicka40f8322018-12-17 16:01:36 -050094--pre-js $BASE_DIR/htmlcanvas/path2d.js \
Kevin Lubick53eabf62018-12-10 12:41:26 -050095--pre-js $BASE_DIR/htmlcanvas/pattern.js \
96--pre-js $BASE_DIR/htmlcanvas/radialgradient.js \
97--pre-js $BASE_DIR/htmlcanvas/postamble.js "
Kevin Lubick006a6f32018-10-19 14:34:34 -040098if [[ $@ == *no_canvas* ]]; then
99 echo "Omitting bindings for HTML Canvas API"
100 HTML_CANVAS_API=""
101fi
102
Kevin Lubickddd0a332018-12-12 10:35:13 -0500103BUILTIN_FONT="$BASE_DIR/fonts/NotoMono-Regular.ttf.cpp"
104if [[ $@ == *no_font* ]]; then
105 echo "Omitting the built-in font(s)"
106 BUILTIN_FONT=""
107else
108 # Generate the font's binary file (which is covered by .gitignore)
109 python tools/embed_resources.py \
110 --name SK_EMBEDDED_FONTS \
111 --input $BASE_DIR/fonts/NotoMono-Regular.ttf \
112 --output $BASE_DIR/fonts/NotoMono-Regular.ttf.cpp \
113 --align 4
114fi
115
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400116# Turn off exiting while we check for ninja (which may not be on PATH)
117set +e
118NINJA=`which ninja`
119if [[ -z $NINJA ]]; then
120 git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
121 NINJA=$BUILD_DIR/depot_tools/ninja
122fi
123# Re-enable error checking
124set -e
Kevin Lubick217056c2018-09-20 17:39:31 -0400125
126echo "Compiling bitcode"
127
Kevin Lubick217056c2018-09-20 17:39:31 -0400128# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
129./bin/gn gen ${BUILD_DIR} \
130 --args="cc=\"${EMCC}\" \
131 cxx=\"${EMCXX}\" \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400132 extra_cflags_cc=[\"-frtti\"] \
Kevin Lubicke805b242018-10-10 14:55:01 -0400133 extra_cflags=[\"-s\",\"USE_FREETYPE=1\",\"-s\",\"USE_LIBPNG=1\", \"-s\", \"WARN_UNALIGNED=1\",
Kevin Lubick2cf29ce2018-10-25 14:46:50 -0400134 \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\", \"-DSK_DISABLE_DAA\", \"-DSK_DISABLE_READBUFFER\",
Mike Klein54378232018-11-08 12:08:05 +0000135 \"-DSK_DISABLE_EFFECT_DESERIALIZATION\",
Kevin Lubickf20c3492018-10-17 09:46:00 -0400136 ${GN_GPU_FLAGS}
Kevin Lubickd45c7812018-10-02 11:33:52 -0400137 ${EXTRA_CFLAGS}
138 ] \
Kevin Lubick217056c2018-09-20 17:39:31 -0400139 is_debug=false \
140 is_official_build=true \
141 is_component_build=false \
142 target_cpu=\"wasm\" \
143 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400144 skia_use_angle = false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400145 skia_use_dng_sdk=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400146 skia_use_egl=true \
Kevin Lubick217056c2018-09-20 17:39:31 -0400147 skia_use_expat=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400148 skia_use_fontconfig=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400149 skia_use_freetype=true \
150 skia_use_icu=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400151 skia_use_libheif=false \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400152 skia_use_system_libjpeg_turbo = false \
153 skia_use_libjpeg_turbo=true \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400154 skia_use_libpng=true \
155 skia_use_libwebp=false \
156 skia_use_lua=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400157 skia_use_piex=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400158 skia_use_vulkan=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400159 skia_use_zlib=true \
160 \
Kevin Lubick93faa672018-10-10 15:54:53 -0400161 skia_enable_ccpr=false \
Kevin Lubick4bf2c262018-10-15 09:35:54 -0400162 skia_enable_nvpr=false \
Kevin Lubick32dfdbe2018-10-18 09:47:01 -0400163 skia_enable_skpicture=false \
Kevin Lubick53965c92018-10-11 08:51:55 -0400164 ${GN_GPU} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400165 skia_enable_fontmgr_empty=false \
166 skia_enable_pdf=false"
167
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400168${NINJA} -C ${BUILD_DIR} libskia.a
Kevin Lubick217056c2018-09-20 17:39:31 -0400169
170export EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js "
171
Kevin Lubick217056c2018-09-20 17:39:31 -0400172echo "Generating final wasm"
173
174# Skottie doesn't end up in libskia and is currently not its own library
175# so we just hack in the .cpp files we need for now.
Kevin Lubickb9d7fb92018-10-19 09:33:12 -0400176# Emscripten prefers that libskia.a goes last in order, otherwise, it
177# may drop symbols that it incorrectly thinks aren't used. One day,
178# Emscripten will use LLD, which may relax this requirement.
Kevin Lubicke805b242018-10-10 14:55:01 -0400179${EMCXX} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400180 $RELEASE_CONF \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400181 -Iexperimental \
Kevin Lubick217056c2018-09-20 17:39:31 -0400182 -Iinclude/c \
183 -Iinclude/codec \
184 -Iinclude/config \
185 -Iinclude/core \
186 -Iinclude/effects \
187 -Iinclude/gpu \
188 -Iinclude/gpu/gl \
189 -Iinclude/pathops \
190 -Iinclude/private \
191 -Iinclude/utils/ \
192 -Imodules/skottie/include \
Florin Malitac2e85f92018-11-29 11:38:12 -0500193 -Imodules/skottie/utils \
Kevin Lubick217056c2018-09-20 17:39:31 -0400194 -Imodules/sksg/include \
195 -Isrc/core/ \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400196 -Isrc/gpu/ \
Kevin Lubick217056c2018-09-20 17:39:31 -0400197 -Isrc/sfnt/ \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400198 -Isrc/shaders/ \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400199 -Isrc/utils/ \
Kevin Lubick217056c2018-09-20 17:39:31 -0400200 -Itools \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400201 -Itools/fonts \
Kevin Lubick2cf29ce2018-10-25 14:46:50 -0400202 -DSK_DISABLE_READBUFFER \
203 -DSK_DISABLE_AAA \
204 -DSK_DISABLE_DAA \
Kevin Lubick53965c92018-10-11 08:51:55 -0400205 $WASM_GPU \
Kevin Lubicke805b242018-10-10 14:55:01 -0400206 -std=c++14 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400207 --bind \
208 --pre-js $BASE_DIR/helper.js \
209 --pre-js $BASE_DIR/interface.js \
Kevin Lubick65846c02019-01-04 14:16:19 -0500210 --post-js $BASE_DIR/ready.js \
Kevin Lubick006a6f32018-10-19 14:34:34 -0400211 $HTML_CANVAS_API \
Kevin Lubickddd0a332018-12-12 10:35:13 -0500212 $BUILTIN_FONT \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400213 $BASE_DIR/canvaskit_bindings.cpp \
Kevin Lubick53965c92018-10-11 08:51:55 -0400214 $WASM_SKOTTIE \
Florin Malitac2e85f92018-11-29 11:38:12 -0500215 $WASM_MANAGED_SKOTTIE \
Kevin Lubickb9d7fb92018-10-19 09:33:12 -0400216 $BUILD_DIR/libskia.a \
Kevin Lubick217056c2018-09-20 17:39:31 -0400217 -s ALLOW_MEMORY_GROWTH=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400218 -s EXPORT_NAME="CanvasKitInit" \
219 -s FORCE_FILESYSTEM=0 \
220 -s MODULARIZE=1 \
221 -s NO_EXIT_RUNTIME=1 \
222 -s STRICT=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400223 -s TOTAL_MEMORY=32MB \
Kevin Lubick217056c2018-09-20 17:39:31 -0400224 -s USE_FREETYPE=1 \
225 -s USE_LIBPNG=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400226 -s WARN_UNALIGNED=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400227 -s WASM=1 \
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400228 -o $BUILD_DIR/canvaskit.js