cmake_minimum_required(VERSION 3.10)

# ========================================================
# Dual-mode CMakeLists.txt: Supports both Catkin and Standalone CMake
# ========================================================

# Detect if building with Catkin
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  message(STATUS "Building robot_laser_geometry with Catkin")
else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building robot_laser_geometry with Standalone CMake")
endif()

project(robot_laser_geometry)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(CTest)

# ========================================================
# Find dependencies
# ========================================================

# Find system dependencies
find_package(Eigen3 REQUIRED)
find_package(GTest REQUIRED)

if(BUILDING_WITH_CATKIN)
  find_package(catkin REQUIRED COMPONENTS
    robot_sensor_msgs
    geometry_msgs
    robot_time
    tf3
    data_convert
  )
endif()

# ========================================================
# Catkin specific configuration
# ========================================================

if(BUILDING_WITH_CATKIN)
  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES robot_laser_geometry
    CATKIN_DEPENDS robot_sensor_msgs geometry_msgs robot_time tf3 data_convert
    DEPENDS Eigen3
  )
endif()

# Include directories
include_directories(
    include
    ${EIGEN3_INCLUDE_DIRS}
    ${GTEST_INCLUDE_DIRS}
)

if(BUILDING_WITH_CATKIN)
  include_directories(${catkin_INCLUDE_DIRS})
endif()

# Create library
add_library(robot_laser_geometry SHARED src/laser_geometry.cpp)

target_include_directories(robot_laser_geometry
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)

# Link libraries
if(BUILDING_WITH_CATKIN)
  target_link_libraries(robot_laser_geometry PUBLIC
      ${catkin_LIBRARIES}
  )
else()
  target_link_libraries(robot_laser_geometry PUBLIC
      robot_sensor_msgs
      geometry_msgs
      robot_time
      tf3
      data_convert
  )
endif()

if(BUILDING_WITH_CATKIN)
  add_dependencies(robot_laser_geometry ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
endif()

if(TARGET Eigen3::Eigen)
    target_link_libraries(robot_laser_geometry PUBLIC Eigen3::Eigen)
else()
    target_include_directories(robot_laser_geometry PUBLIC ${EIGEN3_INCLUDE_DIRS})
endif()

# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(robot_laser_geometry PRIVATE "ROBOT_LASER_GEOMETRY_BUILDING_LIBRARY")

# ========================================================
# Installation
# ========================================================

# --- Cài đặt thư viện vào hệ thống khi chạy make install ---
install(TARGETS robot_laser_geometry
  EXPORT robot_laser_geometry-targets
  ARCHIVE DESTINATION lib      # Thư viện tĩnh .a
  LIBRARY DESTINATION lib      # Thư viện động .so
  RUNTIME DESTINATION bin      # File thực thi (nếu có)
  INCLUDES DESTINATION include # Cài đặt include
)

if(NOT BUILDING_WITH_CATKIN)
  # Cài đặt headers (standalone)
  install(
      DIRECTORY include/
      DESTINATION include/${PROJECT_NAME}
  )
endif()

# --- Xuất export set robot_laser_geometry-targets thành file CMake module ---
# --- Tạo file lib/cmake/robot_laser_geometry/laser_geometry-targets.cmake ---
# --- File này chứa cấu hình giúp project khác có thể dùng ---
# --- Find_package(robot_laser_geometry REQUIRED) ---
# --- Target_link_libraries(my_app PRIVATE robot_laser_geometry::robot_laser_geometry) ---
install(EXPORT robot_laser_geometry-targets 
  FILE robot_laser_geometry-targets.cmake
  NAMESPACE robot_laser_geometry::
  DESTINATION lib/cmake/robot_laser_geometry
)


# Tests
if(BUILD_TESTING)
    enable_testing()
    
    add_executable(projection_test test/projection_test.cpp)
    target_link_libraries(projection_test
        robot_laser_geometry
        GTest::GTest
        GTest::Main
        pthread
    )
    add_test(NAME projection_test COMMAND projection_test)
endif()
