name: Emscripten Build
on: [push, pull_request]

env:
  EM_VERSION: latest
  EM_CACHE_FOLDER: 'emsdk-cache'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@v6

    - name: Setup cache
      id: cache-system-libraries
      uses: actions/cache@v5
      with:
        path: ${{env.EM_CACHE_FOLDER}}
        key: ${{env.EM_VERSION}}-${{ runner.os }}

    - name: Setup Emscripten toolchain
      uses: emscripten-core/setup-emsdk@v16
      with:
        version: ${{ env.EM_VERSION }}
        actions-cache-folder: ${{env.EM_CACHE_FOLDER}}

    - name: Work around emscripten-core/emscripten#26534
      run: npm install -g typescript@5

    - name: Build
      run: |
        mkdir -p out/wasm
        cd out/wasm
        emcmake cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_COMPILE_WARNING_AS_ERROR=YES ../../
        make -j4
        mkdir xsystem35
        mv src/xsystem35.* xsystem35/

    - name: Upload artifacts
      uses: actions/upload-artifact@v7
      with:
        name: xsystem35-wasm
        path: out/wasm/xsystem35
