name: CI - Ubuntu
on: [pull_request, push]

jobs:
  ci:
    name: Build
    runs-on: ubuntu-20.04

    strategy:
      matrix:
        libt_version: ["v1.2.12"]
        qbt_gui: ["GUI=ON", "GUI=OFF"]
      fail-fast: false

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Setup ccache
        uses: hendrikmuhs/ccache-action@v1
        with:
          key: ${{ runner.os }}

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install \
            build-essential cmake git ninja-build pkg-config \
            libssl-dev libgeoip-dev zlib1g-dev \
            libboost-dev libboost-chrono-dev libboost-random-dev libboost-system-dev
        # sudo apt install libqt5svg5-dev qtbase5-dev qttools5-dev # the Qt version in the standard repositories is too old...

      # this will be installed under /opt/qt515. CMake will still find it automatically without additional hints
      # to speed up the process, only the required components are installed rather than the full qt515-meta-full metapackage
      - name: Install Qt
        run: |
          sudo add-apt-repository ppa:beineri/opt-qt-5.15.2-focal
          sudo apt install \
            qt515base qt515svg qt515tools

      - name: Install libtorrent
        run: |
          git clone --branch ${{ matrix.libt_version }} --depth 1 https://github.com/arvidn/libtorrent.git
          cd libtorrent
          git submodule update --init --recursive
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -Ddeprecated-functions=OFF \
            --graphviz=cmake-build-dir/target_graph.dot
          cmake --build build
          sudo cmake --install build

      - name: Build qBittorrent
        run: |
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -D${{ matrix.qbt_gui }} \
            -DVERBOSE_CONFIGURE=ON \
            --graphviz=build/target_graph.dot
          cmake --build build

      - name: Install qBittorrent
        run: sudo cmake --install build

      - name: Upload build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: qBittorrent-CI_ubuntu-20.04-x64_${{ matrix.qbt_gui }}_libtorrent-${{ matrix.libt_version }}
          path: |
            build/compile_commands.json
            build/install_manifest.txt
            build/target_graph.dot
            build/qbittorrent
            build/qbittorrent-nox
            libtorrent/cmake-build-dir/compile_commands.json
            libtorrent/cmake-build-dir/target_graph.dot
