cmake_minimum_required(VERSION 3.10)
project(kalman VERSION 1.0.0 LANGUAGES CXX)

# Chuẩn C++
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Find dependencies
find_package(Eigen3 REQUIRED)

# Thư mục include
include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${EIGEN3_INCLUDE_DIRS}
)

# Tìm tất cả file source
file(GLOB SOURCES "src/kalman.cpp")
file(GLOB HEADERS "include/kalman/kalman.h")

# Tạo thư viện shared (.so)
add_library(kalman SHARED ${SOURCES} ${HEADERS})

# Link libraries
target_link_libraries(kalman
    PUBLIC
        Eigen3::Eigen
)

set_target_properties(kalman PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Set include directories
target_include_directories(kalman
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
        ${EIGEN3_INCLUDE_DIRS}
)

# Tạo executable cho test
add_executable(kalman_node src/kalman-test.cpp)
target_link_libraries(kalman_node
    PRIVATE
        kalman
        Eigen3::Eigen
)

set_target_properties(kalman_node PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Cài đặt header files
install(DIRECTORY include/kalman
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
)

# Cài đặt library
install(TARGETS kalman
    EXPORT kalman-targets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
)

# Cài đặt executable (optional)
install(TARGETS kalman_node
    RUNTIME DESTINATION bin
)

# Export targets
install(EXPORT kalman-targets
    FILE kalman-targets.cmake
    DESTINATION lib/cmake/kalman
)

# Tùy chọn build
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_TESTS "Build test programs" OFF)

# Flags biên dịch
# Warning flags - disabled to suppress warnings during build
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")  # -w suppresses all warnings
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

# In thông tin cấu hình
message(STATUS "=================================")
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Eigen3 found: ${EIGEN3_FOUND}")
message(STATUS "Eigen3 include dir: ${EIGEN3_INCLUDE_DIRS}")
message(STATUS "=================================")
