55# All rights reserved. Use of this source code is governed by a
66# BSD-style license that can be found in the LICENSE file.
77
8- cmake_minimum_required (VERSION 3.7 )
8+ cmake_minimum_required (VERSION 3.4 )
99
10- # The `cmake_minimum_required(VERSION 3.7 ...3.18)` syntax does not work with
10+ # The `cmake_minimum_required(VERSION 3.4 ...3.18)` syntax does not work with
1111# some versions of VS that have a patched CMake 3.11. This forces us to emulate
1212# the behavior using the following workaround:
1313if (${CMAKE_VERSION} VERSION_LESS 3.18)
@@ -41,11 +41,22 @@ include(GNUInstallDirs)
4141include (CMakePackageConfigHelpers)
4242include (CMakeDependentOption)
4343
44- message (STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE} " )
44+ if (NOT pybind11_FIND_QUIETLY)
45+ message (STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE} " )
46+ endif ()
4547
4648# Check if pybind11 is being used directly or via add_subdirectory
4749if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
4850 set (PYBIND11_MASTER_PROJECT ON )
51+
52+ if (OSX AND CMAKE_VERSION VERSION_LESS 3.7)
53+ # Bug in macOS CMake < 3.7 is unable to download catch
54+ message (WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended" )
55+ elseif (WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)
56+ # Only tested with 3.8+ in CI.
57+ message (WARNING "CMAKE 3.8+ tested on Windows, previous versions untested" )
58+ endif ()
59+
4960 message (STATUS "CMake ${CMAKE_VERSION} " )
5061
5162 if (CMAKE_CXX_STANDARD)
@@ -60,13 +71,16 @@ endif()
6071# Options
6172option (PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT} )
6273option (PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT} )
63- option (PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+ " OFF )
74+ option (PYBIND11_NOPYTHON "Disable search for Python " OFF )
6475
6576cmake_dependent_option(
6677 USE_PYTHON_INCLUDE_DIR
6778 "Install pybind11 headers in Python include directory instead of default installation prefix"
6879 OFF "PYBIND11_INSTALL" OFF )
6980
81+ cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
82+ "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF )
83+
7084# NB: when adding a header don't forget to also add it to setup.py
7185set (PYBIND11_HEADERS
7286 include /pybind11/detail/class.h
@@ -118,101 +132,41 @@ endif()
118132string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/" PYBIND11_HEADERS
119133 "${PYBIND11_HEADERS} " )
120134
121- # Classic mode
122-
123- include ("${CMAKE_CURRENT_LIST_DIR} /tools/pybind11Tools.cmake" )
124-
125135# Cache variables so pybind11_add_module can be used in parent projects
126136set (PYBIND11_INCLUDE_DIR
127137 "${CMAKE_CURRENT_LIST_DIR} /include"
128138 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 ()
154139
155140# Note: when creating targets, you cannot use if statements at configure time -
156141# you need generator expressions, because those will be placed in the target file.
157142# You can also place ifs *in* the Config.in, but not here.
158143
159- # Build an interface library target:
160- add_library (pybind11 INTERFACE )
161- add_library (pybind11::pybind11 ALIAS pybind11) # to match exported target
162-
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 ()
144+ # This section builds targets, but does *not* touch Python
178145
179- add_library (module INTERFACE )
180- add_library (pybind11::module ALIAS module)
146+ # Build the headers-only target (no Python included):
147+ add_library (headers INTERFACE )
148+ add_library (pybind11::headers ALIAS headers) # to match exported target
181149
182- target_link_libraries (module INTERFACE pybind11::pybind11 )
150+ include ( " ${CMAKE_CURRENT_SOURCE_DIR} /tools/pybind11Common.cmake" )
183151
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} >>" )
152+ if (NOT PYBIND11_MASTER_PROJECT AND NOT pybind11_FIND_QUIETLY)
153+ message (STATUS "Using pybind11: (version \" ${pybind11_VERSION} \" ${pybind11_VERSION_TYPE} )" )
154+ endif ()
190155
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>" )
156+ # Relative directory setting
157+ if (USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
158+ file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS} )
159+ elseif (USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
160+ file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
198161endif ()
199162
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} >)
163+ # Fill in headers target
164+ target_include_directories (
165+ headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR} >
166+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
167+
168+ target_compile_features (headers INTERFACE cxx_inheriting_constructors cxx_user_literals
169+ cxx_right_angle_brackets)
216170
217171if (PYBIND11_INSTALL)
218172 install (DIRECTORY ${PYBIND11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
@@ -248,14 +202,17 @@ if(PYBIND11_INSTALL)
248202 install (
249203 FILES ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake
250204 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
251- tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake
205+ tools/FindPythonLibsNew.cmake
206+ tools/pybind11Common.cmake
207+ tools/pybind11Tools.cmake
208+ tools/pybind11NewTools.cmake
252209 DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
253210
254211 if (NOT PYBIND11_EXPORT_NAME)
255212 set (PYBIND11_EXPORT_NAME "${PROJECT_NAME} Targets" )
256213 endif ()
257214
258- install (TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME} " )
215+ install (TARGETS headers EXPORT "${PYBIND11_EXPORT_NAME} " )
259216
260217 install (
261218 EXPORT "${PYBIND11_EXPORT_NAME} "
@@ -275,10 +232,28 @@ endif()
275232# BUILD_TESTING takes priority, but only if this is the master project
276233if (PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING)
277234 if (BUILD_TESTING)
278- add_subdirectory (tests)
235+ if (_pybind11_nopython)
236+ message (FATAL_ERROR "Cannot activate tests in NOPYTHON mode" )
237+ else ()
238+ add_subdirectory (tests)
239+ endif ()
279240 endif ()
280241else ()
281242 if (PYBIND11_TEST)
282- add_subdirectory (tests)
243+ if (_pybind11_nopython)
244+ message (FATAL_ERROR "Cannot activate tests in NOPYTHON mode" )
245+ else ()
246+ add_subdirectory (tests)
247+ endif ()
283248 endif ()
284249endif ()
250+
251+ # Better symmetry with find_package(pybind11 CONFIG) mode.
252+ if (NOT PYBIND11_MASTER_PROJECT)
253+ set (pybind11_FOUND
254+ TRUE
255+ CACHE INTERNAL "true if pybind11 and all required components found on the system" )
256+ set (pybind11_INCLUDE_DIR
257+ "${PYBIND11_INCLUDE_DIR} "
258+ CACHE INTERNAL "Directory where pybind11 headers are located" )
259+ endif ()
0 commit comments