name: windows

# build on c/cpp changes or workflow changes
on:
  push:
    paths:
      - 'lib/**.[ch]'
      - 'lib/**.cpp'
      - 'src/**.[ch]'
      - 'src/**.cpp'
      - '**/CMakeLists.txt'
      - 'cmake/Modules/**'
      - 'util/buildbot/**'
      - 'misc/irrlichtmt_tag.txt'
      - 'misc/*.manifest'
      - '.github/workflows/windows.yml'
  pull_request:
    paths:
      - 'lib/**.[ch]'
      - 'lib/**.cpp'
      - 'src/**.[ch]'
      - 'src/**.cpp'
      - '**/CMakeLists.txt'
      - 'cmake/Modules/**'
      - 'util/buildbot/**'
      - 'misc/irrlichtmt_tag.txt'
      - 'misc/*.manifest'
      - '.github/workflows/windows.yml'

jobs:
  mingw32:
    name: "MinGW cross-compiler (32-bit)"
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: Install compiler
        run: |
          sudo apt-get update && sudo apt-get install -y gettext
          sudo ./util/buildbot/download_toolchain.sh i686 /usr

      - name: Build
        run: |
           EXISTING_MINETEST_DIR=$PWD ./util/buildbot/buildwin32.sh B

      - uses: actions/upload-artifact@v3
        with:
          name: mingw32
          path: B/build/*.zip
          if-no-files-found: error

  mingw64:
    name: "MinGW cross-compiler (64-bit)"
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: Install compiler
        run: |
          sudo apt-get update && sudo apt-get install -y gettext
          sudo ./util/buildbot/download_toolchain.sh x86_64 /usr

      - name: Build
        run: |
          EXISTING_MINETEST_DIR=$PWD ./util/buildbot/buildwin64.sh B

      - uses: actions/upload-artifact@v3
        with:
          name: mingw64
          path: B/build/*.zip
          if-no-files-found: error

  msvc:
    name: VS 2019 ${{ matrix.config.arch }}-${{ matrix.type }}
    runs-on: windows-2019
    env:
      VCPKG_VERSION: 8eb57355a4ffb410a2e94c07b4dca2dffbee8e50
      # 2023.10.19
      vcpkg_packages: zlib zstd curl[winssl] openal-soft libvorbis libogg libjpeg-turbo sqlite3 freetype luajit gmp jsoncpp opengl-registry sdl2
    strategy:
      fail-fast: false
      matrix:
        config:
          - {
              arch: x86,
              generator: "-G'Visual Studio 16 2019' -A Win32",
              vcpkg_triplet: x86-windows
            }
          - {
              arch: x64,
              generator: "-G'Visual Studio 16 2019' -A x64",
              vcpkg_triplet: x64-windows
            }
        type: [portable]
#        type: [portable, installer]
# The installer type is working, but disabled, to save runner jobs.
# Enable it, when working on the installer.

    steps:
      - uses: actions/checkout@v3

      - name: Checkout IrrlichtMt
        run: |
          $ref = @(Get-Content misc\irrlichtmt_tag.txt)
          git clone https://github.com/minetest/irrlicht lib\irrlichtmt --depth 1 -b $ref[0]

      - name: Restore from cache and run vcpkg
        uses: lukka/run-vcpkg@v7
        with:
          vcpkgArguments: ${{env.vcpkg_packages}}
          vcpkgDirectory: '${{ github.workspace }}\vcpkg'
          appendedCacheKey: ${{ matrix.config.vcpkg_triplet }}
          vcpkgGitCommitId: ${{ env.VCPKG_VERSION }}
          vcpkgTriplet: ${{ matrix.config.vcpkg_triplet }}

      - name: Minetest CMake
        run: |
          cmake ${{matrix.config.generator}}  `
          -DCMAKE_TOOLCHAIN_FILE="${{ github.workspace }}\vcpkg\scripts\buildsystems\vcpkg.cmake"  `
          -DCMAKE_BUILD_TYPE=Release  `
          -DENABLE_POSTGRESQL=OFF  `
          -DENABLE_LUAJIT=TRUE  `
          -DREQUIRE_LUAJIT=TRUE  `
          -DRUN_IN_PLACE=${{ contains(matrix.type, 'portable') }} .

      - name: Build Minetest
        run: cmake --build . --config Release

      - name: Unittests
        # need this workaround for stdout to work
        run: |
          $proc = start .\bin\Release\minetest.exe --run-unittests -NoNewWindow -Wait -PassThru
          exit $proc.ExitCode
        continue-on-error: true # FIXME!!

      - name: CPack
        run: |
          If ($env:TYPE -eq "installer")
          {
            cpack -G WIX -B "$env:GITHUB_WORKSPACE\Package"
          }
          ElseIf($env:TYPE -eq "portable")
          {
            cpack -G ZIP -B "$env:GITHUB_WORKSPACE\Package"
          }
          rm -r $env:GITHUB_WORKSPACE\Package\_CPack_Packages
        env:
          TYPE: ${{matrix.type}}

      - uses: actions/upload-artifact@v3
        with:
          name: msvc-${{ matrix.config.arch }}-${{ matrix.type }}
          path: .\Package\
          if-no-files-found: error
