#!/usr/bin/env bash # Builds occt.wasm: unmodified Open CASCADE Technology (OCCT) as its own WebAssembly library # module, which the program module cad.wasm links against at run time. # # build_occt.sh [cad.wasm] [occt.wasm] build the OCCT libraries (cached) and link occt.wasm # build_occt.sh --libs only build the libraries; print the install prefix # # Paths default to cad.wasm / occt.wasm next to this script (the layout of the shipped wasm/cad/ # folder), so running it there replaces occt.wasm in place. cad.wasm is read only for the list of # symbols it imports, which occt.wasm then exports. # # Source: the pinned tag + commit in occt.env, fetched from the official repository, refused if # it has local changes. To use your own (e.g. modified) OCCT, set OCCT_SRC to its source tree. # Needs Emscripten $EMSCRIPTEN_VERSION (see occt.env), cmake, ninja, node; binaryen's wasm-opt is optional. # Work files go to OCCT_WORK (default ~/.cache/zolid-occt). set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" # shellcheck source=/dev/null source "$HERE/occt.env" WORK="${OCCT_WORK:-$HOME/.cache/zolid-occt}" JOBS="${JOBS:-$(getconf _NPROCESSORS_ONLN)}" have="$(emcc --version | head -1 | sed -E 's/.* ([0-9]+\.[0-9]+\.[0-9]+).*/\1/')" [ "$have" = "$EMSCRIPTEN_VERSION" ] || echo "warning: Emscripten $have, occt.env pins $EMSCRIPTEN_VERSION" >&2 if [ -n "${OCCT_SRC:-}" ]; then SRC="$OCCT_SRC" ID="custom-$(printf '%s' "$SRC" | cksum | cut -d' ' -f1)" else SRC="$WORK/occt-$OCCT_TAG" ID="$OCCT_COMMIT" [ -d "$SRC/.git" ] || git clone --depth 1 --branch "$OCCT_TAG" "$OCCT_URL" "$SRC" got="$(git -C "$SRC" rev-parse HEAD)" [ "$got" = "$OCCT_COMMIT" ] || { echo "OCCT commit mismatch: $got != $OCCT_COMMIT" >&2; exit 1; } [ -z "$(git -C "$SRC" status --porcelain)" ] || { echo "OCCT source has local changes; set OCCT_SRC to build your own" >&2; exit 1; } fi BUILD="$WORK/build-$ID" PREFIX="$WORK/install-$ID" # EMCC_CFLAGS: emcc appends these after the flags OCCT's CMake adds (-fexceptions, -O3, # -DOCC_CONVERT_SIGNALS), so they win without touching OCCT's files. The prefix map keeps # build-machine paths out of the binary (OCCT's asserts embed __FILE__). export EMCC_CFLAGS="$OCCT_CFLAGS -ffile-prefix-map=$SRC=occt" if [ ! -f "$PREFIX/.done" ] || [ -n "${OCCT_SRC:-}" ]; then emcmake cmake -S "$SRC" -B "$BUILD" -G Ninja \ -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-Os -DCMAKE_CXX_FLAGS=-Os \ -DBUILD_LIBRARY_TYPE=Static -DINSTALL_DIR="$PREFIX" \ -DBUILD_MODULE_FoundationClasses=OFF -DBUILD_MODULE_ModelingData=OFF \ -DBUILD_MODULE_ModelingAlgorithms=OFF -DBUILD_MODULE_Visualization=OFF \ -DBUILD_MODULE_ApplicationFramework=OFF -DBUILD_MODULE_DataExchange=OFF \ -DBUILD_MODULE_DETools=OFF -DBUILD_MODULE_Draw=OFF \ -DBUILD_ADDITIONAL_TOOLKITS="$OCCT_TOOLKITS" \ -DBUILD_DOC_Overview=OFF -DBUILD_Inspector=OFF -DBUILD_USE_PCH=OFF \ -DUSE_FREETYPE=OFF -DUSE_TK=OFF -DUSE_TCL=OFF -DUSE_OPENGL=OFF -DUSE_GLES2=OFF \ -DUSE_XLIB=OFF -DUSE_FREEIMAGE=OFF -DUSE_FFMPEG=OFF -DUSE_OPENVR=OFF \ -DUSE_RAPIDJSON=OFF -DUSE_DRACO=OFF -DUSE_TBB=OFF -DUSE_VTK=OFF -DUSE_EIGEN=OFF >&2 cmake --build "$BUILD" -j "$JOBS" >&2 cmake --install "$BUILD" >&2 touch "$PREFIX/.done" fi if [ "${1:-}" = "--libs" ]; then echo "$PREFIX" exit 0 fi CAD="${1:-$HERE/cad.wasm}" OUT="${2:-$HERE/occt.wasm}" # The interface: every symbol cad.wasm imports. Those it does not define itself must come from # OCCT or the C/C++ runtime (-u pulls them in, --export fails the link if one is missing); those it # also defines (inline functions, templates) are exported when OCCT has them, so one copy is used. RSP="$(mktemp)" trap 'rm -f "$RSP"' EXIT node -e ' const m = new WebAssembly.Module(require("fs").readFileSync(process.argv[1])); const own = new Set(WebAssembly.Module.exports(m).map((e) => e.name)); const names = new Set(WebAssembly.Module.imports(m) .filter((i) => i.module.startsWith("GOT.") || (i.module === "env" && /function|tag/.test(i.kind))) .map((i) => i.name)); for (const n of names) console.log(own.has(n) ? `-Wl,--export-if-defined=${n}` : `-Wl,-u,${n} -Wl,--export=${n}`); ' "$CAD" > "$RSP" # MAIN_MODULE=2 exports only what is asked for; STANDALONE_WASM needs no JavaScript glue (the # loader links the two modules itself). # shellcheck disable=SC2086 em++ $OCCT_CFLAGS "$PREFIX"/lib/libTK*.a "@$RSP" -Wl,-u,calloc -Wl,--export=calloc \ -sMAIN_MODULE=2 -sSTANDALONE_WASM=1 --no-entry \ -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=64MB -sMAXIMUM_MEMORY=2GB -sSTACK_SIZE=4MB \ -Wl,--gc-sections -o "$OUT" if command -v wasm-opt >/dev/null 2>&1; then wasm-opt -Os --strip-debug --strip-producers --enable-exception-handling --enable-bulk-memory \ --enable-sign-ext --enable-mutable-globals --enable-nontrapping-float-to-int "$OUT" -o "$OUT" fi ls -l "$OUT" >&2