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" 44GN_GPU_FLAGS="\"-DIS_WEBGL=1\", \"-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 54WASM_SKOTTIE=" \ 55 $BASE_DIR/skottie_bindings.cpp \ 56 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 modules/skshaper/src/SkShaper.cpp \ 69 modules/skshaper/src/SkShaper_primitive.cpp \ 70 src/core/SkCubicMap.cpp \ 71 src/core/SkTime.cpp \ 72 src/pathops/SkOpBuilder.cpp \ 73 src/utils/SkJSON.cpp \ 74 src/utils/SkParse.cpp " 75if [[ $@ == *no_skottie* ]]; then 76 echo "Omitting Skottie" 77 WASM_SKOTTIE="" 78fi 79 80WASM_MANAGED_SKOTTIE="\ 81 -DSK_INCLUDE_MANAGED_SKOTTIE=1 \ 82 modules/skottie/utils/SkottieUtils.cpp" 83if [[ $@ == *no_managed_skottie* ]]; then 84 echo "Omitting managed Skottie" 85 WASM_MANAGED_SKOTTIE="-DSK_INCLUDE_MANAGED_SKOTTIE=0" 86fi 87 88HTML_CANVAS_API="--pre-js $BASE_DIR/htmlcanvas/preamble.js \ 89--pre-js $BASE_DIR/htmlcanvas/util.js \ 90--pre-js $BASE_DIR/htmlcanvas/color.js \ 91--pre-js $BASE_DIR/htmlcanvas/font.js \ 92--pre-js $BASE_DIR/htmlcanvas/canvas2dcontext.js \ 93--pre-js $BASE_DIR/htmlcanvas/htmlcanvas.js \ 94--pre-js $BASE_DIR/htmlcanvas/imagedata.js \ 95--pre-js $BASE_DIR/htmlcanvas/lineargradient.js \ 96--pre-js $BASE_DIR/htmlcanvas/path2d.js \ 97--pre-js $BASE_DIR/htmlcanvas/pattern.js \ 98--pre-js $BASE_DIR/htmlcanvas/radialgradient.js \ 99--pre-js $BASE_DIR/htmlcanvas/postamble.js " 100if [[ $@ == *no_canvas* ]]; then 101 echo "Omitting bindings for HTML Canvas API" 102 HTML_CANVAS_API="" 103fi 104 105BUILTIN_FONT="$BASE_DIR/fonts/NotoMono-Regular.ttf.cpp" 106if [[ $@ == *no_font* ]]; then 107 echo "Omitting the built-in font(s)" 108 BUILTIN_FONT="" 109else 110 # Generate the font's binary file (which is covered by .gitignore) 111 python tools/embed_resources.py \ 112 --name SK_EMBEDDED_FONTS \ 113 --input $BASE_DIR/fonts/NotoMono-Regular.ttf \ 114 --output $BASE_DIR/fonts/NotoMono-Regular.ttf.cpp \ 115 --align 4 116fi 117 118# Turn off exiting while we check for ninja (which may not be on PATH) 119set +e 120NINJA=`which ninja` 121if [[ -z $NINJA ]]; then 122 git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools 123 NINJA=$BUILD_DIR/depot_tools/ninja 124fi 125# Re-enable error checking 126set -e 127 128echo "Compiling bitcode" 129 130# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh 131./bin/gn gen ${BUILD_DIR} \ 132 --args="cc=\"${EMCC}\" \ 133 cxx=\"${EMCXX}\" \ 134 extra_cflags_cc=[\"-frtti\"] \ 135 extra_cflags=[\"-s\",\"USE_FREETYPE=1\",\"-s\",\"USE_LIBPNG=1\", \"-s\", \"WARN_UNALIGNED=1\", 136 \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\", \"-DSK_DISABLE_DAA\", \"-DSK_DISABLE_READBUFFER\", 137 \"-DSK_DISABLE_EFFECT_DESERIALIZATION\", 138 ${GN_GPU_FLAGS} 139 ${EXTRA_CFLAGS} 140 ] \ 141 is_debug=false \ 142 is_official_build=true \ 143 is_component_build=false \ 144 target_cpu=\"wasm\" \ 145 \ 146 skia_use_angle = false \ 147 skia_use_dng_sdk=false \ 148 skia_use_egl=true \ 149 skia_use_expat=false \ 150 skia_use_fontconfig=false \ 151 skia_use_freetype=true \ 152 skia_use_icu=false \ 153 skia_use_libheif=false \ 154 skia_use_system_libjpeg_turbo = false \ 155 skia_use_libjpeg_turbo=true \ 156 skia_use_libpng=true \ 157 skia_use_libwebp=false \ 158 skia_use_lua=false \ 159 skia_use_piex=false \ 160 skia_use_vulkan=false \ 161 skia_use_zlib=true \ 162 \ 163 skia_enable_ccpr=false \ 164 skia_enable_nvpr=false \ 165 skia_enable_skpicture=false \ 166 ${GN_GPU} \ 167 skia_enable_fontmgr_empty=false \ 168 skia_enable_pdf=false" 169 170${NINJA} -C ${BUILD_DIR} libskia.a 171 172export EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js " 173 174echo "Generating final wasm" 175 176# Skottie doesn't end up in libskia and is currently not its own library 177# so we just hack in the .cpp files we need for now. 178# Emscripten prefers that libskia.a goes last in order, otherwise, it 179# may drop symbols that it incorrectly thinks aren't used. One day, 180# Emscripten will use LLD, which may relax this requirement. 181${EMCXX} \ 182 $RELEASE_CONF \ 183 -Iexperimental \ 184 -Iinclude/c \ 185 -Iinclude/codec \ 186 -Iinclude/config \ 187 -Iinclude/core \ 188 -Iinclude/effects \ 189 -Iinclude/gpu \ 190 -Iinclude/gpu/gl \ 191 -Iinclude/pathops \ 192 -Iinclude/private \ 193 -Iinclude/utils/ \ 194 -Imodules/skottie/include \ 195 -Imodules/skottie/utils \ 196 -Imodules/sksg/include \ 197 -Imodules/skshaper/include \ 198 -Isrc/core/ \ 199 -Isrc/gpu/ \ 200 -Isrc/sfnt/ \ 201 -Isrc/shaders/ \ 202 -Isrc/utils/ \ 203 -Itools \ 204 -Itools/fonts \ 205 -DSK_DISABLE_READBUFFER \ 206 -DSK_DISABLE_AAA \ 207 -DSK_DISABLE_DAA \ 208 $WASM_GPU \ 209 -std=c++14 \ 210 --bind \ 211 --pre-js $BASE_DIR/helper.js \ 212 --pre-js $BASE_DIR/interface.js \ 213 --post-js $BASE_DIR/ready.js \ 214 $HTML_CANVAS_API \ 215 $BUILTIN_FONT \ 216 $BASE_DIR/canvaskit_bindings.cpp \ 217 $WASM_SKOTTIE \ 218 $WASM_MANAGED_SKOTTIE \ 219 $BUILD_DIR/libskia.a \ 220 -s ALLOW_MEMORY_GROWTH=1 \ 221 -s EXPORT_NAME="CanvasKitInit" \ 222 -s FORCE_FILESYSTEM=0 \ 223 -s MODULARIZE=1 \ 224 -s NO_EXIT_RUNTIME=1 \ 225 -s STRICT=1 \ 226 -s TOTAL_MEMORY=32MB \ 227 -s USE_FREETYPE=1 \ 228 -s USE_LIBPNG=1 \ 229 -s WARN_UNALIGNED=1 \ 230 -s WASM=1 \ 231 -o $BUILD_DIR/canvaskit.js 232