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

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

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

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Find dependencies
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}")

  set(PACKAGES_DIR  
    robot_nav_core
    robot_costmap_2d
    robot_cpp
    tf3
  )

  find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED COMPONENTS         
    robot_nav_core
    robot_costmap_2d
    robot_cpp
  )

  find_package(Boost REQUIRED COMPONENTS system thread)
  find_library(TF3_LIBRARY NAMES tf3 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)

  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES ${PROJECT_NAME}
    CATKIN_DEPENDS robot_cpp robot_nav_core robot_costmap_2d 
    DEPENDS Boost
  )

  include_directories(
    include
    ${catkin_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIRS}
    ${TF3_INCLUDE_DIR}
  )
endif()

# Libraries
add_library(${PROJECT_NAME} src/clear_costmap_recovery.cpp)

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

  target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${Boost_LIBRARIES})

else()

  target_link_libraries(${PROJECT_NAME}
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
        ${TF3_LIBRARY}
  )

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

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

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 "*.hpp"
    PATTERN ".svn" EXCLUDE
  )
else()

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

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

  ## Mark cpp header files for installation
  install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
    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 "Libraries: ${PROJECT_NAME}")
  message(STATUS "Dependencies: robot_nav_core, robot_costmap_2d, robot_cpp, Boost")
  message(STATUS "=================================")
endif()
