name: Test WasmEdge Core

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - master
    paths:
      - ".github/workflows/build.yml"
      - ".github/workflows/reusable**"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"
      - "cmake/**"
  pull_request:
    branches:
      - master
      - "proposal/**"
    paths:
      - ".github/workflows/build.yml"
      - ".github/workflows/reusable**"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"
      - "cmake/**"

jobs:
  # TODO: Refactor `lint` with `on.workflow_run`
  # https://docs.github.com/en/actions/using-workflows/triggering-a-workflow
  lint:
    uses: ./.github/workflows/reusable-call-linter.yml

  get_version:
    needs: lint
    name: Retrieve version information
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.prep.outputs.version }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Get version
        id: prep
        run: |
          # Retrieve annotated tags. Details: https://github.com/actions/checkout/issues/290
          git fetch --tags --force
          echo "Set version: $(git describe --match '[0-9].[0-9]*' --tag)"
          echo "version=$(git describe --match '[0-9].[0-9]*' --tag)" >> $GITHUB_OUTPUT

  create_source_tarball:
    needs: [get_version, lint]
    name: Source Tarball
    uses: ./.github/workflows/reusable-create-source-tarball.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_macos:
    needs: [get_version, lint]
    name: macOS
    uses: ./.github/workflows/reusable-build-on-macos.yml
    with:
      version: ${{ needs.get_version.outputs.version }}
      matrix: "[{'name':'MacOS 11 (x86_64)','runner':'macos-11','darwin_version':20,'arch':'x86_64'},
                {'name':'MacOS 12 (x86_64)','runner':'macos-12','darwin_version':21,'arch':'x86_64'},
                {'name':'MacOS 13 (arm64)','runner':'mac-arm64','darwin_version':22,'arch':'arm64'}]"

  build_on_manylinux_2014:
    needs: [get_version, lint]
    name: Manylinux2014
    uses: ./.github/workflows/reusable-build-on-manylinux.yml
    with:
      version: ${{ needs.get_version.outputs.version }}
      matrix: "[{'name':'manylinux 2014 x86_64','runner':'ubuntu-latest','docker_tag':'manylinux2014_x86_64'},
                {'name':'manylinux 2014 aarch64','runner':'linux-arm64','docker_tag':'manylinux2014_aarch64'}]"

  build_on_debian_static:
    needs: [get_version, lint]
    name: Debian (static)
    uses: ./.github/workflows/reusable-build-on-debian-static.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_alpine_static:
    needs: [get_version, lint]
    name: Alpine (static)
    uses: ./.github/workflows/reusable-build-on-alpine-static.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_ubuntu_22_04:
    needs: [get_version, lint]
    name: Ubuntu
    uses: ./.github/workflows/reusable-build-on-ubuntu.yml
    with:
      version: ${{ needs.get_version.outputs.version }}
      matrix: "[{'name':'ubuntu-22.04','compiler':'g++','build_type':'Debug','docker_tag':'ubuntu-build-gcc','tests':true},
                {'name':'ubuntu-22.04','compiler':'g++','build_type':'Release','docker_tag':'ubuntu-build-gcc','tests':true},
                {'name':'ubuntu-22.04','compiler':'clang++','build_type':'Debug','docker_tag':'ubuntu-build-clang','tests':true},
                {'name':'ubuntu-22.04','compiler':'clang++','build_type':'Release','docker_tag':'ubuntu-build-clang','tests':true},
                {'name':'linux-static','compiler':'clang++','build_type':'Release','docker_tag':'ubuntu-build-clang','options':'-DWASMEDGE_BUILD_SHARED_LIB=Off -DWASMEDGE_BUILD_STATIC_LIB=On -DWASMEDGE_LINK_TOOLS_STATIC=On -DWASMEDGE_BUILD_PLUGINS=Off'},
                {'name':'ubuntu-22.04-coverage','compiler':'g++','build_type':'Debug','docker_tag':'ubuntu-build-gcc','coverage':true,'tests':true}]"

  build_on_windows:
    needs: [get_version, lint]
    name: Windows
    uses: ./.github/workflows/reusable-build-on-windows.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_windows_msvc:
    needs: [get_version, lint]
    name: Windows-MSVC
    uses: ./.github/workflows/reusable-build-on-windows-msvc.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_android:
    needs: [get_version, lint]
    name: Android
    uses: ./.github/workflows/reusable-build-on-android.yml
    with:
      version: ${{ needs.get_version.outputs.version }}

  build_on_fedora:
    needs: [get_version, lint]
    name: Fedora
    uses: ./.github/workflows/reusable-build-on-fedora.yml
    with:
      version: ${{ needs.get_version.outputs.version }}
