1cb93a386Sopenharmony_ci#!/bin/bash
2cb93a386Sopenharmony_ci# Copyright 2021 Google LLC
3cb93a386Sopenharmony_ci#
4cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci# found in the LICENSE file.
6cb93a386Sopenharmony_ci
7cb93a386Sopenharmony_ciset -ex
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ciBASE_DIR=`cd $(dirname ${BASH_SOURCE[0]}) && pwd`
10cb93a386Sopenharmony_ci# This expects the environment variable EMSDK to be set
11cb93a386Sopenharmony_ciif [[ ! -d $EMSDK ]]; then
12cb93a386Sopenharmony_ci  cat >&2 << "EOF"
13cb93a386Sopenharmony_ciBe sure to set the EMSDK environment variable to the location of Emscripten SDK:
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci    https://emscripten.org/docs/getting_started/downloads.html
16cb93a386Sopenharmony_ciEOF
17cb93a386Sopenharmony_ci  exit 1
18cb93a386Sopenharmony_cifi
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci# Navigate to SKIA_HOME from where this file is located.
21cb93a386Sopenharmony_cipushd $BASE_DIR/../..
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ciif [[ $@ == *debug* ]]; then
24cb93a386Sopenharmony_ci  echo "Building a Debug build"
25cb93a386Sopenharmony_ci  DEBUG=true
26cb93a386Sopenharmony_ci  EXTRA_CFLAGS="\"-DSK_DEBUG\", \"-DGR_TEST_UTILS\", "
27cb93a386Sopenharmony_ci  RELEASE_CONF="-O1 --js-opts 0 -s DEMANGLE_SUPPORT=1 -frtti -s ASSERTIONS=1 -s GL_ASSERTIONS=1 -g \
28cb93a386Sopenharmony_ci                -DSK_DEBUG"
29cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/tskit_debug"}
30cb93a386Sopenharmony_cielse
31cb93a386Sopenharmony_ci  echo "Building a release build"
32cb93a386Sopenharmony_ci  DEBUG=false
33cb93a386Sopenharmony_ci  BUILD_DIR=${BUILD_DIR:="out/tskit"}
34cb93a386Sopenharmony_ci  RELEASE_CONF="-O3 --closure 1 -DSK_RELEASE"
35cb93a386Sopenharmony_ci  EXTRA_CFLAGS="\"-DSK_RELEASE\", \"-DGR_TEST_UTILS\", "
36cb93a386Sopenharmony_cifi
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ciEXTENSION_OPT="--pre-js $BASE_DIR/build/extension.js \
39cb93a386Sopenharmony_ci$BASE_DIR/bindings/extension.cpp
40cb93a386Sopenharmony_ci"
41cb93a386Sopenharmony_ciif [[ $@ = *no_ext* ]] ; then
42cb93a386Sopenharmony_ci  echo "disabling extension"
43cb93a386Sopenharmony_ci  EXTENSION_OPT=""
44cb93a386Sopenharmony_cifi
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_cimkdir -p $BUILD_DIR
47cb93a386Sopenharmony_ci# sometimes the .a files keep old symbols around - cleaning them out makes sure
48cb93a386Sopenharmony_ci# we get a fresh build.
49cb93a386Sopenharmony_cirm -f $BUILD_DIR/*.a
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_cisource $EMSDK/emsdk_env.sh
52cb93a386Sopenharmony_ciEMCC=`which emcc`
53cb93a386Sopenharmony_ciEMCXX=`which em++`
54cb93a386Sopenharmony_ciEMAR=`which emar`
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ciexport EMCC_CLOSURE_ARGS="--externs $BASE_DIR/build/externs.js "
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ciEMCC_DEBUG=1 ${EMCXX} \
59cb93a386Sopenharmony_ci    $RELEASE_CONF \
60cb93a386Sopenharmony_ci    -I. \
61cb93a386Sopenharmony_ci    -std=c++17 \
62cb93a386Sopenharmony_ci    --bind \
63cb93a386Sopenharmony_ci    --no-entry \
64cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/build/load.js \
65cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/build/memory.js \
66cb93a386Sopenharmony_ci    --pre-js $BASE_DIR/build/core.js \
67cb93a386Sopenharmony_ci    $BASE_DIR/bindings/core.cpp \
68cb93a386Sopenharmony_ci    $EXTENSION_OPT \
69cb93a386Sopenharmony_ci    -s LLD_REPORT_UNDEFINED \
70cb93a386Sopenharmony_ci    -s ALLOW_MEMORY_GROWTH=1 \
71cb93a386Sopenharmony_ci    -s EXPORT_NAME="InitTSKit" \
72cb93a386Sopenharmony_ci    -s EXPORTED_FUNCTIONS=['_malloc','_free'] \
73cb93a386Sopenharmony_ci    -s FORCE_FILESYSTEM=0 \
74cb93a386Sopenharmony_ci    -s FILESYSTEM=0 \
75cb93a386Sopenharmony_ci    -s MODULARIZE=1 \
76cb93a386Sopenharmony_ci    -s NO_EXIT_RUNTIME=1 \
77cb93a386Sopenharmony_ci    -s INITIAL_MEMORY=256MB \
78cb93a386Sopenharmony_ci    -s WASM=1 \
79cb93a386Sopenharmony_ci    -o $BUILD_DIR/tskit.js
80