name: Package release

on:
  workflow_call:
    inputs:
      version:
        description: Version embedded in the compiler and archive names
        required: true
        type: string
      retention_days:
        description: Number of days to retain package artifacts
        required: true
        type: number

permissions:
  contents: read

jobs:
  linux:
    name: Package Linux (${{ matrix.arch }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: amd64
            runner: ubuntu-24.04
          - arch: arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    timeout-minutes: 45
    steps:
      - name: Check out repository
        uses: actions/checkout@v7

      - name: Set up Go
        uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
          cache: true

      - name: Install LLVM 22 development packages
        run: |
          wget -q https://apt.llvm.org/llvm.sh
          chmod +x llvm.sh
          sudo ./llvm.sh 22
          sudo apt-get install --yes clang-22 llvm-22-dev libclang-22-dev libclang-cpp22-dev liblld-22-dev
          test -e /usr/lib/llvm-22/lib/libclang-cpp.so
          echo "/usr/lib/llvm-22/bin" >> "$GITHUB_PATH"
          echo "CGO_CXXFLAGS=-I/usr/lib/llvm-22/include" >> "$GITHUB_ENV"
          echo "CGO_LDFLAGS=-L/usr/lib/llvm-22/lib" >> "$GITHUB_ENV"

      - name: Build release archive
        env:
          RELEASE_VERSION: ${{ inputs.version }}
        run: scripts/package-release-linux.sh "$RELEASE_VERSION" dist

      - name: Upload Linux archive
        uses: actions/upload-artifact@v7
        with:
          name: qk-${{ inputs.version }}-linux-${{ matrix.arch }}
          path: dist/*.tar.gz
          if-no-files-found: error
          compression-level: 0
          retention-days: ${{ inputs.retention_days }}

  macos:
    name: Package macOS (${{ matrix.arch }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: amd64
            runner: macos-15-intel
          - arch: arm64
            runner: macos-15
    runs-on: ${{ matrix.runner }}
    timeout-minutes: 60
    steps:
      - name: Check out repository
        uses: actions/checkout@v7

      - name: Set up Go
        uses: actions/setup-go@v7
        with:
          go-version-file: go.mod
          cache: true

      - name: Install LLVM and LLD
        run: brew install llvm lld

      - name: Build release archive
        env:
          LLVM_CONFIG: ${{ runner.temp }}/llvm-config
          RELEASE_VERSION: ${{ inputs.version }}
        run: |
          ln -s "$(brew --prefix llvm)/bin/llvm-config" "$LLVM_CONFIG"
          scripts/package-release-macos.sh "$RELEASE_VERSION" dist

      - name: Upload macOS archive
        uses: actions/upload-artifact@v7
        with:
          name: qk-${{ inputs.version }}-macos-${{ matrix.arch }}
          path: dist/*.tar.gz
          if-no-files-found: error
          compression-level: 0
          retention-days: ${{ inputs.retention_days }}

  windows:
    name: Package Windows
    runs-on: windows-2025
    timeout-minutes: 60
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - name: Check out repository
        uses: actions/checkout@v7

      - name: Install MSYS2 UCRT64 toolchain
        uses: msys2/setup-msys2@v2
        with:
          msystem: UCRT64
          update: true
          install: >-
            mingw-w64-ucrt-x86_64-go
            mingw-w64-ucrt-x86_64-clang
            mingw-w64-ucrt-x86_64-llvm
            mingw-w64-ucrt-x86_64-lld

      - name: Build release archive
        env:
          RELEASE_VERSION: ${{ inputs.version }}
        run: scripts/package-release-windows.sh "$RELEASE_VERSION" dist

      - name: Upload Windows archive
        uses: actions/upload-artifact@v7
        with:
          name: qk-${{ inputs.version }}-windows
          path: dist/*.tar.gz
          if-no-files-found: error
          compression-level: 0
          retention-days: ${{ inputs.retention_days }}