# 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 NoGui",
            os: ubuntu-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            name: "Ubuntu-latest GCC Qt6",
            os: ubuntu-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++",
          }
        - {
            name: "Ubuntu-latest GCC Qt5",
            os: ubuntu-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++",
          }
        - {
            name: "Ubuntu 22.04 GCC Qt5",
            os: ubuntu-22.04,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            name: "macOS Clang Qt6",
            os: macos-latest,
            build_type: "Debug",
            cc: "clang",
            cxx: "clang++"
          }
        - {
            name: "macOS Clang Qt5",
            os: macos-latest,
            build_type: "Debug",
            cc: "clang",
            cxx: "clang++"
          }
        - {
            name: "Windows MinGW Qt6",
            os: windows-latest,
            build_type: "Debug",
            cc: "gcc",
            cxx: "g++"
          }
        - {
            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@v4

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

      - name: Install common dependencies on all Ubuntu
        if: "startsWith(matrix.config.name, 'Ubuntu')"
        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 Ubuntu-latest GCC Qt6
        if: "startsWith(matrix.config.name, 'Ubuntu-latest GCC Qt6')"
        run: |
          sudo apt-get install libglx-dev libgl1-mesa-dev libvulkan-dev libxkbcommon-dev
          sudo apt-get install qt6-base-dev qt6-base-dev-tools qt6-tools-dev qt6-tools-dev-tools
          sudo apt-get install libqt6core5compat6-dev qt6-l10n-tools qt6-wayland

      - name: Install dependencies on Ubuntu-latest GCC Qt5
        if: "startsWith(matrix.config.name, 'Ubuntu-latest GCC Qt5')"
        run: |
          sudo apt-get install qtcreator qtbase5-dev qt5-qmake qttools5-dev-tools qttools5-dev

      - name: Install dependencies on Ubuntu 22.04 GCC Qt5
        if: "startsWith(matrix.config.name, 'Ubuntu 22.04 GCC Qt5')"
        run: |
          sudo apt-get install qtcreator qtbase5-dev qt5-qmake qttools5-dev-tools qttools5-dev

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

      - 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 Qt6 on Windows
        if: startsWith(matrix.config.name, 'Windows MinGW Qt6')
        uses: jurplel/install-qt-action@v4
        with:
          version: '6.9.*'
          host: 'windows'
          target: 'desktop'
          arch: 'win64_mingw'
          modules: 'qt5compat'
          cache: 'false'

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

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

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

      - name: Configure CMake and Build on Ubuntu GCC Qt6
        shell: bash
        if: "contains(matrix.config.name, 'Ubuntu-latest GCC Qt6')"
        run: |
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -S . -B build
          cmake --build build
          sudo cmake --install build

      - 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 22.04 GCC Qt5')"
        run: |
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -DBUILD_QT5=ON -S . -B build
          cmake --build build
          sudo cmake --install build
          
      - name: Configure CMake and Build on Ubuntu-latest GCC NoGui
        shell: bash
        if: contains(matrix.config.name, 'Ubuntu-latest GCC NoGui')
        run: |
          export CC=gcc
          export CXX=g++
          cmake -G Ninja -DNO_GUI=ON -S . -B build
          cmake --build build
          sudo cmake --install build

      - name: Configure CMake and Build on macOS Clang Qt6
        shell: bash
        if: contains(matrix.config.name, 'macOS Clang Qt6')
        run: |
          export CC=clang
          export CXX=clang++
          cmake -G Ninja -DCMAKE_PREFIX_PATH=/usr/local/opt/qt@6/lib/cmake -S . -B build
          cmake --build build
          sudo cmake --install build

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

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

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

      - name: Test on Linux, MacOS
        shell: bash
        if: ${{ !contains(matrix.config.name, 'Windows MinGW') }}
        run: |
          find . -name "*.h" > cscope.files
          find . -name "*.c" >> cscope.files
          find . -name "*.cpp" >> cscope.files
          find . -name "*.cxx" >> cscope.files
          cqmakedb -v
          cscope -cb
          ctags --fields=+i -n -L cscope.files
          cqmakedb -s cq.db -c cscope.out -t tags -p -d
          cqsearch -s cq.db -p 1 -t CODEQUERY_SW_VERSION -u
          cscope -b -f cscope1.out
          cqmakedb -s cq1.db -c cscope1.out -t tags -p -d
          cqsearch -s cq1.db -p 1 -t CODEQUERY_SW_VERSION -u

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

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

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

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

