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.

184 lines
7.0 KiB

  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. project(pybind11)
  9. # Check if pybind11 is being used directly or via add_subdirectory
  10. set(PYBIND11_MASTER_PROJECT OFF)
  11. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  12. set(PYBIND11_MASTER_PROJECT ON)
  13. endif()
  14. option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
  15. option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
  16. option(PYBIND11_WERROR "Report all warnings as errors" OFF)
  17. # Add a CMake parameter for choosing a desired Python version
  18. set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
  19. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
  20. set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
  21. find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
  22. include(CheckCXXCompilerFlag)
  23. if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
  24. check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
  25. check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
  26. if (HAS_CPP14_FLAG)
  27. set(PYBIND11_CPP_STANDARD -std=c++14)
  28. elseif (HAS_CPP11_FLAG)
  29. set(PYBIND11_CPP_STANDARD -std=c++11)
  30. else()
  31. message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
  32. endif()
  33. set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
  34. "C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)
  35. endif()
  36. # Cache variables so pybind11_add_module can be used in parent projects
  37. set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
  38. set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
  39. set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
  40. set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
  41. set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
  42. # Build a Python extension module:
  43. # pybind11_add_module(<name> source1 [source2 ...])
  44. #
  45. function(pybind11_add_module target_name)
  46. add_library(${target_name} MODULE ${ARGN})
  47. target_include_directories(${target_name}
  48. PRIVATE ${PYBIND11_INCLUDE_DIR}
  49. PRIVATE ${PYTHON_INCLUDE_DIRS})
  50. # The prefix and extension are provided by FindPythonLibsNew.cmake
  51. set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
  52. set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
  53. if(WIN32 OR CYGWIN)
  54. # Link against the Python shared library on Windows
  55. target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
  56. elseif(APPLE)
  57. # It's quite common to have multiple copies of the same Python version
  58. # installed on one's system. E.g.: one copy from the OS and another copy
  59. # that's statically linked into an application like Blender or Maya.
  60. # If we link our plugin library against the OS Python here and import it
  61. # into Blender or Maya later on, this will cause segfaults when multiple
  62. # conflicting Python instances are active at the same time (even when they
  63. # are of the same version).
  64. # Windows is not affected by this issue since it handles DLL imports
  65. # differently. The solution for Linux and Mac OS is simple: we just don't
  66. # link against the Python library. The resulting shared library will have
  67. # missing symbols, but that's perfectly fine -- they will be resolved at
  68. # import time.
  69. target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
  70. endif()
  71. if(NOT MSVC)
  72. # Make sure C++11/14 are enabled
  73. target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
  74. # Enable link time optimization and set the default symbol
  75. # visibility to hidden (very important to obtain small binaries)
  76. string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
  77. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  78. # Check for Link Time Optimization support (GCC/Clang)
  79. check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
  80. if(HAS_LTO_FLAG AND NOT CYGWIN)
  81. target_compile_options(${target_name} PRIVATE -flto)
  82. endif()
  83. # Intel equivalent to LTO is called IPO
  84. if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  85. check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
  86. if(HAS_IPO_FLAG)
  87. target_compile_options(${target_name} PRIVATE -ipo)
  88. endif()
  89. endif()
  90. # Default symbol visibility
  91. target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
  92. # Strip unnecessary sections of the binary on Linux/Mac OS
  93. if(CMAKE_STRIP)
  94. if(APPLE)
  95. add_custom_command(TARGET ${target_name} POST_BUILD
  96. COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
  97. else()
  98. add_custom_command(TARGET ${target_name} POST_BUILD
  99. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
  100. endif()
  101. endif()
  102. endif()
  103. elseif(MSVC)
  104. # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
  105. # needed for bigger binding projects due to the limit to 64k addressable sections
  106. target_compile_options(${target_name} PRIVATE /MP /bigobj)
  107. # Enforce link time code generation on MSVC, except in debug mode
  108. target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
  109. # Fancy generator expressions don't work with linker flags, for reasons unknown
  110. set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
  111. set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
  112. set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
  113. endif()
  114. endfunction()
  115. # Compile with compiler warnings turned on
  116. function(pybind11_enable_warnings target_name)
  117. if(MSVC)
  118. target_compile_options(${target_name} PRIVATE /W4)
  119. else()
  120. target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion)
  121. endif()
  122. if(PYBIND11_WERROR)
  123. if(MSVC)
  124. target_compile_options(${target_name} PRIVATE /WX)
  125. else()
  126. target_compile_options(${target_name} PRIVATE -Werror)
  127. endif()
  128. endif()
  129. endfunction()
  130. set(PYBIND11_HEADERS
  131. include/pybind11/attr.h
  132. include/pybind11/cast.h
  133. include/pybind11/chrono.h
  134. include/pybind11/common.h
  135. include/pybind11/complex.h
  136. include/pybind11/descr.h
  137. include/pybind11/options.h
  138. include/pybind11/eigen.h
  139. include/pybind11/eval.h
  140. include/pybind11/functional.h
  141. include/pybind11/numpy.h
  142. include/pybind11/operators.h
  143. include/pybind11/pybind11.h
  144. include/pybind11/pytypes.h
  145. include/pybind11/stl.h
  146. include/pybind11/stl_bind.h
  147. include/pybind11/typeid.h
  148. )
  149. string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
  150. PYBIND11_HEADERS "${PYBIND11_HEADERS}")
  151. if (PYBIND11_TEST)
  152. add_subdirectory(tests)
  153. endif()
  154. if (PYBIND11_INSTALL)
  155. install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
  156. endif()