name: Build

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

env:
  CTEST_OUTPUT_ON_FAILURE: ON
  CTEST_PARALLEL_LEVEL: 2

defaults:
  run:
    shell: bash -el {0}

jobs:
  ####################
  # Linux / macOS
  ####################

  Unix:
    name: ${{ matrix.name }}-${{ matrix.envelope }} (${{ matrix.config }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-22.04, ubuntu-latest, macOS-13, macOS-14, macOS-latest, windows-2022]
        config: [Debug, Release]
        envelope: [OFF]
        include:
          - os: ubuntu-22.04
            name: ubuntu-22.04
          - os: ubuntu-latest
            name: ubuntu-latest
          - os: macos-13
            name: macOS-13
          - os: macos-14
            name: macOS-14 ARM64
          - os: macos-latest
            name: macOS-latest ARM64     
          - os: windows-2022
            name: Windows
            
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 10
          submodules: "recursive"
    
      - name: Dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install libxi-dev libxcursor-dev libxinerama-dev libxrandr-dev libx11-dev libxcb1-dev libxau-dev libxdmcp-dev

      - name: Setup NMake (Windows)
        if: runner.os == 'Windows'
        uses: ilammy/msvc-dev-cmd@v1

      - name: Setup Conda
        uses: conda-incubator/setup-miniconda@v3
        with:
          channels: conda-forge
          python-version: ${{ matrix.python-version }}
          channel-priority: true
          activate-environment: test-env

      - name: Install Dependencies
        run: conda install cmake=3.29 git -y

      - name: Install Dependencies  (Windows)
        if: runner.os == 'Windows'
        run: conda install mpir zlib -y

      - name: Envs (Windows)
        if: runner.os == 'Windows'
        run: echo "CMAKE_GENERATOR=NMake Makefiles" >> $GITHUB_ENV

      - name: Configure ssl certificate
        if : runner.os == 'Windows'
        run: git config --global http.sslBackend schannel

      - name: Configure
        run: |
          mkdir -p build
          cd build
          cmake .. \
            -DCMAKE_PREFIX_PATH=$CONDA_PREFIX \
            -DCMAKE_BUILD_TYPE=${{ matrix.config }} \
            -DFLOAT_TETWILD_WITH_EXACT_ENVELOPE=${{ matrix.envelope }}

      - name: Build
        run: cd build; conda run -n test-env cmake --build . --parallel 2
        
      - name: Tests
        run: cd build; ctest --verbose --output-on-failure

      - name: Run Example
        run: cd build; ./FloatTetwild_bin --input ../tests/bunny.off --level 0 --stop-energy 100
