CMakeLists.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # CMakeLists.txt -- Build system for the pybind11 test suite
  2. #
  3. # Copyright (c) 2015 Wenzel Jakob <[email protected]>
  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. option(PYBIND11_WERROR "Report all warnings as errors" OFF)
  9. if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  10. # We're being loaded directly, i.e. not via add_subdirectory, so make this
  11. # work as its own project and load the pybind11Config to get the tools we need
  12. project(pybind11_tests CXX)
  13. find_package(pybind11 REQUIRED CONFIG)
  14. endif()
  15. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  16. message(STATUS "Setting tests build type to MinSizeRel as none was specified")
  17. set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
  18. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
  19. "MinSizeRel" "RelWithDebInfo")
  20. endif()
  21. # Full set of test files (you can override these; see below)
  22. set(PYBIND11_TEST_FILES
  23. test_buffers.cpp
  24. test_builtin_casters.cpp
  25. test_call_policies.cpp
  26. test_callbacks.cpp
  27. test_chrono.cpp
  28. test_class.cpp
  29. test_constants_and_functions.cpp
  30. test_copy_move.cpp
  31. test_docstring_options.cpp
  32. test_eigen.cpp
  33. test_enum.cpp
  34. test_eval.cpp
  35. test_exceptions.cpp
  36. test_factory_constructors.cpp
  37. test_iostream.cpp
  38. test_kwargs_and_defaults.cpp
  39. test_local_bindings.cpp
  40. test_methods_and_attributes.cpp
  41. test_modules.cpp
  42. test_multiple_inheritance.cpp
  43. test_numpy_array.cpp
  44. test_numpy_dtypes.cpp
  45. test_numpy_vectorize.cpp
  46. test_opaque_types.cpp
  47. test_operator_overloading.cpp
  48. test_pickling.cpp
  49. test_pytypes.cpp
  50. test_sequences_and_iterators.cpp
  51. test_smart_ptr.cpp
  52. test_stl.cpp
  53. test_stl_binders.cpp
  54. test_virtual_functions.cpp
  55. )
  56. # Invoking cmake with something like:
  57. # cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" ..
  58. # lets you override the tests that get compiled and run. You can restore to all tests with:
  59. # cmake -DPYBIND11_TEST_OVERRIDE= ..
  60. if (PYBIND11_TEST_OVERRIDE)
  61. set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
  62. endif()
  63. string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
  64. # Contains the set of test files that require pybind11_cross_module_tests to be
  65. # built; if none of these are built (i.e. because TEST_OVERRIDE is used and
  66. # doesn't include them) the second module doesn't get built.
  67. set(PYBIND11_CROSS_MODULE_TESTS
  68. test_exceptions.py
  69. test_local_bindings.py
  70. test_stl.py
  71. test_stl_binders.py
  72. )
  73. # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
  74. # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
  75. # skip message).
  76. list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
  77. if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
  78. # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
  79. # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
  80. # produces a fatal error if loaded from a pre-3.0 cmake.
  81. if (NOT CMAKE_VERSION VERSION_LESS 3.0)
  82. find_package(Eigen3 QUIET CONFIG)
  83. if (EIGEN3_FOUND)
  84. if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1)
  85. set(PYBIND11_EIGEN_VIA_TARGET 1)
  86. endif()
  87. endif()
  88. endif()
  89. if (NOT EIGEN3_FOUND)
  90. # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
  91. # tools/FindEigen3.cmake
  92. find_package(Eigen3 QUIET)
  93. endif()
  94. if(EIGEN3_FOUND)
  95. # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
  96. # rather than looking it up in the cmake script); older versions, and the
  97. # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
  98. if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
  99. set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
  100. endif()
  101. message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
  102. else()
  103. list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
  104. message(STATUS "Building tests WITHOUT Eigen")
  105. endif()
  106. endif()
  107. # Optional dependency for some tests (boost::variant is only supported with version >= 1.56)
  108. find_package(Boost 1.56)
  109. # Compile with compiler warnings turned on
  110. function(pybind11_enable_warnings target_name)
  111. if(MSVC)
  112. target_compile_options(${target_name} PRIVATE /W4)
  113. else()
  114. target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
  115. endif()
  116. if(PYBIND11_WERROR)
  117. if(MSVC)
  118. target_compile_options(${target_name} PRIVATE /WX)
  119. else()
  120. target_compile_options(${target_name} PRIVATE -Werror)
  121. endif()
  122. endif()
  123. endfunction()
  124. set(test_targets pybind11_tests)
  125. # Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
  126. foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
  127. list(FIND PYBIND11_PYTEST_FILES ${t} i)
  128. if (i GREATER -1)
  129. list(APPEND test_targets pybind11_cross_module_tests)
  130. break()
  131. endif()
  132. endforeach()
  133. set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
  134. foreach(target ${test_targets})
  135. set(test_files ${PYBIND11_TEST_FILES})
  136. if(NOT target STREQUAL "pybind11_tests")
  137. set(test_files "")
  138. endif()
  139. # Create the binding library
  140. pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
  141. pybind11_enable_warnings(${target})
  142. if(MSVC)
  143. target_compile_options(${target} PRIVATE /utf-8)
  144. endif()
  145. if(EIGEN3_FOUND)
  146. if (PYBIND11_EIGEN_VIA_TARGET)
  147. target_link_libraries(${target} PRIVATE Eigen3::Eigen)
  148. else()
  149. target_include_directories(${target} PRIVATE ${EIGEN3_INCLUDE_DIR})
  150. endif()
  151. target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN)
  152. endif()
  153. if(Boost_FOUND)
  154. target_include_directories(${target} PRIVATE ${Boost_INCLUDE_DIRS})
  155. target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST)
  156. endif()
  157. # Always write the output file directly into the 'tests' directory (even on MSVC)
  158. if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  159. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
  160. foreach(config ${CMAKE_CONFIGURATION_TYPES})
  161. string(TOUPPER ${config} config)
  162. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
  163. endforeach()
  164. endif()
  165. endforeach()
  166. # Make sure pytest is found or produce a fatal error
  167. if(NOT PYBIND11_PYTEST_FOUND)
  168. execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
  169. RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET)
  170. if(pytest_not_found)
  171. message(FATAL_ERROR "Running the tests requires pytest. Please install it manually"
  172. " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
  173. elseif(pytest_version VERSION_LESS 3.0)
  174. message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
  175. "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)")
  176. endif()
  177. set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
  178. endif()
  179. if(CMAKE_VERSION VERSION_LESS 3.2)
  180. set(PYBIND11_USES_TERMINAL "")
  181. else()
  182. set(PYBIND11_USES_TERMINAL "USES_TERMINAL")
  183. endif()
  184. # A single command to compile and run the tests
  185. add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
  186. DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL})
  187. if(PYBIND11_TEST_OVERRIDE)
  188. add_custom_command(TARGET pytest POST_BUILD
  189. COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
  190. endif()
  191. # Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
  192. add_custom_target(check DEPENDS pytest)
  193. # The remaining tests only apply when being built as part of the pybind11 project, but not if the
  194. # tests are being built independently.
  195. if (NOT PROJECT_NAME STREQUAL "pybind11")
  196. return()
  197. endif()
  198. # Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it:
  199. add_custom_command(TARGET pybind11_tests POST_BUILD
  200. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
  201. $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
  202. # Test embedding the interpreter. Provides the `cpptest` target.
  203. add_subdirectory(test_embed)
  204. # Test CMake build using functions and targets from subdirectory or installed location
  205. add_subdirectory(test_cmake_build)