name: build

# 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/**'
      - 'util/ci/**'
      - '.github/workflows/**.yml'
      - 'Dockerfile'
      - '.dockerignore'
  pull_request:
    paths:
      - 'lib/**.[ch]'
      - 'lib/**.cpp'
      - 'src/**.[ch]'
      - 'src/**.cpp'
      - '**/CMakeLists.txt'
      - 'cmake/Modules/**'
      - 'util/buildbot/**'
      - 'util/ci/**'
      - '.github/workflows/**.yml'
      - 'Dockerfile'
      - '.dockerignore'

env:
  MINETEST_POSTGRESQL_CONNECT_STRING: 'host=localhost user=minetest password=minetest dbname=minetest'

jobs:
  # Older gcc version (should be close to our minimum supported version)
  gcc_7:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: |
          source ./util/ci/common.sh
          install_linux_deps g++-7

      - name: Build
        run: |
          ./util/ci/build.sh
        env:
          CC: gcc-7
          CXX: g++-7

      - name: Test
        run: |
          ./bin/minetest --run-unittests

  # Current gcc version
  gcc_12:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: |
          source ./util/ci/common.sh
          install_linux_deps g++-12 libluajit-5.1-dev

      - name: Build
        run: |
          ./util/ci/build.sh
        env:
          CC: gcc-12
          CXX: g++-12

      - name: Test
        run: |
          ./bin/minetest --run-unittests

  # Older clang version (should be close to our minimum supported version)
  clang_7:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: |
          source ./util/ci/common.sh
          install_linux_deps clang-7 valgrind

      - name: Build
        run: |
          ./util/ci/build.sh
        env:
          CC: clang-7
          CXX: clang++-7

      - name: Unittest
        run: |
          ./bin/minetest --run-unittests

      - name: Valgrind
        run: |
          valgrind --leak-check=full --leak-check-heuristics=all --undef-value-errors=no --error-exitcode=9 ./bin/minetest --run-unittests

  # Current clang version
  clang_14:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: |
          source ./util/ci/common.sh
          install_linux_deps clang-14 gdb

      - name: Build
        run: |
          ./util/ci/build.sh
        env:
          CC: clang-14
          CXX: clang++-14

      - name: Test
        run: |
          ./bin/minetest --run-unittests

      - name: Integration test + devtest
        run: |
          ./util/test_multiplayer.sh

  # Build with prometheus-cpp (server-only)
  clang_9_prometheus:
    name: "clang_9 (PROMETHEUS=1)"
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: |
          source ./util/ci/common.sh
          install_linux_deps clang-9

      - name: Build prometheus-cpp
        run: |
          ./util/ci/build_prometheus_cpp.sh

      - name: Build
        run: |
          ./util/ci/build.sh
        env:
          CC: clang-9
          CXX: clang++-9
          CMAKE_FLAGS: "-DENABLE_PROMETHEUS=1 -DBUILD_CLIENT=0"

      - name: Test
        run: |
          ./bin/minetestserver --run-unittests

  docker:
    name: "Docker image"
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Build docker image
        run: |
          docker build . -t minetest:latest
          docker run --rm minetest:latest /usr/local/bin/minetestserver --version

  win32:
    name: "MinGW cross-compiler (32-bit)"
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install compiler
        run: |
          sudo apt-get update && sudo apt-get install -y gettext
          wget http://minetest.kitsunemimi.pw/mingw-w64-i686_11.2.0_ubuntu20.04.tar.xz -O mingw.tar.xz
          sudo tar -xaf mingw.tar.xz -C /usr

      - name: Build
        run: |
           EXISTING_MINETEST_DIR=$PWD ./util/buildbot/buildwin32.sh winbuild
        env:
          NO_PACKAGE: 1

  win64:
    name: "MinGW cross-compiler (64-bit)"
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Install compiler
        run: |
          sudo apt-get update && sudo apt-get install -y gettext
          wget http://minetest.kitsunemimi.pw/mingw-w64-x86_64_11.2.0_ubuntu20.04.tar.xz -O mingw.tar.xz
          sudo tar -xaf mingw.tar.xz -C /usr

      - name: Build
        run: |
          EXISTING_MINETEST_DIR=$PWD ./util/buildbot/buildwin64.sh winbuild
        env:
          NO_PACKAGE: 1

  msvc:
    name: VS 2019 ${{ matrix.config.arch }}-${{ matrix.type }}
    runs-on: windows-2019
    env:
      VCPKG_VERSION: 5cf60186a241e84e8232641ee973395d4fde90e1
      # 2022.02
      vcpkg_packages: zlib zstd curl[winssl] openal-soft libvorbis libogg libjpeg-turbo sqlite3 freetype luajit gmp jsoncpp opengl-registry
    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: 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"
          }
        env:
          TYPE: ${{matrix.type}}

      - name: Package Clean
        run: rm -r $env:GITHUB_WORKSPACE\Package\_CPack_Packages

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