@@ -67,6 +67,9 @@ cmake_dependent_option(
6767 "Install pybind11 headers in Python include directory instead of default installation prefix"
6868 OFF "PYBIND11_INSTALL" OFF )
6969
70+ cmake_dependent_option(PYBIND11_NEW_PYTHON "Force new findPython" OFF
71+ "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF )
72+
7073# NB: when adding a header don't forget to also add it to setup.py
7174set (PYBIND11_HEADERS
7275 include /pybind11/detail/class.h
@@ -118,102 +121,36 @@ endif()
118121string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/" PYBIND11_HEADERS
119122 "${PYBIND11_HEADERS} " )
120123
121- # Classic mode
122-
123- include ("${CMAKE_CURRENT_LIST_DIR} /tools/pybind11Tools.cmake" )
124-
125124# Cache variables so pybind11_add_module can be used in parent projects
126125set (PYBIND11_INCLUDE_DIR
127126 "${CMAKE_CURRENT_LIST_DIR} /include"
128127 CACHE INTERNAL "" )
129- set (PYTHON_INCLUDE_DIRS
130- ${PYTHON_INCLUDE_DIRS}
131- CACHE INTERNAL "" )
132- set (PYTHON_LIBRARIES
133- ${PYTHON_LIBRARIES}
134- CACHE INTERNAL "" )
135- set (PYTHON_MODULE_PREFIX
136- ${PYTHON_MODULE_PREFIX}
137- CACHE INTERNAL "" )
138- set (PYTHON_MODULE_EXTENSION
139- ${PYTHON_MODULE_EXTENSION}
140- CACHE INTERNAL "" )
141- set (PYTHON_VERSION_MAJOR
142- ${PYTHON_VERSION_MAJOR}
143- CACHE INTERNAL "" )
144- set (PYTHON_VERSION_MINOR
145- ${PYTHON_VERSION_MINOR}
146- CACHE INTERNAL "" )
147- set (PYTHON_IS_DEBUG
148- "${PYTHON_IS_DEBUG} "
149- CACHE INTERNAL "" )
150-
151- if (USE_PYTHON_INCLUDE_DIR)
152- file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
153- endif ()
154128
155129# Note: when creating targets, you cannot use if statements at configure time -
156130# you need generator expressions, because those will be placed in the target file.
157131# You can also place ifs *in* the Config.in, but not here.
158132
159- # Build an interface library target:
160- add_library (pybind11 INTERFACE )
161- add_library (pybind11::pybind11 ALIAS pybind11) # to match exported target
133+ # This section builds targets, but does *not* touch Python
162134
163- target_include_directories (
164- pybind11 ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR} >
165- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
166- # Only add Python for build - must be added during the import for config since it has to be re-discovered.
167- target_include_directories (pybind11 SYSTEM INTERFACE $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS} >)
168-
169- if (CMAKE_VERSION VERSION_LESS 3.13)
170- target_compile_features (pybind11 INTERFACE cxx_inheriting_constructors cxx_user_literals
171- cxx_right_angle_brackets)
172- else ()
173- # This was added in CMake 3.8, but we are keeping a consistent breaking
174- # point for the config file at 3.13. A config generated by CMake 3.13+
175- # can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
176- target_compile_features (pybind11 INTERFACE cxx_std_11)
177- endif ()
135+ # Build the headers-only target (no Python included):
136+ add_library (headers INTERFACE )
137+ add_library (pybind11::headers ALIAS headers) # to match exported target
178138
179- add_library (module INTERFACE )
180- add_library (pybind11::module ALIAS module)
139+ target_include_directories (
140+ headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR} >
141+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
181142
182- target_link_libraries (module INTERFACE pybind11::pybind11)
143+ target_compile_features (headers INTERFACE cxx_inheriting_constructors cxx_user_literals
144+ cxx_right_angle_brackets)
183145
184- # See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs
185- # Note: CMake 3.15 allows $<PLATFORM_ID:Windows,Cygwin>
186- target_link_libraries (
187- module
188- INTERFACE
189- "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES} >>" )
146+ include ("${CMAKE_CURRENT_SOURCE_DIR} /tools/pybind11Common.cmake" )
190147
191- if (CMAKE_VERSION VERSION_LESS 3.13)
192- target_link_libraries (module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>" )
193- else ()
194- # SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+
195- # This is safer, because you are ensured the deduplication pass in CMake will not consider
196- # these separate and remove one but not the other.
197- target_link_options (module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>" )
148+ if (USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
149+ file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS} )
150+ elseif (USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
151+ file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
198152endif ()
199153
200- # Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
201- # This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
202- # is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
203- # since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
204- set (clang_4plus
205- "$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>" )
206- set (no_register "$<OR:${clang_4plus} ,$<CXX_COMPILER_ID:AppleClang>>" )
207- set (cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register} >" )
208- set (msvc "$<CXX_COMPILER_ID:MSVC>" )
209- target_compile_options (
210- pybind11 INTERFACE "$<${cxx_no_register} :-Wno-register;-Wno-deprecated-register>"
211- "$<${msvc} :/wd5033>" )
212-
213- add_library (embed INTERFACE )
214- add_library (pybind11::embed ALIAS embed)
215- target_link_libraries (embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES} >)
216-
217154if (PYBIND11_INSTALL)
218155 install (DIRECTORY ${PYBIND11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
219156 # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
@@ -248,14 +185,17 @@ if(PYBIND11_INSTALL)
248185 install (
249186 FILES ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake
250187 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
251- tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake
188+ tools/FindPythonLibsNew.cmake
189+ tools/pybind11Common.cmake
190+ tools/pybind11Tools.cmake
191+ tools/pybind11NewTools.cmake
252192 DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
253193
254194 if (NOT PYBIND11_EXPORT_NAME)
255195 set (PYBIND11_EXPORT_NAME "${PROJECT_NAME} Targets" )
256196 endif ()
257197
258- install (TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME} " )
198+ install (TARGETS headers EXPORT "${PYBIND11_EXPORT_NAME} " )
259199
260200 install (
261201 EXPORT "${PYBIND11_EXPORT_NAME} "
0 commit comments