name: Build
on:
  pull_request:
    types: [ opened, reopened, synchronize ]
  push:
    branches: [ master, release-1.0 ]

jobs:
  includes:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Check for superfluous includes and forward declarations
      run: |
        cd src
        ../utils/find_unused_includes.py

  codecheck:
    runs-on: ubuntu-latest
    steps:
    - name: Installing dependencies
      run: |
        sudo apt-get update
        sudo apt-get install cmake g++ gcc gettext libboost-dev libboost-system-dev libboost-test-dev libcurl4-gnutls-dev libglew-dev libpng-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev python3 zlib1g-dev -y
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Test
      run: |
        ./cmake/codecheck/run_tests.py
    - name: Check
      run: |
        mkdir build
        cd build
        cmake .. -DCMAKE_BUILD_TYPE:STRING="Debug"
        TERM=dumb make -j1 codecheck 2>&1 | tee codecheck.out
    - name: Report
      run: |
        if grep '^[/_.a-zA-Z]\+:[0-9]\+:' build/codecheck.out; then
          echo "You have codecheck warnings (see above). Please fix."
          exit 1
        else
          echo "Codecheck is clear :)"
          exit 0
        fi

  documentation:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Installing python
      uses: actions/setup-python@v1
      with:
        python-version: 3.x
    - name: Generate documentation
      run: |
        pip install sphinx
        cd doc/sphinx
        mkdir source/_static
        ./extract_rst.py
        sphinx-build -W -b json -d build/doctrees source build/json

  clang_tidy:
    runs-on: ubuntu-latest
    steps:
    - name: Installing dependencies
      run: |
        sudo apt-get update
        sudo apt-get install git cmake g++ gcc gettext libboost-dev libboost-system-dev libboost-test-dev libcurl4-gnutls-dev libglew-dev libpng-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev python3 zlib1g-dev clang-tidy python-yaml -y
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Check
      # To run a single check, call:
      # python3 ../utils/run-clang-tidy.py -checks=-*,my-check-prefix* > ../clang-tidy.log

      run: |
        export CC=/usr/bin/clang
        export CXX=/usr/bin/clang++
        mkdir build
        pushd build
        cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
        python3 ../utils/run-clang-tidy.py -checks=*,-android*,-fuchsia* > ../clang-tidy.log
        popd
    - name: Report
      run: utils/check_clang_tidy_results.py clang-tidy.log

  testsuite:
    needs: [clang_tidy, documentation, codecheck, includes]
    strategy:
      fail-fast: false
      matrix:
        config:
          - Release
          - Debug
        os:
          - ubuntu-20.04
        compiler:
          # We test the two newest and the two oldest versions of each of the two compilers
          - clang-10
          - clang-9
          - clang-7
          - clang-6.0
          - g++-10
          - g++-9
        include:
          # Some compilers need an older ubuntu version to run correctly
          - compiler: g++-5
            os: ubuntu-18.04
            config: Debug
          - compiler: g++-5
            os: ubuntu-18.04
            config: Release
          - compiler: g++-6
            os: ubuntu-18.04
            config: Debug
          - compiler: g++-6
            os: ubuntu-18.04
            config: Release
    runs-on: ${{ matrix.os }}
    steps:
    - name: Installing dependencies
      run: |
        sudo apt-get update
        sudo apt-get install git cmake ${{ matrix.compiler }} gettext libboost-dev libboost-system-dev libboost-test-dev libcurl4-gnutls-dev libglew-dev libpng-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev python3 zlib1g-dev mesa-utils linux-generic xserver-xorg-core xserver-xorg xserver-xorg-video-all xserver-xorg-input-all libwayland-egl1-mesa -y
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Compile
      run: |
        CXX="${{ matrix.compiler }}"
        if [ "${CXX:0:1}" == "c" ]
        then
          export CC="/usr/bin/${{ matrix.compiler }}"  # clang-10
          export CXX="${CC/clang/clang++}"             # clang++-10
        else
          export CXX="/usr/bin/${{ matrix.compiler }}" # g++-10
          export CC="${CXX/g++/gcc}"                   # gcc-10
        fi
        mkdir build
        pushd build
        cmake .. -DCMAKE_BUILD_TYPE:STRING="${{ matrix.config }}" -DOPTION_BUILD_TRANSLATIONS="ON" -DOPTION_BUILD_WEBSITE_TOOLS="ON" -DOPTION_ASAN="OFF" -DOPTION_BUILD_CODECHECK="OFF"
        make -k -j$(nproc)
        cd ..
    - name: Website Binaries
      run: |
        /sbin/start-stop-daemon --start --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 800x600x24 -ac +extension GLX
        sleep 1m
        export SDL_VIDEODRIVER=x11
        export DISPLAY=:99.0
        # Ignore transient SDL errors (exit code 2)
        mkdir temp_web
        build/src/website/wl_map_object_info temp_web || [ $? -eq 2 ]
        build/src/website/wl_map_info data/maps/Archipelago_Sea.wmf || [ $? -eq 2 ]
    - name: Testsuite
      run: |
        export SDL_VIDEODRIVER=x11
        export DISPLAY=:99.0
        ./regression_test.py -b build/src/widelands


  windows:
    # Skip this job on master branch
    if: github.ref != 'refs/heads/master'
    needs: [clang_tidy, documentation, codecheck, includes]
    strategy:
      matrix:
        config:
          - Release
          - Debug
        arch:
          - x64
          - x86
    name: Windows ${{ matrix.config }} ${{ matrix.arch }} Build
    runs-on: windows-latest
    steps:
    - name: Installing dependencies
      run: |
        $env:Path += ";C:\msys64\usr\bin"
        pacman.exe --noconfirm -Syyuu --ignore ca-certificates
        pacman.exe --noconfirm -Syyuu --ignore ca-certificates
        if ("${{ matrix.arch }}" -Match "x64") {
          $env:archname = "x86_64"
        } else {
          $env:archname = "i686"
        }
        pacman.exe --noconfirm -S mingw-w64-$env:archname-ninja mingw-w64-$env:archname-SDL2_ttf mingw-w64-$env:archname-SDL2_mixer mingw-w64-$env:archname-SDL2_image mingw-w64-$env:archname-glew mingw-w64-$env:archname-boost mingw-w64-$env:archname-curl unzip
    - name: Fix CURL
      # Replace libcurl.a with proper static version
      run: |
        if ("${{ matrix.arch }}" -Match "x64") {
          $env:bits = "64"
        } else {
          $env:bits = "32"
        }
        $env:curl = "7.74.0"
        $env:Path = "C:/msys64/mingw$env:bits/include;C:/msys64/mingw$env:bits/lib;C:/msys64/usr/bin;C:/msys64/mingw$env:bits/bin;$env:Path"
        curl -Lo curl.zip "https://curl.haxx.se/windows/dl-$env:curl/curl-$env:curl-win$env:bits-mingw.zip"
        unzip curl.zip
        mv -Force curl-$env:curl-win$env:bits-mingw/lib/libcurl.a C:/msys64/mingw$env:bits/lib/
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Compiler
      run: |
        if ("${{ matrix.arch }}" -Match "x64") {
          $env:bits = "64"
        } else {
          $env:bits = "32"
        }
        $env:Path = "C:/msys64/mingw$env:bits/include;C:/msys64/mingw$env:bits/lib;C:/msys64/usr/bin;C:/msys64/mingw$env:bits/bin;$env:Path"
        $env:CC = "C:/msys64/mingw$env:bits/bin/gcc.exe"
        $env:CXX = "C:/msys64/mingw$env:bits/bin/g++.exe"
        mkdir $env:GITHUB_WORKSPACE\build
        cd $env:GITHUB_WORKSPACE\build
        cmake.exe -G Ninja .. -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DOPTION_BUILD_WEBSITE_TOOLS=OFF -DOPTION_BUILD_TRANSLATIONS=ON -DOPTION_BUILD_TESTS=ON -DOPTION_ASAN=OFF -DUSE_XDG=OFF -DOPTION_BUILD_CODECHECK=OFF -DUSE_FLTO_IF_AVAILABLE=OFF -DCMAKE_JOB_POOLS="linking=1" -DCMAKE_JOB_POOL_LINK=linking -DOPTION_BUILD_WINSTATIC=ON
        cmake.exe --build .
        strip -sv ./src/widelands.exe
    - name: InnoSetup
      run: |
        cd $env:GITHUB_WORKSPACE
        # Environment variables needed by our InnoSetup script
        if ("${{ matrix.arch }}" -Match "x64") {
          $env:PLATFORM = "x64"
          $env:MINGWPATH = "C:\msys64\mingw64\bin"
        } else {
          $env:PLATFORM = "x86"
          $env:MINGWPATH = "C:\msys64\mingw32\bin"
        }
        $env:CONFIGURATION = "${{ matrix.config }}"
        $env:APPVEYOR_BUILD_FOLDER = $env:GITHUB_WORKSPACE
        $env:APPVEYOR_BUILD_VERSION = "Widelands-${{ github.sha }}-${{ matrix.config }}-${{ matrix.arch }}"
        strip.exe -sv .\build\src\widelands.exe
        ISCC.exe /o$env:GITHUB_WORKSPACE /fWidelands-${{ github.sha }}-${{ matrix.config }}-${{ matrix.arch }} $env:GITHUB_WORKSPACE\utils\win32\innosetup\Widelands.iss
    - name: Uploading installer
      uses: actions/upload-artifact@v2
      with:
        name: Widelands ${{ matrix.config }} ${{ matrix.arch }} Installer
        path: ${{ github.workspace }}\Widelands-${{ github.sha }}-${{ matrix.config }}-${{ matrix.arch }}.exe

  macos:
    needs: [clang_tidy, documentation, codecheck, includes]
    strategy:
      matrix:
        config:
          - release
          - debug
        compiler:
          - clang
    name: MacOS ${{ matrix.config }} ${{ matrix.compiler }}
    runs-on: macOS-latest
    steps:
    - name: Installing dependencies
      run: |
        export ICU_ROOT="$(brew --prefix icu4c)" && \
        export PATH="/usr/local/opt/ccache/libexec:/usr/local/opt/gettext/bin:$PATH";
        brew install boost curl ccache glew ninja sdl2 sdl2_image sdl2_mixer sdl2_ttf
    - name: Checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
    - name: Building
      run: |
        mkdir build_wl
        cd build_wl/
        ../utils/macos/build_app.sh ${{ matrix.compiler }} ${{ matrix.config }} ../
        DMGPATH="$(pwd)"
        DMGFILE="$(ls *.dmg)"
        echo "DMG file is: $DMGPATH/$DMGFILE"
        mv $DMGFILE ${{ github.workspace }}/Widelands-${{ github.sha }}-${{ matrix.config }}-${{ matrix.compiler }}.dmg
        #echo "::set-env name=dmg::$DMGPATH/$DMGFILE"

    - name: Uploading DMG
      uses: actions/upload-artifact@v2
      with:
        name: Widelands ${{ matrix.config }} ${{ matrix.compiler }} MacOS 10.5 AppImage
        path: ${{ github.workspace }}/Widelands-${{ github.sha }}-${{ matrix.config }}-${{ matrix.compiler }}.dmg
