name: web-assembly
on:
  push:
    branches: [master]
    paths:
    - '.github/workflows/web-assembly.yml'
    - 'CMakeLists.txt'
    - 'cmake/**'
    - 'src/*'
    - 'src/layer/*'
    - 'src/layer/x86/**'
    - 'tests/**'
  pull_request:
    branches: [master]
    paths:
    - '.github/workflows/web-assembly.yml'
    - 'CMakeLists.txt'
    - 'cmake/**'
    - 'src/*'
    - 'src/layer/*'
    - 'src/layer/x86/**'
    - 'tests/**'

env:
  EMSCRIPTEN_VERSION: 3.1.28

concurrency:
  group: web-assembly-${{ github.ref }}
  cancel-in-progress: true
permissions:
  contents: read

jobs:
  webassembly:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: emsdk
      run: |
        git clone https://github.com/emscripten-core/emsdk.git
        cd emsdk
        ./emsdk install $EMSCRIPTEN_VERSION
        ./emsdk activate $EMSCRIPTEN_VERSION
    - name: build-basic
      run: |
        source emsdk/emsdk_env.sh
        export LDFLAGS="-sERROR_ON_WASM_CHANGES_AFTER_LINK -sWASM_BIGINT -O1"
        mkdir build-basic && cd build-basic
        cmake -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DNCNN_THREADS=OFF -DNCNN_OPENMP=OFF -DNCNN_SIMPLEOMP=OFF -DNCNN_SIMPLEOCV=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_SSE2=OFF -DNCNN_AVX2=OFF -DNCNN_AVX=OFF -DNCNN_BUILD_TESTS=ON ..
        cmake --build . -j $(nproc)
    - name: test-basic
      run: |
        cd build-basic
        TESTS_EXECUTABLE_LOADER=node ctest --output-on-failure -j $(nproc)
    - name: build-simd
      run: |
        source emsdk/emsdk_env.sh
        export LDFLAGS="-sERROR_ON_WASM_CHANGES_AFTER_LINK -sWASM_BIGINT -O1"
        mkdir build-simd && cd build-simd
        cmake -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DNCNN_THREADS=OFF -DNCNN_OPENMP=OFF -DNCNN_SIMPLEOMP=OFF -DNCNN_SIMPLEOCV=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_SSE2=ON -DNCNN_AVX2=OFF -DNCNN_AVX=OFF -DNCNN_BUILD_TESTS=ON ..
        cmake --build . -j $(nproc)
    - name: test-simd
      run: |
        cd build-simd
        TESTS_EXECUTABLE_LOADER=node ctest --output-on-failure -j $(nproc)
    - name: build-simd-omp
      run: |
        source emsdk/emsdk_env.sh
        export LDFLAGS="-sERROR_ON_WASM_CHANGES_AFTER_LINK -sWASM_BIGINT -O1"
        mkdir build-simd-omp && cd build-simd-omp
        cmake -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DNCNN_THREADS=ON -DNCNN_OPENMP=ON -DNCNN_SIMPLEOMP=ON -DNCNN_SIMPLEOCV=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_SSE2=ON -DNCNN_AVX2=OFF -DNCNN_AVX=OFF -DNCNN_BUILD_TESTS=ON ..
        cmake --build . -j $(nproc)
    - name: test-simd-omp
      run: |
        cd build-simd-omp
        TESTS_EXECUTABLE_LOADER=node ctest --output-on-failure -j $(nproc)
