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.

201 lines
7.2 KiB

3 months ago
  1. # CMakeLists.txt -- Build system for the pybind11 test suite
  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. 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)
  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_kwargs_and_defaults.cpp
  37. test_methods_and_attributes.cpp
  38. test_modules.cpp
  39. test_multiple_inheritance.cpp
  40. test_numpy_array.cpp
  41. test_numpy_dtypes.cpp
  42. test_numpy_vectorize.cpp
  43. test_opaque_types.cpp
  44. test_operator_overloading.cpp
  45. test_pickling.cpp
  46. test_pytypes.cpp
  47. test_sequences_and_iterators.cpp
  48. test_smart_ptr.cpp
  49. test_stl.cpp
  50. test_stl_binders.cpp
  51. test_virtual_functions.cpp
  52. )
  53. # Invoking cmake with something like:
  54. # cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" ..
  55. # lets you override the tests that get compiled and run. You can restore to all tests with:
  56. # cmake -DPYBIND11_TEST_OVERRIDE= ..
  57. if (PYBIND11_TEST_OVERRIDE)
  58. set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
  59. endif()
  60. string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
  61. # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
  62. # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
  63. # skip message).
  64. list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
  65. if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
  66. # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
  67. # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
  68. # produces a fatal error if loaded from a pre-3.0 cmake.
  69. if (NOT CMAKE_VERSION VERSION_LESS 3.0)
  70. find_package(Eigen3 QUIET CONFIG)
  71. if (EIGEN3_FOUND)
  72. if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1)
  73. set(PYBIND11_EIGEN_VIA_TARGET 1)
  74. endif()
  75. endif()
  76. endif()
  77. if (NOT EIGEN3_FOUND)
  78. # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
  79. # tools/FindEigen3.cmake
  80. find_package(Eigen3 QUIET)
  81. endif()
  82. if(EIGEN3_FOUND)
  83. # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
  84. # rather than looking it up in the cmake script); older versions, and the
  85. # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
  86. if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
  87. set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
  88. endif()
  89. message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
  90. else()
  91. list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
  92. message(STATUS "Building tests WITHOUT Eigen")
  93. endif()
  94. endif()
  95. # Compile with compiler warnings turned on
  96. function(pybind11_enable_warnings target_name)
  97. if(MSVC)
  98. target_compile_options(${target_name} PRIVATE /W4)
  99. else()
  100. target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
  101. endif()
  102. if(PYBIND11_WERROR)
  103. if(MSVC)
  104. target_compile_options(${target_name} PRIVATE /WX)
  105. else()
  106. target_compile_options(${target_name} PRIVATE -Werror)
  107. endif()
  108. endif()
  109. endfunction()
  110. # Create the binding library
  111. pybind11_add_module(pybind11_tests THIN_LTO pybind11_tests.cpp
  112. ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
  113. pybind11_enable_warnings(pybind11_tests)
  114. if(MSVC)
  115. target_compile_options(pybind11_tests PRIVATE /utf-8)
  116. endif()
  117. if(EIGEN3_FOUND)
  118. if (PYBIND11_EIGEN_VIA_TARGET)
  119. target_link_libraries(pybind11_tests PRIVATE Eigen3::Eigen)
  120. else()
  121. target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
  122. endif()
  123. target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
  124. endif()
  125. set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
  126. # Always write the output file directly into the 'tests' directory (even on MSVC)
  127. if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  128. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
  129. foreach(config ${CMAKE_CONFIGURATION_TYPES})
  130. string(TOUPPER ${config} config)
  131. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
  132. endforeach()
  133. endif()
  134. # Make sure pytest is found or produce a fatal error
  135. if(NOT PYBIND11_PYTEST_FOUND)
  136. execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
  137. RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET)
  138. if(pytest_not_found)
  139. message(FATAL_ERROR "Running the tests requires pytest. Please install it manually"
  140. " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
  141. elseif(pytest_version VERSION_LESS 3.0)
  142. message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
  143. "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)")
  144. endif()
  145. set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
  146. endif()
  147. if(CMAKE_VERSION VERSION_LESS 3.2)
  148. set(PYBIND11_USES_TERMINAL "")
  149. else()
  150. set(PYBIND11_USES_TERMINAL "USES_TERMINAL")
  151. endif()
  152. # A single command to compile and run the tests
  153. add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
  154. DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL})
  155. if(PYBIND11_TEST_OVERRIDE)
  156. add_custom_command(TARGET pytest POST_BUILD
  157. COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
  158. endif()
  159. # Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
  160. add_custom_target(check DEPENDS pytest)
  161. # The remaining tests only apply when being built as part of the pybind11 project, but not if the
  162. # tests are being built independently.
  163. if (NOT PROJECT_NAME STREQUAL "pybind11")
  164. return()
  165. endif()
  166. # Add a post-build comment to show the .so size and, if a previous size, compare it:
  167. add_custom_command(TARGET pybind11_tests POST_BUILD
  168. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
  169. $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
  170. # Test embedding the interpreter. Provides the `cpptest` target.
  171. add_subdirectory(test_embed)
  172. # Test CMake build using functions and targets from subdirectory or installed location
  173. add_subdirectory(test_cmake_build)