# Perform a build on MacOS platform with M1 chip.
name: Compiler - Build and Test (MacOS)

on:
  workflow_call:
  workflow_dispatch:
    secrets:
      CONCRETE_CI_SSH_PRIVATE:
        required: true
      CONCRETE_ACTIONS_TOKEN:
        required: true

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

jobs:
  BuildAndTestMacOS:
    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"]
    runs-on: ${{ matrix.runson }}
    steps:
      # A SSH private key is required as some dependencies are from private repos
      - uses: webfactory/ssh-agent@v0.7.0
        with:
          ssh-private-key: ${{ secrets.CONCRETE_CI_SSH_PRIVATE }}

      - uses: actions/checkout@v3
        with:
          submodules: recursive
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

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

      - name: Install Deps
        run: |
          brew install ninja ccache
          pip3.10 install numpy pybind11==2.8 wheel delocate
          pip3.10 install pytest

      - name: Cache compilation (push)
        if: github.event_name == 'push'
        uses: actions/cache@v3
        with:
          path: /Users/runner/Library/Caches/ccache
          key: ${{ runner.os }}-${{ runner.arch }}-compilation-cache-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-compilation-cache-

      - name: Cache compilation (pull_request)
        if: github.event_name == 'pull_request'
        uses: actions/cache@v3
        with:
          path: /Users/runner/Library/Caches/ccache
          key: ${{ runner.os }}-${{ runner.arch }}-compilation-cache-${{ github.event.pull_request.base.sha }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-compilation-cache-

      - name: Get tmpdir path
        if: github.event_name == 'push'
        id: tmpdir-path
        run: echo "::set-output name=TMPDIR_PATH::$TMPDIR"

      # We do run run-check-tests as part of the build, as they aren't that costly
      # and will at least give minimum confidence that the compiler works in PRs
      - name: Build
        run: |
          set -e
          cd compilers/concrete-compiler/compiler
          echo "Debug: ccache statistics (prior to the build):"
          ccache -s
          make Python3_EXECUTABLE=$(which python3.10) all run-check-tests python-package
          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: Test
        run: |
          set -e
          export KEY_CACHE_DIRECTORY=$(mktemp -d)/KeySetCache
          echo "KEY_CACHE_DIRECTORY=$KEY_CACHE_DIRECTORY" >> "${GITHUB_ENV}"
          mkdir $KEY_CACHE_DIRECTORY

          cd compilers/concrete-compiler/compiler
          echo "Debug: ccache statistics (prior to the tests):"
          ccache -s
          export CONCRETE_COMPILER_DATAFLOW_EXECUTION_ENABLED=OFF
          pip3.10 install build/wheels/*macosx*.whl
          make MINIMAL_TESTS=${{ env.MINIMAL_TESTS }} Python3_EXECUTABLE=$(which python3.10) run-tests
          echo "Debug: ccache statistics (after the tests):"
          ccache -s

      - name: Cleanup host
        if: success() || failure()
        run: |
         rm -rf $KEY_CACHE_DIRECTORY
