build.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. #
  3. # Script to build the WASM binary.
  4. #
  5. # The following environment variables can be used to configure the build:
  6. # EMSDK, BUILD, SUFFIX, DEFINES, FEATURES
  7. #
  8. # EMSDK must point to the directory where emsdk is installed
  9. # BUILD should be "small" or "memento"
  10. # DEFINES should be a set of -D defines (e.g. "-DTOFU")
  11. # FEATURES should be a list of "feature=no" (e.g. "brotli=no mujs=no extract=no xps=no svg=no html=no")
  12. # SUFFIX should be a tag to keep build files for different configurations separate
  13. #
  14. EMSDK=${EMSDK:-/opt/emsdk}
  15. BUILD=${BUILD:-small}
  16. DEFINES=${DEFINES:--DTOFU -DTOFU_CJK_EXT}
  17. FEATURES=${FEATURES:-brotli=no mujs=no extract=no xps=no svg=no}
  18. if [ "$BUILD" = "memento" ]
  19. then
  20. MEMENTO="-DMEMENTO -DMEMENTO_STACKTRACE_METHOD=0"
  21. fi
  22. export EMSDK_QUIET=1
  23. source $EMSDK/emsdk_env.sh
  24. emsdk install 4.0.8 >/dev/null || exit
  25. emsdk activate 4.0.8 >/dev/null || exit
  26. mkdir -p dist
  27. rm -f dist/*
  28. echo "COMPILING ($BUILD)"
  29. make --no-print-directory -j $(nproc) \
  30. -C ../.. \
  31. build=$BUILD \
  32. build_suffix=$SUFFIX \
  33. OS=wasm \
  34. XCFLAGS="$MEMENTO $DEFINES" \
  35. $FEATURES \
  36. libs
  37. echo "LINKING"
  38. emcc -o dist/mupdf-wasm.js \
  39. -I ../../include \
  40. -Os -g2 \
  41. $MEMENTO \
  42. --no-entry \
  43. -mno-nontrapping-fptoint \
  44. -fwasm-exceptions \
  45. -sSUPPORT_LONGJMP=wasm \
  46. -sMODULARIZE=1 \
  47. -sEXPORT_ES6=1 \
  48. -sEXPORT_NAME='"libmupdf_wasm"' \
  49. -sALLOW_MEMORY_GROWTH=1 \
  50. -sTEXTDECODER=2 \
  51. -sFILESYSTEM=0 \
  52. -sEXPORTED_RUNTIME_METHODS='["UTF8ToString","lengthBytesUTF8","stringToUTF8","HEAPU8","HEAP32","HEAPU32","HEAPF32"]' \
  53. lib/mupdf.c \
  54. ../../build/wasm/$BUILD$SUFFIX/libmupdf.a \
  55. ../../build/wasm/$BUILD$SUFFIX/libmupdf-third.a
  56. # remove executable bit from wasm-ld output
  57. chmod -x dist/mupdf-wasm.wasm
  58. echo "TYPESCRIPT"
  59. sed < lib/mupdf.c '/#include/d' | emcc -E - | node tools/make-wasm-type.js > lib/mupdf-wasm.d.ts
  60. cp lib/mupdf-wasm.d.ts dist/mupdf-wasm.d.ts
  61. npx tsc -p .
  62. # convert spaces to tabs
  63. sed -i -e 's/ /\t/g' dist/mupdf.js
  64. echo "TERSER"
  65. npx terser --module -c -m -o dist/mupdf-wasm.js dist/mupdf-wasm.js
  66. # npx terser --module -c -m -o dist/mupdf.min.js dist/mupdf.js