Skip to content

Commit

Permalink
Set symbol visibility to hidden for rcl (#391)
Browse files Browse the repository at this point in the history
* Set symbol visibility to hidden for rcl

Enabling symbol visibility feature in gcc and clang compilers.
This will hep find symbol export related issues in linux and
potentially reduce compile times.

Discourse topic link:
https://discourse.ros.org/t/set-symbol-visibility-to-hidden-for-rmw-and-rcl-packages/7981

Signed-off-by: Sachin Suresh Bhat <bhatsach@amazon.com>

* Remove WIN specific compiler definition in configure_rcl

Signed-off-by: Sachin Suresh Bhat <bhatsach@amazon.com>

* Rename macro name rcl_set_symbol_visibility_hidden

Signed-off-by: Sachin Suresh Bhat <bhatsach@amazon.com>

* Change macro to args for rcl_set_symbol_visibility_hidden

Signed-off-by: Sachin Suresh Bhat <bhatsach@amazon.com>
  • Loading branch information
bhatsach authored and wjwwood committed Mar 6, 2019
1 parent b5cdc49 commit 2ff5df0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ find_package(rosidl_generator_c REQUIRED)
find_package(tinydir_vendor REQUIRED)

include_directories(include)

include(cmake/rcl_set_symbol_visibility_hidden.cmake)
include(cmake/get_default_rcl_logging_implementation.cmake)
get_default_rcl_logging_implementation(RCL_LOGGING_IMPL)

Expand Down Expand Up @@ -73,6 +73,7 @@ ament_target_dependencies(${PROJECT_NAME}
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(${PROJECT_NAME} PRIVATE "RCL_BUILDING_DLL")
rcl_set_symbol_visibility_hidden(${PROJECT_NAME} LANGUAGE "C")

install(
TARGETS ${PROJECT_NAME}
Expand Down Expand Up @@ -105,8 +106,12 @@ if(BUILD_TESTING)
add_subdirectory(test)
endif()

ament_package()
ament_package(CONFIG_EXTRAS "rcl-extras.cmake")

install(
DIRECTORY cmake
DESTINATION share/${PROJECT_NAME}
)
install(
DIRECTORY include/
DESTINATION include
Expand Down
67 changes: 67 additions & 0 deletions rcl/cmake/rcl_set_symbol_visibility_hidden.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2019 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Configures ros client library with custom settings.
# The custom settings are all related to library symbol visibility, see:
# https://gcc.gnu.org/wiki/Visibility
# http://www.ibm.com/developerworks/aix/library/au-aix-symbol-visibility/
#
# Below code is heavily referenced from a similar functionality in rmw:
# https://github.com/ros2/rmw/blob/master/rmw/cmake/configure_rmw_library.cmake
#
# :param library_target: the library target
# :type library_target: string
# :param LANGUAGE: Optional flag for the language of the library.
# Allowed values are "C" and "CXX". The default is "CXX".
# :type LANGUAGE: string
#
# @public
#
function(rcl_set_symbol_visibility_hidden library_target)
cmake_parse_arguments(ARG "" "LANGUAGE" "" ${ARGN})
if(ARG_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "rcl_set_symbol_visibility_hidden() called with unused arguments: ${ARG_UNPARSED_ARGUMENTS}")
endif()

if(NOT ARG_LANGUAGE)
set(ARG_LANGUAGE "CXX")
endif()

if(ARG_LANGUAGE STREQUAL "C")
# Set the visibility to hidden by default if possible
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
# Set the visibility of symbols to hidden by default for gcc and clang
# (this is already the default on Windows)
set_target_properties(${library_target}
PROPERTIES
COMPILE_FLAGS "-fvisibility=hidden"
)
endif()

elseif(ARG_LANGUAGE STREQUAL "CXX")
# Set the visibility to hidden by default if possible
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Set the visibility of symbols to hidden by default for gcc and clang
# (this is already the default on Windows)
set_target_properties(${library_target}
PROPERTIES
COMPILE_FLAGS "-fvisibility=hidden -fvisibility-inlines-hidden"
)
endif()

else()
message(FATAL_ERROR "rcl_set_symbol_visibility_hidden() called with unsupported LANGUAGE: '${ARG_LANGUAGE}'")
endif()
endfunction()
15 changes: 15 additions & 0 deletions rcl/rcl-extras.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2019 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include("${rcl_DIR}/rcl_set_symbol_visibility_hidden.cmake")
2 changes: 2 additions & 0 deletions rcl_action/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ ament_target_dependencies(${PROJECT_NAME}
"rmw"
"rosidl_generator_c"
)

rcl_set_symbol_visibility_hidden(${PROJECT_NAME} LANGUAGE "C")
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(${PROJECT_NAME} PRIVATE "RCL_ACTION_BUILDING_DLL")
Expand Down
1 change: 1 addition & 0 deletions rcl_lifecycle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ament_target_dependencies(rcl_lifecycle
"rcutils"
)

rcl_set_symbol_visibility_hidden(${PROJECT_NAME} LANGUAGE "C")
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(rcl_lifecycle PRIVATE "RCL_LIFECYCLE_BUILDING_DLL")
Expand Down
1 change: 1 addition & 0 deletions rcl_yaml_param_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_library(
${rcl_yaml_parser_sources})
ament_target_dependencies(${PROJECT_NAME} "yaml" "rcutils" "rcl")

rcl_set_symbol_visibility_hidden(${PROJECT_NAME} LANGUAGE "C")
# Causes the visibility macros to use dllexport rather than dllimport,
# which is appropriate when building the dll but not consuming it.
target_compile_definitions(${PROJECT_NAME} PRIVATE "RCL_YAML_PARAM_PARSER_BUILDING_DLL")
Expand Down

0 comments on commit 2ff5df0

Please sign in to comment.