Skip to content

Commit

Permalink
Add WebAssembly for NodeJS. (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Mar 3, 2024
1 parent ac6825f commit ed06ced
Show file tree
Hide file tree
Showing 39 changed files with 1,484 additions and 1,921 deletions.
41 changes: 31 additions & 10 deletions .github/workflows/npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ concurrency:

permissions:
contents: read
id-token: write

jobs:
nodejs:
Expand All @@ -20,39 +21,59 @@ jobs:
python-version: ["3.8"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install emsdk
uses: mymindstorm/setup-emsdk@v14

- name: View emsdk version
shell: bash
run: |
emcc -v
echo "--------------------"
emcc --check
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- uses: actions/setup-node@v4
with:
node-version: 13
registry-url: 'https://registry.npmjs.org'

- name: Display node version
shell: bash
run: |
node --version
npm --version
cd nodejs-examples
npm install [email protected] -g
npm install [email protected]
npm --version
- name: Build nodejs package
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
./build-wasm-simd-nodejs.sh
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.js ./scripts/nodejs/
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.wasm ./scripts/nodejs/
SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION"
cd scripts/nodejs
./run.sh
owner=${{ github.repository_owner }}
echo "owner: $owner"
sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g ./package.json
sed -i.bak s/k2-fsa/$owner/g ./package.json
rm package.json.bak
git diff
npm install
rm run.sh
npm ci
# see https://docs.npmjs.com/generating-provenance-statements
npm publish --provenance --access public
1 change: 0 additions & 1 deletion .github/workflows/test-nodejs-npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version: 13
registry-url: 'https://registry.npmjs.org'

- name: Display node version
Expand Down
66 changes: 22 additions & 44 deletions .github/workflows/test-nodejs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,46 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest] #, windows-2019]
os: [ubuntu-latest] #, macos-latest] #, windows-2019]
python-version: ["3.8"]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: ccache
uses: hendrikmuhs/[email protected]
with:
key: ${{ matrix.os }}-Release-ON
- name: Install emsdk
uses: mymindstorm/setup-emsdk@v14

- name: Configure CMake
- name: View emsdk version
shell: bash
run: |
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cmake --version
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./install ..
cmake --build . --target install --config Release
ls -lh install/lib
emcc -v
echo "--------------------"
emcc --check
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Copy files
- uses: actions/setup-node@v4
with:
registry-url: 'https://registry.npmjs.org'

- name: Display node version
shell: bash
run: |
node --version
- name: Build nodejs package
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
os=${{ matrix.os }}
if [[ $os == 'ubuntu-latest' ]]; then
mkdir -p scripts/nodejs/lib/linux-x64
dst=scripts/nodejs/lib/linux-x64
elif [[ $os == 'macos-latest' ]]; then
mkdir -p scripts/nodejs/lib/osx-x64
dst=scripts/nodejs/lib/osx-x64
elif [[ $os == 'windows-2019' ]]; then
mkdir -p scripts/nodejs/lib/win-x64
dst=scripts/nodejs/lib/win-x64
fi
ls -lh build/install/lib/
rm -rf build/install/lib/pkgconfig
cp -v build/install/lib/* $dst/
./build-wasm-simd-nodejs.sh
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.js ./scripts/nodejs/
cp -v build-wasm-simd-nodejs/install/bin/wasm/nodejs/*.wasm ./scripts/nodejs/
- name: replace files
shell: bash
Expand All @@ -89,17 +78,6 @@ jobs:
git diff
cp *.js ../scripts/nodejs
- uses: actions/setup-node@v4
with:
node-version: 13
registry-url: 'https://registry.npmjs.org'

- name: Display node version
shell: bash
run: |
node --version
npm --version
- name: Run tests
shell: bash
run: |
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime GPU support" OFF)
option(SHERPA_ONNX_ENABLE_WASM "Whether to enable WASM" OFF)
option(SHERPA_ONNX_ENABLE_WASM_TTS "Whether to enable WASM for TTS" OFF)
option(SHERPA_ONNX_ENABLE_WASM_ASR "Whether to enable WASM for ASR" OFF)
option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF)
option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON)
option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON)

Expand Down Expand Up @@ -108,6 +109,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_GPU ${SHERPA_ONNX_ENABLE_GPU}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_NODEJS ${SHERPA_ONNX_ENABLE_WASM_NODEJS}")

if(SHERPA_ONNX_ENABLE_WASM_TTS)
if(NOT SHERPA_ONNX_ENABLE_WASM)
Expand All @@ -121,6 +123,12 @@ if(SHERPA_ONNX_ENABLE_WASM_ASR)
endif()
endif()

if(SHERPA_ONNX_ENABLE_WASM_NODEJS)
if(NOT SHERPA_ONNX_ENABLE_WASM)
message(FATAL_ERROR "Please set SHERPA_ONNX_ENABLE_WASM to ON if you enable WASM for NodeJS")
endif()
endif()

if(SHERPA_ONNX_ENABLE_WASM)
add_definitions(-DSHERPA_ONNX_ENABLE_WASM=1)
endif()
Expand Down
63 changes: 63 additions & 0 deletions build-wasm-simd-nodejs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Xiaomi Corporation
#
# This script is to build sherpa-onnx for WebAssembly (NodeJS)
#
# Please use NodeJS >= 18

set -ex

if [ x"$EMSCRIPTEN" == x"" ]; then
if ! command -v emcc &> /dev/null; then
echo "Please install emscripten first"
echo ""
echo "You can use the following commands to install it:"
echo ""
echo "git clone https://github.com/emscripten-core/emsdk.git"
echo "cd emsdk"
echo "git pull"
echo "./emsdk install latest"
echo "./emsdk activate latest"
echo "source ./emsdk_env.sh"
exit 1
else
EMSCRIPTEN=$(dirname $(realpath $(which emcc)))
fi
fi

export EMSCRIPTEN=$EMSCRIPTEN
echo "EMSCRIPTEN: $EMSCRIPTEN"
if [ ! -f $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake ]; then
echo "Cannot find $EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake"
echo "Please make sure you have installed emsdk correctly"
exit 1
fi

mkdir -p build-wasm-simd-nodejs
pushd build-wasm-simd-nodejs

export SHERPA_ONNX_IS_USING_BUILD_WASM_SH=ON

cmake \
-DCMAKE_INSTALL_PREFIX=./install \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \
\
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
-DSHERPA_ONNX_ENABLE_JNI=OFF \
-DSHERPA_ONNX_ENABLE_C_API=ON \
-DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
-DSHERPA_ONNX_ENABLE_GPU=OFF \
-DSHERPA_ONNX_ENABLE_WASM=ON \
-DSHERPA_ONNX_ENABLE_WASM_NODEJS=ON \
-DSHERPA_ONNX_ENABLE_BINARY=OFF \
-DSHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY=OFF \
..
make -j10
make install

ls -lh install/bin/wasm/nodejs
1 change: 1 addition & 0 deletions nodejs-examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
lib
package-lock.json
*.tar.bz2
Loading

0 comments on commit ed06ced

Please sign in to comment.