CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # CMakeLists.txt -- Build system for the pybind11 modules
  2. #
  3. # Copyright (c) 2015 Wenzel Jakob <[email protected]>
  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 2.8.12)
  8. if (POLICY CMP0048)
  9. # cmake warns if loaded from a min-3.0-required parent dir, so silence the warning:
  10. cmake_policy(SET CMP0048 NEW)
  11. endif()
  12. # CMake versions < 3.4.0 do not support try_compile/pthread checks without C as active language.
  13. if(CMAKE_VERSION VERSION_LESS 3.4.0)
  14. project(pybind11)
  15. else()
  16. project(pybind11 CXX)
  17. endif()
  18. # Check if pybind11 is being used directly or via add_subdirectory
  19. set(PYBIND11_MASTER_PROJECT OFF)
  20. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  21. set(PYBIND11_MASTER_PROJECT ON)
  22. endif()
  23. option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
  24. option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
  25. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
  26. include(pybind11Tools)
  27. # Cache variables so pybind11_add_module can be used in parent projects
  28. set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
  29. set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
  30. set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
  31. set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
  32. set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
  33. set(PYTHON_VERSION_MAJOR ${PYTHON_VERSION_MAJOR} CACHE INTERNAL "")
  34. set(PYTHON_VERSION_MINOR ${PYTHON_VERSION_MINOR} CACHE INTERNAL "")
  35. # NB: when adding a header don't forget to also add it to setup.py
  36. set(PYBIND11_HEADERS
  37. include/pybind11/detail/class.h
  38. include/pybind11/detail/common.h
  39. include/pybind11/detail/descr.h
  40. include/pybind11/detail/init.h
  41. include/pybind11/detail/internals.h
  42. include/pybind11/detail/typeid.h
  43. include/pybind11/attr.h
  44. include/pybind11/buffer_info.h
  45. include/pybind11/cast.h
  46. include/pybind11/chrono.h
  47. include/pybind11/common.h
  48. include/pybind11/complex.h
  49. include/pybind11/options.h
  50. include/pybind11/eigen.h
  51. include/pybind11/embed.h
  52. include/pybind11/eval.h
  53. include/pybind11/functional.h
  54. include/pybind11/numpy.h
  55. include/pybind11/operators.h
  56. include/pybind11/pybind11.h
  57. include/pybind11/pytypes.h
  58. include/pybind11/stl.h
  59. include/pybind11/stl_bind.h
  60. )
  61. string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
  62. PYBIND11_HEADERS "${PYBIND11_HEADERS}")
  63. if (PYBIND11_TEST)
  64. add_subdirectory(tests)
  65. endif()
  66. include(GNUInstallDirs)
  67. include(CMakePackageConfigHelpers)
  68. # extract project version from source
  69. file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/detail/common.h" pybind11_version_defines
  70. REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
  71. foreach(ver ${pybind11_version_defines})
  72. if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
  73. set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
  74. endif()
  75. endforeach()
  76. set(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH})
  77. message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}")
  78. option (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF)
  79. if (USE_PYTHON_INCLUDE_DIR)
  80. file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
  81. endif()
  82. if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
  83. # Build an interface library target:
  84. add_library(pybind11 INTERFACE)
  85. add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target
  86. target_include_directories(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
  87. $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
  88. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  89. target_compile_options(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
  90. add_library(module INTERFACE)
  91. add_library(pybind11::module ALIAS module)
  92. if(NOT MSVC)
  93. target_compile_options(module INTERFACE -fvisibility=hidden)
  94. endif()
  95. target_link_libraries(module INTERFACE pybind11::pybind11)
  96. if(WIN32 OR CYGWIN)
  97. target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
  98. elseif(APPLE)
  99. target_link_libraries(module INTERFACE "-undefined dynamic_lookup")
  100. endif()
  101. add_library(embed INTERFACE)
  102. add_library(pybind11::embed ALIAS embed)
  103. target_link_libraries(embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
  104. endif()
  105. if (PYBIND11_INSTALL)
  106. install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  107. # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
  108. set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake")
  109. configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in
  110. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  111. INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  112. # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
  113. # not depend on architecture specific settings or libraries.
  114. set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
  115. unset(CMAKE_SIZEOF_VOID_P)
  116. write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  117. VERSION ${${PROJECT_NAME}_VERSION}
  118. COMPATIBILITY AnyNewerVersion)
  119. set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})
  120. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  121. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  122. tools/FindPythonLibsNew.cmake
  123. tools/pybind11Tools.cmake
  124. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  125. if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
  126. if(NOT PYBIND11_EXPORT_NAME)
  127. set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
  128. endif()
  129. install(TARGETS pybind11 module embed
  130. EXPORT "${PYBIND11_EXPORT_NAME}")
  131. if(PYBIND11_MASTER_PROJECT)
  132. install(EXPORT "${PYBIND11_EXPORT_NAME}"
  133. NAMESPACE "${PROJECT_NAME}::"
  134. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  135. endif()
  136. endif()
  137. endif()