@@ -130,26 +130,39 @@ function(pybind11_add_module target_name)
130
130
131
131
add_library (${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS} )
132
132
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 ()
137
157
138
158
# The prefix and extension are provided by FindPythonLibsNew.cmake
139
159
set_target_properties (${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX} " )
140
160
set_target_properties (${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION} " )
141
161
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 )
153
166
# It's quite common to have multiple copies of the same Python version
154
167
# installed on one's system. E.g.: one copy from the OS and another copy
155
168
# that's statically linked into an application like Blender or Maya.
@@ -172,9 +185,6 @@ function(pybind11_add_module target_name)
172
185
endif ()
173
186
endif ()
174
187
175
- # Make sure C++11/14 are enabled
176
- target_compile_options (${target_name} PUBLIC ${PYBIND11_CPP_STANDARD} )
177
-
178
188
if (ARG_NO_EXTRAS )
179
189
return ()
180
190
endif ()
0 commit comments