cmake_minimum_required(VERSION 3.0.2)
project(tf3 VERSION 1.0.0 LANGUAGES CXX)

if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
  set(BUILDING_WITH_CATKIN TRUE)
  message(STATUS "Building tf3 with Catkin")

else()
  set(BUILDING_WITH_CATKIN FALSE)
  message(STATUS "Building tf3 with Standalone CMake")
endif()

# C++ Standard - must be set before find_package
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Find dependencies
find_package(console_bridge REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)

if (NOT BUILDING_WITH_CATKIN)

  # Enable Position Independent Code
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)

  # Cấu hình RPATH để tránh cycle trong runtime search path
  set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")

else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED)

  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES ${PROJECT_NAME}
    DEPENDS console_bridge Boost
  )

  include_directories(
    include
    ${catkin_INCLUDE_DIRS}
    ${console_bridge_INCLUDE_DIRS}
  )
endif()

# Libraries
add_library(${PROJECT_NAME} SHARED
  src/cache.cpp
  src/buffer_core.cpp
  src/static_cache.cpp
)

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

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

  target_link_libraries(${PROJECT_NAME}
    PUBLIC ${catkin_LIBRARIES}
    PRIVATE Boost::boost Boost::system Boost::thread
    PRIVATE console_bridge::console_bridge
  )

else()

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

  target_link_libraries(${PROJECT_NAME}
    PUBLIC
      Boost::boost Boost::system Boost::thread
      console_bridge::console_bridge
  )

  set_target_properties(${PROJECT_NAME} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
    BUILD_RPATH "${CMAKE_BINARY_DIR}"
    INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
  )

endif()

if(BUILDING_WITH_CATKIN)
  ## Mark libraries for installation
  ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
  install(TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
  )

  ## Mark cpp header files for installation
  install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.h"
    PATTERN ".svn" EXCLUDE
  )

else()

  install(TARGETS ${PROJECT_NAME}
    EXPORT ${PROJECT_NAME}-targets
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
  )

  # Export targets
  install(EXPORT ${PROJECT_NAME}-targets
    FILE ${PROJECT_NAME}-targets.cmake
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION lib/cmake/${PROJECT_NAME}
  )

  ## Mark cpp header files for installation
  install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
    PATTERN ".svn" EXCLUDE
  )

  # Print configuration info
  message(STATUS "=================================")
  message(STATUS "Project: ${PROJECT_NAME}")
  message(STATUS "Version: ${PROJECT_VERSION}")
  message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
  message(STATUS "Dependencies: console_bridge, Boost")
  message(STATUS "=================================")
endif()

# ========================================================
# Example executables
# ========================================================
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/examples/simple_tf3_example.cpp)
  add_executable(${PROJECT_NAME}_example examples/simple_tf3_example.cpp)
  target_include_directories(${PROJECT_NAME}_example PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
  target_link_libraries(${PROJECT_NAME}_example PRIVATE 
    ${PROJECT_NAME}
    pthread
    console_bridge::console_bridge
  )
endif()

# ========================================================
# Test executables
# ========================================================
option(BUILD_TESTING "Build tests" OFF)
if(BUILD_TESTING)
  enable_testing()
  
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/cache_unittest.cpp)
    add_executable(${PROJECT_NAME}_cache_test test/cache_unittest.cpp)
    target_link_libraries(${PROJECT_NAME}_cache_test PRIVATE 
      ${PROJECT_NAME}
      console_bridge::console_bridge
    )
    add_test(NAME ${PROJECT_NAME}_cache_test COMMAND ${PROJECT_NAME}_cache_test)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/static_cache_test.cpp)
    add_executable(${PROJECT_NAME}_static_cache_test test/static_cache_test.cpp)
    target_link_libraries(${PROJECT_NAME}_static_cache_test PRIVATE 
      ${PROJECT_NAME}
      console_bridge::console_bridge
    )
    add_test(NAME ${PROJECT_NAME}_static_cache_test COMMAND ${PROJECT_NAME}_static_cache_test)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/simple_tf3_core.cpp)
    add_executable(${PROJECT_NAME}_simple_test test/simple_tf3_core.cpp)
    target_link_libraries(${PROJECT_NAME}_simple_test PRIVATE 
      ${PROJECT_NAME}
      console_bridge::console_bridge
    )
    add_test(NAME ${PROJECT_NAME}_simple_test COMMAND ${PROJECT_NAME}_simple_test)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/speed_test.cpp)
    add_executable(${PROJECT_NAME}_speed_test test/speed_test.cpp)
    target_link_libraries(${PROJECT_NAME}_speed_test PRIVATE 
      ${PROJECT_NAME}
      console_bridge::console_bridge
    )
    add_test(NAME ${PROJECT_NAME}_speed_test COMMAND ${PROJECT_NAME}_speed_test)
  endif()

  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/test_transform_datatypes.cpp)
    add_executable(${PROJECT_NAME}_transform_datatypes_test test/test_transform_datatypes.cpp)
    target_link_libraries(${PROJECT_NAME}_transform_datatypes_test PRIVATE 
      ${PROJECT_NAME}
      console_bridge::console_bridge
    )
    add_test(NAME ${PROJECT_NAME}_transform_datatypes_test COMMAND ${PROJECT_NAME}_transform_datatypes_test)
  endif()
endif()
