name: Enzyme CI

on: 
  push:
    branches:
      - main
  pull_request:
  merge_group:

concurrency:
  # Skip intermediate builds: always.
  # Cancel intermediate builds: only if it is a pull request build.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

jobs:
  build-linux:
    name: Enzyme CI LLVM ${{ matrix.llvm }} ${{ matrix.build }} ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    
    strategy:
      fail-fast: false
      matrix:
        llvm: ["15", "16"]
        build: ["Release", "Debug"] # "RelWithDebInfo"
        os: [ubuntu-22.04]
  
    timeout-minutes: 30 

    steps:
    - name: add llvm 
      run: |
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
          sudo apt-add-repository "deb http://apt.llvm.org/`lsb_release -c | cut -f2`/ llvm-toolchain-`lsb_release -c | cut -f2`-${{ matrix.llvm }} main" || true
          sudo apt-get install -y cmake gcc g++ llvm-${{ matrix.llvm }}-dev libzstd-dev
          sudo python3 -m pip install --upgrade pip lit
    - uses: actions/checkout@v4
    - name: mkdir
      run: rm -rf build && mkdir build
    - name: cmake
      working-directory: build
      run: cmake ../enzyme -DCMAKE_BUILD_TYPE=${{ matrix.build }} -DLLVM_EXTERNAL_LIT=`which lit` -DLLVM_DIR=/usr/lib/llvm-${{ matrix.llvm }}/lib/cmake/llvm
    - name: make
      working-directory: build
      run: make -j `nproc`
    - name: make check-typeanalysis
      working-directory: build
      run: make -j `nproc` check-typeanalysis
    - name: make check-activityanalysis
      working-directory: build
      run: make -j `nproc` check-activityanalysis
    - name: make check-enzyme
      working-directory: build
      run: make  -j `nproc` check-enzyme-bench
  
  build-macos:
    name: Enzyme CI LLVM ${{ matrix.llvm }} ${{ matrix.build }} macOS
    runs-on: macos-latest
    
    strategy:
      fail-fast: false
      matrix:
        llvm: ["15", "16"]
        build: ["Release", "Debug"] # "RelWithDebInfo"
  
    timeout-minutes: 30 
    steps:
    - name: add llvm 
      run: |
          brew update
          brew uninstall cmake
          brew install llvm@${{ matrix.llvm }} make cmake
          sudo python3 -m pip install --upgrade --break-system-packages --user pip lit requests
    - uses: actions/checkout@v4
    - name: mkdir
      run: rm -rf build && mkdir build
    - name: cmake
      working-directory: build
      run: cmake ../enzyme -DCMAKE_BUILD_TYPE=${{ matrix.build }} -DLLVM_EXTERNAL_LIT=`find /Users/runner/Library/Python/ -iname lit` -DLLVM_DIR=`brew --prefix llvm@${{ matrix.llvm }}`/lib/cmake/llvm
    - name: make
      working-directory: build
      run: make -j 3
    - name: make check-typeanalysis
      working-directory: build
      run: make -j 3 check-typeanalysis
    - name: make check-activityanalysis
      working-directory: build
      run: make -j 3 check-activityanalysis
    - name: make check-enzyme
      working-directory: build
      run: make -j3 check-enzyme-bench

  build-xcode:
    name: Enzyme CI LLVM ${{ matrix.llvm }} ${{ matrix.build }} macOS XCode
    runs-on: macos-latest

    strategy:
      fail-fast: false
      matrix:
        llvm: ["16"]
        build: ["Release"] # "RelWithDebInfo"

    timeout-minutes: 30
    steps:
    - name: add llvm
      run: |
          brew uninstall cmake
          brew install llvm@${{ matrix.llvm }} make cmake gcc
          sudo python3 -m pip install --upgrade --break-system-packages --user pip lit
    - uses: actions/checkout@v4
    - name: mkdir
      run: rm -rf build && mkdir build
    - name: cmake
      working-directory: build
      run: |
          cmake ../enzyme -GXcode -DCMAKE_BUILD_TYPE=${{ matrix.build }} -DLLVM_EXTERNAL_LIT=`which lit` -DLLVM_DIR=`brew --prefix llvm@${{ matrix.llvm }}`/lib/cmake/llvm
          cmake --build . --parallel 3
