Skip to content

Commit a421b9b

Browse files
pybind11Tools: Use interface target library when able
1 parent 060936f commit a421b9b

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

docs/compiling.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ to an independently constructed (through ``add_library``, not
189189
Studio (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
190190
explanation on why these are needed.
191191

192+
.. note::
193+
194+
``pybind11_add_module`` will use this interface library target in addition
195+
to the above compiler flags if using a version of CMake greater than 3.0.
196+
192197
Embedding the Python interpreter
193198
--------------------------------
194199

tools/pybind11Tools.cmake

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,50 +130,58 @@ function(pybind11_add_module target_name)
130130

131131
add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
132132

133-
target_include_directories(${target_name}
134-
PRIVATE ${PYBIND11_INCLUDE_DIR} # from project CMakeLists.txt
135-
PRIVATE ${pybind11_INCLUDE_DIR} # from pybind11Config
136-
PRIVATE ${PYTHON_INCLUDE_DIRS})
137-
138-
# The prefix and extension are provided by FindPythonLibsNew.cmake
139-
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
140-
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
133+
if(TARGET pybind11::module)
134+
# Use interface target library.
135+
target_link_libraries(${target_name} PRIVATE pybind11::module)
136+
else()
137+
target_include_directories(${target_name}
138+
PRIVATE ${PYBIND11_INCLUDE_DIR} # from project CMakeLists.txt
139+
PRIVATE ${pybind11_INCLUDE_DIR} # from pybind11Config
140+
PRIVATE ${PYTHON_INCLUDE_DIRS})
141+
142+
# -fvisibility=hidden is required to allow multiple modules compiled against
143+
# different pybind versions to work properly, and for some features (e.g.
144+
# py::module_local). We force it on everything inside the `pybind11`
145+
# namespace; also turning it on for a pybind module compilation here avoids
146+
# potential warnings or issues from having mixed hidden/non-hidden types.
147+
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
148+
149+
if(WIN32 OR CYGWIN)
150+
# Link against the Python shared library on Windows
151+
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
152+
endif()
141153

142-
# -fvisibility=hidden is required to allow multiple modules compiled against
143-
# different pybind versions to work properly, and for some features (e.g.
144-
# py::module_local). We force it on everything inside the `pybind11`
145-
# namespace; also turning it on for a pybind module compilation here avoids
146-
# potential warnings or issues from having mixed hidden/non-hidden types.
147-
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
148-
149-
if(WIN32 OR CYGWIN)
150-
# Link against the Python shared library on Windows
151-
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
152-
elseif(APPLE)
153-
# It's quite common to have multiple copies of the same Python version
154-
# installed on one's system. E.g.: one copy from the OS and another copy
155-
# that's statically linked into an application like Blender or Maya.
156-
# If we link our plugin library against the OS Python here and import it
157-
# into Blender or Maya later on, this will cause segfaults when multiple
158-
# conflicting Python instances are active at the same time (even when they
159-
# are of the same version).
160-
161-
# Windows is not affected by this issue since it handles DLL imports
162-
# differently. The solution for Linux and Mac OS is simple: we just don't
163-
# link against the Python library. The resulting shared library will have
164-
# missing symbols, but that's perfectly fine -- they will be resolved at
165-
# import time.
166-
167-
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
168-
169-
if(ARG_SHARED)
170-
# Suppress CMake >= 3.0 warning for shared libraries
171-
set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ON)
154+
# Make sure C++11/14 are enabled
155+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
156+
157+
if(APPLE)
158+
# It's quite common to have multiple copies of the same Python version
159+
# installed on one's system. E.g.: one copy from the OS and another copy
160+
# that's statically linked into an application like Blender or Maya.
161+
# If we link our plugin library against the OS Python here and import it
162+
# into Blender or Maya later on, this will cause segfaults when multiple
163+
# conflicting Python instances are active at the same time (even when they
164+
# are of the same version).
165+
166+
# Windows is not affected by this issue since it handles DLL imports
167+
# differently. The solution for Linux and Mac OS is simple: we just don't
168+
# link against the Python library. The resulting shared library will have
169+
# missing symbols, but that's perfectly fine -- they will be resolved at
170+
# import time.
171+
172+
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
173+
174+
if(ARG_SHARED)
175+
# Suppress CMake >= 3.0 warning for shared libraries
176+
set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ON)
177+
endif()
172178
endif()
179+
173180
endif()
174181

175-
# Make sure C++11/14 are enabled
176-
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
182+
# The prefix and extension are provided by FindPythonLibsNew.cmake
183+
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
184+
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
177185

178186
if(ARG_NO_EXTRAS)
179187
return()

0 commit comments

Comments
 (0)