Skip to content

Commit

Permalink
Switch nav2_map_server to use modern CMake idioms. (#4557)
Browse files Browse the repository at this point in the history
* Switch nav2_map_server to use modern CMake idioms.

Signed-off-by: Chris Lalancette <[email protected]>

* Make library_name depend on map_io_library_name.

It requires it for proper operation.

Signed-off-by: Chris Lalancette <[email protected]>

---------

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jul 24, 2024
1 parent f34ca63 commit e03013b
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 121 deletions.
201 changes: 106 additions & 95 deletions nav2_map_server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,143 @@ project(nav2_map_server)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake_modules)

find_package(ament_cmake REQUIRED)
find_package(GRAPHICSMAGICKCPP REQUIRED)
find_package(lifecycle_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(nav2_common REQUIRED)
find_package(nav2_msgs REQUIRED)
find_package(nav2_util REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(nav2_msgs REQUIRED)
find_package(yaml_cpp_vendor REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(std_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(nav2_util REQUIRED)
find_package(GRAPHICSMAGICKCPP REQUIRED)
find_package(yaml_cpp_vendor REQUIRED)
find_package(yaml-cpp REQUIRED)

nav2_package()

include_directories(include)

set(map_server_executable map_server)

set(library_name ${map_server_executable}_core)

set(map_io_library_name map_io)

set(map_saver_cli_executable map_saver_cli)

set(map_saver_server_executable map_saver_server)

set(costmap_filter_info_server_executable costmap_filter_info_server)

add_executable(${map_server_executable}
src/map_server/main.cpp)

add_executable(${map_saver_cli_executable}
src/map_saver/main_cli.cpp)

add_executable(${map_saver_server_executable}
src/map_saver/main_server.cpp)

add_executable(${costmap_filter_info_server_executable}
src/costmap_filter_info/main.cpp)

set(map_io_library_name map_io)

set(library_name ${map_server_executable}_core)

add_library(${map_io_library_name} SHARED
src/map_mode.cpp
src/map_io.cpp)
target_include_directories(${map_io_library_name}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_include_directories(${map_io_library_name}
PRIVATE
${GRAPHICSMAGICKCPP_INCLUDE_DIRS})
target_link_libraries(${map_io_library_name} PUBLIC
nav2_util::nav2_util_core
${nav_msgs_TARGETS}
)
target_link_libraries(${map_io_library_name} PRIVATE
${GRAPHICSMAGICKCPP_LIBRARIES}
tf2::tf2
yaml-cpp::yaml-cpp
)

add_library(${library_name} SHARED
src/map_server/map_server.cpp
src/map_saver/map_saver.cpp
src/costmap_filter_info/costmap_filter_info_server.cpp)
target_include_directories(${library_name}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_link_libraries(${library_name} PUBLIC
${map_io_library_name}
${nav_msgs_TARGETS}
${nav2_msgs_TARGETS}
nav2_util::nav2_util_core
rclcpp::rclcpp
rclcpp_lifecycle::rclcpp_lifecycle
)
target_link_libraries(${library_name} PRIVATE
${lifecycle_msgs_TARGETS}
rclcpp_components::component
yaml-cpp::yaml-cpp
)

set(map_io_dependencies
yaml_cpp_vendor
nav_msgs
nav2_util
tf2)

set(map_server_dependencies
rclcpp
rclcpp_lifecycle
rclcpp_components
nav_msgs
nav2_msgs
yaml_cpp_vendor
std_msgs
nav2_util)

set(map_saver_dependencies
rclcpp
rclcpp_lifecycle
nav_msgs
nav2_msgs
nav2_util)

ament_target_dependencies(${map_server_executable}
${map_server_dependencies})

ament_target_dependencies(${map_saver_cli_executable}
${map_saver_dependencies})

ament_target_dependencies(${map_saver_server_executable}
${map_saver_dependencies})

ament_target_dependencies(${costmap_filter_info_server_executable}
${map_saver_dependencies})

ament_target_dependencies(${library_name}
${map_server_dependencies})

ament_target_dependencies(${map_io_library_name}
${map_io_dependencies})

target_link_libraries(${library_name}
${map_io_library_name})

target_link_libraries(${map_server_executable}
${library_name})

if(WIN32)
target_compile_definitions(${map_server_executable} PRIVATE
YAML_CPP_DLL)
endif()

target_link_libraries(${map_saver_cli_executable}
${library_name})

target_link_libraries(${map_saver_server_executable}
${library_name})

target_link_libraries(${costmap_filter_info_server_executable}
${library_name})
add_executable(${map_server_executable}
src/map_server/main.cpp)
target_include_directories(${map_server_executable}
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_link_libraries(${map_server_executable} PRIVATE
${library_name}
${map_io_library_name}
rclcpp::rclcpp
)

target_include_directories(${map_io_library_name} SYSTEM PRIVATE
${GRAPHICSMAGICKCPP_INCLUDE_DIRS})
add_executable(${map_saver_cli_executable}
src/map_saver/main_cli.cpp)
target_include_directories(${map_saver_cli_executable}
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_link_libraries(${map_saver_cli_executable} PRIVATE
${library_name}
${map_io_library_name}
${nav_msgs_TARGETS}
${nav2_msgs_TARGETS}
nav2_util::nav2_util_core
rclcpp::rclcpp
rclcpp_lifecycle::rclcpp_lifecycle
)

target_link_libraries(${map_io_library_name}
${GRAPHICSMAGICKCPP_LIBRARIES}
yaml-cpp::yaml-cpp)
add_executable(${map_saver_server_executable}
src/map_saver/main_server.cpp)
target_include_directories(${map_saver_server_executable}
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_link_libraries(${map_saver_server_executable} PRIVATE
${library_name}
${map_io_library_name}
${nav_msgs_TARGETS}
${nav2_msgs_TARGETS}
nav2_util::nav2_util_core
rclcpp::rclcpp
rclcpp_lifecycle::rclcpp_lifecycle
)

if(WIN32)
target_compile_definitions(${map_io_library_name} PRIVATE
YAML_CPP_DLL)
endif()
add_executable(${costmap_filter_info_server_executable}
src/costmap_filter_info/main.cpp)
target_include_directories(${costmap_filter_info_server_executable}
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
target_link_libraries(${costmap_filter_info_server_executable} PRIVATE
${library_name}
${map_io_library_name}
${nav_msgs_TARGETS}
${nav2_msgs_TARGETS}
nav2_util::nav2_util_core
rclcpp::rclcpp
rclcpp_lifecycle::rclcpp_lifecycle
)

rclcpp_components_register_nodes(${library_name} "nav2_map_server::CostmapFilterInfoServer")
rclcpp_components_register_nodes(${library_name} "nav2_map_server::MapSaver")
rclcpp_components_register_nodes(${library_name} "nav2_map_server::MapServer")

install(TARGETS
${library_name} ${map_io_library_name}
EXPORT ${library_name}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
Expand All @@ -143,7 +151,7 @@ install(TARGETS
RUNTIME DESTINATION lib/${PROJECT_NAME})

install(DIRECTORY include/
DESTINATION include/)
DESTINATION include/${PROJECT_NAME})

install(DIRECTORY launch DESTINATION share/${PROJECT_NAME})

Expand All @@ -154,6 +162,7 @@ if(BUILD_TESTING)
ament_lint_auto_find_test_dependencies()

find_package(ament_cmake_gtest REQUIRED)
ament_find_gtest()
add_subdirectory(test)
endif()

Expand All @@ -162,5 +171,7 @@ ament_export_libraries(
${library_name}
${map_io_library_name}
)
ament_export_dependencies(${map_io_dependencies} ${map_server_dependencies} yaml-cpp)
ament_export_dependencies(nav_msgs nav2_msgs nav2_util rclcpp rclcpp_lifecycle)
ament_export_targets(${library_name})

ament_package()
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/state.hpp"
#include "nav2_util/lifecycle_node.hpp"
#include "nav2_msgs/msg/costmap_filter_info.hpp"

Expand Down
3 changes: 2 additions & 1 deletion nav2_map_server/include/nav2_map_server/map_saver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#ifndef NAV2_MAP_SERVER__MAP_SAVER_HPP_
#define NAV2_MAP_SERVER__MAP_SAVER_HPP_

#include <string>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/state.hpp"
#include "nav2_util/lifecycle_node.hpp"
#include "nav2_msgs/srv/save_map.hpp"

Expand Down
6 changes: 3 additions & 3 deletions nav2_map_server/include/nav2_map_server/map_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
#ifndef NAV2_MAP_SERVER__MAP_SERVER_HPP_
#define NAV2_MAP_SERVER__MAP_SERVER_HPP_

#include <string>
#include <memory>
#include <functional>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "nav2_util/lifecycle_node.hpp"
#include "nav_msgs/msg/occupancy_grid.hpp"
#include "nav_msgs/srv/get_map.hpp"
#include "nav2_msgs/srv/load_map.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/state.hpp"

namespace nav2_map_server
{
Expand Down
1 change: 1 addition & 0 deletions nav2_map_server/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<depend>nav2_msgs</depend>
<depend>nav2_util</depend>
<depend>graphicsmagick</depend>
<depend>lifecycle_msgs</depend>

<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
Expand Down
1 change: 1 addition & 0 deletions nav2_map_server/src/map_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "nav2_util/geometry_utils.hpp"

#include "yaml-cpp/yaml.h"

#include "tf2/LinearMath/Matrix3x3.h"
#include "tf2/LinearMath/Quaternion.h"
#include "nav2_util/occ_grid_values.hpp"
Expand Down
20 changes: 14 additions & 6 deletions nav2_map_server/test/component/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
include_directories(${PROJECT_SOURCE_DIR}/test)

# map_server component test
ament_add_gtest_executable(test_map_server_node
test_map_server_node.cpp
${PROJECT_SOURCE_DIR}/test/test_constants.cpp
)
ament_target_dependencies(test_map_server_node rclcpp nav_msgs)
target_include_directories(test_map_server_node
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/test>")
target_link_libraries(test_map_server_node
rclcpp::rclcpp
${nav_msgs_TARGETS}
${library_name}
)

Expand All @@ -25,17 +27,23 @@ ament_add_gtest_executable(test_map_saver_node
test_map_saver_node.cpp
${PROJECT_SOURCE_DIR}/test/test_constants.cpp
)

ament_target_dependencies(test_map_saver_node rclcpp nav_msgs)
target_include_directories(test_map_saver_node
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/test>")
target_link_libraries(test_map_saver_node
rclcpp::rclcpp
${nav_msgs_TARGETS}
${library_name}
${map_io_library_name}
)

add_executable(test_map_saver_publisher
test_map_saver_publisher.cpp
${PROJECT_SOURCE_DIR}/test/test_constants.cpp
)

target_include_directories(test_map_saver_publisher
PRIVATE
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/test>")
target_link_libraries(test_map_saver_publisher
${map_io_library_name}
)
Expand Down
7 changes: 2 additions & 5 deletions nav2_map_server/test/map_saver_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
include_directories(${PROJECT_SOURCE_DIR}/test)

# map_saver CLI
ament_add_gtest(test_map_saver_cli
test_map_saver_cli.cpp
${PROJECT_SOURCE_DIR}/test/test_constants.cpp
)

ament_target_dependencies(test_map_saver_cli rclcpp nav_msgs)
target_link_libraries(test_map_saver_cli
${dependencies}
rclcpp::rclcpp
${nav_msgs_TARGETS}
)
6 changes: 3 additions & 3 deletions nav2_map_server/test/map_saver_cli/test_map_saver_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ TEST(MapSaverCLI, CLITest)
std::string(
"ros2 run nav2_map_server map_saver_cli "
"-t map_failure --occ 100 --free 2 --mode trinary --fmt png -f ") + file_path +
std::string("--ros-args __node:=map_saver_test_node");
std::string(" --ros-args --remap __node:=map_saver_test_node");
return_code = system(command.c_str());
EXPECT_EQ(return_code, 65280);
EXPECT_EQ(return_code, 256);

rclcpp::Rate(0.25).sleep();

Expand Down Expand Up @@ -140,7 +140,7 @@ TEST(MapSaverCLI, CLITest)

command =
std::string(
"ros2 run nav2_map_server map_saver_cli --ros-args -r __node:=map_saver_test_node");
"ros2 run nav2_map_server map_saver_cli --ros-args --remap __node:=map_saver_test_node");
return_code = system(command.c_str());
EXPECT_EQ(return_code, 0);
}
Loading

0 comments on commit e03013b

Please sign in to comment.