diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 9808f3d25..9921aeb35 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 686450bc3..96879d3ce 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 aee60db28..3fa452f97 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()