You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
5.7 KiB

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