name: Platform builds

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

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 22.04)
            arch: aarch64
            distro: ubuntu22.04
          - name: s390x (Debian "bookworm")
            arch: s390x
            distro: bookworm
          - name: ppc64le (Debian "bookworm")
            arch: ppc64le
            distro: bookworm

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

      - uses: uraimo/run-on-arch-action@v2.8.1
        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
              ubuntu20.04|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 zlib1g-dev libspeexdsp-dev libpng-dev \
                                    < /dev/null > /dev/null
                # Meson from Debian repository is too old for DOSBox Staging
                apt-get -qq install python3-setuptools python3-pip \
                                    < /dev/null > /dev/null
                pip3 install meson ninja
                ;;
              ubuntu22.04|bookworm)
                # 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 meson libasound2-dev libgtest-dev \
                                    libopusfile-dev libsdl2-dev zlib1g-dev libspeexdsp-dev libpng-dev \
                                    < /dev/null > /dev/null
                ;;
              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
            #  - zlib-ng fails to compile for ARM targets, work around by just disabling
            #    it until a proper fix can be found
            meson setup \
                  -Duse_fluidsynth=false \
                  -Duse_sdl2_net=false \
                  -Duse_opengl=false \
                  -Duse_mt32emu=false \
                  -Duse_slirp=false \
                  -Duse_alsa=false \
                  -Duse_zlib_ng=false \
                  build
            # Build
            meson compile -C build
            # Test
            meson test --num-processes 128 -t 0 -C build
