name: HLE RSP

on:
  push:
    paths-ignore:
      - '**/*.md'
      - '.{gitattributes,gitignore,travis.yml}'
      - 'appveyor.yml,README'
  pull_request:
    paths-ignore:
      - '**/*.md'
      - '.{gitattributes,gitignore,travis.yml}'
      - 'appveyor.yml,README'
  schedule:
    - cron: '30 14 21 * *'
  workflow_dispatch:

jobs:

  Linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          - cc: GCC
            platform: x64
            bits: 64
          - cc: GCC
            platform: x86
            bits: 32
          - cc: Clang
            platform: x64
            bits: 64
          - cc: Clang
            platform: x86
            bits: 32
    name: Linux / ${{ matrix.cc }} / ${{ matrix.platform }}
    runs-on: ubuntu-22.04
    if: (github.event_name == 'schedule' && github.repository == 'mupen64plus/mupen64plus-rsp-hle') || (github.event_name != 'schedule')
    steps:
      - uses: actions/checkout@v3
      - name: Get build dependencies and arrange the environment
        run: |
          echo "G_REV=$(git rev-parse --short HEAD)" >> "${GITHUB_ENV}"
          sudo apt-get update
          sudo apt-get --reinstall -y install gcc-multilib g++-multilib libc6 libc6-dev-i386
          sudo ldconfig
      - name: Build and related stuff, backup binaries
        run: |
          if [[ ${{ matrix.bits }} -eq 32 ]]; then export PIC="1" CPU_TUNE="-msse2 -mtune=pentium4"; else CPU_TUNE="-mtune=core2"; fi
          export OPTFLAGS="-O2 -flto ${CPU_TUNE}"
          ORIG="$(pwd)"
          CC="gcc"
          CXX="g++"
          if [[ "${{ matrix.cc }}" != "GCC" ]]; then
            CC="clang"
            CXX="clang++"
          fi
          ${CC} --version
          echo ""
          git clone --depth 1 https://github.com/mupen64plus/mupen64plus-core.git ../mupen64plus-core
          mkdir pkg
          echo ""
          echo ":: ${{ matrix.cc }} ${{ matrix.platform }} ::"
          echo ""
          make -C projects/unix clean
          echo ""
          make CC="${CC}" CXX="${CXX}" BITS="${{ matrix.bits }}" -C projects/unix all -j4
          echo ""
          make -C projects/unix install DESTDIR="${ORIG}/pkg/"
          echo ""
          ls -gG pkg/usr/local/lib/mupen64plus/*.so
          echo ""
          ldd pkg/usr/local/lib/mupen64plus/mupen64plus-rsp-hle.so
          tar cvzf pkg/mupen64plus-rsp-hle-linux-${{ matrix.platform }}-g${{ env.G_REV }}.tar.gz -C pkg/ "usr"
      - name: Upload artifact
        if: matrix.cc == 'GCC'
        uses: actions/upload-artifact@v3
        with:
          name: mupen64plus-rsp-hle-linux-${{ matrix.platform }}-g${{ env.G_REV }}
          path: pkg/*.tar.gz

  MSYS2:
    strategy:
      fail-fast: false
      matrix:
        include:
          - cc: GCC
            platform: x64
            cross: x86_64
            bits: 64
          - cc: GCC
            platform: x86
            cross: i686
            bits: 32
    name: Windows / MSYS2 ${{ matrix.cc }} / ${{ matrix.platform }}
    runs-on: windows-2022
    if: (github.event_name == 'schedule' && github.repository == 'mupen64plus/mupen64plus-rsp-hle') || (github.event_name != 'schedule')
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - uses: actions/checkout@v3
      - uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW${{ matrix.bits }}
          update: true
          install: >-
            git
            libtool
            make
            mingw-w64-${{ matrix.cross }}-gcc
            mingw-w64-${{ matrix.cross }}-toolchain
      - name: Build and related stuff, backup binaries
        run: |
          echo "G_REV=$(git rev-parse --short HEAD)" >> "${GITHUB_ENV}"
          if [[ ${{ matrix.bits }} -eq 32 ]]; then CPU_TUNE="-msse2 -mtune=pentium4"; else CPU_TUNE="-mtune=core2"; fi
          export OPTFLAGS="-O2 -flto ${CPU_TUNE}"
          ORIG="$(pwd)"
          CC="gcc"
          CXX="g++"
          ${CC} --version
          echo ""
          git clone --depth 1 https://github.com/mupen64plus/mupen64plus-core.git ../mupen64plus-core
          mkdir pkg
          echo ""
          echo ":: ${{ matrix.cc }} ${{ matrix.platform }} ::"
          echo ""
          make -C projects/unix clean
          echo ""
          make CC="${CC}" CXX="${CXX}" BITS="${{ matrix.bits }}" -C projects/unix all -j4
          echo ""
          make -C projects/unix install PLUGINDIR="" SHAREDIR="" BINDIR="" MANDIR="" LIBDIR="" APPSDIR="" ICONSDIR="icons" INCDIR="api" LDCONFIG="true" DESTDIR="${ORIG}/pkg/"
          echo ""
          ls -gG pkg/*.dll
          echo ""
          ldd pkg/mupen64plus-rsp-hle.dll
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: mupen64plus-rsp-hle-msys2-${{ matrix.platform }}-g${{ env.G_REV }}
          path: pkg/*

  MSVC:
    strategy:
      fail-fast: false
      matrix:
        include:
          - toolset: v143
            platform: x64
            vs: 2022
          - toolset: v141_xp
            platform: x86
            vs: 2019
    name: Windows / MSVC with ${{ matrix.toolset }} / ${{ matrix.platform }}
    runs-on: windows-${{ matrix.vs }}
    if: (github.event_name == 'schedule' && github.repository == 'mupen64plus/mupen64plus-rsp-hle') || (github.event_name != 'schedule')
    defaults:
      run:
        shell: cmd
    steps:
      - uses: actions/checkout@v3
      - uses: microsoft/setup-msbuild@v1
      - name: Build and related stuff, backup binaries
        run: |
          for /f "tokens=1" %%R in ('git rev-parse --short HEAD') do echo G_REV=%%R>> "%GITHUB_ENV%"
          set "ARCH=${{ matrix.platform }}"
          if [%ARCH%] == [x86] set "ARCH=Win32"
          echo.
          msbuild --version
          echo.
          git clone --depth 1 https://github.com/mupen64plus/mupen64plus-core.git ..\mupen64plus-core
          md pkg
          echo.
          msbuild projects\msvc\mupen64plus-rsp-hle.vcxproj /p:Configuration=Release;Platform=%ARCH%;PlatformToolset=${{ matrix.toolset }}
          echo.
          copy "projects\msvc\%ARCH%\Release\mupen64plus-rsp-hle.dll" pkg\
          dir pkg\*.dll
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: mupen64plus-rsp-hle-msvc-${{ matrix.platform }}-g${{ env.G_REV }}
          path: pkg/*

  Nightly-build:
    runs-on: ubuntu-latest
    if: github.ref_name == 'master'
    needs: [Linux, MSYS2, MSVC]
    steps:
      - uses: actions/checkout@v3
      - name: Download artifacts
        uses: actions/download-artifact@v3
        with:
          path: binaries
      - name: Get some tools
        run: |
          sudo apt-get update
          sudo apt-get -y install hashdeep
      - name: Creating new artifacts and update nightly-build
        run: |
          mkdir pkg
          cd binaries
          for BIN in *; do
            cd "${BIN}"
            if [[ "${BIN:20:4}" == "msys" ]]; then
              echo ":: Creating ${BIN}.zip"
              zip -r "../../pkg/${BIN}.zip" *
            elif [[ "${BIN:20:4}" == "msvc" ]]; then
              echo ":: Creating ${BIN}.zip"
              zip -r "../../pkg/${BIN}.zip" *
            else
              echo ":: Recovering ${BIN}.tar.gz"
              mv *.tar.gz ../../pkg/
            fi
            cd ..
          done
          cd ../pkg
          echo ""
          for BIN in *; do
            ls -gG ${BIN}
            tigerdeep -lz ${BIN} >> ../${BIN:0:19}.tiger.txt
            sha256sum ${BIN} >> ../${BIN:0:19}.sha256.txt
            sha512sum ${BIN} >> ../${BIN:0:19}.sha512.txt
            b2sum ${BIN} >> ../${BIN:0:19}.blake2.txt
          done
          mv ../*.tiger.txt .
          mv ../*.sha*.txt .
          mv ../*.blake2.txt .
          echo ""
          echo "TIGER:"
          cat *.tiger.txt
          echo ""
          echo "SHA256:"
          cat *.sha256.txt
          echo ""
          echo "SHA512:"
          cat *.sha512.txt
          echo ""
          echo "BLAKE2:"
          cat *.blake2.txt
          echo ""
          git tag -f nightly-build
          git push -f origin nightly-build
      - name: Nightly-build
        uses: ncipollo/release-action@v1
        with:
          prerelease: true
          allowUpdates: true
          removeArtifacts: true
          replacesArtifacts: false
          tag: nightly-build
          artifacts: pkg/*
