cmake_minimum_required(VERSION 3.10)
project(imu_integrator_cpp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(Eigen3 REQUIRED)
find_package(OpenMP REQUIRED)
# Auto-detect pybind11 from the active Python environment (works with conda)
execute_process(
    COMMAND python -c "import pybind11; print(pybind11.get_cmake_dir())"
    OUTPUT_VARIABLE pybind11_DIR
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
find_package(pybind11 REQUIRED)

# ----------------------------------------------------------------------
# SOPHUS
# ----------------------------------------------------------------------
# Auto-detect Sophus from the active Python environment (installed via sophuspy)
if(NOT DEFINED Sophus_DIR OR Sophus_DIR STREQUAL "")
    execute_process(
        COMMAND python -c "import sophus, os; print(os.path.join(os.path.dirname(sophus.__file__), 'cmake'))"
        OUTPUT_VARIABLE Sophus_DIR
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()

find_package(Sophus REQUIRED)

pybind11_add_module(imu_integrator_cpp
    imu_integrator.cpp
    imu_integrator_bindings.cpp
)

target_link_libraries(imu_integrator_cpp
    PRIVATE Eigen3::Eigen Sophus::Sophus OpenMP::OpenMP_CXX
)

# Aggressive optimization flags
target_compile_options(imu_integrator_cpp PRIVATE
    -O3                    # Maximum optimization
    -march=native          # Optimize for current CPU (AVX2, FMA, etc.)
    -ffast-math            # Aggressive FP optimizations (faster sin/cos/exp)
    -funroll-loops         # Unroll loops
    -ftree-vectorize       # Auto-vectorization
)

target_compile_definitions(imu_integrator_cpp PRIVATE
    EIGEN_NO_DEBUG         # Disable Eigen debug checks
    NDEBUG                 # Disable assertions
)
