name: Build on Ubuntu

on:
  workflow_call:
    inputs:
      version:
        type: string
        required: true
      matrix: # [ { name, runner, docker_tag }, ... ]
        type: string
        required: true
      release:
        type: boolean

jobs:
  build_on_ubuntu:
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJSON(inputs.matrix) }}
    name: ${{ matrix.name }} (${{ matrix.compiler }}, ${{ matrix.build_type }})
    runs-on: ubuntu-latest
    env:
      BUILD_TESTS: OFF
    container: wasmedge/wasmedge:${{ matrix.docker_tag }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Set environment variables for tests
        if: ${{ matrix.tests }}
        run: |
          echo "BUILD_TESTS=ON" | tee -a $GITHUB_ENV
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Build (${{ matrix.compiler }}, ${{ matrix.build_type }})
        if: ${{ ! matrix.coverage }}
        shell: bash
        env:
          CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
        run: |
          if [[ "${{ matrix.compiler }}" == "clang++" ]]; then
            cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DWASMEDGE_LINK_LLVM_STATIC=ON -DWASMEDGE_BUILD_TESTS=$BUILD_TESTS -DWASMEDGE_BUILD_PACKAGE="TGZ" ${{ matrix.options }} .
          else
            cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DWASMEDGE_BUILD_TESTS=$BUILD_TESTS -DWASMEDGE_BUILD_PACKAGE="TGZ" ${{ matrix.options }} .
          fi
          cmake --build build
          cmake --build build --target package
      - name: Test
        if: ${{ !matrix.coverage && env.BUILD_TESTS }}
        run: |
          export LD_LIBRARY_PATH="$(pwd)/build/lib/api:$LD_LIBRARY_PATH"
          cd build
          ./tools/wasmedge/wasmedge -v
          ctest
          cd -
      - name: Build (${{ matrix.compiler }}, Coverage)
        if: ${{ matrix.coverage }}
        run: |
          apt update
          apt install -y gcovr
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_BUILD_TESTS=ON -DWASMEDGE_BUILD_COVERAGE=ON .
          cmake --build build
          LD_LIBRARY_PATH=$(pwd)/build/lib/api cmake --build build --target codecov
      - name: Upload artifact
        if: ${{ !inputs.release && !matrix.coverage }}
        uses: actions/upload-artifact@v3
        with:
          name: WasmEdge-${{ inputs.version }}-${{ matrix.name }}-${{ matrix.compiler }}-${{ matrix.build_type }}.tar.gz
          path: build/WasmEdge-${{ inputs.version }}-Linux.tar.gz
      - name: Upload package tarball
        if: ${{ inputs.release }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          type -p curl >/dev/null || (apt update && apt install curl -y)
          curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
          && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
          && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
          && apt update \
          && apt install gh -y
          mv build/WasmEdge-${{ inputs.version }}-Linux.tar.gz WasmEdge-${{ inputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Create and upload coverage report to Codecov
        if: ${{ matrix.coverage }}
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./build/codecov.xml
          name: codecov-wasmedge
          fail_ci_if_error: true
