From 230fa53f168b3a185dadfa9fb35472a101012eaa Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 14 Jan 2021 08:31:48 -0500 Subject: [PATCH] fix: Don't override global settings for VISIBILITY if set (#2793) Sometimes programmers want to control this, and while it can be changed after the fact, it's commonly set via a CMAKE_ variable; if that variable is set, we should respect that (like the CMAKE_INTERPROCEDURAL_OPTIMIZATION setting). --- tools/pybind11Config.cmake.in | 2 +- tools/pybind11NewTools.cmake | 14 ++++++++++++-- tools/pybind11Tools.cmake | 9 +++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 9808f3d2..9921aeb3 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -87,7 +87,7 @@ you can either use the basic targets, or use the FindPython tools: target_link_libraries(MyModule2 pybind11::headers) set_target_properties(MyModule2 PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON - CXX__VISIBILITY_PRESET ON + CXX_VISIBILITY_PRESET ON VISIBLITY_INLINES_HIDDEN ON) If you build targets yourself, you may be interested in stripping the output diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index 686450bc..96879d3c 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -214,8 +214,18 @@ function(pybind11_add_module target_name) target_link_libraries(${target_name} PRIVATE pybind11::python2_no_register) endif() - set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden" - CUDA_VISIBILITY_PRESET "hidden") + # -fvisibility=hidden is required to allow multiple modules compiled against + # different pybind versions to work properly, and for some features (e.g. + # py::module_local). We force it on everything inside the `pybind11` + # namespace; also turning it on for a pybind module compilation here avoids + # potential warnings or issues from having mixed hidden/non-hidden types. + if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET) + set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden") + endif() + + if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET) + set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden") + endif() # If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions if("${type}" STREQUAL "MODULE" AND (NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index aee60db2..3fa452f9 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -174,8 +174,13 @@ function(pybind11_add_module target_name) # py::module_local). We force it on everything inside the `pybind11` # namespace; also turning it on for a pybind module compilation here avoids # potential warnings or issues from having mixed hidden/non-hidden types. - set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden" - CUDA_VISIBILITY_PRESET "hidden") + if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET) + set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden") + endif() + + if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET) + set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden") + endif() if(ARG_NO_EXTRAS) return()