name: CI

on:
  create:
    tags:
  push:
    branches:
      - main
  pull_request:

jobs:
  lint:
    name: lint
    runs-on: ubuntu-latest
    steps:
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
    - name: install tools
      run: |
        pip3 install flake8==7.3.0
        sudo apt-get install clang-format
    - run: flake8
    - run: ./scripts/clang-format-diff.sh
      env:
        GITHUB_EVENT_BEFORE: ${{ github.event.before }}
  build:
    name: build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # Temporarily replaced macos-latest with macos-14.
        # See https://github.com/WebAssembly/wabt/issues/2654
        os: [ubuntu-latest, macos-14, windows-latest]
    steps:
    - uses: actions/setup-python@v5
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: install ninja (linux)
      run: sudo apt-get install ninja-build
      if: matrix.os == 'ubuntu-latest'
    - name: install ninja (osx)
      run: brew install ninja
      if: startsWith(matrix.os, 'macos-')
    - name: install ninja (win)
      run: choco install ninja
      if: matrix.os == 'windows-latest'
    - name: mkdir
      run: mkdir -p out
    - name: tool versions
      run: |
        cmake --version
        ninja --version
    - name: cmake
      run: cmake .. -G Ninja -DWERROR=ON -Werror=dev -DCMAKE_ERROR_DEPRECATED=OFF
      working-directory: out
      if: matrix.os != 'windows-latest'
    - name: cmake (windows)
      run: cmake .. -DWERROR=ON -Werror=dev -DCMAKE_ERROR_DEPRECATED=OFF
      working-directory: out
      if: matrix.os == 'windows-latest'
    - name: build
      run: cmake --build out
    - name: check if generated files are up-to-date
      run: python ./scripts/check_clean.py
    - name: unittests
      run: cmake --build out --target run-unittests
    - name: c-api-tests
      run: cmake --build out --target run-c-api-tests
    - name: tests
      run: cmake --build out --target run-tests
  emscripten:
    name: emscripten
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: build
      run: |
        docker run -di --name emscripten -v $(pwd):/src emscripten/emsdk:latest bash
        docker exec emscripten emcc -v
        docker exec emscripten emcmake cmake -B emscripten -DWERROR=ON
        docker exec -w /src/emscripten emscripten make -j $(nproc)

  wasi:
    name: wasi
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
      - name: build-tools
        run: |
          docker run -di --name wasi-sdk -v $(pwd):/src --workdir /src ghcr.io/webassembly/wasi-sdk:wasi-sdk-20 bash
          docker exec wasi-sdk cmake -S . -B out -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -DBUILD_TESTS=0 -DBUILD_LIBWASM=0
          docker exec wasi-sdk cmake --build out --config Release --target install
      - uses: ./.github/actions/release-archive
        with:
          os: wasi

  sanitize:
    name: sanitize
    runs-on: ubuntu-24.04
    env:
      USE_NINJA: "1"
      CC: "clang"
      WASM2C_CFLAGS: "-march=x86-64-v2" # currently required for SIMDe to pass some tests on x86-64
    strategy:
      matrix:
        sanitizer: [asan, ubsan, fuzz]
        type: [debug, release]
    steps:
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
      with:
        submodules: true
    - run: sudo apt-get install ninja-build
    - name: workaround for ASLR+ASAN compatibility # See https://github.com/actions/runner/issues/3207
      run: sudo sysctl -w vm.mmap_rnd_bits=28
    - run: make clang-${{ matrix.type }}-${{ matrix.sanitizer }}
    - if: ${{ matrix.sanitizer }} != fuzz
      run: make test-clang-${{ matrix.type }}-${{ matrix.sanitizer }}
  build-wasm2c-memchecked:
    name: wasm2c-memchecked
    runs-on: ubuntu-latest
    env:
      USE_NINJA: "1"
      CC: "clang" # used by the wasm2c tests
      WASM2C_CFLAGS: "-march=x86-64-v2 -fsanitize=address -DWASM_RT_USE_MMAP=0"
    steps:
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
      with:
        submodules: true
    - run: sudo apt-get install ninja-build
    - name: workaround for ASLR+ASAN compatibility # See https://github.com/actions/runner/issues/3207
      run: sudo sysctl -w vm.mmap_rnd_bits=28
    - run: make clang-debug-asan
    - run: make test-clang-debug-asan

  build-min-cmake:
    name: min-cmake
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - name: Install Ninja
      run: sudo apt-get install ninja-build
    - name: Detect minimum CMake version
      run: >
        awk 'match($0, /cmake_minimum_required\(VERSION *([0-9]+\.[0-9]+)\)/, a)
        { print "WABT_CMAKE_VER=" a[1]; exit; }' CMakeLists.txt | tee $GITHUB_ENV
    - name: Install minimum CMake
      run: |
        python -m pip install -U setuptools wheel pip
        python -m pip install "cmake==${WABT_CMAKE_VER}.*"
        cmake --version

    - name: configure (OpenSSL)
      run: cmake -G Ninja -S . -B out -DCMAKE_BUILD_TYPE=Release
    - name: build (OpenSSL)
      run: cmake --build out
    - name: install (OpenSSL)
      run: cmake --install out --prefix install

    - name: configure (PicoSHA2)
      run: cmake -G Ninja -S . -B out-picosha -DCMAKE_BUILD_TYPE=Release -DUSE_INTERNAL_SHA256=ON
    - name: build (PicoSHA2)
      run: cmake --build out-picosha
    - name: install (PicoSHA2)
      run: cmake --install out-picosha --prefix install-picosha

    - name: test CMake package (OpenSSL)
      run: >
        ctest --build-and-test scripts/example-project example-openssl
        --build-generator Ninja
        --build-options -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PWD/install
        --test-command ctest

    - name: test CMake package (PicoSHA2)
      run: >
        ctest --build-and-test scripts/example-project example-picosha
        --build-generator Ninja
        --build-options -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PWD/install-picosha
        --test-command ctest

    - name: unittests
      run: cmake --build out --target run-unittests
    - name: c-api-tests
      run: cmake --build out --target run-c-api-tests
    - name: tests
      run: cmake --build out --target run-tests
  build-rlbox:
    name: rlbox
    runs-on: ubuntu-latest
    env:
      USE_NINJA: "1"
      WASM2C_CC: "clang"
      WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING -DWASM_RT_ALLOW_SEGUE=1 -DWASM_RT_SEGUE_FREE_SEGMENT=1 -mfsgsbase -DWASM_RT_SANITY_CHECKS=1 -Wno-pass-failed"
    steps:
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
      with:
        submodules: true
    - run: sudo apt-get install ninja-build
    - run: make clang-debug
    - name: tests (wasm2c tests excluding memory64)
      run: ./test/run-tests.py wasm2c --exclude-dir memory64

  build-cross:
    runs-on: ubuntu-latest
    # Temporatily disabled until we can get it fixed:
    # https://github.com/WebAssembly/wabt/issues/2655
    if: ${{ false }}
    strategy:
      fail-fast: false
      matrix:
        arch: [s390x]
    services: # still faster on debian...
      distcc:
        image: debian:latest
        options: --health-cmd distccmon-text --health-interval 5s --health-start-period 5m debian:latest bash -c "apt-get update && apt-get install -y g++-s390x-linux-gnu distcc && distccd --daemon --no-detach"
        ports:
          - 3632:3632
    env:
      QEMU_LD_PREFIX: /usr/${{matrix.arch}}-linux-gnu/
    steps:
    - uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
      with:
        platforms: ${{matrix.arch}}
        image: "tonistiigi/binfmt:master"
    - name: apt-get update
      run: sudo apt-get update
    - name: install ninja
      run: sudo apt-get install ninja-build
    - name: install the toolchain
      run: sudo apt-get install g++-${{matrix.arch}}-linux-gnu
    - name: install distcc
      run: sudo apt-get install distcc
    - name: mkdir distcc symlinks
      run: sudo mkdir -p /opt/bin/distcc_symlinks
    - name: distcc symlink
      run: sudo ln -s /usr/bin/distcc /opt/bin/distcc_symlinks/${{matrix.arch}}-linux-gnu-gcc # only CC is needed
    - name: cmake
      run: cmake -S . -B out -G Ninja -DCMAKE_TOOLCHAIN_FILE=../scripts/TC-${{matrix.arch}}.cmake -DWERROR=OFF -Werror=dev -DCMAKE_ERROR_DEPRECATED=OFF
    - name: build
      run: cmake --build out
    - name: check if generated files are up-to-date
      run: python ./scripts/check_clean.py
    - name: unittests
      run: cmake --build out --target run-unittests
    - name: tests
      run: cmake --build out --target run-tests
