@@ -130,50 +130,58 @@ 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} )
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 ()
141
153
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 ()
172
178
endif ()
179
+
173
180
endif ()
174
181
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} " )
177
185
178
186
if (ARG_NO_EXTRAS )
179
187
return ()
0 commit comments