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.

239 lines
8.6 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. # Check if Eigen is available
  74. set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools")
  75. find_package(Eigen3 QUIET)
  76. # Include path for pybind11 header files
  77. include_directories(include)
  78. # Include path for Python header files
  79. include_directories(${PYTHON_INCLUDE_DIR})
  80. set(PYBIND11_HEADERS
  81. include/pybind11/attr.h
  82. include/pybind11/cast.h
  83. include/pybind11/common.h
  84. include/pybind11/complex.h
  85. include/pybind11/descr.h
  86. include/pybind11/eigen.h
  87. include/pybind11/functional.h
  88. include/pybind11/numpy.h
  89. include/pybind11/operators.h
  90. include/pybind11/pybind11.h
  91. include/pybind11/pytypes.h
  92. include/pybind11/stl.h
  93. include/pybind11/stl_bind.h
  94. include/pybind11/typeid.h
  95. )
  96. set(PYBIND11_EXAMPLES
  97. example/example1.cpp
  98. example/example2.cpp
  99. example/example3.cpp
  100. example/example4.cpp
  101. example/example5.cpp
  102. example/example6.cpp
  103. example/example7.cpp
  104. example/example8.cpp
  105. example/example9.cpp
  106. example/example10.cpp
  107. example/example11.cpp
  108. example/example12.cpp
  109. example/example13.cpp
  110. example/example14.cpp
  111. example/example15.cpp
  112. example/example16.cpp
  113. example/example17.cpp
  114. example/issues.cpp
  115. )
  116. if (EIGEN3_FOUND)
  117. include_directories(${EIGEN3_INCLUDE_DIR})
  118. list(APPEND PYBIND11_EXAMPLES example/eigen.cpp)
  119. add_definitions(-DPYBIND11_TEST_EIGEN)
  120. message(STATUS "Building Eigen testcase")
  121. else()
  122. message(STATUS "NOT Building Eigen testcase")
  123. endif()
  124. # Create the binding library
  125. add_library(example SHARED
  126. ${PYBIND11_HEADERS}
  127. example/example.cpp
  128. ${PYBIND11_EXAMPLES}
  129. )
  130. # Don't add a 'lib' prefix to the shared library
  131. set_target_properties(example PROPERTIES PREFIX "")
  132. # Always write the output file directly into the 'example' directory (even on MSVC)
  133. set(CompilerFlags
  134. LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
  135. LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
  136. RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
  137. RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
  138. foreach(CompilerFlag ${CompilerFlags})
  139. set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
  140. endforeach()
  141. if (WIN32)
  142. if (MSVC)
  143. # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
  144. # needed for bigger binding projects due to the limit to 64k addressable sections
  145. set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS /MP /bigobj)
  146. # Enforce size-based optimization and link time code generation on MSVC
  147. # (~30% smaller binaries in experiments); do nothing in debug mode.
  148. set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS
  149. "$<$<CONFIG:Release>:/Os>" "$<$<CONFIG:Release>:/GL>"
  150. "$<$<CONFIG:MinSizeRel>:/Os>" "$<$<CONFIG:MinSizeRel>:/GL>"
  151. "$<$<CONFIG:RelWithDebInfo>:/Os>" "$<$<CONFIG:RelWithDebInfo>:/GL>"
  152. )
  153. set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELEASE "/LTCG ")
  154. set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL "/LTCG ")
  155. set_property(TARGET example APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO "/LTCG ")
  156. endif()
  157. # .PYD file extension on Windows
  158. set_target_properties(example PROPERTIES SUFFIX ".pyd")
  159. # Link against the Python shared library
  160. target_link_libraries(example ${PYTHON_LIBRARY})
  161. elseif (UNIX)
  162. # It's quite common to have multiple copies of the same Python version
  163. # installed on one's system. E.g.: one copy from the OS and another copy
  164. # that's statically linked into an application like Blender or Maya.
  165. # If we link our plugin library against the OS Python here and import it
  166. # into Blender or Maya later on, this will cause segfaults when multiple
  167. # conflicting Python instances are active at the same time (even when they
  168. # are of the same version).
  169. # Windows is not affected by this issue since it handles DLL imports
  170. # differently. The solution for Linux and Mac OS is simple: we just don't
  171. # link against the Python library. The resulting shared library will have
  172. # missing symbols, but that's perfectly fine -- they will be resolved at
  173. # import time.
  174. # .SO file extension on Linux/Mac OS
  175. set_target_properties(example PROPERTIES SUFFIX ".so")
  176. # Optimize for a small binary size
  177. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  178. set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
  179. endif()
  180. # Strip unnecessary sections of the binary on Linux/Mac OS
  181. if(APPLE)
  182. set_target_properties(example PROPERTIES MACOSX_RPATH ".")
  183. set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
  184. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  185. add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
  186. endif()
  187. else()
  188. if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
  189. add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
  190. endif()
  191. endif()
  192. endif()
  193. enable_testing()
  194. set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
  195. if (MSVC OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
  196. set(RUN_TEST ${RUN_TEST} --relaxed)
  197. endif()
  198. foreach(VALUE ${PYBIND11_EXAMPLES})
  199. string(REGEX REPLACE "^example/(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
  200. add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
  201. endforeach()
  202. if (PYBIND11_INSTALL)
  203. install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
  204. endif()