name: Build release packages

on:
  push:
    branches-ignore:
      - 'gerrit/*'
    tags:
      - 'v*'

jobs:
  build-linux:
    name: ${{ matrix.config.name }}
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: 'Build on Linux (gcc)',
            options: 'qbs.installPrefix:""
                      modules.cpp.compilerWrapper:ccache
                      modules.qbsbuildconfig.enableAddressSanitizer:false
                      project.withTests:false
                      modules.qbsbuildconfig.enableUnitTests:false
                      products.qbs_archive.targetName:qbs-linux-${{ github.run_id }}',
            script: './scripts/build-qbs-with-qbs.sh',
            cacheid: 'gcc-release',
          }
    env:
      BUILD_OPTIONS: ${{ matrix.config.options }}
      WITH_TESTS: 0
    steps:
    - uses: actions/checkout@v1
    - name: Create .ccache dir
      run: mkdir -p ~/.ccache
    - name: prepare timestamp
      id: get-timestamp
      run: echo "timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_OUTPUT
      shell: bash
    - name: ccache cache files
      uses: actions/cache@v4
      with:
        path: ~/.ccache
        key: ${{ runner.os }}-${{ matrix.config.cacheid }}-ccache-${{ steps.get-timestamp.outputs.timestamp }}
        restore-keys: ${{ runner.os }}-${{ matrix.config.cacheid }}-ccache
    - name: Pull the Docker Image
      run: docker compose pull noble-qt6
    - name: Print ccache stats
      run: docker compose run noble-qt6 ccache -s
    - name: Build Qbs
      run: docker compose run noble-qt6 ${{ matrix.config.script }}
    - name: Print ccache stats
      run: docker compose run noble-qt6 ccache -s
    - name: Get archive name
      id: get-archive-name
      run: echo "archive-name=$(git describe)" >> $GITHUB_OUTPUT
    - name: Upload artifacts
      uses: 'actions/upload-artifact@v4'
      with:
        name: qbs-linux-${{ github.run_id }}.tar.gz
        path: release/qbs-linux-${{ github.run_id }}.tar.gz

  build-macos:
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.runner }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: 'Build on macOS (arm64)',
            runner: 'macos-14',
            profile: 'xcode_15_1-macosx-arm64',
            toolchain: 'clang_64',
            options: 'qbs.installPrefix:""
                      modules.cpp.compilerWrapper:ccache
                      project.withTests:false
                      modules.qbsbuildconfig.enableUnitTests:false
                      modules.qbsbuildconfig.enableAddressSanitizer:false
                      products.qbs_archive.targetName:qbs-macos-arm64-${{ github.run_id }}',
            suffix: 'macos-arm64',
          }
        - {
            name: 'Build on macOS (x86_64)',
            runner: 'macos-14',
            profile: 'xcode_15_1-macosx-x86_64',
            toolchain: 'clang_64',
            options: 'qbs.installPrefix:""
                      modules.cpp.compilerWrapper:ccache
                      project.withTests:false
                      modules.qbsbuildconfig.enableUnitTests:false
                      modules.qbsbuildconfig.enableAddressSanitizer:false
                      products.qbs_archive.targetName:qbs-macos-x86_64-${{ github.run_id }}',
            suffix: 'macos-x86_64',
          }
    env:
      BUILD_OPTIONS: ${{ matrix.config.options }}
      WITH_TESTS: 0
    steps:
    - uses: actions/checkout@v1
    - name: Create .ccache dir
      run: mkdir -p ~/.ccache
    - name: prepare timestamp
      id: get-timestamp
      run: echo "timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_OUTPUT
      shell: bash
    - name: ccache cache files
      uses: actions/cache@v4
      with:
        path: ~/.ccache
        key: ${{ runner.os }}-release-ccache-${{ steps.get-timestamp.outputs.timestamp }}
        restore-keys: ${{ runner.os }}-release-ccache
    - name: Install required packages
      run: brew install ccache
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.13'
    - name: Install Python dependencies
      run: |
        python -m pip install --upgrade pip
        pip install beautifulsoup4 lxml
    - name: Install Qt
      uses: ./.github/actions/download-qt
      with:
        toolchain: ${{ matrix.config.toolchain }}
    - name: Install Qt Creator
      uses: ./.github/actions/download-qtc
    - name: Setup Qbs
      run: |
        qbs setup-toolchains --detect
        qbs setup-qt --detect
        qbs config profiles.qt.baseProfile ${{ matrix.config.profile }}
        qbs config defaultProfile qt
        qbs config --list
    - name: Print ccache stats
      run: ccache -s
    - name: Build Qbs
      run: scripts/build-qbs-with-qbs.sh
    - name: Print ccache stats
      run: ccache -s
    - name: Upload artifacts
      uses: 'actions/upload-artifact@v4'
      with:
        name: qbs-${{ matrix.config.suffix }}-${{ github.run_id }}.tar.gz
        path: release/qbs-${{ matrix.config.suffix }}-${{ github.run_id }}.tar.gz

  build-windows-with-docker:
    name: Build on Windows (Docker)
    runs-on: windows-2022
    timeout-minutes: 60
    env:
      WITH_TESTS: 0
      QT_ASSUME_STDERR_HAS_CONSOLE: 1
    steps:
    - uses: actions/checkout@v1
    - name: Create .ccache dir
      run: mkdir -p ~/.ccache
      shell: bash
    - name: Pull the Windows Image
      run: docker compose pull windows
    - name: Build Qbs
      run: >
        docker compose run --rm windows qbs build
        -p dist
        qbs.buildVariant:release
        project.withTests:false
        modules.qbsbuildconfig.enableBundledQt:true
        modules.qbsbuildconfig.enableUnitTests:false
        modules.cpp.treatWarningsAsErrors:true
        project.withDocumentation:true
        config:release profile:qt64
    - name: Get archive name
      id: get-archive-name
      run: echo "archive-name=$(git describe)" >> $GITHUB_OUTPUT
    - name: Upload artifacts
      uses: 'actions/upload-artifact@v4'
      with:
        name: qbs-windows-${{ github.run_id }}
        path: |
          release/qbs.*.nupkg
          release/qbs-windows-*.zip

  create-archives:
    name: Create Archives
    runs-on: ubuntu-latest
    needs: [build-macos, build-linux, build-windows-with-docker]
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Get version name
      id: get-version-name
      run: echo "version-name=$(cat VERSION)" >> $GITHUB_OUTPUT
    - name: Create directories
      run: |
        mkdir release
        mkdir tmp
    - name: Copy sources
      run: rsync -av --exclude='.git/' --exclude='tmp/' . ./tmp/qbs-src-${{ steps.get-version-name.outputs.version-name }}
    - name: Zip Archive
      run: |
        cd tmp/
        zip -r ../release/qbs-src-${{ steps.get-version-name.outputs.version-name }}.zip qbs-src-${{ steps.get-version-name.outputs.version-name }}
    - name: Tarball Archive
      run: |
        cd tmp/
        tar czf ../release/qbs-src-${{ steps.get-version-name.outputs.version-name }}.tar.gz qbs-src-${{ steps.get-version-name.outputs.version-name }}
    - name: Download Linux artifacts
      uses: actions/download-artifact@v4
      with:
        name: qbs-linux-${{ github.run_id }}.tar.gz
        path: ./tmp
    - name: Repack Linux artifact
      run: |
        mkdir -p tmp/qbs-linux-x86_64-${{ steps.get-version-name.outputs.version-name }}
        tar xzf ./tmp/qbs-linux-${{ github.run_id }}.tar.gz -C tmp/qbs-linux-x86_64-${{ steps.get-version-name.outputs.version-name }}
        cd tmp/
        tar czf ../release/qbs-linux-x86_64-${{ steps.get-version-name.outputs.version-name }}.tar.gz qbs-linux-x86_64-${{ steps.get-version-name.outputs.version-name }}
    - name: Download Windows artifacts
      uses: actions/download-artifact@v4
      with:
        name: qbs-windows-${{ github.run_id }}
        path: ./tmp/release
    - name: Copy Windows artifacts
      run: |
        cp ./tmp/release/qbs.*.nupkg ./release
        cp ./tmp/release/qbs-windows-x86_64-*.zip ./release
    - name: Download macOS arm64 artifacts
      uses: actions/download-artifact@v4
      with:
        name: qbs-macos-arm64-${{ github.run_id }}.tar.gz
        path: ./tmp/
    - name: Repack Macos arm64 artifact
      run: |
        mkdir -p tmp/qbs-macos-arm64-${{ steps.get-version-name.outputs.version-name }}
        tar xzf ./tmp/qbs-macos-arm64-${{ github.run_id }}.tar.gz -C tmp/qbs-macos-arm64-${{ steps.get-version-name.outputs.version-name }}
        cd tmp/
        tar czf ../release/qbs-macos-arm64-${{ steps.get-version-name.outputs.version-name }}.tar.gz qbs-macos-arm64-${{ steps.get-version-name.outputs.version-name }}
    - name: Download macOS x86_64 artifacts
      uses: actions/download-artifact@v4
      with:
        name: qbs-macos-x86_64-${{ github.run_id }}.tar.gz
        path: ./tmp
    - name: Repack Macos x86_64 artifact
      run: |
        mkdir -p tmp/qbs-macos-x86_64-${{ steps.get-version-name.outputs.version-name }}
        tar xzf ./tmp/qbs-macos-x86_64-${{ github.run_id }}.tar.gz -C tmp/qbs-macos-x86_64-${{ steps.get-version-name.outputs.version-name }}
        cd tmp/
        tar czf ../release/qbs-macos-x86_64-${{ steps.get-version-name.outputs.version-name }}.tar.gz qbs-macos-x86_64-${{ steps.get-version-name.outputs.version-name }}
    - name: Copy changelog
      run: cp changelogs/changes-${{ steps.get-version-name.outputs.version-name }}.md release || echo "changelog not found"
    - name: Generate checksums
      run: |
        cd release/
        find . -type f -printf '%f\n' | grep -v .txt | xargs md5sum > md5sums.txt
        find . -type f -printf '%f\n' | grep -v .txt | xargs sha256sum > sha256sums.txt
    - name: Get archive name
      id: get-archive-name
      run: echo "archive-name=$(git describe)" >> $GITHUB_OUTPUT
    - name: Upload artifacts
      uses: 'actions/upload-artifact@v4'
      with:
        name: qbs-release-${{ steps.get-archive-name.outputs.archive-name }}
        path: |
          release/qbs-src-*.zip
          release/qbs-src-*.tar.gz
          release/qbs-linux-*.tar.gz
          release/qbs-macos-*.tar.gz
          release/qbs.*.nupkg
          release/qbs-windows-x86_64-*.zip
          release/changes-*.md
          release/md5sums.txt
          release/sha256sums.txt

