# Copyright (c) 2023 Sebastian Pipping <sebastian@pipping.org>
# Licensed under the GNU General Public License Version 2.

name: Build on Linux

on:
  pull_request:
  push:
  schedule:
    - cron: '0 3 * * 5'  # Every Friday at 3am

jobs:
  linux:
    name: Build (${{ matrix.cc }})
    runs-on: ${{ matrix.runs-on }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - cc: gcc-9
            cxx: g++-9
            clang_major_version: null
            clang_repo_suffix: null
            runs-on: ubuntu-22.04
          - cc: gcc-13
            cxx: g++-13
            clang_major_version: null
            clang_repo_suffix: null
            runs-on: ubuntu-22.04
          - cc: clang-17
            cxx: clang++-17
            clang_major_version: 17
            clang_repo_suffix: -17
            runs-on: ubuntu-22.04
    steps:
      - name: Add Clang/LLVM repositories
        if: "${{ contains(matrix.cxx, 'clang') }}"
        run: |-
          set -x
          source /etc/os-release
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
          sudo add-apt-repository "deb http://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}${{ matrix.clang_repo_suffix }} main"

      - name: Install build dependencies
        run: |-
          sudo apt-get update
          sudo apt-get install --yes --no-install-recommends \
            cmake \
            extra-cmake-modules \
            libkf5config-dev \
            libkf5i18n-dev \
            libkf5iconthemes-dev \
            libkf5windowsystem-dev \
            libkf5xmlgui-dev \
            nasm \
            qtbase5-dev

      - name: Install build dependency Clang ${{ matrix.clang_major_version }}
        if: "${{ contains(matrix.cxx, 'clang') }}"
        run: |-
          sudo apt-get install --yes --no-install-recommends -V \
              clang-${{ matrix.clang_major_version }}

      - name: Checkout Git branch
        uses: actions/checkout@v4

      - name: 'Configure with CMake'
        run: |-
          cmake_args=(
            -DCMAKE_C_COMPILER="${{ matrix.cc }}"
            -DCMAKE_CXX_COMPILER="${{ matrix.cxx }}"
            -S ./
            -B build/
          )
          set -x
          cmake "${cmake_args[@]}"

      - name: 'Build KDbg'
        run: |-
          set -x
          make -C build -j$(nproc) VERBOSE=1

      - name: 'Install KDbg'
        run: |-
          set -x -o pipefail
          make -C build install DESTDIR="${PWD}"/ROOT/
          find ROOT/ | sort | xargs ls -ld

      - name: 'Build test programs'
        run: |-
          set -x
          make -C kdbg/testprogs/ -j$(nproc) all clean
