build: add the standalone branch, and scope the include paths to the target

find_package(catkin REQUIRED ...) sat at top level, so a build without catkin
died at configure time, pointing at packages that are present in the tree. What
is absent is the <pkg>Config.cmake files only a catkin workspace produces.

Standalone names the CMake targets directly; the root CMakeLists adds every
dependency before this package, so their include directories arrive through
each target's PUBLIC interface.

The plugin goes to the top of the build tree in standalone mode. boost::dll
loads it by filename and PluginLoaderHelper resolves that against the build
directory, so a plugin left in its own subdirectory builds and is then
invisible at runtime.

The SYSTEM include paths moved from a directory-wide include_directories() onto
the target. This package also builds a test target, and a directory-wide path
lands on every target in it whether or not that target should see it.

sim/ is installed in both modes. The launch files resolve it through
$(find dock_pattern_action) under catkin; standalone has no catkin layout, so
it goes under the data directory instead.
This commit is contained in:
2026-08-27 10:37:37 +07:00
parent 1a961b8c62
commit 302dd113f3

View File

@@ -5,20 +5,30 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
# ==================================================================================================
# Phát hiện chế độ build
#
# Gói này build được ở HAI chế độ và phải giữ cả hai:
# - catkin (`catkin_make` trong T800_ws)
# - standalone (root CMakeLists của pnkx_nav_core, không có catkin)
#
# Trước đây chỉ có nhánh catkin, và `find_package(catkin REQUIRED ...)` nằm ở cấp cao nhất nên
# build standalone chết ngay lúc configure, với thông báo chỉ vào một gói ĐANG CÓ trong cây —
# thứ thiếu là file `<pkg>Config.cmake` mà chỉ workspace catkin mới sinh ra.
# ==================================================================================================
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building dock_pattern_action with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building dock_pattern_action with Standalone CMake")
endif()
find_package(Boost REQUIRED) find_package(Boost REQUIRED)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(yaml-cpp REQUIRED) find_package(yaml-cpp REQUIRED)
find_package(Eigen3 REQUIRED) find_package(Eigen3 REQUIRED)
find_package(catkin REQUIRED COMPONENTS
action_core
dock_pattern_detector
robot_line_extraction
robot_sensor_ports
robot_cpp
robot_sensor_msgs
)
# tf3 không được build bởi workspace này (cài sẵn ở /usr/local/lib/libtf3.so). # tf3 không được build bởi workspace này (cài sẵn ở /usr/local/lib/libtf3.so).
find_library(TF3_LIBRARY find_library(TF3_LIBRARY
NAMES tf3 NAMES tf3
@@ -29,7 +39,18 @@ if(NOT TF3_LIBRARY)
message(FATAL_ERROR "tf3 not found — install tf3 (/usr/local/lib/libtf3.so) before building") message(FATAL_ERROR "tf3 not found — install tf3 (/usr/local/lib/libtf3.so) before building")
endif() endif()
catkin_package( 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 CATKIN_DEPENDS
action_core action_core
dock_pattern_detector dock_pattern_detector
@@ -37,15 +58,28 @@ catkin_package(
robot_sensor_ports robot_sensor_ports
robot_cpp robot_cpp
robot_sensor_msgs robot_sensor_msgs
) )
# Header của dependency vào dạng SYSTEM: gói này build với -Wall -Wextra, mà robot_cpp sinh hàng else()
# 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.
include_directories(SYSTEM # Ở chế độ standalone không có catkin để tra gói. Root CMakeLists của pnkx_nav_core đã
${catkin_INCLUDE_DIRS} # `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
${Boost_INCLUDE_DIRS} # sang qua interface PUBLIC, không phải khai lại đường dẫn.
${EIGEN3_INCLUDE_DIRS} set(DOCK_PATTERN_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. # Handler là plugin Boost.DLL, nạp theo khoá `library_path` trong YAML — KHÔNG link thẳng vào ai.
add_library(dock_pattern_action_handler SHARED add_library(dock_pattern_action_handler SHARED
@@ -54,9 +88,25 @@ add_library(dock_pattern_action_handler SHARED
target_compile_options(dock_pattern_action_handler PRIVATE -Wall -Wextra) target_compile_options(dock_pattern_action_handler PRIVATE -Wall -Wextra)
add_dependencies(dock_pattern_action_handler ${catkin_EXPORTED_TARGETS}) # Header của dependency vào dạng SYSTEM: gói này build với -Wall -Wextra, mà 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 chứ không phải `include_directories()` toàn thư mục: gói này còn có target test,
# và một đường include toàn cục thì mọi target đều dính, kể cả những cái không nên.
target_include_directories(dock_pattern_action_handler SYSTEM
PRIVATE
${Boost_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIRS}
)
target_link_libraries(dock_pattern_action_handler if(BUILDING_WITH_CATKIN)
target_include_directories(dock_pattern_action_handler SYSTEM
PRIVATE ${catkin_INCLUDE_DIRS})
add_dependencies(dock_pattern_action_handler ${catkin_EXPORTED_TARGETS})
target_link_libraries(dock_pattern_action_handler
PUBLIC PUBLIC
${catkin_LIBRARIES} ${catkin_LIBRARIES}
PRIVATE PRIVATE
@@ -65,21 +115,59 @@ target_link_libraries(dock_pattern_action_handler
Threads::Threads Threads::Threads
${CMAKE_DL_LIBS} ${CMAKE_DL_LIBS}
${TF3_LIBRARY} ${TF3_LIBRARY}
) )
# Model + launch để dựng tấm chữ V trong Gazebo. Không phải build artifact, nhưng launch file tra # Model + launch để dựng tấm chữ V / máng cong trong Gazebo. Không phải build artifact, nhưng
# đường dẫn qua $(find dock_pattern_action) nên phải có mặt sau khi install. # launch file tra đường dẫn qua $(find dock_pattern_action) nên phải có mặt sau khi install.
install(DIRECTORY sim/ install(DIRECTORY sim/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/sim DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/sim
) )
install(TARGETS dock_pattern_action_handler install(TARGETS dock_pattern_action_handler
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
) )
if(CATKIN_ENABLE_TESTING) else()
target_link_libraries(dock_pattern_action_handler
PUBLIC
${DOCK_PATTERN_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.
# Đổ .so về gốc build như mọi plugin khác của cây standalone, nếu không runtime không thấy nó.
set_target_properties(dock_pattern_action_handler PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
include(GNUInstallDirs)
install(TARGETS dock_pattern_action_handler
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Sim asset đi cùng gói ở cả hai chế độ; chỉ khác đích đến vì standalone không có layout catkin.
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.
if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
if(NOT COMMAND catkin_add_gtest) if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND") message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif() endif()