Tests used to build whenever CATKIN_ENABLE_TESTING was on. They now also need -DBUILD_ROBOT_SENSOR_PORTS_TESTS=ON, in both build modes.
311 lines
8.0 KiB
CMake
311 lines
8.0 KiB
CMake
cmake_minimum_required(VERSION 3.0.2)
|
|
project(robot_sensor_ports VERSION 0.1.0 LANGUAGES CXX)
|
|
|
|
# ========================================================
|
|
# Build mode detection
|
|
# ========================================================
|
|
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
|
|
set(BUILDING_WITH_CATKIN TRUE)
|
|
message(STATUS "Building robot_sensor_ports with Catkin")
|
|
else()
|
|
set(BUILDING_WITH_CATKIN FALSE)
|
|
message(STATUS "Building robot_sensor_ports with Standalone CMake")
|
|
endif()
|
|
|
|
|
|
# ========================================================
|
|
# C++ Standard
|
|
# ========================================================
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
|
|
|
|
# ========================================================
|
|
# Common dependencies
|
|
# ========================================================
|
|
# Boost chỉ dùng phần header (shared_ptr, make_shared, pointer_cast) — không link component nào.
|
|
find_package(Boost REQUIRED)
|
|
|
|
find_package(Threads REQUIRED)
|
|
find_package(yaml-cpp REQUIRED)
|
|
|
|
|
|
# ========================================================
|
|
# Standalone configuration
|
|
# ========================================================
|
|
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}")
|
|
|
|
# Test/robot_sensor_ports -> Test -> AMR_T800. Cây nguồn của nav core nằm ngay dưới đó.
|
|
set(PNKX_NAV_CORE_SRC_DIR
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/../../.."
|
|
)
|
|
|
|
set(WORKSPACE_DEVEL_LIB_DIR
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/../../../../devel/lib"
|
|
)
|
|
|
|
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}
|
|
|
|
/opt/ros/noetic/include
|
|
/usr/local/include
|
|
)
|
|
|
|
if(EXISTS ${WORKSPACE_DEVEL_LIB_DIR})
|
|
link_directories(${WORKSPACE_DEVEL_LIB_DIR})
|
|
endif()
|
|
|
|
link_directories(
|
|
/usr/local/lib
|
|
)
|
|
|
|
# Gói này KHÔNG kéo costmap, tf3 hay boost::dll: nó chỉ cất message và trả con trỏ. Đây là lý do
|
|
# mọi tầng khác phụ thuộc được vào nó mà không tạo vòng.
|
|
set(STANDALONE_LIBRARIES
|
|
robot_cpp
|
|
robot_time
|
|
robot_xmlrpcpp
|
|
)
|
|
|
|
include_directories(
|
|
include
|
|
)
|
|
|
|
include_directories(SYSTEM
|
|
${STANDALONE_INCLUDE_DIRS}
|
|
${Boost_INCLUDE_DIRS}
|
|
)
|
|
|
|
|
|
# ========================================================
|
|
# Catkin configuration
|
|
# ========================================================
|
|
else()
|
|
|
|
find_package(catkin REQUIRED COMPONENTS
|
|
robot_cpp
|
|
robot_time
|
|
robot_sensor_msgs
|
|
robot_xmlrpcpp
|
|
)
|
|
|
|
catkin_package(
|
|
INCLUDE_DIRS
|
|
include
|
|
|
|
LIBRARIES
|
|
robot_sensor_ports
|
|
|
|
CATKIN_DEPENDS
|
|
robot_cpp
|
|
robot_time
|
|
robot_sensor_msgs
|
|
robot_xmlrpcpp
|
|
|
|
DEPENDS
|
|
Boost
|
|
)
|
|
|
|
include_directories(
|
|
include
|
|
)
|
|
|
|
# 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.
|
|
include_directories(SYSTEM
|
|
${catkin_INCLUDE_DIRS}
|
|
${Boost_INCLUDE_DIRS}
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
# ========================================================
|
|
# Library
|
|
# ========================================================
|
|
add_library(robot_sensor_ports SHARED
|
|
src/sensor_type.cpp
|
|
src/sensor_store.cpp
|
|
src/sensor_store_config.cpp
|
|
)
|
|
|
|
target_compile_options(robot_sensor_ports PRIVATE -Wall -Wextra)
|
|
|
|
|
|
if(BUILDING_WITH_CATKIN)
|
|
|
|
add_dependencies(robot_sensor_ports
|
|
${${PROJECT_NAME}_EXPORTED_TARGETS}
|
|
${catkin_EXPORTED_TARGETS}
|
|
)
|
|
|
|
target_include_directories(robot_sensor_ports
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
$<INSTALL_INTERFACE:include>
|
|
|
|
${Boost_INCLUDE_DIRS}
|
|
)
|
|
|
|
target_link_libraries(robot_sensor_ports
|
|
PUBLIC
|
|
${catkin_LIBRARIES}
|
|
|
|
PRIVATE
|
|
Boost::boost
|
|
|
|
yaml-cpp
|
|
|
|
Threads::Threads
|
|
)
|
|
|
|
else()
|
|
|
|
target_include_directories(robot_sensor_ports
|
|
PUBLIC
|
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
$<INSTALL_INTERFACE:include>
|
|
)
|
|
|
|
# SYSTEM phải đứng ngay sau tên target. Include trong cây nguồn phải bọc BUILD_INTERFACE,
|
|
# nếu không install(EXPORT) fail "prefixed in the source directory" khi package nằm in-tree.
|
|
target_include_directories(robot_sensor_ports SYSTEM
|
|
PUBLIC
|
|
"$<BUILD_INTERFACE:${STANDALONE_INCLUDE_DIRS}>"
|
|
${Boost_INCLUDE_DIRS}
|
|
)
|
|
|
|
target_link_libraries(robot_sensor_ports
|
|
PUBLIC
|
|
${STANDALONE_LIBRARIES}
|
|
|
|
PRIVATE
|
|
Boost::boost
|
|
|
|
yaml-cpp
|
|
|
|
Threads::Threads
|
|
)
|
|
|
|
# .so ra root build dir như action_core/mission_adapters, để runtime tìm thấy ở cùng một chỗ.
|
|
set_target_properties(robot_sensor_ports PROPERTIES
|
|
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
BUILD_RPATH "${CMAKE_BINARY_DIR}"
|
|
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
# ========================================================
|
|
# Install
|
|
# ========================================================
|
|
if(BUILDING_WITH_CATKIN)
|
|
|
|
install(TARGETS robot_sensor_ports
|
|
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()
|
|
|
|
include(GNUInstallDirs)
|
|
|
|
install(TARGETS robot_sensor_ports
|
|
EXPORT ${PROJECT_NAME}-targets
|
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
|
)
|
|
|
|
install(EXPORT ${PROJECT_NAME}-targets
|
|
FILE ${PROJECT_NAME}-targets.cmake
|
|
NAMESPACE ${PROJECT_NAME}::
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
|
|
)
|
|
|
|
install(DIRECTORY include/${PROJECT_NAME}/
|
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
|
|
FILES_MATCHING PATTERN "*.h"
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
# ========================================================
|
|
# Tests
|
|
# ========================================================
|
|
# Tests are OFF by default in both build modes; enable with -DBUILD_ROBOT_SENSOR_PORTS_TESTS=ON.
|
|
option(BUILD_ROBOT_SENSOR_PORTS_TESTS "Build the robot_sensor_ports unit tests" OFF)
|
|
|
|
if(BUILD_ROBOT_SENSOR_PORTS_TESTS AND CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
|
|
|
|
if(NOT COMMAND catkin_add_gtest)
|
|
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
|
|
endif()
|
|
|
|
set(ROBOT_SENSOR_PORTS_TESTS
|
|
sensor_type_test
|
|
sensor_store_test
|
|
sensor_store_config_test
|
|
)
|
|
|
|
foreach(test_name ${ROBOT_SENSOR_PORTS_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, đúng quy trình verify của repo (chạy thẳng ./devel/lib/<pkg>/<test>).
|
|
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}/include
|
|
)
|
|
|
|
target_include_directories(${test_name} SYSTEM
|
|
PRIVATE
|
|
${catkin_INCLUDE_DIRS}
|
|
${Boost_INCLUDE_DIRS}
|
|
)
|
|
|
|
# catkin_add_gtest dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
|
|
target_link_libraries(${test_name}
|
|
robot_sensor_ports
|
|
${catkin_LIBRARIES}
|
|
yaml-cpp
|
|
pthread
|
|
)
|
|
|
|
# Đường tới cây config test — ctest không mang theo PNKX_NAV_CORE_CONFIG_DIR của shell.
|
|
target_compile_definitions(${test_name} PRIVATE
|
|
ROBOT_SENSOR_PORTS_TEST_CONFIG_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/config"
|
|
)
|
|
endif()
|
|
endforeach()
|
|
|
|
endif()
|