name: Conda-CI

on: [push,pull_request]

jobs:
  eigenpy-conda:
    name: ${{ matrix.os }} - ${{ matrix.build_type }} ${{ matrix.cxx_options }}
    runs-on: ${{ matrix.os }}
    env:
      CCACHE_DIR: ${{ matrix.CCACHE_DIR }}

    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest", "macos-latest"]
        cxx_options: ['', '-mavx2']
        build_type: [Release, Debug]
        python-version: ["3.10", "3.11", "3.12"]

        include:
          - os: ubuntu-latest
            CCACHE_DIR: /home/runner/.ccache
          - os: macos-latest
            CCACHE_DIR: /Users/runner/.ccache

        exclude:
          - build_type: Debug
            cxx_options: -mavx2
            os: macos-latest
          - build_type: Release
            cxx_options: -mavx2
            os: macos-latest

    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - uses: conda-incubator/setup-miniconda@v2
      with:
        activate-environment: eigenpy
        auto-update-conda: true
        environment-file: .github/workflows/conda/environment.yml
        python-version: ${{ matrix.python-version }}

    - uses: actions/cache@v3
      with:
        path: ${{ env.CCACHE_DIR }}
        key: ccache-${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.cxx_options }}

    - name: Prepare cache dir
      run: sudo mkdir -p ${CCACHE_DIR}/tmp && sudo chown -R $(id -un) ${CCACHE_DIR}

    - name: Install cmake and update conda
      shell: bash -l {0}
      run: |
        conda activate eigenpy
        conda install cmake ccache -c conda-forge
        conda list

    - name: Build EigenPy
      shell: bash -l {0}
      run: |
        conda activate eigenpy
        echo $CONDA_PREFIX

        mkdir build
        cd build

        cmake .. -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DPYTHON_EXECUTABLE=$(which python3) -DGENERATE_PYTHON_STUBS=ON -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_CXX_FLAGS=${{ matrix.cxx_options }}
        make
        make build_tests
        export CTEST_OUTPUT_ON_FAILURE=1
        make test
        make install

    - name: Test packaging
      shell: bash -l {0}
      run: |
        conda activate eigenpy

        cmake -B test-packaging -S unittest/packaging/cmake -DPYTHON_EXECUTABLE=$(which python3)

    - name: Configure hpp-fcl
      shell: bash -l {0}
      run: |
        conda activate eigenpy
        conda install -c conda-forge octomap assimp qhull

        git clone https://github.com/humanoid-path-planner/hpp-fcl.git --recursive
        cd hpp-fcl
        mkdir build && cd build
        cmake .. -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=$(which python3)

    - name: Uninstall EigenPy
      shell: bash -l {0}
      run: |
        cd build
        make uninstall