build-wasm.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. function _print_usage {
  4. echo "Usage: $0 --target=web|nodejs [--no-container] -- [parameters for .devcontainer/env.sh]"
  5. echo
  6. echo "Use target 'web' to build for Threema Desktop and the examples."
  7. echo "Use target 'nodejs' to build for the bindings tests."
  8. echo "Use --no-container to not source a devcontainer environment."
  9. }
  10. while [[ "$#" -gt 0 ]]; do
  11. case "$1" in
  12. -h | --help)
  13. _print_usage
  14. exit 0
  15. ;;
  16. --target=web)
  17. _target='web'
  18. _build_dir='web'
  19. ;;
  20. --target=nodejs)
  21. _target='experimental-nodejs-module'
  22. _build_dir='nodejs'
  23. ;;
  24. --no-container)
  25. _no_container=1
  26. ;;
  27. --)
  28. shift;
  29. break
  30. ;;
  31. *)
  32. echo "Unknown parameter passed: $1"
  33. _print_usage
  34. exit 1
  35. ;;
  36. esac
  37. shift
  38. done
  39. if [[ -z ${_target+x} ]] || [[ -z ${_build_dir+x} ]] ; then
  40. _print_usage
  41. exit 1
  42. fi
  43. # Clean and load dev container
  44. cd "$(dirname "$0")/.."
  45. [[ -d ./build/wasm/$_build_dir ]] && rm -r ./build/wasm/$_build_dir
  46. if [[ -z ${_no_container+x} ]] ; then
  47. source ./.devcontainer/env.sh
  48. fi
  49. # Build library as WASM
  50. cargo build \
  51. -F wasm \
  52. -p libthreema \
  53. --target wasm32-unknown-unknown \
  54. --release
  55. # Create WASM bindings for Rust code
  56. wasm-bindgen \
  57. --split-linked-modules \
  58. --encode-into always \
  59. --reference-types \
  60. --out-dir ./build/wasm/$_build_dir \
  61. --target $_target \
  62. ./target/wasm32-unknown-unknown/release/libthreema.wasm
  63. # Optimise WASM
  64. wasm-opt \
  65. ./build/wasm/$_build_dir/libthreema_bg.wasm \
  66. -o ./build/wasm/$_build_dir/libthreema_bg.wasm \
  67. -O \
  68. --enable-reference-types
  69. # Unload dev container if it was started
  70. if [[ -z ${_no_container+x} ]] ; then
  71. deactivate --only-if-started
  72. fi