Files
env_monitor_core/CMakeLists.txt
duongtd 92b9bcc924 feat: IntrusionDetector plugin and EnvContext host services
- env_context.h: non-owning TF buffer, sensor reader and frame names;
  host calls setContext() after initialize()
- IntrusionDetector reads raw LaserScan, looks up TF at the scan stamp
  and flags new points close to the unpadded footprint; a point counts
  only if the reference scan shows that spot was free
- stall detector, env types, golden data and tests updated
2026-11-27 22:06:59 +07:00

294 lines
8.3 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(env_monitor_core VERSION 0.1.0 LANGUAGES CXX)
# Hai chế độ build như recovery_core / action_core: catkin (T800_ws) và CMake standalone (pnkx_nav_core).
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building env_monitor_core with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building env_monitor_core with Standalone CMake")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Boost REQUIRED COMPONENTS
system
filesystem
)
find_package(yaml-cpp REQUIRED)
# tf3 is installed outside catkin, as in move_base2.
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()
find_path(TF3_INCLUDE_DIRS NAMES tf3/LinearMath/Transform.h)
if(NOT TF3_INCLUDE_DIRS)
message(FATAL_ERROR "tf3 headers not found — install tf3 headers before building")
endif()
set(TF3_LIBRARIES ${TF3_LIBRARY})
if(NOT BUILDING_WITH_CATKIN)
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}")
set(PNKX_NAV_CORE_SRC_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/../.."
)
file(GLOB STANDALONE_PACKAGE_INCLUDE_DIRS
LIST_DIRECTORIES true
${PNKX_NAV_CORE_SRC_DIR}/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/include
${PNKX_NAV_CORE_SRC_DIR}/*/*/*/*/include
)
set(STANDALONE_INCLUDE_DIRS
${STANDALONE_PACKAGE_INCLUDE_DIRS}
/usr/local/include
)
# Khi cấu hình từ gốc pnkx_nav_core, các tên này là CMake target và mang theo include dir của chúng.
set(PACKAGES_DIR
robot_time
robot_cpp
robot_sensor_ports
robot_sensor_msgs
)
else()
find_package(catkin REQUIRED COMPONENTS
robot_time
robot_cpp
robot_sensor_ports
robot_sensor_msgs
)
catkin_package(
INCLUDE_DIRS
include
# Chỉ lõi: detector nạp động bằng boost::dll, gói phụ thuộc không được link thẳng vào chúng.
LIBRARIES
env_monitor_core
CATKIN_DEPENDS
robot_time
robot_cpp
robot_sensor_ports
robot_sensor_msgs
DEPENDS
Boost
TF3
)
include_directories(SYSTEM
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
endif()
# Link chung cho lõi và plugin, theo chế độ build.
function(env_monitor_core_wire target)
target_compile_options(${target} PRIVATE -Wall -Wextra)
target_include_directories(${target}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${TF3_INCLUDE_DIRS}
)
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(BUILDING_WITH_CATKIN)
add_dependencies(${target}
${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS}
)
target_link_libraries(${target}
PUBLIC
${catkin_LIBRARIES}
${TF3_LIBRARY}
PRIVATE
Boost::boost
Boost::system
Boost::filesystem
yaml-cpp
${CMAKE_DL_LIBS}
)
else()
target_include_directories(${target}
PRIVATE
${STANDALONE_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
target_link_libraries(${target}
PUBLIC
${PACKAGES_DIR}
${TF3_LIBRARY}
PRIVATE
Boost::boost
Boost::system
Boost::filesystem
yaml-cpp
${CMAKE_DL_LIBS}
)
set_target_properties(${target} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}
BUILD_RPATH
"${CMAKE_BINARY_DIR}"
INSTALL_RPATH
"${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
endfunction()
# ========================================================
# Lõi: host EnvMonitor (phần chung, nạp + gộp detector) và đọc/kiểm tham số. Không chứa detector nào.
# ========================================================
add_library(env_monitor_core SHARED
src/env_monitor.cpp
src/env_monitor_config.cpp
)
env_monitor_core_wire(env_monitor_core)
# ========================================================
# Detector có sẵn — mỗi cái một .so, nạp bằng boost::dll theo `<Type>/library_path` trong YAML.
# Detector của gói khác làm y như vậy: depend env_monitor_core, include env_monitor_core/env_detector.h.
# ========================================================
set(ENV_MONITOR_CORE_PLUGIN_TARGETS
env_monitor_core_intrusion_detector
env_monitor_core_contact_detector
env_monitor_core_stall_detector
env_monitor_core_unreachable_detector
)
foreach(plugin contact stall unreachable intrusion)
set(target env_monitor_core_${plugin}_detector)
add_library(${target} SHARED plugins/${plugin}_detector.cpp)
env_monitor_core_wire(${target})
target_link_libraries(${target} PUBLIC env_monitor_core)
endforeach()
# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
install(
TARGETS
env_monitor_core
${ENV_MONITOR_CORE_PLUGIN_TARGETS}
ARCHIVE DESTINATION
${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION
${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION
${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(
DIRECTORY
include/${PROJECT_NAME}/
DESTINATION
${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING
PATTERN "*.h"
)
else()
install(
TARGETS
env_monitor_core
${ENV_MONITOR_CORE_PLUGIN_TARGETS}
EXPORT
${PROJECT_NAME}-targets
ARCHIVE DESTINATION
lib
LIBRARY DESTINATION
lib
RUNTIME DESTINATION
bin
)
install(
EXPORT
${PROJECT_NAME}-targets
FILE
${PROJECT_NAME}-targets.cmake
NAMESPACE
${PROJECT_NAME}::
DESTINATION
lib/cmake/${PROJECT_NAME}
)
install(
DIRECTORY
include/${PROJECT_NAME}/
DESTINATION
include/${PROJECT_NAME}
FILES_MATCHING
PATTERN "*.h"
)
endif()
# ========================================================
# Tests — OFF mặc định; bật bằng -DBUILD_ENV_MONITOR_CORE_TESTS=ON (chỉ nhánh catkin).
# ========================================================
option(BUILD_ENV_MONITOR_CORE_TESTS "Build env_monitor_core tests" OFF)
if(BUILD_ENV_MONITOR_CORE_TESTS AND BUILDING_WITH_CATKIN AND CATKIN_ENABLE_TESTING)
if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif()
# Plugin chỉ cho test: ABI lệch cố ý, để kiểm EnvMonitor từ chối nạp nó.
add_library(env_monitor_core_test_stale_abi_detector SHARED test/plugins/stale_abi_detector.cpp)
env_monitor_core_wire(env_monitor_core_test_stale_abi_detector)
target_link_libraries(env_monitor_core_test_stale_abi_detector PUBLIC env_monitor_core)
set(ENV_MONITOR_CORE_TESTS
env_monitor_test
env_monitor_golden_test
env_monitor_loading_test
intrusion_detector_test
)
foreach(test_name ${ENV_MONITOR_CORE_TESTS})
catkin_add_gtest(${test_name} test/${test_name}.cpp)
if(TARGET ${test_name})
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make` thường.
set_target_properties(${test_name} PROPERTIES EXCLUDE_FROM_ALL FALSE)
target_compile_options(${test_name} PRIVATE -Wall -Wextra)
target_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test)
target_link_libraries(${test_name}
env_monitor_core
${catkin_LIBRARIES}
yaml-cpp
)
if(test_name STREQUAL "intrusion_detector_test")
target_link_libraries(${test_name} env_monitor_core_intrusion_detector)
endif()
# Detector được nạp qua boost::dll từ devel/lib, đúng đường runtime đi: phải build xong trước khi test chạy.
add_dependencies(${test_name} ${ENV_MONITOR_CORE_PLUGIN_TARGETS} env_monitor_core_test_stale_abi_detector)
target_compile_definitions(${test_name} PRIVATE
ENV_MONITOR_CORE_TEST_LIBRARY_DIR="${CATKIN_DEVEL_PREFIX}/lib"
ENV_MONITOR_CORE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/data"
)
endif()
endforeach()
endif()