# Copyright 2025-2026, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

name: Release Pipeline

permissions: read-all

on:
  workflow_dispatch:
    inputs:
      ref:
        description: 'ISPC reference to checkout (SHA/tag/branch)'
        required: true
        type: string
        default: 'main'

jobs:
  define-params:
    # Only run in the main ispc/ispc repo for release builds.
    if: github.repository == 'ispc/ispc'
    runs-on: ubuntu-22.04
    outputs:
      ispc_version: ${{ steps.define.outputs.ispc_version }}
      docker_dir: ${{ steps.define.outputs.docker_dir }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          fetch-depth: 0
          path: ispc
          ref: ${{ inputs.ref }}

      - name: Define ISPC version
        id: define
        run: |
          file_path="ispc/common/version.h"
          version_line=$(grep '#define ISPC_VERSION ' "$file_path")
          if [[ -n "$version_line" ]]; then
            # Extract the version number
            VERSION=$(echo "$version_line" | awk -F'"' '{print $2}')
            if [[ "$VERSION" == *"dev"* ]]; then
              echo "Version has a dev suffix, so it is a trunk archive"
              DOCKER_DIR="docker/ubuntu/18.04/cpu_ispc_build"
            else
              echo "Version does not have a dev suffix, so it is a release archive"
              DOCKER_DIR="docker/v${VERSION}/ubuntu18.04/"
            fi
          else
            echo "Version line not found in the file."
          fi
          echo "ispc_version=${VERSION}" >> "$GITHUB_OUTPUT"
          echo "ispc_version=${VERSION}" >> "$GITHUB_STEP_SUMMARY"
          echo "docker_dir=${DOCKER_DIR}" >> "$GITHUB_OUTPUT"
          echo "docker_dir=${DOCKER_DIR}" >> "$GITHUB_STEP_SUMMARY"


  aarch64:
    runs-on: ubuntu-22.04-arm
    needs: [define-params]
    steps:
      - name: Free disk space
        uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
        with:
          tool-cache: false
          android: false
          dotnet: true
          haskell: false
          large-packages: true
          docker-images: true
          swap-storage: true

      - name: Checkout repository
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          fetch-depth: 0
          ref: ${{ inputs.ref }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0

      - name: Build Docker image
        working-directory: ${{ needs.define-params.outputs.docker_dir }}
        run: |
          docker build --no-cache \
            --build-arg XE_DEPS=OFF \
            --build-arg LTO=ON \
            --build-arg SHA=${INPUTS_REF} \
            -t ispc-release .
        env:
          INPUTS_REF: ${{ inputs.ref }}

      - name: Extract tarball from container
        run: |
          CONTAINER_ID=$(docker create ispc-release)
          docker cp "$CONTAINER_ID":/usr/local/src/ ./

      - name: Upload artifact
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: ispc_aarch64
          path: src/ispc-*.tar.gz

  x86_64:
    runs-on: ubuntu-22.04
    needs: [define-params]
    steps:
      - name: Free disk space
        uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
        with:
          tool-cache: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true

      - name: Checkout repository
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          fetch-depth: 0
          ref: ${{ inputs.ref }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0

      - name: Build Docker image
        working-directory: ${{ needs.define-params.outputs.docker_dir }}
        run: |
          docker build --no-cache \
            --build-arg LTO=ON \
            --build-arg SHA=${INPUTS_REF} \
            -t ispc-release .
        env:
          INPUTS_REF: ${{ inputs.ref }}

      - name: Extract tarball from container
        run: |
          CONTAINER_ID=$(docker create ispc-release)
          docker cp "$CONTAINER_ID":/usr/local/src/ ./

      - name: Upload artifact
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: ispc_x86_64
          path: src/ispc-*.tar.gz
