From 646292a2c5bdabefc30f3f64e07edbb4fe192c33 Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Fri, 26 Jan 2024 17:21:30 +0100 Subject: [PATCH] Use visibility hidden by default (#392) * Use visibility hidden by default The commit changes the gz-cmake projects to use C/C++ visibility hidden by default. This is a potential breaking changed for projects using gz-cmake but the benefits in terms of creating portable code and time spend by the loader could be huge. To avoid this change, the EXPOSE_SYMBOLS_BY_DEFAULT flag can be used in the gz_configure_project call. The change deprecates the HIDDEN_SYMBOLS_BY_DEFAULT flag. --------- Signed-off-by: Jose Luis Rivero Co-authored-by: Michael Carroll Co-authored-by: Addisu Z. Taddese --- Migration.md | 11 +++++++++++ cmake/GzConfigureBuild.cmake | 10 ++++++++-- cmake/GzSetCompilerFlags.cmake | 8 ++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Migration.md b/Migration.md index 581cbae8..5e60c5ba 100644 --- a/Migration.md +++ b/Migration.md @@ -9,6 +9,17 @@ release will remove the deprecated code. 1. The minimum required cmake version is now 3.22.1. +1. **Breaking**: C/C++ projects enable the `visibility=hidden` compiler flag by default. + gz-cmake4 changes gz-cmake projects to use C/C++ visibility hidden + by default. This is a potential breaking changed for projects using + gz-cmake but the benefits in terms of creating portable code and + time spend by the loader could be relevant. + To avoid this change, the EXPOSE_SYMBOLS_BY_DEFAULT flag can be used in + the gz_configure_project call. + The change deprecates the HIDDEN_SYMBOLS_BY_DEFAULT flag that can be + removed. + + ## Gazebo CMake 2.X to 3.X 1. **Breaking**: Examples are now built using native cmake. diff --git a/cmake/GzConfigureBuild.cmake b/cmake/GzConfigureBuild.cmake index 0ee02988..3122c15f 100644 --- a/cmake/GzConfigureBuild.cmake +++ b/cmake/GzConfigureBuild.cmake @@ -24,16 +24,22 @@ ################################################# # Configure the build of the Gazebo project # Pass the argument HIDE_SYMBOLS_BY_DEFAULT to configure symbol visibility so -# that symbols are hidden unless explicitly marked as visible. +# that symbols are hidden unless explicitly marked as visible. (DEPRECATED) +# Pass the argument EXPOSE_SYMBOLS_BY_DEFAULT to configure symbol visibility +# so symbols are exposed unless explicitly marked as hidden. # Pass the argument QUIT_IF_BUILD_ERRORS to have this macro quit cmake when the # build_errors macro(gz_configure_build) # Parse the arguments that are passed in - set(options HIDE_SYMBOLS_BY_DEFAULT QUIT_IF_BUILD_ERRORS) + set(options HIDE_SYMBOLS_BY_DEFAULT EXPOSE_SYMBOLS_BY_DEFAULT QUIT_IF_BUILD_ERRORS) set(oneValueArgs) set(multiValueArgs COMPONENTS) cmake_parse_arguments(gz_configure_build "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + if(gz_configure_build_HIDE_SYMBOLS_BY_DEFAULT) + message(DEPRECATION "HIDE_SYMBOLS_BY_DEFAULT is now the default. You can safely remove the option.") + endif() + #============================================================================ # Examine the build type. If we do not recognize the type, we will generate # an error, so this must come before the error handling. diff --git a/cmake/GzSetCompilerFlags.cmake b/cmake/GzSetCompilerFlags.cmake index e2e42824..c6003b10 100644 --- a/cmake/GzSetCompilerFlags.cmake +++ b/cmake/GzSetCompilerFlags.cmake @@ -113,12 +113,12 @@ endmacro() # Internal to gz-cmake. macro(_gz_setup_gcc_or_clang) - if(gz_configure_build_HIDE_SYMBOLS_BY_DEFAULT) - set(CMAKE_C_VISIBILITY_PRESET "hidden") - set(CMAKE_CXX_VISIBILITY_PRESET "hidden") - else() + if(gz_configure_build_EXPOSE_SYMBOLS_BY_DEFAULT) set(CMAKE_C_VISIBILITY_PRESET "default") set(CMAKE_CXX_VISIBILITY_PRESET "default") + else() + set(CMAKE_C_VISIBILITY_PRESET "hidden") + set(CMAKE_CXX_VISIBILITY_PRESET "hidden") endif()