### Install Executables Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Installs the container executables to the lib/${PROJECT_NAME} directory. ```cmake # Install executables install( TARGETS component_container component_container_mt component_container_isolated component_container_event RUNTIME DESTINATION lib/${PROJECT_NAME} ) ``` -------------------------------- ### Install Node Main Template Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Installs the node_main.cpp.in template file to the share directory. ```cmake set(node_main_template_install_dir "share/${PROJECT_NAME}") install(FILES src/node_main.cpp.in DESTINATION ${node_main_template_install_dir}) ``` -------------------------------- ### Install Libraries and Exports Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Installs the component and component_manager libraries and creates export rules. ```cmake install( TARGETS component component_manager EXPORT export_${PROJECT_NAME} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin ) ``` -------------------------------- ### Test Dependencies and Setup Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Finds testing packages and sets up environment variables and library directories for tests. ```cmake if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) find_package(ament_cmake_google_benchmark REQUIRED) find_package(benchmark REQUIRED) # Give cppcheck hints about macro definitions coming from outside this package get_target_property(ament_cmake_cppcheck_ADDITIONAL_INCLUDE_DIRS benchmark::benchmark INTERFACE_INCLUDE_DIRECTORIES) ament_lint_auto_find_test_dependencies() set(components "") add_library(test_component SHARED test/components/test_component.cpp) target_link_libraries(test_component PRIVATE component) #rclcpp_components_register_nodes(test_component "test_rclcpp_components::TestComponent") set(components "${components}test_rclcpp_components::TestComponentFoo;$\n") set(components "${components}test_rclcpp_components::TestComponentBar;$\n") set(components "${components}test_rclcpp_components::TestComponentNoNode;$\n") # A properly formed resource only has one ';', this is used to catch an invalid resource entry set(invalid_components "test_rclcpp_components::TestComponentFoo;;$\n") file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$/share/ament_index/resource_index/rclcpp_components/${PROJECT_NAME}" CONTENT "${components}") file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$/share/ament_index/resource_index/rclcpp_components/invalid_${PROJECT_NAME}" CONTENT "${invalid_components}") set(append_library_dirs "${CMAKE_CURRENT_BINARY_DIR}") if(WIN32) set(append_library_dirs "${append_library_dirs}/$") endif() ament_add_gtest(test_component_manager test/test_component_manager.cpp APPEND_ENV AMENT_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$ APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_component_manager) target_link_libraries(test_component_manager component_manager) endif() ament_add_gtest(test_component_manager_api test/test_component_manager_api.cpp APPEND_ENV AMENT_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$ APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_component_manager_api) target_link_libraries(test_component_manager_api component_manager) endif() ament_add_google_benchmark(benchmark_components test/benchmark/benchmark_components.cpp APPEND_ENV AMENT_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$ APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET benchmark_components) target_link_libraries(benchmark_components component_manager) endif() endif() ``` -------------------------------- ### Install Include Directories Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Installs the public include directory for the rclcpp_components package. ```cmake # Install include directories install( DIRECTORY include/ DESTINATION include/${PROJECT_NAME} ) ``` -------------------------------- ### Install Include Directory Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Installs the package's include directory to the build binary directory. This makes headers available during the build process. ```cmake install( DIRECTORY include/ ${CMAKE_CURRENT_BINARY_DIR}/include/ DESTINATION include/${PROJECT_NAME} ) ``` -------------------------------- ### Install rclcpp Library Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Installs the rclcpp library targets, including archives, libraries, and runtime binaries, to their respective destinations. ```cmake install( TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin ) ``` -------------------------------- ### Example of Confusing Errors with Dynamic Typing Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Demonstrates how parameters could previously accept any type, leading to runtime errors and unexpected behavior. ```bash $ ros2 run demo_nodes_py listener & $ ros2 param set /listener use_sim_time not_a_boolean [ERROR] [1614712713.233733147] [listener]: use_sim_time parameter set to something besides a bool Set parameter successful $ ros2 param get /listener use_sim_time String value is: not_a_boolean ``` -------------------------------- ### Find ROS Packages Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/CMakeLists.txt Finds required ROS packages for building and testing. Ensure these packages are installed in your ROS environment. ```cmake find_package(rosidl_default_generators REQUIRED) find_package(test_msgs REQUIRED) ``` -------------------------------- ### Set Public Include Directories Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Configures public include directories for the rclcpp library, making its headers accessible to dependent packages during build and installation. ```cmake target_include_directories(${PROJECT_NAME} PUBLIC "$" "$" "$") ``` -------------------------------- ### Explicitly cast integer to float for double parameters Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/api_review_march_2020.md When passing integer values to double parameters from the command line or YAML files, an explicit cast to a floating-point number is required to avoid a ParameterTypeException. This example shows the correct way to provide a floating-point value. ```bash ros2 run foo_package foo_node --ros-args -p foo_arg:=1.0 ``` -------------------------------- ### Add Google Benchmark for Parameter Client Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Configures a Google benchmark for the parameter client component. It links against the project's main library. ```cmake ament_add_google_benchmark(benchmark_parameter_client benchmark_parameter_client.cpp) if(TARGET benchmark_parameter_client) target_link_libraries(benchmark_parameter_client ${PROJECT_NAME}) endif() ``` -------------------------------- ### Add Build Definitions Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Adds a preprocessor definition for the test resources directory. ```cmake add_definitions(-DTEST_RESOURCES_DIRECTORY="${TEST_RESOURCES_DIRECTORY}") ``` -------------------------------- ### Get Typesupport Target Name Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Retrieves the target name for C++ typesupport libraries, which is necessary for linking against generated interface libraries. ```cmake rosidl_get_typesupport_target(cpp_typesupport_target "${PROJECT_NAME}_test_msgs" "rosidl_typesupport_cpp") ``` -------------------------------- ### Enabling Dynamic Typing for Parameters in Python Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Demonstrates how to enable dynamic typing for a parameter in Python by setting the 'dynamic_typing' field in the descriptor. ```python rcl_interfaces.msg.ParameterDescriptor descriptor; descriptor.dynamic_typing = True; node.declare_parameter("dynamically_typed_parameter", None, descriptor); ``` -------------------------------- ### Enabling Dynamic Typing for Parameters in C++ Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Shows how to set the 'dynamic_typing' field in a ParameterDescriptor to allow a parameter to change its type. ```cpp rcl_interfaces::msg::ParameterDescriptor descriptor; descriptor.dynamic_typing = true; node->declare_parameter("dynamically_typed_parameter", rclcpp::ParameterValue{}, descriptor); ``` -------------------------------- ### Find ament_cmake_google_benchmark Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Finds the ament_cmake_google_benchmark package, which is required for setting up Google benchmarks. ```cmake find_package(ament_cmake_google_benchmark REQUIRED) ``` -------------------------------- ### Add GMock Executable for QoS Event Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Adds a GMock executable for testing QoS event handling. It links the necessary libraries for the test, including the project's own library, mimick, rcutils, rmw, and test_msgs. ```cmake ament_add_gmock_executable(test_qos_event test_qos_event.cpp) if(TARGET test_qos_event) target_link_libraries(test_qos_event ${PROJECT_NAME} mimick rcutils::rcutils rmw::rmw test_msgs::test_msgs) endif() ``` -------------------------------- ### Package Configuration Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_lifecycle/CMakeLists.txt Finalizes the package configuration, performing necessary checks and generating package metadata. ```cmake ament_package() ``` -------------------------------- ### Add Performance Test for Action Client Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_action/test/benchmark/CMakeLists.txt Defines a performance test target for the action client. Ensure the 'performance_test_fixture' package is found and linked. ```cmake add_performance_test( benchmark_action_client benchmark_action_client.cpp TIMEOUT 240) if(TARGET benchmark_action_client) target_link_libraries(benchmark_action_client ${PROJECT_NAME} rclcpp::rclcpp test_msgs::test_msgs) endif() ``` -------------------------------- ### Add Benchmark Init/Shutdown Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the initialization and shutdown of rclcpp components. It links against the project's main library. ```cmake add_performance_test(benchmark_init_shutdown benchmark_init_shutdown.cpp) if(TARGET benchmark_init_shutdown) target_link_libraries(benchmark_init_shutdown ${PROJECT_NAME}) endif() ``` -------------------------------- ### Add Benchmark Client Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the client component. It links against the project's main library and necessary ROS 2 interfaces. ```cmake add_performance_test(benchmark_client benchmark_client.cpp) if(TARGET benchmark_client) target_link_libraries(benchmark_client ${PROJECT_NAME} rcl_interfaces::rcl_interfaces test_msgs::test_msgs) endif() ``` -------------------------------- ### Add GMock Executable for Generic Pub/Sub Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Adds a GMock executable for testing generic publish/subscribe functionality. It links the project's library, rcl, and test_msgs. ```cmake ament_add_gmock_executable(test_generic_pubsub test_generic_pubsub.cpp) if(TARGET test_generic_pubsub) target_link_libraries(test_generic_pubsub ${PROJECT_NAME} rcl::rcl test_msgs::test_msgs) endif() ``` -------------------------------- ### Declaring an Integer Parameter Explicitly in C++ Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Illustrates how to declare a parameter with a specific type (int64_t) without providing a default value, using a template. ```cpp // method signature template Node::declare_parameter(std::string name, rcl_interfaces::msg::ParameterDescriptor = rcl_interfaces::msg::ParameterDescriptor{}); // or alternatively Node::declare_parameter(std::string name, rclcpp::ParameterType type, rcl_interfaces::msg::ParameterDescriptor = rcl_interfaces::msg::ParameterDescriptor{}); // examples node->declare_parameter("my_integer_parameter"); // declare an integer parameter node->declare_parameter("another_integer_parameter", rclcpp::ParameterType::PARAMETER_INTEGER); // another way to do the same ``` -------------------------------- ### Add Benchmark Node Parameters Interface Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the node's parameter interface. It links against the project's main library. ```cmake add_performance_test(benchmark_node_parameters_interface benchmark_node_parameters_interface.cpp) if(TARGET benchmark_node_parameters_interface) target_link_libraries(benchmark_node_parameters_interface ${PROJECT_NAME}) endif() ``` -------------------------------- ### Conditionally Declaring a Parameter in C++ Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Demonstrates how to declare a parameter based on the value of another parameter, ensuring a required parameter is available only when needed. ```cpp auto mode = node->declare_parameter("mode", "modeA"); // "mode" parameter is an string if (mode == "modeB") { node->declare_parameter("param_needed_when_mode_b"); // when "modeB", the user must provide "param_needed_when_mode_b" } ``` -------------------------------- ### Set Test Resources Directory Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/CMakeLists.txt Defines a variable pointing to the directory containing test resources. This is used to access files needed during testing. ```cmake set(TEST_RESOURCES_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/resources") ``` -------------------------------- ### Add GMock Executable for Subscription Content Filter Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Adds a GMock executable for testing subscription content filtering. It links the project's library, mimick, and test_msgs. ```cmake ament_add_gmock_executable(test_subscription_content_filter test_subscription_content_filter.cpp) if(TARGET test_subscription_content_filter) target_link_libraries(test_subscription_content_filter ${PROJECT_NAME} mimick test_msgs::test_msgs) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Subscription Options Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing subscription options. It links against the project's main library. ```cmake ament_add_ros_isolated_gtest(test_subscription_options test_subscription_options.cpp) if(TARGET test_subscription_options) target_link_libraries(test_subscription_options ${PROJECT_NAME}) endif() ``` -------------------------------- ### Export Include Directories and Libraries Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_lifecycle/CMakeLists.txt Exports include directories and libraries for the project. This is typically used for older CMake versions or specific export mechanisms. ```cmake ament_export_include_directories("include/${PROJECT_NAME}") ament_export_libraries(${PROJECT_NAME}) ``` -------------------------------- ### Define Test for Rosout QoS Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a test executable for the rosout QoS component. It links the project's library, rcl, and rmw. ```cmake ament_add_ros_isolated_gtest(test_rosout_qos test_rosout_qos.cpp) if(TARGET test_rosout_qos) target_link_libraries(test_rosout_qos ${PROJECT_NAME} rcl::rcl rmw::rmw) endif() ``` -------------------------------- ### Add Benchmark Node Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the node component. It links against the project's main library. ```cmake add_performance_test(benchmark_node benchmark_node.cpp) if(TARGET benchmark_node) target_link_libraries(benchmark_node ${PROJECT_NAME}) endif() ``` -------------------------------- ### Add Performance Test for Action Server Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_action/test/benchmark/CMakeLists.txt Defines a performance test target for the action server. This benchmark is configured with a timeout and links against the project's main library, rclcpp, and test_msgs. ```cmake add_performance_test( benchmark_action_server benchmark_action_server.cpp TIMEOUT 120) if(TARGET benchmark_action_server) target_link_libraries(benchmark_action_server ${PROJECT_NAME} rclcpp::rclcpp test_msgs::test_msgs) endif() ``` -------------------------------- ### Include Custom CMake Module Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/CMakeLists.txt Includes a custom CMake module for adding build failure tests. This module should be located at the specified path relative to the source directory. ```cmake include(cmake/rclcpp_add_build_failure_test.cmake) ``` -------------------------------- ### Add Subdirectories for Build Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/CMakeLists.txt Adds subdirectories to the build process. This allows for modular build configurations for different parts of the project, such as benchmarks and the main library. ```cmake add_subdirectory(benchmark) add_subdirectory(rclcpp) ``` -------------------------------- ### Add GMock Executable for Adding Callback Groups Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Adds a GMock executable to test the functionality of adding callback groups to an executor. It links the project's library and test_msgs. ```cmake ament_add_gmock_executable(test_add_callback_groups_to_executor test_add_callback_groups_to_executor.cpp) if(TARGET test_add_callback_groups_to_executor) target_link_libraries(test_add_callback_groups_to_executor ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` -------------------------------- ### Find performance_test_fixture Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Finds the performance_test_fixture package, used for creating performance tests. ```cmake find_package(performance_test_fixture REQUIRED) ``` -------------------------------- ### Generate Version Header Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Generates a version header file for the package. This is useful for including version information in the compiled code. ```cmake ament_generate_version_header(${PROJECT_NAME}) ``` -------------------------------- ### Define Component Interface Library Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Defines an interface library for components, specifying include directories and linking against class_loader and rclcpp. ```cmake add_library(component INTERFACE) target_include_directories(component INTERFACE "$" "$") target_link_libraries(component INTERFACE class_loader::class_loader rclcpp::rclcpp) ``` -------------------------------- ### Generate ROSidl Interfaces Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Generates ROSidl interfaces for testing purposes, specifying message files and dependencies. ```cmake rosidl_generate_interfaces(${PROJECT_NAME}_test_msgs ../msg/Header.msg ../msg/LargeMessage.msg ../msg/MessageWithHeader.msg ../msg/String.msg DEPENDENCIES builtin_interfaces LIBRARY_NAME ${PROJECT_NAME} SKIP_INSTALL ) ``` -------------------------------- ### SyncParametersClient::get_parameters error handling Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/api_review_march_2020.md The SyncParametersClient::get_parameters method returns an empty vector on error, which is indistinguishable from a valid response when an empty parameter list is requested. This can lead to ambiguity in detecting error cases. ```cpp rclcpp/parameter_client.cpp ``` -------------------------------- ### Find Python3 Package Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Finds the Python3 package, requiring it and requesting the Interpreter component. ```cmake find_package(Python3 REQUIRED COMPONENTS Interpreter) ``` -------------------------------- ### Add Benchmark Service Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the service component. It links against the project's main library and necessary ROS 2 interfaces. ```cmake add_performance_test(benchmark_service benchmark_service.cpp) if(TARGET benchmark_service) target_link_libraries(benchmark_service ${PROJECT_NAME} rcl_interfaces::rcl_interfaces test_msgs::test_msgs) endif() ``` -------------------------------- ### Generate Node Interface Traits Header Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Generates node interface traits headers by expanding a template file using a Python script. This command is executed via Python3::Interpreter and depends on a watched template file. ```cmake file(GLOB interface_files "include/rclcpp/node_interfaces/node_*_interface.hpp") foreach(interface_file ${interface_files}) get_filename_component(interface_name ${interface_file} NAME_WE) # "watch" template for changes configure_file( "resource/interface_traits.hpp.em" "${CMAKE_CURRENT_BINARY_DIR}/${interface_name}_traits.hpp.em.watch" COPYONLY ) set(python_${interface_name}_traits "import em" "em.invoke(['-D', 'interface_name = \'${interface_name}\'', '-o', 'include/rclcpp/node_interfaces/${interface_name}_traits.hpp', '${CMAKE_CURRENT_SOURCE_DIR}/resource/interface_traits.hpp.em'])") string(REPLACE ";" "$" python_${interface_name}_traits "${python_${interface_name}_traits}") add_custom_command(OUTPUT include/rclcpp/node_interfaces/${interface_name}_traits.hpp COMMAND ${CMAKE_COMMAND} -E make_directory "include/rclcpp/node_interfaces" COMMAND Python3::Interpreter ARGS -c "${python_${interface_name}_traits}" DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${interface_name}_traits.hpp.em.watch" COMMENT "Expanding interface_traits.hpp.em into ${interface_name}_traits.hpp" VERBATIM ) list(APPEND ${PROJECT_NAME}_SRCS include/rclcpp/node_interfaces/${interface_name}_traits.hpp) # "watch" template for changes configure_file( "resource/get_interface.hpp.em" "get_${interface_name}.hpp.em.watch" COPYONLY ) set(python_get_${interface_name} "import em" "em.invoke(['-D', 'interface_name = \'${interface_name}\'', '-o', 'include/rclcpp/node_interfaces/get_${interface_name}.hpp', '${CMAKE_CURRENT_SOURCE_DIR}/resource/get_interface.hpp.em'])") string(REPLACE ";" "$" python_get_${interface_name} "${python_get_${interface_name}}") add_custom_command(OUTPUT include/rclcpp/node_interfaces/get_${interface_name}.hpp COMMAND ${CMAKE_COMMAND} -E make_directory "include/rclcpp/node_interfaces" COMMAND Python3::Interpreter ARGS -c "${python_get_${interface_name}}" DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/get_${interface_name}.hpp.em.watch" COMMENT "Expanding get_interface.hpp.em into get_${interface_file}.hpp" VERBATIM ) list(APPEND ${PROJECT_NAME}_SRCS include/rclcpp/node_interfaces/get_${interface_name}.hpp) endforeach() ``` -------------------------------- ### Define Test for Graph Listener Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a test executable for the graph listener component. It links the project's library and mimick. ```cmake ament_add_ros_isolated_gtest(test_graph_listener test_graph_listener.cpp) ament_add_test_label(test_graph_listener mimick) if(TARGET test_graph_listener) target_link_libraries(test_graph_listener ${PROJECT_NAME} mimick) endif() ``` -------------------------------- ### Find CMake Packages Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Finds required CMake packages for testing and RMw implementation. ```cmake find_package(ament_cmake_gtest REQUIRED) find_package(rmw_implementation_cmake REQUIRED) ``` -------------------------------- ### Deprecated Parameter Declaration in C++ Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Highlights a deprecated method of declaring a parameter by only providing its name, which now generates a build warning. ```cpp node->declare_parameter("my_param"); // this generates a build warning ``` -------------------------------- ### Define Component Container MT Executable Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Defines the component_container_mt executable and links it against the component_manager library and rclcpp. ```cmake add_executable( component_container_mt src/component_container_mt.cpp ) target_link_libraries(component_container_mt PRIVATE component_manager rclcpp::rclcpp) ``` -------------------------------- ### Define Test for Rosout Subscription Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a test executable for the rosout subscription component. It links the project's library and rcl_interfaces. ```cmake ament_add_ros_isolated_gtest(test_rosout_subscription test_rosout_subscription.cpp) if(TARGET test_rosout_subscription) target_link_libraries(test_rosout_subscription ${PROJECT_NAME} rcl_interfaces::rcl_interfaces) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Warmup Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing executor warmup behavior. It includes test_waitable.cpp and links against the project's main library and test_msgs. ```cmake ament_add_ros_isolated_gtest( test_executors_warmup executors/test_executors_warmup.cpp executors/test_waitable.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}" TIMEOUT 180) if(TARGET test_executors_warmup) target_link_libraries(test_executors_warmup ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` -------------------------------- ### Declaring an Integer Parameter Explicitly in Python Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Shows how to declare a parameter with a specific type (INTEGER) without a default value, using the ParameterType enum. ```python # method signature Node.declare_parameter(name: str, param_type: rclpy.Parameter.Type, descriptor: rcl_interfaces.msg.ParameterDescriptor = rcl_interfaces.msg.ParameterDescriptor()) # example node.declare_parameter('my_integer_parameter', rclpy.Parameter.Type.INTEGER); # declare an integer parameter ``` -------------------------------- ### Define Component Container Event Executable Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Defines the component_container_event executable and links it against the component_manager library and rclcpp. ```cmake add_executable( component_container_event src/component_container_event.cpp ) target_link_libraries(component_container_event PRIVATE component_manager rclcpp::rclcpp) ``` -------------------------------- ### Define Component Container Executable Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Defines the component_container executable and links it against the component_manager library and rclcpp. ```cmake add_executable( component_container src/component_container.cpp ) target_link_libraries(component_container PRIVATE component_manager rclcpp::rclcpp) ``` -------------------------------- ### Export Dependencies Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_lifecycle/CMakeLists.txt Declares the package's build-time and link-time dependencies. This ensures that all required packages are available during the build process. ```cmake ament_export_dependencies(lifecycle_msgs rcl rclcpp rcl_interfaces rcl_lifecycle rcutils rosidl_typesupport_cpp) ``` -------------------------------- ### Define Isolated ROS 2 Test with Dependencies Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Use ament_add_ros_isolated_gtest to define a test executable and link it against the main rclcpp library and other necessary packages. This is a common pattern for setting up tests in the rclcpp package. ```cmake ament_add_ros_isolated_gtest( test_allocator_common allocator/test_allocator_common.cpp) if(TARGET test_allocator_common) target_link_libraries(test_allocator_common ${PROJECT_NAME}) endif() ``` ```cmake ament_add_ros_isolated_gtest( test_allocator_deleter allocator/test_allocator_deleter.cpp) if(TARGET test_allocator_deleter) target_link_libraries(test_allocator_deleter ${PROJECT_NAME}) endif() ``` ```cmake ament_add_ros_isolated_gtest( test_exceptions exceptions/test_exceptions.cpp) ament_add_test_label(test_exceptions mimick) if(TARGET test_exceptions) target_link_libraries(test_exceptions ${PROJECT_NAME} mimick) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_allocator_memory_strategy strategies/test_allocator_memory_strategy.cpp) if(TARGET test_allocator_memory_strategy) target_link_libraries(test_allocator_memory_strategy ${PROJECT_NAME} rcpputils::rcpputils test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_message_pool_memory_strategy strategies/test_message_pool_memory_strategy.cpp) if(TARGET test_message_pool_memory_strategy) target_link_libraries(test_message_pool_memory_strategy ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_any_service_callback test_any_service_callback.cpp) if(TARGET test_any_service_callback) target_link_libraries(test_any_service_callback ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_any_subscription_callback test_any_subscription_callback.cpp) if(TARGET test_any_subscription_callback) target_link_libraries(test_any_subscription_callback ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_client test_client.cpp) ament_add_test_label(test_client mimick) if(TARGET test_client) target_link_libraries(test_client ${PROJECT_NAME} mimick rcl_interfaces::rcl_interfaces test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_clock_conditional test_clock_conditional.cpp) ament_add_test_label(test_clock_conditional mimick) if(TARGET test_clock_conditional) target_link_libraries(test_clock_conditional ${PROJECT_NAME} mimick rcl_interfaces::rcl_interfaces test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_copy_all_parameter_values test_copy_all_parameter_values.cpp) if(TARGET test_copy_all_parameter_values) target_link_libraries(test_copy_all_parameter_values ${PROJECT_NAME}) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_create_timer test_create_timer.cpp) if(TARGET test_create_timer) target_link_libraries(test_create_timer ${PROJECT_NAME}) target_include_directories(test_create_timer PRIVATE ./) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_generic_client test_generic_client.cpp) ament_add_test_label(test_generic_client mimick) if(TARGET test_generic_client) target_link_libraries(test_generic_client ${PROJECT_NAME} mimick rcl_interfaces::rcl_interfaces rmw::rmw rosidl_runtime_cpp::rosidl_runtime_cpp rosidl_typesupport_cpp::rosidl_typesupport_cpp test_msgs::test_msgs ) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_generic_service test_generic_service.cpp) ament_add_test_label(test_generic_service mimick) if(TARGET test_generic_service) target_link_libraries(test_generic_service ${PROJECT_NAME} mimick rcl_interfaces::rcl_interfaces rmw::rmw rosidl_runtime_cpp::rosidl_runtime_cpp rosidl_typesupport_cpp::rosidl_typesupport_cpp test_msgs::test_msgs ) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_client_common test_client_common.cpp) ament_add_test_label(test_client_common mimick) if(TARGET test_client_common) target_link_libraries(test_client_common ${PROJECT_NAME} mimick rcl_interfaces::rcl_interfaces rmw::rmw rosidl_runtime_cpp::rosidl_runtime_cpp rosidl_typesupport_cpp::rosidl_typesupport_cpp test_msgs::test_msgs ) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_create_subscription test_create_subscription.cpp) if(TARGET test_create_subscription) target_link_libraries(test_create_subscription ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_expand_topic_or_service_name test_expand_topic_or_service_name.cpp) ament_add_test_label(test_expand_topic_or_service_name mimick) if(TARGET test_expand_topic_or_service_name) target_link_libraries(test_expand_topic_or_service_name ${PROJECT_NAME} mimick rcl::rcl rmw::rmw) endif() ``` ```cmake ament_add_ros_isolated_gtest(test_function_traits test_function_traits.cpp) if(TARGET test_function_traits) target_link_libraries(test_function_traits ${PROJECT_NAME}) endif() ``` ```cmake ament_add_ros_isolated_gtest( test_future_return_code test_future_return_code.cpp) if(TARGET test_future_return_code) target_link_libraries(test_future_return_code ${PROJECT_NAME}) endif() ``` ```cmake ament_add_ros_isolated_gmock(test_intra_process_manager test_intra_process_manager.cpp) ``` -------------------------------- ### Deprecated Parameter Declaration in Python Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Shows a deprecated way to declare a parameter in Python by only specifying the name, which results in a user warning. ```python node.declare_parameter("my_param"); # this generates a python user warning ``` -------------------------------- ### Add ROS Isolated GTest for Subscription Topic Statistics Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing subscription topic statistics. It links against the project's main library, libstatistics_collector, statistics_msgs, and test_msgs. ```cmake ament_add_ros_isolated_gtest(test_subscription_topic_statistics topic_statistics/test_subscription_topic_statistics.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}" ) if(TARGET test_subscription_topic_statistics) target_link_libraries(test_subscription_topic_statistics ${PROJECT_NAME} libstatistics_collector::libstatistics_collector statistics_msgs::statistics_msgs test_msgs::test_msgs ) endif() ``` -------------------------------- ### Configure Python3 Find Behavior Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Sets CMake policies to control how Python 3 is found, preferring system-installed versions and stopping the search after the first match. Requires CMake 3.20 or later. ```cmake cmake_policy(SET CMP0094 NEW) set(Python3_FIND_UNVERSIONED_NAMES FIRST) ``` -------------------------------- ### Synchronous parameter client call without timeout Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/api_review_march_2020.md The SyncParametersClient::set_parameters method does not accept a timeout option. This can lead to indefinite waiting if the service call encounters issues, such as the server going down. ```cpp rclcpp::ParameterClient::set_parameters ``` -------------------------------- ### AsyncParametersClient naming inconsistency Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/api_review_march_2020.md The class name AsyncParametersClient uses a plural form, which is inconsistent with the singular filenames for ParameterService and the typical naming convention. ```cpp rclcpp/parameter_client.hpp ``` -------------------------------- ### Define and Link gtest Target Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/CMakeLists.txt Defines a Google Test target for testing gtest macros and links it to the project's main library. This is used for testing the test utilities themselves. ```cmake ament_add_gtest(test_rclcpp_gtest_macros utils/test_rclcpp_gtest_macros.cpp) if(TARGET test_rclcpp_gtest_macros) target_link_libraries(test_rclcpp_gtest_macros ${PROJECT_NAME}) endif() ``` -------------------------------- ### Invalid Parameter Declaration with Default Value in Python Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Demonstrates an invalid parameter declaration in Python using None as the default value, which now raises an error. ```python node.declare_parameter("my_param", None); # not valid, will raise error ``` -------------------------------- ### Function to Test Across RMW Implementations Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a function that iterates through different RMW implementations and runs specific tests with environment variables set for each implementation. This is crucial for ensuring cross-RMW compatibility. ```cmake function(test_on_all_rmws) set(rmw_implementation_env_var RMW_IMPLEMENTATION=${rmw_implementation}) ament_add_ros_isolated_gmock_test( test_qos_event TEST_NAME test_qos_event${target_suffix} ENV ${rmw_implementation_env_var} ) ament_add_test_label(test_qos_event${target_suffix} mimick) ament_add_ros_isolated_gmock_test( test_generic_pubsub TEST_NAME test_generic_pubsub${target_suffix} ENV ${rmw_implementation_env_var} ) ament_add_ros_isolated_gmock_test( test_add_callback_groups_to_executor TEST_NAME test_add_callback_groups_to_executor${target_suffix} ENV ${rmw_implementation_env_var} TIMEOUT 120 ) ament_add_ros_isolated_gmock_test( test_subscription_content_filter TEST_NAME test_subscription_content_filter${target_suffix} ENV ${rmw_implementation_env_var} TIMEOUT 120 ) ament_add_test_label(test_subscription_content_filter${target_suffix} mimick) endfunction() call_for_each_rmw_implementation(test_on_all_rmws) ``` -------------------------------- ### Define Component Manager Library Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_components/CMakeLists.txt Defines the shared library for the component manager, including public and private include directories and linked libraries. ```cmake add_library( component_manager SHARED src/component_manager.cpp ) target_include_directories(component_manager PUBLIC "$" "$") target_link_libraries(component_manager PUBLIC class_loader::class_loader composition_interfaces::composition_interfaces rclcpp::rclcpp rmw::rmw ) target_link_libraries(component_manager PRIVATE ament_index_cpp::ament_index_cpp rcpputils::rcpputils rcl_interfaces::rcl_interfaces ) target_compile_definitions(component_manager PRIVATE "RCLCPP_COMPONENTS_BUILDING_LIBRARY") ``` -------------------------------- ### Add ROS Isolated GTest for Static Storage Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing static storage policies. It links against the project's main library and test_msgs. ```cmake ament_add_ros_isolated_gtest(test_static_storage wait_set_policies/test_static_storage.cpp) if(TARGET test_static_storage) target_link_libraries(test_static_storage ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Storage Policy Common Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing common storage policy behaviors. It links against the project's main library, mimick, and test_msgs. ```cmake ament_add_ros_isolated_gtest(test_storage_policy_common wait_set_policies/test_storage_policy_common.cpp) ament_add_test_label(test_storage_policy_common mimick) if(TARGET test_storage_policy_common) target_link_libraries(test_storage_policy_common ${PROJECT_NAME} mimick test_msgs::test_msgs) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Dynamic Storage Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing dynamic storage policies. It links against the project's main library and test_msgs. ```cmake ament_add_ros_isolated_gtest(test_dynamic_storage wait_set_policies/test_dynamic_storage.cpp) if(TARGET test_dynamic_storage) target_link_libraries(test_dynamic_storage ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` -------------------------------- ### Define rclcpp Library Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Defines the main rclcpp library using the collected source files. Includes conditional compilation for Windows to silence C++17 deprecation warnings. ```cmake add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS}) # TODO(wjwwood): address all deprecation warnings and then remove this if(WIN32) target_compile_definitions(${PROJECT_NAME} PUBLIC "_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS") endif() ``` -------------------------------- ### Add ROS Isolated GTest for Guard Condition Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing guard conditions. It links against the project's main library and mimick. ```cmake ament_add_ros_isolated_gtest(test_guard_condition test_guard_condition.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}") ament_add_test_label(test_guard_condition mimick) if(TARGET test_guard_condition) target_link_libraries(test_guard_condition ${PROJECT_NAME} mimick) endif() ``` -------------------------------- ### Add Benchmark Executor Test Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/benchmark/CMakeLists.txt Defines a performance test for the executor component. It links against the project's main library and test message types. ```cmake add_performance_test(benchmark_executor benchmark_executor.cpp) if(TARGET benchmark_executor) target_link_libraries(benchmark_executor ${PROJECT_NAME} test_msgs::test_msgs) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Events Queue Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing the events queue. It links against the project's main library. ```cmake ament_add_ros_isolated_gtest(test_events_queue executors/test_events_queue.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_events_queue) target_link_libraries(test_events_queue ${PROJECT_NAME}) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Intra-Process Waitable Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing intra-process waitables. This snippet is incomplete as it lacks the target_link_libraries call. ```cmake ament_add_ros_isolated_gtest(test_intra_process_waitable waitables/test_intra_process_waitable.cpp) if(TARGET test_intra_process_waitable) ``` -------------------------------- ### Invalid Parameter Declaration with Default Value in C++ Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/doc/notes_on_statically_typed_parameters.md Illustrates an invalid parameter declaration in C++ where a parameter is declared with an empty default value, which now throws an exception. ```cpp node->declare_parameter("my_param", rclcpp::ParameterValue{}); // not valid, will throw exception ``` -------------------------------- ### Link Public Libraries Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Specifies public library dependencies for the rclcpp library. These are linked by default and their C++ version is exported as a minimum requirement to consuming packages. ```cmake target_link_libraries(${PROJECT_NAME} PUBLIC builtin_interfaces::builtin_interfaces libstatistics_collector::libstatistics_collector rcl::rcl rcl_interfaces::rcl_interfaces rcl_yaml_param_parser::rcl_yaml_param_parser rcpputils::rcpputils rcutils::rcutils rmw::rmw rosgraph_msgs::rosgraph_msgs rosidl_dynamic_typesupport::rosidl_dynamic_typesupport rosidl_runtime_c::rosidl_runtime_c rosidl_runtime_cpp::rosidl_runtime_cpp rosidl_typesupport_cpp::rosidl_typesupport_cpp statistics_msgs::statistics_msgs tracetools::tracetools ${CMAKE_THREAD_LIBS_INIT} # Note we link public on purpose, as we want to export # the used C++ version as minimum to all consuming packages ament_cmake_ros_core::ament_ros_defaults ) ``` -------------------------------- ### Set Test Properties Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Sets specific properties for tests, such as timeout values. This is done after ament_package() to ensure the test targets are properly defined. ```cmake if(TEST cppcheck) # must set the property after ament_package() set_tests_properties(cppcheck PROPERTIES TIMEOUT 1200) endif() if(TEST cpplint) set_tests_properties(cpplint PROPERTIES TIMEOUT 180) endif() ``` -------------------------------- ### Add ROS Isolated GTest for Wait Set Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/test/rclcpp/CMakeLists.txt Defines a gtest executable for testing wait sets. It links against the project's main library, rcl_interfaces, and test_msgs. ```cmake ament_add_ros_isolated_gtest(test_wait_set test_wait_set.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_wait_set) target_link_libraries(test_wait_set ${PROJECT_NAME} rcl_interfaces::rcl_interfaces test_msgs::test_msgs) endif() ``` -------------------------------- ### Export Dependencies Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp/CMakeLists.txt Declares the package's dependencies on other ROS 2 packages and libraries. This is crucial for build system integration. ```cmake ament_export_dependencies( builtin_interfaces libstatistics_collector rcl rcl_interfaces rcl_yaml_param_parser rcpputils rcutils rmw rosgraph_msgs rosidl_dynamic_typesupport rosidl_runtime_c rosidl_runtime_cpp rosidl_typesupport_cpp statistics_msgs tracetools ) ``` -------------------------------- ### Export Modern CMake Targets Source: https://github.com/ros2/rclcpp/blob/rolling/rclcpp_lifecycle/CMakeLists.txt Exports the main CMake target for the project, allowing other packages to link against it using modern CMake practices. ```cmake ament_export_targets(${PROJECT_NAME}) ```