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.

170 lines
6.7 KiB

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