name: CI Tests

on: [pull_request,push]

jobs:
  Ubuntu:

    runs-on: ubuntu-${{ matrix.ubuntu_ver }}

    strategy:
      fail-fast: false
      matrix:
        ubuntu_ver: [18.04, 20.04]

    name: ubuntu-${{ matrix.ubuntu_ver }}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Install prerequisties
        run: |
          wget http://download.opensuse.org/repositories/home:hpcoder1/xUbuntu_${{ matrix.ubuntu_ver }}/Release.key
          sudo apt-key add - < Release.key
          sudo add-apt-repository -y \
            'deb http://download.opensuse.org/repositories/home:/hpcoder1/xUbuntu_${{ matrix.ubuntu_ver }}/ /'
          sudo apt install -y --no-install-recommends \
            qtbase5-dev \
            qttools5-dev \
            libqt5svg5-dev \
            libqt5opengl5-dev \
            libmuparser-dev \
            libgsl-dev \
            libqwt5-qt5-dev \
            pyqt5-dev \
            python3-pyqt5 \
            libgtest-dev \
            python3-numpy \
            xvfb

      - name: Build and Install GTest
        if: matrix.ubuntu_ver == 18.04
        run: |
          mkdir gtest && cd gtest
          cmake /usr/src/googletest
          make -j$(nproc)
          sudo make install

      - name: Configuring
        run: |
          mkdir build && cd build
          cmake .. -DSEARCH_FOR_UPDATES=OFF -DDOWNLOAD_LINKS=OFF -DORIGIN_IMPORT=ON -DSCRIPTING_PYTHON=ON \
            -DBUILD_TESTS=ON

      - name: Building
        run: |
          xvfb-run make -j$(nproc) -C build

      - name: Testing
        run: |
          cd build
          xvfb-run ctest --output-on-failure

  MSVC:

    runs-on: windows-latest

    strategy:
      fail-fast: false
      matrix:
        include:
          - qt_ver: 5.12.10
            python: OFF
          - qt_ver: 5.15.2
            python: ON

    name: MSVC_Qt_${{ matrix.qt_ver }}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Cache Qt
        id: cache-qt
        uses: actions/cache@v1
        with:
          path: ../Qt
          key: ${{ runner.os }}-${{ matrix.qt_ver }}-QtCache

      - name: Install Qt
        uses: jurplel/install-qt-action@v2
        with:
          version: ${{ matrix.qt_ver }}
          cached: ${{ steps.cache-qt.outputs.cache-hit }}

      - uses: lukka/run-vcpkg@v6
        with:
          vcpkgGitCommitId: 50ea8c0ab7aca3bb9245bba7fc877ad2f2a4464c
          setupOnly: true
      - name: Install VCPKG packages
        run: |
          Add-Content ${{ env.VCPKG_ROOT }}\triplets\x64-windows.cmake "set(VCPKG_BUILD_TYPE release)" 
          ${{ env.VCPKG_ROOT }}\vcpkg install zlib gsl muparser opengl gl2ps gtest --triplet x64-windows

      - name: Install Python packages
        if: ${{ matrix.qt_ver == '5.15.2' }}
        run: |
         pip install sip==5.5.0 pyqt5==5.15.2  PyQt5-sip==12.8.1 numpy

      - name: Configuring
        run: |
          mkdir build; cd build
          cmake .. -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}\scripts\buildsystems\vcpkg.cmake `
            -DORIGIN_IMPORT=ON -DSCRIPTING_PYTHON=${{ matrix.python }} -DBUILD_TESTS=ON

      - name: Building
        run: |
          cmake --build build --config Release -- -maxCpuCount

      - name: Testing
        run: |
          cd build
          ctest -C Release --output-on-failure

  MinGW-w64:

    runs-on: windows-latest

    name: MinGW-w64

    defaults:
      run:
        shell: msys2 {0}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Install MinGW-w64 packages
        uses: msys2/setup-msys2@v2
        with:
          update: true
          install: >-
            make
            mingw-w64-x86_64-cmake
            mingw-w64-x86_64-gcc
            mingw-w64-x86_64-qt5
            mingw-w64-x86_64-muparser
            mingw-w64-x86_64-gsl
            mingw-w64-x86_64-gl2ps
            mingw-w64-x86_64-gtest
            mingw-w64-x86_64-sip4
            mingw-w64-x86_64-python-pyqt5
            mingw-w64-x86_64-python-numpy

      - name: Configuring
        run: |
          mkdir build && cd build
          cmake .. -G"MSYS Makefiles" -DORIGIN_IMPORT=ON -DSCRIPTING_PYTHON=ON -DBUILD_TESTS=ON

      - name: Building
        run: |
          make -C build -j$(nproc)

      - name: Testing
        run: |
          cd build
          ctest --output-on-failure

  macos:

    runs-on: macos-latest

    name: macos

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Cache Qt
        id: cache-qt
        uses: actions/cache@v1
        with:
          path: ../Qt
          key: ${{ runner.os }}-QtCache

      - name: Install Qt
        uses: jurplel/install-qt-action@v2
        with:
          cached: ${{ steps.cache-qt.outputs.cache-hit }}

      - name: Install prerequisties
        run: |
         brew install muparser gsl gl2ps googletest
         brew tap-new $USER/local-sip
         brew extract --version=4.19.25 sip $USER/local-sip
         brew install sip@4.19.25
         pip install pyqt5 numpy

      - name: Configuring
        run: |
          mkdir build && cd build
          cmake .. -DORIGIN_IMPORT=ON -DSCRIPTING_PYTHON=ON -DPython3_ROOT_DIR=$pythonLocation \
            -DBUILD_TESTS=ON

      - name: Building
        run: |
          make -j$(sysctl -n hw.logicalcpu) -C build

      - name: Testing
        run: |
          cd build
          ctest --output-on-failure
