name: concrete-compiler test macos-cpu

on:
  workflow_dispatch:
  pull_request:
    paths:
      - .github/workflows/concrete_compiler_test_macos_cpu.yml
      - compilers/**
      - backends/**
      - tools/**
  push:
    branches:
      - 'main'
      - 'release/*'

env:
  ACTION_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
  SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
  SLACK_USERNAME: ${{ secrets.BOT_USERNAME }}
  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

concurrency:
  group: concrete_compiler_test_macos_cpu_${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  build-and-test:
    strategy:
      # if a failure happens, we want to know if it's specific
      # to the architecture or the operating system
      fail-fast: false
      matrix:
        runson: ["aws-mac1-metal", "aws-mac2-metal"]
        python-version: ["3.10"]
    runs-on: ${{ matrix.runson }}
    outputs:
      slack_message: ${{ steps.prepare_slack_notif.outputs.slack_message }}
      slack_color: ${{ steps.prepare_slack_notif.outputs.slack_color }}
    env:
      pip: pip${{ matrix.python-version }}
      python: python${{ matrix.python-version }}
      concrete-compiler-dir: ${{ github.workspace }}/compilers/concrete-compiler/compiler
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          submodules: recursive
          fetch-depth: 0
      - name: Setup rust toolchain for concrete-cpu
        uses: ./.github/actions/setup_rust_toolchain_for_concrete_cpu
      - name: Set python variables
        run: |
          {
            echo "PYTHON_EXEC=$(which ${{ env.python }})"
          } >> "${GITHUB_ENV}"
      - name: Install dependencies
        run: |
          brew install ninja ccache zstd
          ${{ env.pip }} install -r ${{ env.concrete-compiler-dir }}/lib/Bindings/Python/requirements_dev.txt
          ${{ env.pip }} install -r ${{ env.concrete-compiler-dir }}/../llvm-project/mlir/python/requirements.txt
      - name: Build compiler
        run: |
          set -e
          cd ${{ env.concrete-compiler-dir }}
          echo "Debug: ccache statistics (prior to the build):"
          ccache -s
          cargo install cxxbridge-cmd
          make Python3_EXECUTABLE="${PYTHON_EXEC}" all
          echo "Debug: ccache statistics (after the build):"
          ccache -s
      - name: Enable complete tests on push to main
        if: github.ref == 'refs/heads/main'
        run: echo "MINIMAL_TESTS=OFF" >> "${GITHUB_ENV}"
      - name: Enable minimal tests otherwise
        if: github.ref != 'refs/heads/main'
        run: echo "MINIMAL_TESTS=ON" >> "${GITHUB_ENV}"
      - name: Create keyset cache directory
        run: |
          KEY_CACHE_DIRECTORY=$(mktemp -d)/KeySetCache
          export KEY_CACHE_DIRECTORY
          echo "KEY_CACHE_DIRECTORY=${KEY_CACHE_DIRECTORY}" >> "${GITHUB_ENV}"
          mkdir "${KEY_CACHE_DIRECTORY}"
      - name: Test
        run: |
          set -e
          cd ${{ env.concrete-compiler-dir }}
          export CONCRETE_COMPILER_DATAFLOW_EXECUTION_ENABLED=OFF
          make MINIMAL_TESTS=${{ env.MINIMAL_TESTS }} Python3_EXECUTABLE="${PYTHON_EXEC}" run-tests
      - name: Cleanup host
        if: success() || failure()
        run: |
         rm -rf "${KEY_CACHE_DIRECTORY}"
      - name: Prepare Slack Notification
        id: prepare_slack_notif
        if: ${{ failure() && github.ref == 'refs/heads/main' }}
        continue-on-error: true
        run: |
          echo "slack_message=build-and-test(macos) finished with status: ${{ job.status }}. (${{ env.ACTION_RUN_URL }})" >> "$GITHUB_OUTPUT"
          echo "slack_color=${{ job.status }}" >> "$GITHUB_OUTPUT"

  slack-notif-macos:
    needs: ["build-and-test"]
    runs-on: "ubuntu-latest"
    if: always()
    steps:
      - name: Slack Notification
        # we want to check that prepare_slack_notif was run
        if: ${{ needs.build-and-test.outputs.slack_color != '' }}
        uses: rtCamp/action-slack-notify@c33737706dea87cd7784c687dadc9adf1be59990
        env:
          SLACK_COLOR: ${{ needs.build-and-test.outputs.slack_color }}
          SLACK_MESSAGE: ${{ needs.build-and-test.outputs.slack_message }}
