cmake_minimum_required(VERSION 3.10)

# Tên dự án
project(nav_core_adapter VERSION 1.0.0 LANGUAGES CXX)

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

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

set(PACKAGES_DIR geometry_msgs robot_std_msgs nav_core nav_core2 robot_nav_2d_utils pthread)

find_package(Boost REQUIRED COMPONENTS filesystem system)

# Thư mục include
include_directories(
        ${PROJECT_SOURCE_DIR}/include
)
# Tạo thư viện shared (.so)
add_library(costmap_adapter src/costmap_adapter.cpp)
target_link_libraries(costmap_adapter PRIVATE ${PACKAGES_DIR})
target_include_directories(costmap_adapter PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

add_library(local_planner_adapter src/local_planner_adapter.cpp)
target_link_libraries(local_planner_adapter 
  PRIVATE
    Boost::filesystem
    Boost::system
    dl               
    costmap_adapter 
    robot_cpp
    ${PACKAGES_DIR}
)
target_include_directories(local_planner_adapter PRIVATE
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

add_library(global_planner_adapter src/global_planner_adapter.cpp)
target_link_libraries(global_planner_adapter 
  PRIVATE
    Boost::filesystem
    Boost::system
    dl               
    costmap_adapter 
    ${PACKAGES_DIR}
)

target_include_directories(global_planner_adapter PRIVATE
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

# Ensure runtime loader prefers in-tree adapter libs over system/ROS libs.
# Without this, dlopen() may resolve libcostmap_adapter.so from /opt/ros/noetic/lib,
# which has a different ABI (Costmap2DROBOT) and causes missing symbols.
set(_NAV_CORE_ADAPTER_RPATH
    "${CMAKE_BINARY_DIR}/src/Navigations/Cores/nav_core_adapter"
    "${CMAKE_BINARY_DIR}/src/Libraries/robot_costmap_2d"
    "${CMAKE_BINARY_DIR}/src/Libraries/tf3"
    "${CMAKE_BINARY_DIR}/src/Libraries/robot_time"
    "${CMAKE_BINARY_DIR}/src/Libraries/robot_cpp"
)

set_target_properties(costmap_adapter PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_target_properties(local_planner_adapter PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_target_properties(global_planner_adapter PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

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

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

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

# 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 "Headers found:")
foreach(hdr ${HEADERS})
    message(STATUS "  - ${hdr}")
endforeach()
message(STATUS "=================================")