# Heavily inspired (i.e. copy/paste) from https://gist.github.com/NickNaso/0d478f1481686d5bcc868cac06620a60
name: CMake Build Matrix

# Controls when the action will run. Triggers the workflow on push
on: 
  push:
  pull_request:
  release:
    # tags:
    # - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config: 
        - {
            name: "Ubuntu-latest GCC Qt5",
            os: ubuntu-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++",
          }
        - {
            name: "Ubuntu 18.04 GCC Qt5",
            os: ubuntu-18.04,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            name: "Ubuntu 18.04 GCC Qt4",
            os: ubuntu-18.04,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            name: "Ubuntu-latest GCC NoGui",
            os: ubuntu-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            name: "macOS Clang Qt5",
            os: macos-latest,
            build_type: "Debug",
            cc: "clang",
            cxx: "clang++"
          }
        - {
            name: "Windows MinGW Qt5",
            os: windows-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Print env
        run: |
          echo github.event.action: ${{ github.event.action }}
          echo github.event_name: ${{ github.event_name }}

      - name: Install dependencies on Ubuntu GCC Qt5
        if: "startsWith(matrix.config.name, 'Ubuntu-latest GCC Qt5') || startsWith(matrix.config.name, 'Ubuntu 18.04 GCC Qt5')"
        run: |
          sudo apt-get update
          sudo apt-get install build-essential g++ git cmake ninja-build sqlite3 libsqlite3-dev qt5-default qttools5-dev-tools qttools5-dev cscope exuberant-ctags
          uname -a
          lsb_release -a
          gcc -v && g++ -v && cmake --version && ninja --version

      - name: Install dependencies on Ubuntu 18.04 GCC Qt4
        if: startsWith(matrix.config.name, 'Ubuntu 18.04 GCC Qt4')
        run: |
          sudo apt-get update
          sudo apt-get install build-essential g++ git cmake ninja-build sqlite3 libsqlite3-dev qt4-dev-tools cscope exuberant-ctags
          uname -a
          lsb_release -a
          gcc -v && g++ -v && cmake --version && ninja --version

      - name: Install dependencies on Ubuntu-latest GCC NoGui
        if: startsWith(matrix.config.name, 'Ubuntu-latest GCC NoGui')
        run: |
          sudo apt-get update
          sudo apt-get install build-essential g++ git cmake ninja-build sqlite3 libsqlite3-dev cscope exuberant-ctags
          uname -a
          lsb_release -a
          gcc -v && g++ -v && cmake --version && ninja --version

      - name: Install dependencies on macOS Clang Qt5
        if: startsWith(matrix.config.name, 'macOS Clang Qt5')
        run: |
          brew install cmake ninja qt@5 sqlite cscope ctags
          clang -v
          cmake --version
          ninja --version
          brew info qt@5

      - name: Install Qt5 on Windows
        if: startsWith(matrix.config.name, 'Windows MinGW Qt5')
        uses: jurplel/install-qt-action@v2
        with:
          version: '5.15.2'
          host: 'windows'
          target: 'desktop'
          arch: 'win64_mingw81'
          cached: 'false'

      - name: Install sqlite3 on Windows
        shell: cmd
        if: startsWith(matrix.config.name, 'Windows MinGW Qt5')
        run: vcpkg install sqlite3:x64-windows

      - name: Install ninja on Windows
        if: startsWith(matrix.config.name, 'Windows MinGW Qt5')
        uses: seanmiddleditch/gha-setup-ninja@master

      - name: Configure CMake and Build on Ubuntu GCC Qt5
        shell: bash
        if: "contains(matrix.config.name, 'Ubuntu-latest GCC Qt5') || contains(matrix.config.name, 'Ubuntu 18.04 GCC Qt5')"
        run: |
          mkdir -p build
          cd build
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -DBUILD_QT5=ON ..
          ninja
          ninja package
          sudo ninja install
          
      - name: Configure CMake and Build on Ubuntu 18.04 GCC Qt4
        shell: bash
        if: contains(matrix.config.name, 'Ubuntu 18.04 GCC Qt4')
        run: |
          mkdir -p build
          cd build
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -DBUILD_QT5=OFF ..
          ninja
          ninja package
          sudo ninja install

      - name: Configure CMake and Build on Ubuntu-latest GCC NoGui
        shell: bash
        if: contains(matrix.config.name, 'Ubuntu-latest GCC NoGui')
        run: |
          mkdir -p build
          cd build
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -DNO_GUI=ON ..
          ninja
          ninja package
          sudo ninja install

      - name: Configure CMake and Build on macOS Clang Qt5
        shell: bash
        if: contains(matrix.config.name, 'macOS Clang Qt5')
        run: |
          mkdir -p build
          cd build
          export CC=clang
          export CXX=clang++
          cmake -G Ninja -DBUILD_QT5=ON -DCMAKE_PREFIX_PATH=/usr/local/opt/qt@5/lib/cmake ..
          ninja
          sudo ninja install

      - name: Configure CMake and Build on Windows MinGW Qt5
        shell: cmd
        if: contains(matrix.config.name, 'Windows MinGW Qt5')
        run: call "windows-install/win64/buildqt5win64_github.bat"

      - name: Test on Linux, MacOS
        shell: bash
        if: ${{ !contains(matrix.config.name, 'Windows MinGW Qt5') }}
        run: |
          cqmakedb -v
          cscope -cbR
          ctags --fields=+i -nR
          cqmakedb -s cq.db -c cscope.out -t tags -p -d
          cqsearch -s cq.db -p 1 -t CODEQUERY_SW_VERSION -u

      - name: Test on Windows
        shell: cmd
        if: contains(matrix.config.name, 'Windows MinGW Qt5')
        run: call "windows-install/win64/test_github.bat"

      - name: Pack on Windows
        shell: cmd
        if: contains(matrix.config.name, 'Windows MinGW Qt5')
        run: call "windows-install/win64/pack_github.bat"

