Skip to content

Commit 230fa53

Browse files
authored
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).
1 parent 1faf4a8 commit 230fa53

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

tools/pybind11Config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ you can either use the basic targets, or use the FindPython tools:
8787
target_link_libraries(MyModule2 pybind11::headers)
8888
set_target_properties(MyModule2 PROPERTIES
8989
INTERPROCEDURAL_OPTIMIZATION ON
90-
CXX__VISIBILITY_PRESET ON
90+
CXX_VISIBILITY_PRESET ON
9191
VISIBLITY_INLINES_HIDDEN ON)
9292
9393
If you build targets yourself, you may be interested in stripping the output

tools/pybind11NewTools.cmake

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,18 @@ function(pybind11_add_module target_name)
214214
target_link_libraries(${target_name} PRIVATE pybind11::python2_no_register)
215215
endif()
216216

217-
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden"
218-
CUDA_VISIBILITY_PRESET "hidden")
217+
# -fvisibility=hidden is required to allow multiple modules compiled against
218+
# different pybind versions to work properly, and for some features (e.g.
219+
# py::module_local). We force it on everything inside the `pybind11`
220+
# namespace; also turning it on for a pybind module compilation here avoids
221+
# potential warnings or issues from having mixed hidden/non-hidden types.
222+
if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
223+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
224+
endif()
225+
226+
if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
227+
set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
228+
endif()
219229

220230
# If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
221231
if("${type}" STREQUAL "MODULE" AND (NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST

tools/pybind11Tools.cmake

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,13 @@ function(pybind11_add_module target_name)
174174
# py::module_local). We force it on everything inside the `pybind11`
175175
# namespace; also turning it on for a pybind module compilation here avoids
176176
# potential warnings or issues from having mixed hidden/non-hidden types.
177-
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden"
178-
CUDA_VISIBILITY_PRESET "hidden")
177+
if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
178+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
179+
endif()
180+
181+
if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
182+
set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
183+
endif()
179184

180185
if(ARG_NO_EXTRAS)
181186
return()

0 commit comments

Comments
 (0)