name: Platform builds

on:
  schedule: [cron: '0 14 * * *']

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

jobs:
  repo-check:
    name: Repository commit check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false

      - id: commit-check
        run: echo "has-commits=$(./scripts/has-commits-since.sh '24 hours ago')"  >> $GITHUB_OUTPUT
    outputs:
      has-commits: ${{ steps.commit-check.outputs.has-commits }}

  build_on_platform:
    name: ${{ matrix.name }}
    # The host should always be linux
    runs-on: ubuntu-20.04
    needs: repo-check
    if: needs.repo-check.outputs.has-commits == 'true'
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: ARMv6 (Raspbian)
            arch: armv6
            distro: bullseye
          - name: ARMv7 (Debian “bullseye”)
            arch: armv7
            distro: bullseye
          - name: ARM64 (Ubuntu 20.04)
            arch: aarch64
            distro: ubuntu20.04
          - name: s390x (Fedora)
            arch: s390x
            distro: fedora_latest
          - name: ppc64le (Fedora)
            arch: ppc64le
            distro: fedora_latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: false

      - uses: uraimo/run-on-arch-action@master
        name: Build artifact
        id: build
        with:
          arch: ${{ matrix.arch }}
          distro: ${{ matrix.distro }}

          # Not required, but speeds up builds
          githubToken: ${{ github.token }}

          # Create an artifacts directory
          setup: |
            mkdir -p "${PWD}/artifacts"

          # Mount the artifacts directory as /artifacts in the container
          dockerRunArgs: |
            --volume "${PWD}/artifacts:/artifacts"

          # The shell to run commands with in the container
          shell: /bin/sh

          # Install some dependencies in the container. This speeds up builds if
          # you are also using githubToken. Any dependencies installed here will
          # be part of the container image that gets cached, so subsequent
          # builds don't have to re-install them. The image layer is cached
          # publicly in your project's package repository, so it is vital that
          # no secrets are present in the container state or logs.
          install: |
            case "${{ matrix.distro }}" in
              ubuntu*|jessie|stretch|buster|bullseye)
                # we only care about issues
                apt-get -qq update  < /dev/null > /dev/null
                apt-get -qq install git curl findutils procps tar zstd screenfetch \
                                    < /dev/null > /dev/null
                apt-get -qq install cmake g++ build-essential ccache libasound2-dev libgtest-dev \
                                    libopusfile-dev libsdl2-dev python3-setuptools python3-pip zlib1g-dev \
                                    libspeexdsp-dev \
                                    < /dev/null > /dev/null
                pip3 install meson ninja
                ;;
              fedora*)
                dnf -q -y update
                dnf -q -y install git which curl findutils procps-ng tar zstd screenfetch
                dnf -q -y install cmake desktop-file-utils gcc gcc-c++ gmock-devel gtest-devel \
                                  libappstream-glib libatomic meson opusfile-devel SDL2-devel zlib-devel
                ;;
              alpine*)
                apk update
                apk add git
                ;;
            esac

          run: |
            # Log the environment
            sh ./scripts/log-env.sh
            screenfetch

            # Setup
            #  - use a minimal build because python fetching wraps segfaults under Docker
            meson setup \
                  -Duse_fluidsynth=false \
                  -Duse_sdl2_net=false \
                  -Duse_opengl=false \
                  -Duse_mt32emu=false \
                  -Duse_slirp=false \
                  -Duse_alsa=false \
                  build
            # Build
            meson compile -C build
            # Test
            meson test --num-processes 128 -t 0 -C build
