name: Build PoseLib

on:
  pull_request:
    branches:
      - '**'
  push:
    branches:    
      - master

env:
  BUILD_CONFIGURATION: Release
  LIBRARY_TYPE: OFF  # Static libraries
  BUILD_DIR: _build
  INSTALL_DIR: _install

jobs:
  CI-pipeline:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
      with:
        # Fetches all submodules recursively.
        submodules: 'recursive'

    - name: Install Eigen3 Dependency
      run: |
        sudo apt-get update && \
        sudo apt-get install -y libeigen3-dev clang-format

    - name: Check formatting using clang-format
      run: |
        git ls-files | grep -E '\.(cc|h)$' | xargs clang-format -i --verbose
        git diff --exit-code || (echo "Code format check failed, run clang-format to fix it." && exit 1)


    - name: Setup CMake Configuration
      run: |
        cmake \
          -B ${{github.workspace}}/${{env.BUILD_DIR}} \
          -DWITH_BENCHMARK=ON \
          -DCMAKE_BUILD_TYPE=${{env.BUILD_CONFIGURATION}} \
          -DBUILD_SHARED_LIBS=${{env.LIBRARY_TYPE}} \
          -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/${{env.INSTALL_DIR}}

    - name: Compile and Install
      run: |
        cmake \
          --build ${{github.workspace}}/${{env.BUILD_DIR}} \
          --target install \
          -j $(nproc)

    - name: Execute Benchmark
      run: ${{github.workspace}}/${{env.INSTALL_DIR}}/bin/benchmark
