name: Build

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        compiler: [msvc, gcc, clang]
        qt_version: [qt5, qt6]
        exclude:
          - os: ubuntu-latest
            compiler: msvc
          - os: macos-latest
            compiler: gcc
          - os: macos-latest
            compiler: msvc
          - os: windows-latest
            compiler: clang

    runs-on: ${{ matrix.os }}

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

      - name: Setup CMake and Ninja
        uses: lukka/get-cmake@latest

      - if: matrix.compiler == 'msvc'
        uses: ilammy/msvc-dev-cmd@v1

      - name: Install Dependencies (Windows only)
        if: runner.os == 'Windows'
        uses: crazy-max/ghaction-chocolatey@v3
        with:
          args: install pkgconfiglite

      - name: Install Qt (Mac/Linux only)
        if: runner.os != 'Windows'
        run: |
          if [ '${{ runner.os }}' == 'macOS' ]; then
            if [ '${{ matrix.qt_version }}' == 'qt6' ]; then
              brew install qt6 pkg-config
              brew link qt6 --force
              echo "$(brew --prefix qt6)/bin" >> $GITHUB_PATH
            else
              brew install qt5 pkg-config
              brew link qt5 --force
              echo "$(brew --prefix qt5)/bin" >> $GITHUB_PATH
            fi
          elif [ '${{ runner.os }}' == 'Linux' ]; then
            sudo apt-get update
            if [ '${{ matrix.qt_version }}' == 'qt6' ]; then
              sudo apt-get install -y qt6-base-dev qtchooser qmake6 qt6-base-dev-tools qt6-tools-dev pkg-config libsecret-1-dev
            else
              sudo apt-get install -y qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools qttools5-dev pkg-config libsecret-1-dev
            fi
          fi

      - name: Set AQT Version and Arch (Windows only)
        if: runner.os == 'Windows'
        id: set_qt_arch
        shell: pwsh
        run: |
          $qtVer = "${{ matrix.qt_version == 'qt5' && '5.15.2' || '6.5.3' }}"
          $qtArch = "${{ matrix.compiler == 'msvc' && 'msvc2019_64' ||
               matrix.qt_version == 'qt5' && matrix.compiler == 'gcc' && 'mingw81' ||
               matrix.qt_version == 'qt6' && matrix.compiler == 'gcc' && 'mingw' }}"
          echo "QT_VER=$qtVer" >> $env:GITHUB_OUTPUT
          echo "QT_ARCH=$qtArch" >> $env:GITHUB_OUTPUT

      - name: Install Qt (Windows only)
        if: runner.os == 'Windows'
        uses: jurplel/install-qt-action@v4
        with:
          version: ${{ steps.set_qt_arch.outputs.QT_VER }}
          arch: win64_${{ steps.set_qt_arch.outputs.QT_ARCH }}
          dir: 'C:\'
          cache: true
          tools: 'tools_mingw1310'

      - name: Setup Build Directory
        run: |
          mkdir -p ${{ github.workspace }}/work/build/${{ github.event.repository.name }}

      - name: Run CMake (Windows only)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          if ( '${{ matrix.compiler }}' -eq 'msvc' ) {
            $generator = "NMake Makefiles"
          } else {
            $generator = "MinGW Makefiles"
          }
          if ( '${{ matrix.compiler }}' -eq 'gcc' ) {
            echo "C:/Qt/Tools/mingw1310_64/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
            $env:PATH = "C:\Qt\Tools\mingw1310_64\bin;$env:PATH"
          }
          cd "${{ github.workspace }}/work/build/${{ github.event.repository.name }}"
          cmake -G $generator "${{ github.workspace }}" `
            -DCMAKE_BUILD_TYPE=Release `
            -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/install" `
            -DCMAKE_PREFIX_PATH=$env:QT_ROOT_DIR `
            ${{ matrix.qt_version == 'qt6' && '-DBUILD_WITH_QT6=true' || '' }}

      - name: Run CMake (Mac/Linux Only)
        if: runner.os != 'Windows'
        run: |
          cd ${{ github.workspace }}/work/build/${{ github.event.repository.name }}
          cmake -G Ninja ${{ github.workspace }} \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/install" \
            ${{ matrix.qt_version == 'qt6' && '-DBUILD_WITH_QT6=true' || '' }}

      - name: Build and Install
        run: |
          cd ${{ github.workspace }}/work/build/${{ github.event.repository.name }}
          cmake --build .
          cmake --install . --prefix "${{ github.workspace }}/install"

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: build-artifact ${{ matrix.os }} ${{ matrix.compiler }} ${{ matrix.qt_version }}
          path: ${{ github.workspace }}/work/build/${{ github.event.repository.name }}

  build-android:
    runs-on: ubuntu-latest

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

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17

      - name: Setup CMake and Ninja
        uses: lukka/get-cmake@latest

      - name: Install Qt for Android (arm64-v8a)
        uses: jurplel/install-qt-action@v4
        with:
          version: '6.8.3'
          target: android
          arch: android_arm64_v8a
          cache: true
          extra: --autodesktop

      - name: Build and install qtkeychain (Android)
        run: |
          cmake -S ${{ github.workspace }} -B build/qtkeychain \
            -DCMAKE_TOOLCHAIN_FILE=$QT_ROOT_DIR/lib/cmake/Qt6/qt.toolchain.cmake \
            -DCMAKE_BUILD_TYPE=Release \
            -DBUILD_WITH_QT6=ON \
            -DBUILD_TRANSLATIONS=OFF
          cmake --build build/qtkeychain
          cmake --install build/qtkeychain --prefix ${{ github.workspace }}/install

      - name: Build TestAppExample (Android)
        run: |
          cmake -S ${{ github.workspace }}/TestAppExample -B build/testapp \
            -DCMAKE_TOOLCHAIN_FILE=$QT_ROOT_DIR/lib/cmake/Qt6/qt.toolchain.cmake \
            -DCMAKE_BUILD_TYPE=Release \
            -DQt6Keychain_DIR=${{ github.workspace }}/install/lib/cmake/Qt6Keychain \
            -DBUILD_WITH_QT6=ON
          cmake --build build/testapp

  build-ios:
    runs-on: macos-latest

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

      - name: Setup CMake and Ninja
        uses: lukka/get-cmake@latest

      - name: Install Qt for iOS
        uses: jurplel/install-qt-action@v4
        with:
          version: '6.8.3'
          target: ios
          cache: true
          extra: --autodesktop

      - name: Build and install qtkeychain (iOS)
        run: |
          cmake -S ${{ github.workspace }} -B build/qtkeychain \
            -G Xcode \
            -DCMAKE_TOOLCHAIN_FILE=$QT_ROOT_DIR/lib/cmake/Qt6/qt.toolchain.cmake \
            -DBUILD_WITH_QT6=ON \
            -DBUILD_TRANSLATIONS=OFF \
            -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO \
            -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
          cmake --build build/qtkeychain --config Release
          cmake --install build/qtkeychain --prefix ${{ github.workspace }}/install --config Release

      - name: Build TestAppExample (iOS)
        run: |
          cmake -S ${{ github.workspace }}/TestAppExample -B build/testapp \
            -G Xcode \
            -DCMAKE_TOOLCHAIN_FILE=$QT_ROOT_DIR/lib/cmake/Qt6/qt.toolchain.cmake \
            -DQt6Keychain_DIR=${{ github.workspace }}/install/lib/cmake/Qt6Keychain \
            -DBUILD_WITH_QT6=ON \
            -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO \
            -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
          cmake --build build/testapp --config Release
