name: numba_linux-aarch64_conda_builder

on:
  schedule:
    - cron: '10 7 * * 6'  # Saturday 7:10 AM UTC - linux-aarch64 conda
  pull_request:
    paths:
      - .github/workflows/numba_linux-aarch64_conda_builder.yml
      - buildscripts/condarecipe.local/**
      - .github/workflows/conda_workflow_matrix.json
  workflow_dispatch:
    inputs:
      llvmlite_run_id:
        description: 'llvmlite workflow run ID (optional)'
        required: false
        type: string

# Add concurrency control
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

env:
  CONDA_CHANNEL_NUMBA: numba/label/dev

jobs:
  load-matrix:
    runs-on: ubuntu-latest
    outputs:
      build-matrix-json: ${{ steps.parse_matrix.outputs.build-matrix-json }}
      test-matrix-json: ${{ steps.parse_matrix.outputs.test-matrix-json }}
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - id: parse_matrix
        name: Parse Workflow Matrix JSON
        run: |
          BUILD_JSON=$(jq -c .conda_build_matrix .github/workflows/conda_workflow_matrix.json)
          TEST_JSON=$(jq -c .conda_test_matrix .github/workflows/conda_workflow_matrix.json)
          echo "Build Matrix JSON: $BUILD_JSON"
          echo "Test Matrix JSON: $TEST_JSON"
          echo "build-matrix-json=$BUILD_JSON" >> $GITHUB_OUTPUT
          echo "test-matrix-json=$TEST_JSON" >> $GITHUB_OUTPUT

  linux-aarch64-build-conda:
    name: linux-aarch64-build-conda (py ${{ matrix.python-version }}, np ${{ matrix.numpy_build }})
    needs: load-matrix
    runs-on: ubuntu-24.04-arm
    env:
      EXTRA_CHANNELS: ''
    defaults:
      run:
        shell: bash -elx {0}
    strategy:
      matrix:
        include: ${{ fromJson(needs.load-matrix.outputs.build-matrix-json) }}
      fail-fast: false

    steps:
      - name: Clone repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Setup Miniconda
        uses: conda-incubator/setup-miniconda@835234971496cad1653abb28a638a281cf32541f # v3.2.0
        with:
          python-version: 3.12
          auto-update-conda: true
          auto-activate-base: true
          activate-environment: true

      - name: Download llvmlite Artifact
        if: ${{ inputs.llvmlite_run_id != '' }}
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: llvmlite-linux-aarch64-py${{ matrix.python-version }}
          path: llvmlite_conda
          run-id: ${{ inputs.llvmlite_run_id }}
          repository: numba/llvmlite
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install conda-build
        run: conda install conda-build "conda-libmamba-solver<25.11"

      - name: Build Numba conda package
        run: |
          CONDA_CHANNEL_DIR="conda_channel_dir"
          mkdir -p $CONDA_CHANNEL_DIR

          # Set channel based on whether llvmlite artifact was downloaded
          if [ "${{ inputs.llvmlite_run_id }}" != "" ]; then
            LLVMLITE_CHANNEL="file://${{ github.workspace }}/llvmlite_conda"
          else
            LLVMLITE_CHANNEL="${{ env.CONDA_CHANNEL_NUMBA }}"
          fi

          if [ -n "${EXTRA_CHANNELS}" ]; then
            extra_args=(${EXTRA_CHANNELS})
          else
            extra_args=()
          fi

          conda build --debug -c $LLVMLITE_CHANNEL "${extra_args[@]}" -c defaults \
            --python=${{ matrix.python-version }} \
            --numpy=${{ matrix.numpy_build }} blas=*=openblas \
            buildscripts/condarecipe.local/ \
            --output-folder=$CONDA_CHANNEL_DIR \
            --no-test

      - name: Upload numba conda package
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: numba_linux-aarch64_conda_py${{ matrix.python-version }}
          path: conda_channel_dir
          compression-level: 0
          retention-days: 7
          if-no-files-found: error

  linux-aarch64-test:
    name: linux-aarch64-test-conda (py ${{ matrix.python-version }}, np ${{ matrix.numpy_test }})
    needs: [load-matrix, linux-aarch64-build-conda]
    runs-on: ubuntu-24.04-arm
    env:
      EXTRA_CHANNELS: ''
    defaults:
      run:
        shell: bash -elx {0}
    strategy:
      matrix:
        include: ${{ fromJson(needs.load-matrix.outputs.test-matrix-json) }}
      fail-fast: false

    steps:
      - name: Clone repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Setup Miniconda
        uses: conda-incubator/setup-miniconda@835234971496cad1653abb28a638a281cf32541f # v3.2.0
        with:
          python-version: 3.12
          auto-update-conda: true
          auto-activate-base: true
          activate-environment: true

      - name: Download llvmlite artifact
        if: ${{ inputs.llvmlite_run_id != '' }}
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: llvmlite-linux-aarch64-py${{ matrix.python-version }}
          path: llvmlite_conda
          run-id: ${{ inputs.llvmlite_run_id }}
          repository: numba/llvmlite
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download numba artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: numba_linux-aarch64_conda_py${{ matrix.python-version }}

      - name: Install conda-build
        run: |
          conda install conda-build "conda-libmamba-solver<25.11"

      - name: Install catchsegv
        run: |
          sudo apt-get update
          sudo apt-get install -y glibc-tools
          # verify catchsegv is installed
          catchsegv --version

      - name: Run tests
        env:
          TESTS_TO_RUN: "numba.tests"
          NUMBA_ENABLE_CUDASIM: 1
        run: |
          # Set channel based on whether llvmlite artifact was downloaded
          if [ "${{ inputs.llvmlite_run_id }}" != "" ]; then
            LLVMLITE_CHANNEL="file://${{ github.workspace }}/llvmlite_conda"
          else
            LLVMLITE_CHANNEL="${{ env.CONDA_CHANNEL_NUMBA }}"
          fi

          if [ -n "${EXTRA_CHANNELS}" ]; then
            extra_args=(${EXTRA_CHANNELS})
          else
            extra_args=()
          fi

          # List directories to find the correct package path
          echo "Listing downloaded artifact contents:"
          find . -type f -name "numba*.conda" | sort

          # Try to find the package using the correct architecture directory
          NUMBA_PKG=$(find . -type f -name "numba*.conda" | head -1)

          if [ -z "$NUMBA_PKG" ]; then
            echo "ERROR: Could not find numba conda package"
            exit 1
          fi

          echo "Using package: $NUMBA_PKG"

          # Run the test with the found package
          conda build -c $LLVMLITE_CHANNEL "${extra_args[@]}" -c defaults \
            --python=${{ matrix.python-version }} \
            --extra-deps numpy=${{ matrix.numpy_test }} blas=*=openblas \
            --test \
            "$NUMBA_PKG"
