name: Reusable build workflow

permissions: read-all

on:
  workflow_call:
    inputs:
      os_version:
        required: true
        type: string
      qt_version:
        required: true
        type: string
      shared:
        required: false
        default: "ON"
        type: string
      zlib_const:
        required: false
        default: "OFF"
        type: string
      qtextcodec:
        required: false
        default: "ON"
        type: string
      runs_on:
        required: true
        type: string
      run_tests:
        required: false
        default: true
        type: boolean
      cross_extract:
        required: false
        default: false
        type: boolean
    secrets:
        GH_TOKEN:
            required: false

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  # All aqt options: https://ddalcino.github.io/aqt-list-server/
  BUILD_TYPE: Release

jobs:
  build:
    runs-on:  ${{ inputs.runs_on }}
    name: Build-${{ inputs.runs_on }}-Qt-${{ inputs.qt_version }}-shared-${{ inputs.shared }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install Qt6
        if: "startsWith(inputs.qt_version, '6.')"
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ inputs.qt_version }}
          cache: 'true'
          cache-key-prefix: ${{ runner.os }}-Qt-Cache-${{ inputs.qt_version }}
          dir: ${{ github.workspace }}/Qt
          modules: 'qt5compat'

      - name: Install Qt5
        if: "startsWith(inputs.qt_version, '5.')"
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ inputs.qt_version }}
          cache: 'true'
          cache-key-prefix: ${{ runner.os }}-Qt-Cache-${{ inputs.qt_version }}
          dir: ${{ github.workspace }}/Qt

      - name: Install libraries (Linux Only)
        if: contains(inputs.runs_on, 'ubuntu')
        run: |
          sudo apt-get update &&
          sudo apt-get install -y --no-install-recommends \
          zlib1g-dev libbz2-dev

      - name: Configure CMake
        run: cmake -DCMAKE_BUILD_TYPE="${{env.BUILD_TYPE}}" -DBUILD_SHARED_LIBS=${{ inputs.shared }} -DQUAZIP_ENABLE_TESTS=ON -DZLIB_CONST=${{ inputs.zlib_const }} -DQUAZIP_ENABLE_QTEXTCODEC=${{ inputs.qtextcodec }} -B "${{github.workspace}}/build"

      - name: Build
        run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

      - name: Run tests
        if: inputs.run_tests && !inputs.cross_extract
        working-directory: ${{github.workspace}}/build
        env:
          TEST_CR_COMPRESS: "true"
          #TEST_ZIP_UNZIP_LARGE: true
        run: ctest --verbose

      - name: Upload cp.zip to GitHub Actions Storage
        if: inputs.run_tests && !inputs.cross_extract
        uses: actions/upload-artifact@v7
        with:
          retention-days: 1
          overwrite: true
          name: "${{ inputs.runs_on }}_qt${{ inputs.qt_version }}_shared${{ inputs.shared }}_cp"
          path: build/quazip/cp.zip

      - name: Download bundle containing cp.zip
        if: inputs.cross_extract
        uses: actions/download-artifact@v8
        with:
          path: build/quazip
          merge-multiple: false

      - name: Run extract tests
        if: inputs.cross_extract
        working-directory: ${{github.workspace}}/build
        env:
          TEST_CR_DECOMPRESS: "true"
        run: ctest --verbose


