cmake_minimum_required(VERSION 3.10)
project(nav_c_api VERSION 1.0.0 LANGUAGES CXX)

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

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

# Find Boost (filesystem needed for plugin path / boost::dll usage)
find_package(Boost REQUIRED COMPONENTS filesystem system)

# Dependencies (robot_nav_2d_utils_conversions provides poseStampedToPose2D used by move_base_core/navigation.h)
set(PACKAGES_DIR
    move_base_core
    robot_nav_2d_utils
    tf3
    robot_time
    robot_cpp
    geometry_msgs
    angles
    data_convert
    robot_protocol_msgs
)

# Thư mục include
include_directories(
    ${PROJECT_SOURCE_DIR}/include
)

# Tìm tất cả file source
file(GLOB SOURCES 
    "src/*.cpp" 
    "src/std_msgs/*.cpp" 
    "src/sensor_msgs/*.cpp" 
    "src/geometry_msgs/*.cpp"
    "src/nav_msgs/*.cpp"
    "src/nav_2d_msgs/*.cpp"
    "src/robot_nav_msgs/*.cpp"
    "src/robot_geometry_msgs/*.cpp"
    "src/map_msgs/*.cpp"
)
file(GLOB HEADERS 
    "include/*.h" 
    "include/std_msgs/*.h" 
    "include/sensor_msgs/*.h" 
    "include/geometry_msgs/*.h"
    "include/nav_msgs/*.h"
    "include/nav_2d_msgs/*.h"
    "include/robot_nav_msgs/*.h"
    "include/robot_geometry_msgs/*.h"
    "include/map_msgs/*.h"
)

# Tạo thư viện shared (.so)
add_library(nav_c_api SHARED ${SOURCES} ${HEADERS})

# Link libraries
target_link_libraries(nav_c_api
    PUBLIC
        ${PACKAGES_DIR}
    PRIVATE
        Boost::boost
        Boost::filesystem
        Boost::system
        dl
)

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

# Set include directories
target_include_directories(nav_c_api
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)

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

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

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

# 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 "=================================")

