Skip to content

Commit 387bf7b

Browse files
mergify[bot]ottojo
andauthored
add NO_UNDEFINED_SYMBOLS to rclcpp_components_register_node cmake macro (#2746) (#2764)
Signed-off-by: Jonas Otto <[email protected]> Signed-off-by: Jonas Otto <[email protected]> (cherry picked from commit c31daa6) Co-authored-by: Jonas Otto <[email protected]>
1 parent 9db7659 commit 387bf7b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

rclcpp_components/cmake/rclcpp_components_register_node.cmake

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
# :type EXECUTOR: string
2929
# :param RESOURCE_INDEX: the ament resource index to register the components
3030
# :type RESOURCE_INDEX: string
31+
# :param NO_UNDEFINED_SYMBOLS: add linker flags to deny undefined symbols
32+
# :type NO_UNDEFINED_SYMBOLS: option
3133
#
3234
macro(rclcpp_components_register_node target)
33-
cmake_parse_arguments(ARGS "" "PLUGIN;EXECUTABLE;EXECUTOR;RESOURCE_INDEX" "" ${ARGN})
35+
cmake_parse_arguments(ARGS "NO_UNDEFINED_SYMBOLS" "PLUGIN;EXECUTABLE;EXECUTOR;RESOURCE_INDEX" "" ${ARGN})
3436
if(ARGS_UNPARSED_ARGUMENTS)
3537
message(FATAL_ERROR "rclcpp_components_register_node() called with unused "
3638
"arguments: ${ARGS_UNPARSED_ARGUMENTS}")
@@ -67,6 +69,26 @@ macro(rclcpp_components_register_node target)
6769
"${_RCLCPP_COMPONENTS_${resource_index}__NODES}${component};${_path}/$<TARGET_FILE_NAME:${target}>\n")
6870
list(APPEND _RCLCPP_COMPONENTS_PACKAGE_RESOURCE_INDICES ${resource_index})
6971

72+
if(ARGS_NO_UNDEFINED_SYMBOLS AND WIN32)
73+
message(WARNING "NO_UNDEFINED_SYMBOLS is enabled for target \"${target}\", but this is unsupported on windows.")
74+
elseif(ARGS_NO_UNDEFINED_SYMBOLS AND NOT WIN32)
75+
check_cxx_compiler_flag("-Wl,--no-undefined" linker_supports_no_undefined)
76+
if(linker_supports_no_undefined)
77+
target_link_options("${target}" PRIVATE "-Wl,--no-undefined")
78+
else()
79+
message(WARNING "NO_UNDEFINED_SYMBOLS is enabled for target \"${target}\",\
80+
but the linker does not support the \"--no-undefined\" flag.")
81+
endif()
82+
83+
check_cxx_compiler_flag("-Wl,--no-allow-shlib-undefined" linker_supports_no_allow_shlib_undefined)
84+
if(linker_supports_no_allow_shlib_undefined)
85+
target_link_options("${target}" PRIVATE "-Wl,--no-allow-shlib-undefined")
86+
else()
87+
message(WARNING "NO_UNDEFINED_SYMBOLS is enabled for target \"${target}\",\
88+
but the linker does not support the \"--no-allow-shlib-undefined\" flag.")
89+
endif()
90+
endif()
91+
7092
configure_file(${rclcpp_components_NODE_TEMPLATE}
7193
${PROJECT_BINARY_DIR}/rclcpp_components/node_main_configured_${node}.cpp.in)
7294
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/rclcpp_components/node_main_${node}.cpp

0 commit comments

Comments
 (0)