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

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

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

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}")

  set(PACKAGES_DIR  
    robot_nav_2d_msgs
    robot_geometry_msgs
    robot_nav_msgs
    nav_grid
    robot_nav_core2
    tf3
    robot_tf3_geometry_msgs
    robot_cpp 
    data_convert
  )

else()

# ========================================================
# Catkin specific configuration
# ========================================================
  find_package(catkin REQUIRED COMPONENTS         
    robot_nav_2d_msgs
    robot_geometry_msgs
    robot_nav_msgs
    nav_grid
    robot_nav_core2
    tf3
    robot_tf3_geometry_msgs
    robot_cpp 
  )

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

  catkin_package(
    INCLUDE_DIRS include
    LIBRARIES ${PROJECT_NAME}_conversions ${PROJECT_NAME}_path_ops ${PROJECT_NAME}_polygons ${PROJECT_NAME}_bounds ${PROJECT_NAME}_tf_help ${PROJECT_NAME}
    CATKIN_DEPENDS robot_cpp robot_geometry_msgs robot_nav_2d_msgs robot_nav_msgs nav_grid robot_nav_core2
    DEPENDS console_bridge Boost
  )

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

# Libraries
add_library(${PROJECT_NAME}_conversions src/conversions.cpp)
add_library(${PROJECT_NAME}_path_ops src/path_ops.cpp)
add_library(${PROJECT_NAME}_polygons src/polygons.cpp src/footprint.cpp)
add_library(${PROJECT_NAME}_bounds src/bounds.cpp)
add_library(${PROJECT_NAME}_tf_help src/tf_help.cpp)
add_library(${PROJECT_NAME} INTERFACE)

if(BUILDING_WITH_CATKIN)
  add_dependencies(${PROJECT_NAME}_conversions ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  add_dependencies(${PROJECT_NAME}_path_ops ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  add_dependencies(${PROJECT_NAME}_polygons ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  add_dependencies(${PROJECT_NAME}_bounds ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  add_dependencies(${PROJECT_NAME}_tf_help ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

  target_link_libraries(${PROJECT_NAME}_conversions ${PACKAGES_DIR} ${Boost_LIBRARIES})
  target_link_libraries(${PROJECT_NAME}_path_ops ${PACKAGES_DIR} ${Boost_LIBRARIES})
  target_link_libraries(${PROJECT_NAME}_polygons ${PACKAGES_DIR} ${Boost_LIBRARIES})
  target_link_libraries(${PROJECT_NAME}_bounds ${PACKAGES_DIR} ${Boost_LIBRARIES})
  target_link_libraries(${PROJECT_NAME}_tf_help ${PACKAGES_DIR} ${Boost_LIBRARIES})
  target_link_libraries(${PROJECT_NAME} INTERFACE ${PACKAGES_DIR} ${Boost_LIBRARIES})

else()

  target_link_libraries(${PROJECT_NAME}_conversions
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )
  target_link_libraries(${PROJECT_NAME}_path_ops
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )
  target_link_libraries(${PROJECT_NAME}_polygons
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )
  target_link_libraries(${PROJECT_NAME}_bounds
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )
  target_link_libraries(${PROJECT_NAME}_tf_help
      PUBLIC
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )
  target_link_libraries(${PROJECT_NAME}
      INTERFACE
        ${PACKAGES_DIR}
        ${Boost_LIBRARIES}
  )

  set_target_properties(${PROJECT_NAME}_conversions PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  set_target_properties(${PROJECT_NAME}_path_ops PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  set_target_properties(${PROJECT_NAME}_polygons PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  set_target_properties(${PROJECT_NAME}_bounds PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  set_target_properties(${PROJECT_NAME}_tf_help PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
  )
  # Note: ${PROJECT_NAME} is an INTERFACE library, so it cannot have LIBRARY_OUTPUT_DIRECTORY

  target_include_directories(${PROJECT_NAME}_conversions
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
  )
  target_include_directories(${PROJECT_NAME}_path_ops
      PUBLIC
          $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
          $<INSTALL_INTERFACE:include>
  )
  target_include_directories(${PROJECT_NAME}_polygons
      PUBLIC
          $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
          $<INSTALL_INTERFACE:include>
  )
  target_include_directories(${PROJECT_NAME}_bounds
      PUBLIC
          $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
          $<INSTALL_INTERFACE:include>
  )
  target_include_directories(${PROJECT_NAME}_tf_help
      PUBLIC
          $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
          $<INSTALL_INTERFACE:include>
  )
  target_include_directories(${PROJECT_NAME}
      INTERFACE
          $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
          $<INSTALL_INTERFACE:include>
  )

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} ${PROJECT_NAME}_conversions ${PROJECT_NAME}_path_ops ${PROJECT_NAME}_polygons ${PROJECT_NAME}_bounds ${PROJECT_NAME}_tf_help
  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}/ include/mapbox
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
    PATTERN ".svn" EXCLUDE
  )
else()

  install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_conversions ${PROJECT_NAME}_path_ops ${PROJECT_NAME}_polygons ${PROJECT_NAME}_bounds ${PROJECT_NAME}_tf_help
      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}/ include/mapbox
    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}_conversions, ${PROJECT_NAME}_path_ops, ${PROJECT_NAME}_polygons, ${PROJECT_NAME}_bounds, ${PROJECT_NAME}_tf_help")
  message(STATUS "Dependencies: robot_nav_2d_msgs, robot_geometry_msgs, robot_nav_msgs, nav_grid, robot_nav_core2, tf3, console_bridge, Boost")
  message(STATUS "=================================")
endif()





