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.

124 lines
4.9 KiB

8 years 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. set(PYBIND11_HEADERS
  29. include/pybind11/attr.h
  30. include/pybind11/cast.h
  31. include/pybind11/chrono.h
  32. include/pybind11/class_support.h
  33. include/pybind11/common.h
  34. include/pybind11/complex.h
  35. include/pybind11/descr.h
  36. include/pybind11/options.h
  37. include/pybind11/eigen.h
  38. include/pybind11/eval.h
  39. include/pybind11/functional.h
  40. include/pybind11/numpy.h
  41. include/pybind11/operators.h
  42. include/pybind11/pybind11.h
  43. include/pybind11/pytypes.h
  44. include/pybind11/stl.h
  45. include/pybind11/stl_bind.h
  46. include/pybind11/typeid.h
  47. )
  48. string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
  49. PYBIND11_HEADERS "${PYBIND11_HEADERS}")
  50. if (PYBIND11_TEST)
  51. add_subdirectory(tests)
  52. endif()
  53. include(GNUInstallDirs)
  54. include(CMakePackageConfigHelpers)
  55. # extract project version from source
  56. file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/common.h" pybind11_version_defines
  57. REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
  58. foreach(ver ${pybind11_version_defines})
  59. if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
  60. set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
  61. endif()
  62. endforeach()
  63. set(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH})
  64. message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}")
  65. option (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF)
  66. if (USE_PYTHON_INCLUDE_DIR)
  67. file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
  68. endif()
  69. if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
  70. # Build an interface library target:
  71. add_library(module INTERFACE)
  72. target_include_directories(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
  73. $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
  74. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  75. if(WIN32 OR CYGWIN)
  76. target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
  77. elseif(APPLE)
  78. target_link_libraries(module INTERFACE "-undefined dynamic_lookup")
  79. endif()
  80. target_compile_options(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
  81. add_library(pybind11::module ALIAS module) # to match exported target
  82. endif()
  83. if (PYBIND11_INSTALL)
  84. install(FILES ${PYBIND11_HEADERS}
  85. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/pybind11)
  86. # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
  87. set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake")
  88. configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in
  89. "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  90. INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  91. write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  92. VERSION ${${PROJECT_NAME}_VERSION}
  93. COMPATIBILITY AnyNewerVersion)
  94. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
  95. ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
  96. tools/FindPythonLibsNew.cmake
  97. tools/pybind11Tools.cmake
  98. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  99. if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
  100. install(TARGETS module
  101. EXPORT "${PROJECT_NAME}Targets")
  102. install(EXPORT "${PROJECT_NAME}Targets"
  103. NAMESPACE "${PROJECT_NAME}::"
  104. DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
  105. endif()
  106. endif()