CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # CMakeLists.txt -- Build system for the pybind11 modules
  2. #
  3. # Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
  4. #
  5. # All rights reserved. Use of this source code is governed by a
  6. # BSD-style license that can be found in the LICENSE file.
  7. cmake_minimum_required(VERSION 3.4)
  8. # The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with
  9. # some versions of VS that have a patched CMake 3.11. This forces us to emulate
  10. # the behavior using the following workaround:
  11. if(${CMAKE_VERSION} VERSION_LESS 3.18)
  12. cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  13. else()
  14. cmake_policy(VERSION 3.18)
  15. endif()
  16. # Extract project version from source
  17. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h"
  18. pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
  19. foreach(ver ${pybind11_version_defines})
  20. if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]])
  21. set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}")
  22. endif()
  23. endforeach()
  24. if(PYBIND11_VERSION_PATCH MATCHES [[\.([a-zA-Z0-9]+)$]])
  25. set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}")
  26. endif()
  27. string(REGEX MATCH "^[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}")
  28. project(
  29. pybind11
  30. LANGUAGES CXX
  31. VERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}")
  32. # Standard includes
  33. include(GNUInstallDirs)
  34. include(CMakePackageConfigHelpers)
  35. include(CMakeDependentOption)
  36. if(NOT pybind11_FIND_QUIETLY)
  37. message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
  38. endif()
  39. # Avoid infinite recursion if tests include this as a subdirectory
  40. if(DEFINED PYBIND11_MASTER_PROJECT)
  41. set(PYBIND11_TEST OFF)
  42. endif()
  43. # Check if pybind11 is being used directly or via add_subdirectory
  44. if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT DEFINED PYBIND11_MASTER_PROJECT)
  45. ### Warn if not an out-of-source builds
  46. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
  47. set(lines
  48. "You are building in-place. If that is not what you intended to "
  49. "do, you can clean the source directory with:\n"
  50. "rm -r CMakeCache.txt CMakeFiles/ cmake_uninstall.cmake pybind11Config.cmake "
  51. "pybind11ConfigVersion.cmake tests/CMakeFiles/\n")
  52. message(AUTHOR_WARNING ${lines})
  53. endif()
  54. set(PYBIND11_MASTER_PROJECT ON)
  55. if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)
  56. # Bug in macOS CMake < 3.7 is unable to download catch
  57. message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended")
  58. elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)
  59. # Only tested with 3.8+ in CI.
  60. message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested")
  61. endif()
  62. message(STATUS "CMake ${CMAKE_VERSION}")
  63. if(CMAKE_CXX_STANDARD)
  64. set(CMAKE_CXX_EXTENSIONS OFF)
  65. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  66. endif()
  67. set(pybind11_system "")
  68. else()
  69. set(PYBIND11_MASTER_PROJECT OFF)
  70. set(pybind11_system SYSTEM)
  71. endif()
  72. # Options
  73. option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
  74. option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
  75. option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
  76. cmake_dependent_option(
  77. USE_PYTHON_INCLUDE_DIR
  78. "Install pybind11 headers in Python include directory instead of default installation prefix"
  79. OFF "PYBIND11_INSTALL" OFF)
  80. cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
  81. "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)
  82. # NB: when adding a header don't forget to also add it to setup.py
  83. set(PYBIND11_HEADERS
  84. include/pybind11/detail/class.h
  85. include/pybind11/detail/common.h
  86. include/pybind11/detail/descr.h
  87. include/pybind11/detail/init.h
  88. include/pybind11/detail/internals.h
  89. include/pybind11/detail/type_caster_base.h
  90. include/pybind11/detail/typeid.h
  91. include/pybind11/attr.h
  92. include/pybind11/buffer_info.h
  93. include/pybind11/cast.h
  94. include/pybind11/chrono.h
  95. include/pybind11/common.h
  96. include/pybind11/complex.h
  97. include/pybind11/options.h
  98. include/pybind11/eigen.h
  99. include/pybind11/embed.h
  100. include/pybind11/eval.h
  101. include/pybind11/gil.h
  102. include/pybind11/iostream.h
  103. include/pybind11/functional.h
  104. include/pybind11/numpy.h
  105. include/pybind11/operators.h
  106. include/pybind11/pybind11.h
  107. include/pybind11/pytypes.h
  108. include/pybind11/stl.h
  109. include/pybind11/stl_bind.h
  110. include/pybind11/stl/filesystem.h)
  111. # Compare with grep and warn if mismatched
  112. if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
  113. file(
  114. GLOB_RECURSE _pybind11_header_check
  115. LIST_DIRECTORIES false
  116. RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
  117. CONFIGURE_DEPENDS "include/pybind11/*.h")
  118. set(_pybind11_here_only ${PYBIND11_HEADERS})
  119. set(_pybind11_disk_only ${_pybind11_header_check})
  120. list(REMOVE_ITEM _pybind11_here_only ${_pybind11_header_check})
  121. list(REMOVE_ITEM _pybind11_disk_only ${PYBIND11_HEADERS})
  122. if(_pybind11_here_only)
  123. message(AUTHOR_WARNING "PYBIND11_HEADERS has extra files:" ${_pybind11_here_only})
  124. endif()
  125. if(_pybind11_disk_only)
  126. message(AUTHOR_WARNING "PYBIND11_HEADERS is missing files:" ${_pybind11_disk_only})
  127. endif()
  128. endif()
  129. # CMake 3.12 added list(TRANSFORM <list> PREPEND
  130. # But we can't use it yet
  131. string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS
  132. "${PYBIND11_HEADERS}")
  133. # Cache variable so this can be used in parent projects
  134. set(pybind11_INCLUDE_DIR
  135. "${CMAKE_CURRENT_LIST_DIR}/include"
  136. CACHE INTERNAL "Directory where pybind11 headers are located")
  137. # Backward compatible variable for add_subdirectory mode
  138. if(NOT PYBIND11_MASTER_PROJECT)
  139. set(PYBIND11_INCLUDE_DIR
  140. "${pybind11_INCLUDE_DIR}"
  141. CACHE INTERNAL "")
  142. endif()
  143. # Note: when creating targets, you cannot use if statements at configure time -
  144. # you need generator expressions, because those will be placed in the target file.
  145. # You can also place ifs *in* the Config.in, but not here.
  146. # This section builds targets, but does *not* touch Python
  147. # Non-IMPORT targets cannot be defined twice
  148. if(NOT TARGET pybind11_headers)
  149. # Build the headers-only target (no Python included):
  150. # (long name used here to keep this from clashing in subdirectory mode)
  151. add_library(pybind11_headers INTERFACE)
  152. add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target
  153. add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/remember
  154. target_include_directories(
  155. pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${pybind11_INCLUDE_DIR}>
  156. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  157. target_compile_features(pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals
  158. cxx_right_angle_brackets)
  159. else()
  160. # It is invalid to install a target twice, too.
  161. set(PYBIND11_INSTALL OFF)
  162. endif()
  163. include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake")
  164. # Relative directory setting
  165. if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
  166. file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS})
  167. elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
  168. file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
  169. endif()
  170. if(PYBIND11_INSTALL)
  171. install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  172. set(PYBIND11_CMAKECONFIG_INSTALL_DIR
  173. "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}"
  174. CACHE STRING "install path for pybind11Config.cmake")
  175. if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
  176. set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
  177. else()
  178. set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}")
  179. endif()
  180. configure_package_config_file(
  181. tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  182. INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  183. if(CMAKE_VERSION VERSION_LESS 3.14)
  184. # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
  185. # not depend on architecture specific settings or libraries.
  186. set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
  187. unset(CMAKE_SIZEOF_VOID_P)
  188. write_basic_package_version_file(
  189. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  190. VERSION ${PROJECT_VERSION}
  191. COMPATIBILITY AnyNewerVersion)
  192. set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})
  193. else()
  194. # CMake 3.14+ natively supports header-only libraries
  195. write_basic_package_version_file(
  196. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  197. VERSION ${PROJECT_VERSION}
  198. COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
  199. endif()
  200. install(
  201. FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  202. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  203. tools/FindPythonLibsNew.cmake
  204. tools/pybind11Common.cmake
  205. tools/pybind11Tools.cmake
  206. tools/pybind11NewTools.cmake
  207. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  208. if(NOT PYBIND11_EXPORT_NAME)
  209. set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
  210. endif()
  211. install(TARGETS pybind11_headers EXPORT "${PYBIND11_EXPORT_NAME}")
  212. install(
  213. EXPORT "${PYBIND11_EXPORT_NAME}"
  214. NAMESPACE "pybind11::"
  215. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  216. # Uninstall target
  217. if(PYBIND11_MASTER_PROJECT)
  218. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in"
  219. "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
  220. add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P
  221. ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  222. endif()
  223. endif()
  224. # BUILD_TESTING takes priority, but only if this is the master project
  225. if(PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING)
  226. if(BUILD_TESTING)
  227. if(_pybind11_nopython)
  228. message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
  229. else()
  230. add_subdirectory(tests)
  231. endif()
  232. endif()
  233. else()
  234. if(PYBIND11_TEST)
  235. if(_pybind11_nopython)
  236. message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
  237. else()
  238. add_subdirectory(tests)
  239. endif()
  240. endif()
  241. endif()
  242. # Better symmetry with find_package(pybind11 CONFIG) mode.
  243. if(NOT PYBIND11_MASTER_PROJECT)
  244. set(pybind11_FOUND
  245. TRUE
  246. CACHE INTERNAL "True if pybind11 and all required components found on the system")
  247. endif()