pybind11Tools.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # tools/pybind11Tools.cmake -- Build system for the pybind11 modules
  2. #
  3. # Copyright (c) 2020 Wenzel Jakob <wenzel.jakob@epfl.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. # Built-in in CMake 3.5+
  8. include(CMakeParseArguments)
  9. if(pybind11_FIND_QUIETLY)
  10. set(_pybind11_quiet QUIET)
  11. else()
  12. set(_pybind11_quiet "")
  13. endif()
  14. # If this is the first run, PYTHON_VERSION can stand in for PYBIND11_PYTHON_VERSION
  15. if(NOT DEFINED PYBIND11_PYTHON_VERSION AND DEFINED PYTHON_VERSION)
  16. message(WARNING "Set PYBIND11_PYTHON_VERSION to search for a specific version, not "
  17. "PYTHON_VERSION (which is an output). Assuming that is what you "
  18. "meant to do and continuing anyway.")
  19. set(PYBIND11_PYTHON_VERSION
  20. "${PYTHON_VERSION}"
  21. CACHE STRING "Python version to use for compiling modules")
  22. unset(PYTHON_VERSION)
  23. unset(PYTHON_VERSION CACHE)
  24. elseif(DEFINED PYBIND11_PYTHON_VERSION)
  25. # If this is set as a normal variable, promote it
  26. set(PYBIND11_PYTHON_VERSION
  27. "${PYBIND11_PYTHON_VERSION}"
  28. CACHE STRING "Python version to use for compiling modules")
  29. else()
  30. # Make an empty cache variable.
  31. set(PYBIND11_PYTHON_VERSION
  32. ""
  33. CACHE STRING "Python version to use for compiling modules")
  34. endif()
  35. # A user can set versions manually too
  36. set(Python_ADDITIONAL_VERSIONS
  37. "3.10;3.9;3.8;3.7;3.6;3.5;3.4"
  38. CACHE INTERNAL "")
  39. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
  40. find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED ${_pybind11_quiet})
  41. list(REMOVE_AT CMAKE_MODULE_PATH -1)
  42. # Cache variables so pybind11_add_module can be used in parent projects
  43. set(PYTHON_INCLUDE_DIRS
  44. ${PYTHON_INCLUDE_DIRS}
  45. CACHE INTERNAL "")
  46. set(PYTHON_LIBRARIES
  47. ${PYTHON_LIBRARIES}
  48. CACHE INTERNAL "")
  49. set(PYTHON_MODULE_PREFIX
  50. ${PYTHON_MODULE_PREFIX}
  51. CACHE INTERNAL "")
  52. set(PYTHON_MODULE_EXTENSION
  53. ${PYTHON_MODULE_EXTENSION}
  54. CACHE INTERNAL "")
  55. set(PYTHON_VERSION_MAJOR
  56. ${PYTHON_VERSION_MAJOR}
  57. CACHE INTERNAL "")
  58. set(PYTHON_VERSION_MINOR
  59. ${PYTHON_VERSION_MINOR}
  60. CACHE INTERNAL "")
  61. set(PYTHON_VERSION
  62. ${PYTHON_VERSION}
  63. CACHE INTERNAL "")
  64. set(PYTHON_IS_DEBUG
  65. "${PYTHON_IS_DEBUG}"
  66. CACHE INTERNAL "")
  67. if(PYBIND11_MASTER_PROJECT)
  68. if(PYTHON_MODULE_EXTENSION MATCHES "pypy")
  69. if(NOT DEFINED PYPY_VERSION)
  70. execute_process(
  71. COMMAND ${PYTHON_EXECUTABLE} -c
  72. [=[import sys; sys.stdout.write(".".join(map(str, sys.pypy_version_info[:3])))]=]
  73. OUTPUT_VARIABLE pypy_version)
  74. set(PYPY_VERSION
  75. ${pypy_version}
  76. CACHE INTERNAL "")
  77. endif()
  78. message(STATUS "PYPY ${PYPY_VERSION} (Py ${PYTHON_VERSION})")
  79. else()
  80. message(STATUS "PYTHON ${PYTHON_VERSION}")
  81. endif()
  82. endif()
  83. # Only add Python for build - must be added during the import for config since
  84. # it has to be re-discovered.
  85. #
  86. # This needs to be an target to it is included after the local pybind11
  87. # directory, just in case there are multiple versions of pybind11, we want the
  88. # one we expect.
  89. add_library(pybind11::python_headers INTERFACE IMPORTED)
  90. set_property(TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  91. "$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>")
  92. set_property(
  93. TARGET pybind11::pybind11
  94. APPEND
  95. PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
  96. set(pybind11_INCLUDE_DIRS
  97. "${pybind11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}"
  98. CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")
  99. # Python debug libraries expose slightly different objects before 3.8
  100. # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
  101. # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
  102. if(PYTHON_IS_DEBUG)
  103. set_property(
  104. TARGET pybind11::pybind11
  105. APPEND
  106. PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
  107. endif()
  108. set_property(
  109. TARGET pybind11::module
  110. APPEND
  111. PROPERTY
  112. INTERFACE_LINK_LIBRARIES pybind11::python_link_helper
  113. "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>")
  114. if(PYTHON_VERSION VERSION_LESS 3)
  115. set_property(
  116. TARGET pybind11::pybind11
  117. APPEND
  118. PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python2_no_register)
  119. endif()
  120. set_property(
  121. TARGET pybind11::embed
  122. APPEND
  123. PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
  124. function(pybind11_extension name)
  125. # The prefix and extension are provided by FindPythonLibsNew.cmake
  126. set_target_properties(${name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
  127. SUFFIX "${PYTHON_MODULE_EXTENSION}")
  128. endfunction()
  129. # Build a Python extension module:
  130. # pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
  131. # [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...])
  132. #
  133. function(pybind11_add_module target_name)
  134. set(options "MODULE;SHARED;EXCLUDE_FROM_ALL;NO_EXTRAS;SYSTEM;THIN_LTO;OPT_SIZE")
  135. cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
  136. if(ARG_MODULE AND ARG_SHARED)
  137. message(FATAL_ERROR "Can't be both MODULE and SHARED")
  138. elseif(ARG_SHARED)
  139. set(lib_type SHARED)
  140. else()
  141. set(lib_type MODULE)
  142. endif()
  143. if(ARG_EXCLUDE_FROM_ALL)
  144. set(exclude_from_all EXCLUDE_FROM_ALL)
  145. else()
  146. set(exclude_from_all "")
  147. endif()
  148. add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
  149. target_link_libraries(${target_name} PRIVATE pybind11::module)
  150. if(ARG_SYSTEM)
  151. message(
  152. STATUS
  153. "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets"
  154. )
  155. endif()
  156. pybind11_extension(${target_name})
  157. # -fvisibility=hidden is required to allow multiple modules compiled against
  158. # different pybind versions to work properly, and for some features (e.g.
  159. # py::module_local). We force it on everything inside the `pybind11`
  160. # namespace; also turning it on for a pybind module compilation here avoids
  161. # potential warnings or issues from having mixed hidden/non-hidden types.
  162. if(NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
  163. set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
  164. endif()
  165. if(NOT DEFINED CMAKE_CUDA_VISIBILITY_PRESET)
  166. set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
  167. endif()
  168. if(ARG_NO_EXTRAS)
  169. return()
  170. endif()
  171. if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
  172. if(ARG_THIN_LTO)
  173. target_link_libraries(${target_name} PRIVATE pybind11::thin_lto)
  174. else()
  175. target_link_libraries(${target_name} PRIVATE pybind11::lto)
  176. endif()
  177. endif()
  178. if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
  179. pybind11_strip(${target_name})
  180. endif()
  181. if(MSVC)
  182. target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
  183. endif()
  184. if(ARG_OPT_SIZE)
  185. target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
  186. endif()
  187. endfunction()
  188. # Provide general way to call common Python commands in "common" file.
  189. set(_Python
  190. PYTHON
  191. CACHE INTERNAL "" FORCE)