Files
trolley_detection_action/CMakeLists.txt
duongtd f25a887b91 build: add BUILD_TROLLEY_DETECTION_ACTION_TESTS option, off by default
Tests used to build whenever CATKIN_ENABLE_TESTING was on. They now also
need -DBUILD_TROLLEY_DETECTION_ACTION_TESTS=ON, in both build modes.
2026-11-17 20:36:12 +07:00

198 lines
6.7 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(trolley_detection_action VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ==================================================================================================
# Phát hiện chế độ build — theo mẫu dock_pattern_action, giữ cả hai:
# - catkin (`catkin_make` trong workspace phát triển)
# - standalone (root CMakeLists của pnkx_nav_core, không có catkin)
# ==================================================================================================
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building trolley_detection_action with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building trolley_detection_action with Standalone CMake")
endif()
find_package(Boost REQUIRED)
find_package(Threads REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(Eigen3 REQUIRED)
# tf3 không được build bởi workspace này (cài sẵn ở /usr/local/lib/libtf3.so).
find_library(TF3_LIBRARY
NAMES tf3
PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu
)
if(NOT TF3_LIBRARY)
message(FATAL_ERROR "tf3 not found — install tf3 (/usr/local/lib/libtf3.so) before building")
endif()
if(BUILDING_WITH_CATKIN)
find_package(catkin REQUIRED COMPONENTS
action_core
dock_pattern_detector
robot_line_extraction
robot_sensor_ports
robot_cpp
robot_sensor_msgs
)
catkin_package(
CATKIN_DEPENDS
action_core
dock_pattern_detector
robot_line_extraction
robot_sensor_ports
robot_cpp
robot_sensor_msgs
)
else()
# Root CMakeLists của pnkx_nav_core đã `add_subdirectory` mọi gói trước gói này, nên chỉ cần NÊU
# TÊN TARGET: include của chúng tự lan sang qua interface PUBLIC.
set(TROLLEY_DETECTION_ACTION_STANDALONE_LIBRARIES
action_core
dock_pattern_detector
robot_line_extraction
robot_sensor_ports
robot_cpp
robot_sensor_msgs
)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_RPATH "${CMAKE_BINARY_DIR}")
endif()
# Handler là plugin Boost.DLL, nạp theo khoá `library_path` trong YAML — KHÔNG link thẳng vào ai.
add_library(trolley_detection_action_handler SHARED
plugins/trolley_detection_action_handler.cpp
)
target_compile_options(trolley_detection_action_handler PRIVATE -Wall -Wextra)
# Header của dependency vào dạng SYSTEM: robot_cpp sinh hàng trăm warning riêng của nó — để lẫn vào
# thì warning của chính gói này vô hình. Gắn vào TARGET, không `include_directories()` toàn thư mục.
target_include_directories(trolley_detection_action_handler SYSTEM
PRIVATE
${Boost_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIRS}
)
if(BUILDING_WITH_CATKIN)
target_include_directories(trolley_detection_action_handler SYSTEM
PRIVATE ${catkin_INCLUDE_DIRS})
add_dependencies(trolley_detection_action_handler ${catkin_EXPORTED_TARGETS})
target_link_libraries(trolley_detection_action_handler
PUBLIC
${catkin_LIBRARIES}
PRIVATE
Boost::boost
yaml-cpp
Threads::Threads
${CMAKE_DL_LIBS}
${TF3_LIBRARY}
)
# Model + launch để dựng xe 4 chân có dải phản quang trong Gazebo. Launch tra đường dẫn qua
# $(find trolley_detection_action) nên phải có mặt sau khi install.
install(DIRECTORY sim/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/sim
)
install(TARGETS trolley_detection_action_handler
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
else()
target_link_libraries(trolley_detection_action_handler
PUBLIC
${TROLLEY_DETECTION_ACTION_STANDALONE_LIBRARIES}
PRIVATE
Boost::boost
yaml-cpp
Threads::Threads
${CMAKE_DL_LIBS}
${TF3_LIBRARY}
)
# Plugin được nạp bằng boost::dll theo TÊN FILE, và `PluginLoaderHelper` tra trong thư mục build.
set_target_properties(trolley_detection_action_handler PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
include(GNUInstallDirs)
install(TARGETS trolley_detection_action_handler
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY sim/
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/sim
)
endif()
# Test chạy qua `catkin_add_gtest` và cần `CATKIN_DEVEL_PREFIX` để biết plugin nằm ở đâu, nên chỉ
# có ở nhánh catkin. Ở standalone, plugin vẫn build — chỉ không có test.
# Test mặc định OFF trong cả hai chế độ build; bật bằng -DBUILD_TROLLEY_DETECTION_ACTION_TESTS=ON.
option(BUILD_TROLLEY_DETECTION_ACTION_TESTS "Build the trolley_detection_action unit tests" OFF)
if(BUILD_TROLLEY_DETECTION_ACTION_TESTS AND CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif()
catkin_add_gtest(trolley_detection_action_test test/trolley_detection_action_test.cpp)
if(TARGET trolley_detection_action_test)
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make` thường.
set_target_properties(trolley_detection_action_test PROPERTIES EXCLUDE_FROM_ALL FALSE)
target_compile_options(trolley_detection_action_test PRIVATE -Wall -Wextra)
target_include_directories(trolley_detection_action_test SYSTEM
PRIVATE ${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIRS}
)
# Plugin KHÔNG được link thẳng: nó chỉ export alias Boost.DLL và được nạp qua ActionRegistry,
# đúng đường mà runtime đi.
target_link_libraries(trolley_detection_action_test
${catkin_LIBRARIES}
${Boost_LIBRARIES}
yaml-cpp
pthread
${CMAKE_DL_LIBS}
${TF3_LIBRARY}
)
add_dependencies(trolley_detection_action_test trolley_detection_action_handler)
# ctest không mang theo PNKX_NAV_CORE_CONFIG_DIR hay LD_LIBRARY_PATH của shell.
target_compile_definitions(trolley_detection_action_test PRIVATE
TROLLEY_DETECTION_ACTION_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
TROLLEY_DETECTION_ACTION_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/data"
TROLLEY_DETECTION_ACTION_TEST_LIBRARY_DIR="${CATKIN_DEVEL_PREFIX}/lib"
)
endif()
endif()