name: Config heavy

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

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 }}

  config_all:
    name: ${{ matrix.system.name }} ${{ matrix.conf.configure_flags }}
    runs-on: ${{ matrix.system.os }}
    needs: repo-check
    if: needs.repo-check.outputs.has-commits == 'true'
    strategy:
      max-parallel: 8
      matrix:
        system:
          - name: Windows
            os: windows-latest
          - name: macOS
            os: macos-12
          - name: Linux
            os: ubuntu-20.04
        conf:
          - configure_flags:  -Duse_slirp=false
          - configure_flags:  -Duse_sdl2_net=false
          - configure_flags:  -Duse_opengl=false         # TODO opengl is always disabled on msys2
          - configure_flags:  -Duse_fluidsynth=false
          - configure_flags:  -Denable_debugger=normal
          - configure_flags:  -Denable_debugger=heavy
          - configure_flags:  -Ddynamic_core=dyn-x86
          - configure_flags:  -Ddynamic_core=dynrec
          - configure_flags:  -Ddynamic_core=none

    env:
      CHERE_INVOKING: yes

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

      - name:  Check repo for commits
        id:    repo-meta
        shell: bash
        run:   echo "has-commits=$(./scripts/has-commits-since.sh '24 hours ago')" >> $GITHUB_OUTPUT
      - name:  Get Date
        id:    get-date
        if:    matrix.system.name == 'Windows'
        shell: bash
        run:   echo "date=$(date +%Y-%W)" >> $GITHUB_OUTPUT

      - uses:  actions/cache@v4
        id:    cache-msys2
        if:    matrix.system.name == 'Windows'
        with:
          path: 'C:/tools/msys64'
          key: msys2-64-${{ steps.get-date.outputs.date }}-6

      - name:  Install MSYS2 (Windows)
        if:    >
          matrix.system.name == 'Windows' &&
          steps.cache-msys2.outputs.cache-hit != 'true'
        run:   choco install msys2 --no-progress

      - name:  Install dependencies ${{ matrix.conf.without_packages }} (Windows)
        if:    >
          matrix.system.name == 'Windows' &&
          steps.cache-msys2.outputs.cache-hit != 'true'
        shell: python scripts\msys-bash.py {0}
        run: |
           pacman -S --noconfirm $(cat packages/windows-latest-msys2.txt)
           .github/scripts/shrink-msys2.sh

      - name:  Install dependencies (Linux)
        if:    matrix.system.name == 'Linux'
        run: |
          sudo apt-get -y update
          sudo apt-get install $(cat packages/ubuntu-20.04-apt.txt)
          sudo pip3 install --upgrade meson ninja

      - name:  Install dependencies (macOS)
        if:    matrix.system.name == 'macOS'
        run: |
          brew install $(cat packages/macos-12-brew.txt)

      - name: Cache subprojects
        id:   cache-subprojects
        uses: actions/cache@v4
        with:
          path: subprojects.tar
          key: subprojects-${{ hashFiles('subprojects/*.wrap') }}-1
          enableCrossOsArchive: true

      - if:   steps.cache-subprojects.outputs.cache-hit != 'true'
        name: Generate subprojects cache
        run:  scripts/fetch-and-tar-subprojects.sh

      - name: Extract subprojects cache
        run:  scripts/extract-subprojects-tar.sh

      - name:  Build ${{ matrix.conf.configure_flags }} (Windows)
        if:    matrix.system.name == 'Windows'
        shell: python scripts\msys-bash.py {0}
        run: |
          export PATH="/mingw64/bin:$PATH"
          meson setup \
            -Dbuildtype=debug \
            -Dunit_tests=disabled \
            -Duse_opengl=false \
            ${{ matrix.conf.configure_flags }} \
            build
          cat build/meson-logs/meson-log.txt
          meson compile -C build

      - name:  Build ${{ matrix.conf.configure_flags }} (${{ matrix.system.name }})
        if:    matrix.system.name == 'macOS' || matrix.system.name == 'Linux'
        run: |
          meson setup \
            -Dunit_tests=disabled \
            ${{ matrix.conf.configure_flags }} \
            build
          meson compile -C build
