Skip to content

Commit 124e651

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

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
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: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,39 @@ 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})
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()
153+
154+
# Make sure C++11/14 are enabled
155+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
156+
endif()
137157

138158
# The prefix and extension are provided by FindPythonLibsNew.cmake
139159
set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
140160
set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
141161

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)
162+
# NB These properties are not incorporated via `pybind11Config.cmake.in`, so
163+
# we will incorporate the flags regardless of using the interface target
164+
# library or not.
165+
if(APPLE)
153166
# It's quite common to have multiple copies of the same Python version
154167
# installed on one's system. E.g.: one copy from the OS and another copy
155168
# that's statically linked into an application like Blender or Maya.
@@ -172,9 +185,6 @@ function(pybind11_add_module target_name)
172185
endif()
173186
endif()
174187

175-
# Make sure C++11/14 are enabled
176-
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
177-
178188
if(ARG_NO_EXTRAS)
179189
return()
180190
endif()

0 commit comments

Comments
 (0)