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.

220 lines
7.8 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)
  8. project(pybind11)
  9. option(PYBIND11_INSTALL "Install pybind11 header files?" ON)
  10. # Add a CMake parameter for choosing a desired Python version
  11. set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
  12. include(CheckCXXCompilerFlag)
  13. # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
  14. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  15. message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
  16. set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
  17. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
  18. "MinSizeRel" "RelWithDebInfo")
  19. endif()
  20. string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
  21. set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
  22. if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
  23. find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
  24. if (NOT PythonLibs_FOUND)
  25. find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
  26. endif()
  27. else()
  28. find_package(PythonLibs REQUIRED)
  29. endif()
  30. # The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next line to work
  31. string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}")
  32. find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
  33. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  34. CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
  35. CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
  36. if (HAS_CPP14_FLAG)
  37. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
  38. elseif (HAS_CPP11_FLAG)
  39. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  40. else()
  41. message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
  42. endif()
  43. # Enable link time optimization and set the default symbol
  44. # visibility to hidden (very important to obtain small binaries)
  45. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  46. # Default symbol visibility
  47. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
  48. # Check for Link Time Optimization support
  49. # (GCC/Clang)
  50. CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
  51. if (HAS_LTO_FLAG)
  52. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
  53. endif()
  54. # Intel equivalent to LTO is called IPO
  55. if (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  56. CHECK_CXX_COMPILER_FLAG("-ipo" HAS_IPO_FLAG)
  57. if (HAS_IPO_FLAG)
  58. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ipo")
  59. endif()
  60. endif()
  61. endif()
  62. endif()
  63. # Compile with compiler warnings turned on
  64. if(MSVC)
  65. if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
  66. string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  67. else()
  68. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  69. endif()
  70. elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
  71. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
  72. endif()
  73. # Include path for Python header files
  74. include_directories(${PYTHON_INCLUDE_DIR})
  75. # Include path for pybind11 header files
  76. include_directories(include)
  77. set(PYBIND11_HEADERS
  78. include/pybind11/attr.h
  79. include/pybind11/cast.h
  80. include/pybind11/common.h
  81. include/pybind11/complex.h
  82. include/pybind11/descr.h
  83. include/pybind11/functional.h
  84. include/pybind11/numpy.h
  85. include/pybind11/operators.h
  86. include/pybind11/pybind11.h
  87. include/pybind11/pytypes.h
  88. include/pybind11/stl.h
  89. include/pybind11/typeid.h
  90. )
  91. set(PYBIND11_EXAMPLES
  92. example/example1.cpp
  93. example/example2.cpp
  94. example/example3.cpp
  95. example/example4.cpp
  96. example/example5.cpp
  97. example/example6.cpp
  98. example/example7.cpp
  99. example/example8.cpp
  100. example/example9.cpp
  101. example/example10.cpp
  102. example/example11.cpp
  103. example/example12.cpp
  104. example/example13.cpp
  105. example/example14.cpp
  106. example/example15.cpp
  107. example/example16.cpp
  108. example/issues.cpp
  109. )
  110. # Create the binding library
  111. add_library(example SHARED
  112. ${PYBIND11_HEADERS}
  113. example/example.cpp
  114. ${PYBIND11_EXAMPLES}
  115. )
  116. # Don't add a 'lib' prefix to the shared library
  117. set_target_properties(example PROPERTIES PREFIX "")
  118. # Always write the output file directly into the 'example' directory (even on MSVC)
  119. set(CompilerFlags
  120. LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
  121. LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
  122. RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
  123. RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
  124. foreach(CompilerFlag ${CompilerFlags})
  125. set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
  126. endforeach()
  127. if (WIN32)
  128. if (MSVC)
  129. # /bigobj is needed for bigger binding projects due to the limit to 64k
  130. # addressable sections. /MP enables multithreaded builds (relevant when
  131. # there are many files).
  132. set_target_properties(example PROPERTIES COMPILE_FLAGS "/MP /bigobj ")
  133. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  134. # Enforce size-based optimization and link time code generation on MSVC
  135. # (~30% smaller binaries in experiments).
  136. set_target_properties(example APPEND_STRING PROPERTY COMPILE_FLAGS "/Os /GL ")
  137. set_target_properties(example APPEND_STRING PROPERTY LINK_FLAGS "/LTCG ")
  138. endif()
  139. endif()
  140. # .PYD file extension on Windows
  141. set_target_properties(example PROPERTIES SUFFIX ".pyd")
  142. # Link against the Python shared library
  143. target_link_libraries(example ${PYTHON_LIBRARY})
  144. elseif (UNIX)
  145. # It's quite common to have multiple copies of the same Python version
  146. # installed on one's system. E.g.: one copy from the OS and another copy
  147. # that's statically linked into an application like Blender or Maya.
  148. # If we link our plugin library against the OS Python here and import it
  149. # into Blender or Maya later on, this will cause segfaults when multiple
  150. # conflicting Python instances are active at the same time (even when they
  151. # are of the same version).
  152. # Windows is not affected by this issue since it handles DLL imports
  153. # differently. The solution for Linux and Mac OS is simple: we just don't
  154. # link against the Python library. The resulting shared library will have
  155. # missing symbols, but that's perfectly fine -- they will be resolved at
  156. # import time.
  157. # .SO file extension on Linux/Mac OS
  158. set_target_properties(example PROPERTIES SUFFIX ".so")
  159. # Optimize for a small binary size
  160. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  161. set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
  162. endif()
  163. # Strip unnecessary sections of the binary on Linux/Mac OS
  164. if(APPLE)
  165. set_target_properties(example PROPERTIES MACOSX_RPATH ".")
  166. set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
  167. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  168. add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
  169. endif()
  170. else()
  171. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  172. add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
  173. endif()
  174. endif()
  175. endif()
  176. enable_testing()
  177. set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
  178. if (MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  179. set(RUN_TEST ${RUN_TEST} --relaxed)
  180. endif()
  181. foreach(VALUE ${PYBIND11_EXAMPLES})
  182. string(REGEX REPLACE "^example/(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
  183. add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
  184. endforeach()
  185. if (PYBIND11_INSTALL)
  186. install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
  187. endif()