blob: 4167cb7bdd4a5fb74ebb0893e2c5b2d6e0a8cf0c [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 Lubick6fccc9d2018-11-20 15:55:10 -050023RELEASE_CONF="-Oz --closure 1 --llvm-lto 3 -DSK_RELEASE --pre-js $BASE_DIR/release.js"
Kevin Lubickd45c7812018-10-02 11:33:52 -040024EXTRA_CFLAGS="\"-DSK_RELEASE\""
Kevin Lubick217056c2018-09-20 17:39:31 -040025if [[ $@ == *debug* ]]; then
26 echo "Building a Debug build"
Kevin Lubickd45c7812018-10-02 11:33:52 -040027 EXTRA_CFLAGS="\"-DSK_DEBUG\""
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050028 RELEASE_CONF="-O0 --js-opts 0 -s DEMANGLE_SUPPORT=1 -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g3 \
29 -DPATHKIT_TESTING -DSK_DEBUG --pre-js $BASE_DIR/debug.js"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040030 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_debug"}
Kevin Lubickf9910ea2018-12-03 14:49:15 -050031elif [[ $@ == *profiling* ]]; then
32 echo "Building a build for profiling"
33 RELEASE_CONF="-O3 --source-map-base /node_modules/canvaskit/bin/ --profiling -g4 -DSK_RELEASE --pre-js $BASE_DIR/release.js"
34 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_profile"}
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040035else
36 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm"}
Kevin Lubick217056c2018-09-20 17:39:31 -040037fi
38
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040039mkdir -p $BUILD_DIR
40
Kevin Lubick53965c92018-10-11 08:51:55 -040041GN_GPU="skia_enable_gpu=true"
Kevin Lubick2cf29ce2018-10-25 14:46:50 -040042GN_GPU_FLAGS="\"-DIS_WEBGL=1\", \"-DSK_DISABLE_LEGACY_SHADERCONTEXT\","
43WASM_GPU="-lEGL -lGLESv2 -DSK_SUPPORT_GPU=1 \
Kevin Lubickb07204a2018-11-20 14:07:42 -050044 -DSK_DISABLE_LEGACY_SHADERCONTEXT --pre-js $BASE_DIR/cpu.js --pre-js $BASE_DIR/gpu.js"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040045if [[ $@ == *cpu* ]]; then
46 echo "Using the CPU backend instead of the GPU backend"
Kevin Lubick53965c92018-10-11 08:51:55 -040047 GN_GPU="skia_enable_gpu=false"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040048 GN_GPU_FLAGS=""
Kevin Lubick5b90b842018-10-17 07:57:18 -040049 WASM_GPU="-DSK_SUPPORT_GPU=0 --pre-js $BASE_DIR/cpu.js"
Kevin Lubick53965c92018-10-11 08:51:55 -040050fi
51
52WASM_SKOTTIE="-DSK_INCLUDE_SKOTTIE=1 \
53 modules/skottie/src/Skottie.cpp \
54 modules/skottie/src/SkottieAdapter.cpp \
55 modules/skottie/src/SkottieAnimator.cpp \
56 modules/skottie/src/SkottieJson.cpp \
57 modules/skottie/src/SkottieLayer.cpp \
58 modules/skottie/src/SkottieLayerEffect.cpp \
59 modules/skottie/src/SkottiePrecompLayer.cpp \
60 modules/skottie/src/SkottieProperty.cpp \
61 modules/skottie/src/SkottieShapeLayer.cpp \
62 modules/skottie/src/SkottieTextLayer.cpp \
63 modules/skottie/src/SkottieValue.cpp \
64 modules/sksg/src/*.cpp \
65 src/core/SkCubicMap.cpp \
66 src/core/SkTime.cpp \
67 src/pathops/SkOpBuilder.cpp \
68 src/utils/SkJSON.cpp \
69 src/utils/SkParse.cpp "
70if [[ $@ == *no_skottie* ]]; then
71 echo "Omitting Skottie"
72 WASM_SKOTTIE="-DSK_INCLUDE_SKOTTIE=0"
73fi
74
Florin Malitac2e85f92018-11-29 11:38:12 -050075WASM_MANAGED_SKOTTIE="\
76 -DSK_INCLUDE_MANAGED_SKOTTIE=1 \
77 modules/skottie/utils/SkottieUtils.cpp"
78if [[ $@ == *no_managed_skottie* ]]; then
79 echo "Omitting managed Skottie"
80 WASM_MANAGED_SKOTTIE="-DSK_INCLUDE_MANAGED_SKOTTIE=0"
81fi
82
Kevin Lubick006a6f32018-10-19 14:34:34 -040083HTML_CANVAS_API="--pre-js $BASE_DIR/htmlcanvas/canvas2d.js"
84if [[ $@ == *no_canvas* ]]; then
85 echo "Omitting bindings for HTML Canvas API"
86 HTML_CANVAS_API=""
87fi
88
Kevin Lubick8e9750d2018-10-09 09:36:35 -040089# Turn off exiting while we check for ninja (which may not be on PATH)
90set +e
91NINJA=`which ninja`
92if [[ -z $NINJA ]]; then
93 git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
94 NINJA=$BUILD_DIR/depot_tools/ninja
95fi
96# Re-enable error checking
97set -e
Kevin Lubick217056c2018-09-20 17:39:31 -040098
99echo "Compiling bitcode"
100
Kevin Lubick217056c2018-09-20 17:39:31 -0400101# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
102./bin/gn gen ${BUILD_DIR} \
103 --args="cc=\"${EMCC}\" \
104 cxx=\"${EMCXX}\" \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400105 extra_cflags_cc=[\"-frtti\"] \
Kevin Lubicke805b242018-10-10 14:55:01 -0400106 extra_cflags=[\"-s\",\"USE_FREETYPE=1\",\"-s\",\"USE_LIBPNG=1\", \"-s\", \"WARN_UNALIGNED=1\",
Kevin Lubick2cf29ce2018-10-25 14:46:50 -0400107 \"-DSKNX_NO_SIMD\", \"-DSK_DISABLE_AAA\", \"-DSK_DISABLE_DAA\", \"-DSK_DISABLE_READBUFFER\",
Mike Klein54378232018-11-08 12:08:05 +0000108 \"-DSK_DISABLE_EFFECT_DESERIALIZATION\",
Kevin Lubickf20c3492018-10-17 09:46:00 -0400109 ${GN_GPU_FLAGS}
Kevin Lubickd45c7812018-10-02 11:33:52 -0400110 ${EXTRA_CFLAGS}
111 ] \
Kevin Lubick217056c2018-09-20 17:39:31 -0400112 is_debug=false \
113 is_official_build=true \
114 is_component_build=false \
115 target_cpu=\"wasm\" \
116 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400117 skia_use_angle = false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400118 skia_use_dng_sdk=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400119 skia_use_egl=true \
Kevin Lubick217056c2018-09-20 17:39:31 -0400120 skia_use_expat=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400121 skia_use_fontconfig=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400122 skia_use_freetype=true \
123 skia_use_icu=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400124 skia_use_libheif=false \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400125 skia_use_system_libjpeg_turbo = false \
126 skia_use_libjpeg_turbo=true \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400127 skia_use_libpng=true \
128 skia_use_libwebp=false \
129 skia_use_lua=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400130 skia_use_piex=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400131 skia_use_vulkan=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400132 skia_use_zlib=true \
133 \
Kevin Lubick93faa672018-10-10 15:54:53 -0400134 skia_enable_ccpr=false \
Kevin Lubick4bf2c262018-10-15 09:35:54 -0400135 skia_enable_nvpr=false \
Kevin Lubick32dfdbe2018-10-18 09:47:01 -0400136 skia_enable_skpicture=false \
Kevin Lubick53965c92018-10-11 08:51:55 -0400137 ${GN_GPU} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400138 skia_enable_fontmgr_empty=false \
139 skia_enable_pdf=false"
140
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400141${NINJA} -C ${BUILD_DIR} libskia.a
Kevin Lubick217056c2018-09-20 17:39:31 -0400142
143export EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js "
144
Kevin Lubick217056c2018-09-20 17:39:31 -0400145echo "Generating final wasm"
146
147# Skottie doesn't end up in libskia and is currently not its own library
148# so we just hack in the .cpp files we need for now.
Kevin Lubickb9d7fb92018-10-19 09:33:12 -0400149# Emscripten prefers that libskia.a goes last in order, otherwise, it
150# may drop symbols that it incorrectly thinks aren't used. One day,
151# Emscripten will use LLD, which may relax this requirement.
Kevin Lubicke805b242018-10-10 14:55:01 -0400152${EMCXX} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400153 $RELEASE_CONF \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400154 -Iexperimental \
Kevin Lubick217056c2018-09-20 17:39:31 -0400155 -Iinclude/c \
156 -Iinclude/codec \
157 -Iinclude/config \
158 -Iinclude/core \
159 -Iinclude/effects \
160 -Iinclude/gpu \
161 -Iinclude/gpu/gl \
162 -Iinclude/pathops \
163 -Iinclude/private \
164 -Iinclude/utils/ \
165 -Imodules/skottie/include \
Florin Malitac2e85f92018-11-29 11:38:12 -0500166 -Imodules/skottie/utils \
Kevin Lubick217056c2018-09-20 17:39:31 -0400167 -Imodules/sksg/include \
168 -Isrc/core/ \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400169 -Isrc/gpu/ \
Kevin Lubick217056c2018-09-20 17:39:31 -0400170 -Isrc/sfnt/ \
Kevin Lubickb5ae3b52018-11-03 07:51:19 -0400171 -Isrc/shaders/ \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400172 -Isrc/utils/ \
Kevin Lubick217056c2018-09-20 17:39:31 -0400173 -Itools \
Kevin Lubick134be1d2018-10-30 15:05:04 -0400174 -Itools/fonts \
Kevin Lubick2cf29ce2018-10-25 14:46:50 -0400175 -DSK_DISABLE_READBUFFER \
176 -DSK_DISABLE_AAA \
177 -DSK_DISABLE_DAA \
Kevin Lubick53965c92018-10-11 08:51:55 -0400178 $WASM_GPU \
Kevin Lubicke805b242018-10-10 14:55:01 -0400179 -std=c++14 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400180 --bind \
181 --pre-js $BASE_DIR/helper.js \
182 --pre-js $BASE_DIR/interface.js \
Kevin Lubick006a6f32018-10-19 14:34:34 -0400183 $HTML_CANVAS_API \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400184 $BASE_DIR/canvaskit_bindings.cpp \
Kevin Lubick217056c2018-09-20 17:39:31 -0400185 tools/fonts/SkTestFontMgr.cpp \
186 tools/fonts/SkTestTypeface.cpp \
Kevin Lubick53965c92018-10-11 08:51:55 -0400187 $WASM_SKOTTIE \
Florin Malitac2e85f92018-11-29 11:38:12 -0500188 $WASM_MANAGED_SKOTTIE \
Kevin Lubickb9d7fb92018-10-19 09:33:12 -0400189 $BUILD_DIR/libskia.a \
Kevin Lubick217056c2018-09-20 17:39:31 -0400190 -s ALLOW_MEMORY_GROWTH=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400191 -s EXPORT_NAME="CanvasKitInit" \
192 -s FORCE_FILESYSTEM=0 \
193 -s MODULARIZE=1 \
194 -s NO_EXIT_RUNTIME=1 \
195 -s STRICT=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400196 -s TOTAL_MEMORY=32MB \
Kevin Lubick217056c2018-09-20 17:39:31 -0400197 -s USE_FREETYPE=1 \
198 -s USE_LIBPNG=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400199 -s WARN_UNALIGNED=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400200 -s WASM=1 \
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400201 -o $BUILD_DIR/canvaskit.js