name: Build Eigenpy on Windows via Conda
on: [push,pull_request]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
          os: [windows-latest]
    steps:
    - uses: actions/checkout@v2
    - name: Checkout submodules
      run: |
        git submodule update --init
    - uses: goanpeca/setup-miniconda@v1
      env:
        ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
      with:
        activate-environment: eigenpy
        environment-file: .github/workflows/conda/environment.yml
        python-version: 3.7
    - name: Build Eigenpy
      shell: cmd /C CALL {0}
      env:
        ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
      run: |
        :: unset extra Boost envs
        set Boost_ROOT=
        set BOOST_ROOT_1_69_0=
        set BOOST_ROOT_1_72_0=
        set PATH=%PATH:C:\hostedtoolcache\windows\Boost\1.72.0;=%
        if errorlevel 1 exit 1

        :: 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 "NMake Makefiles" ^
          -DCMAKE_INSTALL_PREFIX=%CONDA_PREFIX%\Library ^
          -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

        :: Build stubs
        git clone https://github.com/jcarpent/pybind11-stubgen.git
        python "%CD%\pybind11-stubgen\pybind11_stubgen\__init__.py" -o %CONDA_PREFIX%\Lib\site-packages\eigenpy eigenpy --boost-python --ignore-invalid signature --no-setup-py --root-module-suffix ""
        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 "NMake Makefiles" ^
          -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 "NMake Makefiles" ^
          -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