blob: 7cfdd042b452b4f991de236ed8dd889f4c71b03f [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 Lubickd45c7812018-10-02 11:33:52 -040023RELEASE_CONF="-Oz --closure 1 --llvm-lto 3 -DSK_RELEASE"
24EXTRA_CFLAGS="\"-DSK_RELEASE\""
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040025
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 Lubick3d99b1e2018-10-16 10:15:01 -040029 RELEASE_CONF="-O0 --js-opts 0 -s DEMANGLE_SUPPORT=1 -s SAFE_HEAP=1 -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g3 -DPATHKIT_TESTING -DSK_DEBUG"
30 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm_debug"}
31else
32 BUILD_DIR=${BUILD_DIR:="out/canvaskit_wasm"}
Kevin Lubick217056c2018-09-20 17:39:31 -040033fi
34
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040035mkdir -p $BUILD_DIR
36
Kevin Lubick53965c92018-10-11 08:51:55 -040037GN_GPU="skia_enable_gpu=true"
38WASM_GPU="-lEGL -lGLESv2 -DSK_SUPPORT_GPU=1"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040039if [[ $@ == *cpu* ]]; then
40 echo "Using the CPU backend instead of the GPU backend"
Kevin Lubick53965c92018-10-11 08:51:55 -040041 GN_GPU="skia_enable_gpu=false"
Kevin Lubick3d99b1e2018-10-16 10:15:01 -040042 GN_GPU_FLAGS=""
Kevin Lubick53965c92018-10-11 08:51:55 -040043 WASM_GPU="-DSK_SUPPORT_GPU=0"
44fi
45
46WASM_SKOTTIE="-DSK_INCLUDE_SKOTTIE=1 \
47 modules/skottie/src/Skottie.cpp \
48 modules/skottie/src/SkottieAdapter.cpp \
49 modules/skottie/src/SkottieAnimator.cpp \
50 modules/skottie/src/SkottieJson.cpp \
51 modules/skottie/src/SkottieLayer.cpp \
52 modules/skottie/src/SkottieLayerEffect.cpp \
53 modules/skottie/src/SkottiePrecompLayer.cpp \
54 modules/skottie/src/SkottieProperty.cpp \
55 modules/skottie/src/SkottieShapeLayer.cpp \
56 modules/skottie/src/SkottieTextLayer.cpp \
57 modules/skottie/src/SkottieValue.cpp \
58 modules/sksg/src/*.cpp \
59 src/core/SkCubicMap.cpp \
60 src/core/SkTime.cpp \
61 src/pathops/SkOpBuilder.cpp \
62 src/utils/SkJSON.cpp \
63 src/utils/SkParse.cpp "
64if [[ $@ == *no_skottie* ]]; then
65 echo "Omitting Skottie"
66 WASM_SKOTTIE="-DSK_INCLUDE_SKOTTIE=0"
67fi
68
Kevin Lubick8e9750d2018-10-09 09:36:35 -040069# Turn off exiting while we check for ninja (which may not be on PATH)
70set +e
71NINJA=`which ninja`
72if [[ -z $NINJA ]]; then
73 git clone "https://chromium.googlesource.com/chromium/tools/depot_tools.git" --depth 1 $BUILD_DIR/depot_tools
74 NINJA=$BUILD_DIR/depot_tools/ninja
75fi
76# Re-enable error checking
77set -e
Kevin Lubick217056c2018-09-20 17:39:31 -040078
79echo "Compiling bitcode"
80
Kevin Lubick217056c2018-09-20 17:39:31 -040081# Inspired by https://github.com/Zubnix/skia-wasm-port/blob/master/build_bindings.sh
82./bin/gn gen ${BUILD_DIR} \
83 --args="cc=\"${EMCC}\" \
84 cxx=\"${EMCXX}\" \
Kevin Lubickd45c7812018-10-02 11:33:52 -040085 extra_cflags_cc=[\"-frtti\"] \
Kevin Lubicke805b242018-10-10 14:55:01 -040086 extra_cflags=[\"-s\",\"USE_FREETYPE=1\",\"-s\",\"USE_LIBPNG=1\", \"-s\", \"WARN_UNALIGNED=1\",
87 \"-DIS_WEBGL=1\", \"-DSKNX_NO_SIMD\",
Kevin Lubickd45c7812018-10-02 11:33:52 -040088 ${EXTRA_CFLAGS}
89 ] \
Kevin Lubick217056c2018-09-20 17:39:31 -040090 is_debug=false \
91 is_official_build=true \
92 is_component_build=false \
93 target_cpu=\"wasm\" \
94 \
Kevin Lubicke805b242018-10-10 14:55:01 -040095 skia_use_angle = false \
Kevin Lubick217056c2018-09-20 17:39:31 -040096 skia_use_dng_sdk=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -040097 skia_use_egl=true \
Kevin Lubick217056c2018-09-20 17:39:31 -040098 skia_use_expat=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -040099 skia_use_fontconfig=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400100 skia_use_freetype=true \
101 skia_use_icu=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400102 skia_use_libheif=false \
103 skia_use_libjpeg_turbo=false \
104 skia_use_libpng=true \
105 skia_use_libwebp=false \
106 skia_use_lua=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400107 skia_use_piex=false \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400108 skia_use_vulkan=false \
Kevin Lubick217056c2018-09-20 17:39:31 -0400109 skia_use_zlib=true \
110 \
Kevin Lubick93faa672018-10-10 15:54:53 -0400111 skia_enable_ccpr=false \
Kevin Lubick4bf2c262018-10-15 09:35:54 -0400112 skia_enable_nvpr=false \
Kevin Lubick53965c92018-10-11 08:51:55 -0400113 ${GN_GPU} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400114 skia_enable_fontmgr_empty=false \
115 skia_enable_pdf=false"
116
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400117${NINJA} -C ${BUILD_DIR} libskia.a
Kevin Lubick217056c2018-09-20 17:39:31 -0400118
119export EMCC_CLOSURE_ARGS="--externs $BASE_DIR/externs.js "
120
Kevin Lubick217056c2018-09-20 17:39:31 -0400121echo "Generating final wasm"
122
123# Skottie doesn't end up in libskia and is currently not its own library
124# so we just hack in the .cpp files we need for now.
Kevin Lubicke805b242018-10-10 14:55:01 -0400125${EMCXX} \
Kevin Lubick217056c2018-09-20 17:39:31 -0400126 $RELEASE_CONF \
127 -Iinclude/c \
128 -Iinclude/codec \
129 -Iinclude/config \
130 -Iinclude/core \
131 -Iinclude/effects \
132 -Iinclude/gpu \
133 -Iinclude/gpu/gl \
134 -Iinclude/pathops \
135 -Iinclude/private \
136 -Iinclude/utils/ \
137 -Imodules/skottie/include \
138 -Imodules/sksg/include \
139 -Isrc/core/ \
140 -Isrc/utils/ \
141 -Isrc/sfnt/ \
142 -Itools/fonts \
143 -Itools \
Kevin Lubick53965c92018-10-11 08:51:55 -0400144 $WASM_GPU \
Kevin Lubicke805b242018-10-10 14:55:01 -0400145 -std=c++14 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400146 --bind \
147 --pre-js $BASE_DIR/helper.js \
148 --pre-js $BASE_DIR/interface.js \
Kevin Lubickd45c7812018-10-02 11:33:52 -0400149 $BASE_DIR/canvaskit_bindings.cpp \
Kevin Lubick217056c2018-09-20 17:39:31 -0400150 $BUILD_DIR/libskia.a \
Kevin Lubick217056c2018-09-20 17:39:31 -0400151 tools/fonts/SkTestFontMgr.cpp \
152 tools/fonts/SkTestTypeface.cpp \
Kevin Lubick53965c92018-10-11 08:51:55 -0400153 $WASM_SKOTTIE \
Kevin Lubick217056c2018-09-20 17:39:31 -0400154 -s ALLOW_MEMORY_GROWTH=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400155 -s EXPORT_NAME="CanvasKitInit" \
156 -s FORCE_FILESYSTEM=0 \
157 -s MODULARIZE=1 \
158 -s NO_EXIT_RUNTIME=1 \
159 -s STRICT=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400160 -s TOTAL_MEMORY=32MB \
Kevin Lubick217056c2018-09-20 17:39:31 -0400161 -s USE_FREETYPE=1 \
162 -s USE_LIBPNG=1 \
Kevin Lubicke805b242018-10-10 14:55:01 -0400163 -s WARN_UNALIGNED=1 \
Kevin Lubick217056c2018-09-20 17:39:31 -0400164 -s WASM=1 \
Kevin Lubick8e9750d2018-10-09 09:36:35 -0400165 -o $BUILD_DIR/canvaskit.js