name: Build Eigenpy on Windows via Conda
on: [push,pull_request]
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ${{ matrix.os }}
    env:
      CCACHE_BASEDIR: "${GITHUB_WORKSPACE}"
      CCACHE_DIR: "${GITHUB_WORKSPACE}/.ccache"
      CCACHE_COMPRESS: true
      CCACHE_COMPRESSLEVEL: 6

    strategy:
      fail-fast: false
      matrix:
          os: [windows-latest]

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

    - uses: conda-incubator/setup-miniconda@v3
      with:
        activate-environment: eigenpy
        auto-update-conda: true
        environment-file: .github/workflows/conda/environment_windows.yml
        python-version: "3.10"
        auto-activate-base: false

    - uses: actions/cache@v3
      with:
        path: .ccache
        key: ccache-windows-conda-${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.cxx_options }}-${{ matrix.python-version }}-${{ github.sha }}
        restore-keys: ccache-windows-conda-${{ matrix.os }}-${{ matrix.build_type }}-${{ matrix.cxx_options }}-${{ matrix.python-version }}-

    - name: Build Eigenpy
      shell: cmd /C CALL {0}
      run: |
        :: start building
        call "%programfiles%\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
        if errorlevel 1 exit 1

        mkdir build
        pushd build
        cmake ^
          -G "Ninja" ^
          -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache ^
          -DCMAKE_BUILD_TYPE=Release ^
          -DGENERATE_PYTHON_STUBS=ON ^
          -DPYTHON_SITELIB=%CONDA_PREFIX%\Lib\site-packages ^
          -DPYTHON_EXECUTABLE=%CONDA_PREFIX%\python.exe ^
          ..
        if errorlevel 1 exit 1

        :: Build and Install
        cmake --build . --config Release --target install
        if errorlevel 1 exit 1

        :: Testing
        ctest --output-on-failure -C Release -V
        if errorlevel 1 exit 1

        :: Test Python import
        cd ..
        python -c "import eigenpy"
        if errorlevel 1 exit 1

        :: Test packaging

        cmake -B test-packaging -S unittest/packaging/cmake ^
          -G "Ninja" ^
          -DPYTHON_EXECUTABLE=%CONDA_PREFIX%\python.exe
        if errorlevel 1 exit 1

        :: Configure hpp-fcl
        conda install -c conda-forge octomap assimp qhull
        git clone --recursive https://github.com/humanoid-path-planner/hpp-fcl.git
        pushd hpp-fcl
        mkdir build
        pushd build
        cmake ^
          -G "Ninja" ^
          -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^
          -DCMAKE_BUILD_TYPE=Release ^
          -DPYTHON_EXECUTABLE=%CONDA_PREFIX%\python.exe ^
          ..
        cd ..
        if errorlevel 1 exit 1

        :: Uninstall
        cmake --build . --config Release --target uninstall
        if errorlevel 1 exit 1

  check:
    if: always()
    name: check-windows-conda

    needs:
    - build

    runs-on: Ubuntu-latest

    steps:
    - name: Decide whether the needed jobs succeeded or failed
      uses: re-actors/alls-green@release/v1
      with:
        jobs: ${{ toJSON(needs) }}
