name: CI - macOS

on: [pull_request, push]

permissions:
  actions: write

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: ${{ github.head_ref != '' }}

jobs:
  ci:
    name: Build
    runs-on: macos-latest

    strategy:
      fail-fast: false
      matrix:
        libt_version: ["2.0.8", "1.2.18"]
        qbt_gui: ["GUI=ON", "GUI=OFF"]
        qt_version: ["5.15.2", "6.4.0"]
        exclude:
          - libt_version: "1.2.18"
            qt_version: "6.4.0"

    env:
      boost_path: "${{ github.workspace }}/../boost"
      openssl_root: /usr/local/opt/openssl@1.1

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

      - name: Install dependencies
        run: |
          export \
            HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 \
            HOMEBREW_NO_INSTALL_CLEANUP=1
          brew update > /dev/null
          brew install \
            cmake ninja \
            openssl@1.1 zlib

      - name: Setup ccache
        uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false

      - name: Install boost
        run: |
          curl \
            -L \
            -o "${{ runner.temp }}/boost.tar.bz2" \
            "https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.bz2"
          tar -xf "${{ runner.temp }}/boost.tar.bz2" -C "${{ github.workspace }}/.."
          mv "${{ github.workspace }}/.."/boost_* "${{ env.boost_path }}"

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ matrix.qt_version }}
          archives: qtbase qtdeclarative qtsvg qttools
          # Not sure why Qt made a hard dependency on qtdeclarative, try removing it when Qt > 6.4.0

      - name: Install libtorrent
        run: |
          git clone \
            --branch v${{ matrix.libt_version }} \
            --depth 1 \
            --recurse-submodules \
            https://github.com/arvidn/libtorrent.git
          cd libtorrent
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_CXX_STANDARD=17 \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DBOOST_ROOT="${{ env.boost_path }}" \
            -Ddeprecated-functions=OFF \
            -DOPENSSL_ROOT_DIR="${{ env.openssl_root }}"
          cmake --build build
          sudo cmake --install build

      - name: Build qBittorrent (Qt5)
        if: ${{ startsWith(matrix.qt_version, 5) }}
        run: |
          CXXFLAGS="$CXXFLAGS -Werror -Wno-error=deprecated-declarations" \
          LDFLAGS="$LDFLAGS -gz" \
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DBOOST_ROOT="${{ env.boost_path }}" \
            -DOPENSSL_ROOT_DIR="${{ env.openssl_root }}" \
            -DTESTING=ON \
            -DVERBOSE_CONFIGURE=ON \
            -D${{ matrix.qbt_gui }}
          cmake --build build --target qbt_update_translations
          cmake --build build
          cmake --build build --target check

      - name: Build qBittorrent (Qt6)
        if: ${{ startsWith(matrix.qt_version, 6) }}
        run: |
          CXXFLAGS="$CXXFLAGS -Wno-gnu-zero-variadic-macro-arguments -Werror -Wno-error=deprecated-declarations" \
          LDFLAGS="$LDFLAGS -gz" \
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DBOOST_ROOT="${{ env.boost_path }}" \
            -DOPENSSL_ROOT_DIR="${{ env.openssl_root }}" \
            -DQT6=ON \
            -DTESTING=ON \
            -DVERBOSE_CONFIGURE=ON \
            -D${{ matrix.qbt_gui }}
          cmake --build build --target qbt_update_translations
          cmake --build build
          cmake --build build --target check

      - name: Prepare build artifacts
        run: |
          mkdir upload
          mkdir upload/cmake
          cp build/compile_commands.json upload/cmake
          mkdir upload/cmake/libtorrent
          cp libtorrent/build/compile_commands.json upload/cmake/libtorrent

      - name: Upload build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: build-info_macOS_${{ matrix.qbt_gui }}_libtorrent-${{ matrix.libt_version }}_Qt-${{ matrix.qt_version }}
          path: upload
