name: Compiler - Distributed Build and Test (CPU)

on:
  workflow_dispatch:
    inputs:
      instance_id:
        description: 'Instance ID'
        type: string
      instance_image_id:
        description: 'Instance AMI ID'
        type: string
      instance_type:
        description: 'Instance product type'
        type: string
      runner_name:
        description: 'Action runner name'
        type: string
      request_id:
        description: 'Slab request ID'
        type: string
      matrix_item:
        description: 'Build matrix item'
        type: string


env:
  GLIB_VER: 2_28

jobs:
  BuildAndTest:
    name: Build and test compiler on Slurm cluster in EC2
    runs-on: distributed-ci
    if: ${{ !cancelled() }}
    steps:
      - name: Instance configuration used
        run: |
          echo "ID: ${{ inputs.instance_id }}"
          echo "AMI: ${{ inputs.instance_image_id }}"
          echo "Type: ${{ inputs.instance_type }}"
          echo "Request ID: ${{ inputs.request_id }}"
          echo "Matrix item: ${{ inputs.matrix_item }}"

      - name: Instance cleanup
        run: |
          sudo rm -rf /home/ubuntu/actions-runner/_work/concrete/concrete
          mkdir -p /home/ubuntu/actions-runner/_work/concrete/concrete
          docker system prune -af

      - name: Fetch repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          submodules: recursive
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

      - name: Set up home
        # "Install rust" step require root user to have a HOME directory which is not set.
        run: |
          echo "HOME=/shared" >> "${GITHUB_ENV}"

      - name: Export specific variables (CPU)
        if: ${{ !startswith(inputs.instance_type, 'p3.') }}
        run: |
          echo "CUDA_SUPPORT=OFF" >> "${GITHUB_ENV}"
          echo "DATAFLOW_EXECUTION_ENABLED=ON" >> "${GITHUB_ENV}"

      - name: Setup rust toolchain for concrete-cpu
        uses: ./.github/workflows/setup_rust_toolchain_for_concrete_cpu

      - name: Build compiler benchmarks
        run: |
          set -e
          git config --global --add safe.directory '*'
          cd compilers/concrete-compiler/compiler
          rm -rf /shared/build
          make HPX_DIR=/shared/hpx install-hpx-from-source
          make HPX_DIR=/shared/hpx BUILD_DIR=/shared/build CCACHE=ON DATAFLOW_EXECUTION_ENABLED=ON BINDINGS_PYTHON_ENABLED=OFF CUDA_SUPPORT=${{ env.CUDA_SUPPORT }} build-end-to-end-dataflow-tests

      - name: Run end-to-end benchmarks
        run: |
          set -e
          cd compilers/concrete-compiler/compiler
          make BUILD_DIR=/shared/build run-end-to-end-distributed-tests

      - name: Instance cleanup
        run: |
          sudo rm -rf /home/ubuntu/actions-runner/_work/concrete/concrete/*
          docker system prune -af
