name: release

on:
  pull_request:
  push:
    tags:
      - "v*.*.*"

jobs:
  linux:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        arch: [aarch64, riscv64, x86, x86_64]
    steps:
      - uses: actions/checkout@v4
      - uses: jirutka/setup-alpine@v1
        with:
          arch: ${{matrix.arch}}
          packages: "build-base make cmake"
      - name: build
        shell: alpine.sh {0}
        run: |
          mkdir build
          cd build
          cmake -DQJS_BUILD_CLI_STATIC=ON ..
          cd ..
          cmake --build build --target qjs_exe -j$(getconf _NPROCESSORS_ONLN)
          cmake --build build --target qjsc -j$(getconf _NPROCESSORS_ONLN)
          mv build/qjs build/qjs-linux-${{matrix.arch}}
          mv build/qjsc build/qjsc-linux-${{matrix.arch}}
      - name: check
        shell: alpine.sh {0}
        run: |
          file build/*-linux-${{matrix.arch}}
      - name: upload
        uses: actions/upload-artifact@v4
        with:
          name: qjs-linux-${{matrix.arch}}
          path: build/*-linux-${{matrix.arch}}

  macos:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4
      - name: build
        run: |
          mkdir build
          cd build
          cmake -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" ..
          make -j$(getconf _NPROCESSORS_ONLN)
          make -C .. amalgam # writes build/quickjs-amalgam.zip
          mv qjs qjs-darwin
          mv qjsc qjsc-darwin
      - name: check
        run: |
          lipo -info build/qjs-darwin build/qjsc-darwin
      - name: upload amalgamation
        uses: actions/upload-artifact@v4
        with:
          name: quickjs-amalgam.zip
          path: build/quickjs-amalgam.zip
          compression-level: 0 # already compressed
      - name: upload
        uses: actions/upload-artifact@v4
        with:
          name: qjs-darwin
          path: build/*-darwin

  windows:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        arch: [x86, x86_64]
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - uses: actions/checkout@v4
      - name: Setup MSYS2
        uses: msys2/setup-msys2@v2
        with:
          msystem: ${{ matrix.arch == 'x86' && 'mingw32' || 'ucrt64' }}
          install: >-
            git
            make
          pacboy: >-
            cmake:p
            ninja:p
            toolchain:p
      - name: build
        run: |
          make
          mv build/qjs.exe build/qjs-windows-${{matrix.arch}}.exe
          mv build/qjsc.exe build/qjsc-windows-${{matrix.arch}}.exe
      - name: check
        run: |
          file build/qjs-windows-${{matrix.arch}}.exe
          ldd build/qjs-windows-${{matrix.arch}}.exe build/qjsc-windows-${{matrix.arch}}.exe
      - name: upload
        uses: actions/upload-artifact@v4
        with:
          name: qjs-windows-${{matrix.arch}}
          path: build/*-windows-${{matrix.arch}}.exe

  wasi:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      - name: setup wasi-sdk
        run: |
          wget -nv https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.deb -P /tmp
          sudo apt install /tmp/wasi-sdk*.deb
      - name: build
        run: |
          cmake -B build -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake
          make -C build qjs_exe
          mv build/qjs build/qjs-wasi.wasm
      - name: upload
        uses: actions/upload-artifact@v4
        with:
          name: qjs-wasi
          path: build/qjs-wasi.wasm

  upload-to-release:
    needs: [linux, macos, windows, wasi]
    runs-on: ubuntu-22.04
    steps:
      - name: get assets
        uses: actions/download-artifact@v4
        with:
          path: build
          merge-multiple: true
      - run: ls -R build
      - name: release
        if: ${{ startsWith(github.ref, 'refs/tags/v') }}
        uses: softprops/action-gh-release@v1
        with:
          files: |
            build/*
