cmake_minimum_required(VERSION 3.10)
project(mkt_plugins 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)

# Build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Find system dependencies
find_package(Boost REQUIRED COMPONENTS system)

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

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

# Dependencies packages (internal libraries)
set(PACKAGES_DIR
    score_algorithm
    robot_nav_2d_msgs
    robot_nav_2d_utils
    nav_core2
    geometry_msgs
    robot_nav_msgs
    robot_std_msgs
    robot_sensor_msgs
    angles
)

###########
## Build ##
###########

# Library 1: goal_checker
add_library(${PROJECT_NAME}_goal_checker SHARED
    src/goal_checker.cpp
    src/simple_goal_checker.cpp
    src/equation_line.cpp
)

target_link_libraries(${PROJECT_NAME}_goal_checker
    PUBLIC ${PACKAGES_DIR}
    PRIVATE Boost::boost
)

set_target_properties(${PROJECT_NAME}_goal_checker PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

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

# Library 3: standard_traj_generator
add_library(${PROJECT_NAME}_standard_traj_generator SHARED
    src/kinematic_parameters.cpp
    src/xy_theta_iterator.cpp
    src/standard_traj_generator.cpp
    src/limited_accel_generator.cpp
)

target_link_libraries(${PROJECT_NAME}_standard_traj_generator
    PUBLIC ${PACKAGES_DIR}
    PRIVATE Boost::boost
)

set_target_properties(${PROJECT_NAME}_standard_traj_generator PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

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

#############
## Install ##
#############

# Cài đặt header files
install(DIRECTORY include/${PROJECT_NAME}/
    DESTINATION include/${PROJECT_NAME}
    FILES_MATCHING PATTERN "*.h"
    PATTERN ".svn" EXCLUDE
)

# Cài đặt libraries
install(TARGETS 
    ${PROJECT_NAME}_standard_traj_generator 
    ${PROJECT_NAME}_goal_checker
    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}
)

# Tùy chọn build
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_TESTS "Build test programs" OFF)

# 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 "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "=================================")
