name: Build

on: [push, pull_request]

jobs:
  Build:
    name: ${{ matrix.platform.name }}
    runs-on: ${{ matrix.platform.os }}

    defaults:
      run:
        shell: ${{ matrix.platform.shell }}

    strategy:
      fail-fast: false
      matrix:
        platform:
        - { name: Windows (mingw32),        os: windows-latest, shell: 'msys2 {0}', msystem: mingw32, msys-env: mingw-w64-i686, cc: gcc }
        - { name: Windows (mingw64+clang),  os: windows-latest, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, cc: clang }
        - { name: Linux (CMake),            os: ubuntu-20.04,   shell: sh,    flags: true }
        - { name: Linux (autotools),        os: ubuntu-20.04,   shell: sh,    autotools: true }
        - { name: MacOS (CMake),            os: macos-latest,   shell: sh }
        - { name: MacOS (autotools),        os: macos-latest,   shell: sh,    autotools: true }

    steps:
    - name: Set up MSYS2
      if: matrix.platform.shell == 'msys2 {0}'
      uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.platform.msystem }}
        install: >-
          ${{ matrix.platform.msys-env }}-${{ matrix.platform.cc }}
          ${{ matrix.platform.msys-env }}-cmake
          ${{ matrix.platform.msys-env }}-ninja
          ${{ matrix.platform.msys-env }}-pkg-config
    - name: Configure MSYS2 compiler
      if: matrix.platform.shell == 'msys2 {0}'
      run: |
        if test x${{ matrix.platform.cc}} == xgcc; then
          echo "CC=gcc" >> $GITHUB_ENV
          echo "CXX=g++" >> $GITHUB_ENV
        fi
        if test x${{ matrix.platform.cc}} == xclang; then
          echo "CC=clang" >> $GITHUB_ENV
          echo "CXX=clang++" >> $GITHUB_ENV
        fi

    - name: Setup Linux dependencies
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install \
          autoconf \
          gnome-desktop-testing \
          wayland-protocols \
          pkg-config \
          ninja-build \
          libasound2-dev \
          libdbus-1-dev \
          libegl1-mesa-dev \
          libgl1-mesa-dev \
          libgles2-mesa-dev \
          libglu1-mesa-dev \
          libibus-1.0-dev \
          libpulse-dev \
          libsdl2-2.0-0 \
          libsndio-dev \
          libudev-dev \
          libwayland-dev \
          libwayland-client++0 \
          wayland-scanner++ \
          libwayland-cursor++0 \
          libx11-dev \
          libxcursor-dev \
          libxext-dev \
          libxi-dev \
          libxinerama-dev \
          libxkbcommon-dev \
          libxrandr-dev \
          libxss-dev \
          libxt-dev \
          libxv-dev \
          libxxf86vm-dev \
          libdrm-dev \
          libgbm-dev\
          libpulse-dev \
          libpango1.0-dev
        sudo apt install meson
        git clone --depth 1 https://gitlab.gnome.org/jadahl/libdecor.git --branch 0.1.0
        cd libdecor
        meson build --buildtype release -Ddemo=false -Ddbus=disabled
        ninja -v -C build
        sudo meson install -C build
    - name: Setup Macos dependencies
      if: runner.os == 'macOS'
      run: |
        brew install \
          ninja
    - uses: actions/checkout@v2
    - name: Check that versioning is consistent
      # We only need to run this once: arbitrarily use the Linux/CMake build
      if: "runner.os == 'Linux' && ! matrix.platform.autotools"
      run: ./test/versioning.sh
    - name: Configure (CMake)
      if: "! matrix.platform.autotools"
      run: |
        cmake -S . -B build -G Ninja \
        -DSDL_TESTS=ON \
        -DSDL_INSTALL_TESTS=ON \
        -DCMAKE_INSTALL_PREFIX=cmake_prefix \
        -DCMAKE_BUILD_TYPE=Release
    - name: Build (CMake)
      if: "! matrix.platform.autotools"
      run: |
        cmake --build build/ --config Release --verbose --parallel
    - name: Run build-time tests (CMake)
      if: "! matrix.platform.autotools"
      run: |
        set -eu
        export SDL_TESTS_QUICK=1
        ctest -VV --test-dir build/
    - name: Install (CMake)
      if: "! matrix.platform.autotools"
      run: |
        set -eu
        cmake --install build/ --config Release
        echo "SDL2_DIR=$(pwd)/cmake_prefix" >> $GITHUB_ENV
        ( cd cmake_prefix; find ) | LC_ALL=C sort -u
    - name: Configure (Autotools)
      if: matrix.platform.autotools
      run: |
        set -eu
        rm -fr build-autotools
        mkdir build-autotools
        ./autogen.sh
        (
          cd build-autotools
          ${{ github.workspace }}/configure \
            --prefix=${{ github.workspace }}/autotools_prefix \
        )
        if test "${{ runner.os }}" != "macOS" ; then
          curdir="$(pwd)"
          multiarch="$(dpkg-architecture -qDEB_HOST_MULTIARCH)"
          (
            mkdir -p build-autotools/test
            cd build-autotools/test
            ${{ github.workspace }}/test/configure \
              --x-includes=/usr/include \
              --x-libraries="/usr/lib/${multiarch}" \
              --prefix=${{ github.workspace }}/autotools_prefix \
              SDL_CFLAGS="-I${curdir}/include" \
              SDL_LIBS="-L${curdir}/build-autotools/build/.libs -lSDL2" \
              ac_cv_lib_SDL2_ttf_TTF_Init=no \
              ${NULL+}
          )
        fi
    - name: Build (Autotools)
      if: matrix.platform.autotools
      run: |
        set -eu
        parallel="$(getconf _NPROCESSORS_ONLN)"
        make -j"${parallel}" -C build-autotools V=1
        if test "${{ runner.os }}" != "macOS" ; then
          make -j"${parallel}" -C build-autotools/test V=1
        fi
    - name: Run build-time tests (Autotools)
      if: ${{ matrix.platform.autotools && (runner.os != 'macOS') }}
      run: |
        set -eu
        curdir="$(pwd)"
        parallel="$(getconf _NPROCESSORS_ONLN)"
        export SDL_TESTS_QUICK=1
        make -j"${parallel}" -C build-autotools/test check LD_LIBRARY_PATH="${curdir}/build-autotools/build/.libs"
    - name: Install (Autotools)
      if: matrix.platform.autotools
      run: |
        set -eu
        curdir="$(pwd)"
        parallel="$(getconf _NPROCESSORS_ONLN)"
        make -j"${parallel}" -C build-autotools install V=1
        if test "${{ runner.os }}" != "macOS" ; then
          make -j"${parallel}" -C build-autotools/test install V=1
        fi
        ( cd autotools_prefix; find . ) | LC_ALL=C sort -u
        echo "SDL2_DIR=$(pwd)/autotools_prefix" >> $GITHUB_ENV
    - name: Verify CMake configuration files
      run: |
        cmake -S cmake/test -B cmake_config_build -G Ninja \
          -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_PREFIX_PATH=${{ env.SDL2_DIR }}
        cmake --build cmake_config_build --verbose
    - name: Verify sdl2-config
      run: |
        export CC=${{ matrix.platform.cc || 'gcc' }}
        export PATH=${{ env.SDL2_DIR }}/bin:$PATH
        cmake/test/test_sdlconfig.sh
    - name: Verify sdl2.pc
      run: |
        export CC=${{ matrix.platform.cc || 'gcc' }}
        export PKG_CONFIG_PATH=${{ env.SDL2_DIR }}/lib/pkgconfig
        cmake/test/test_pkgconfig.sh
    - name: Distcheck (Autotools)
      if: matrix.platform.autotools
      run: |
        set -eu
        parallel="$(getconf _NPROCESSORS_ONLN)"
        make -j"${parallel}" -C build-autotools dist V=1
        # Similar to Automake `make distcheck`: check that the tarball
        # release is sufficient to do a new build
        mkdir distcheck
        tar -C distcheck -zxf build-autotools/SDL2-*.tar.gz
        ( cd distcheck/SDL2-* && ./configure )
        make -j"${parallel}" -C distcheck/SDL2-*
    - name: Run installed-tests (Autotools)
      if: "runner.os == 'Linux' && matrix.platform.autotools"
      run: |
        set -eu
        parallel="$(getconf _NPROCESSORS_ONLN)"
        sudo make -j"${parallel}" -C build-autotools install
        sudo make -j"${parallel}" -C build-autotools/test install
        export SDL_TESTS_QUICK=1
        # We need to set LD_LIBRARY_PATH because it isn't in the default
        # linker search path. We don't need to set XDG_DATA_DIRS for
        # ginsttest-runner, because /usr/local/share *is* in the default
        # search path for that.
        env --chdir=/ \
            LD_LIBRARY_PATH=/usr/local/lib \
            SDL_AUDIODRIVER=dummy \
            SDL_VIDEODRIVER=dummy \
            ginsttest-runner --tap SDL2