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.

238 lines
8.8 KiB

8 years 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_alias_initialization.cpp
  24. test_buffers.cpp
  25. test_callbacks.cpp
  26. test_chrono.cpp
  27. test_class_args.cpp
  28. test_constants_and_functions.cpp
  29. test_copy_move_policies.cpp
  30. test_docstring_options.cpp
  31. test_eigen.cpp
  32. test_enum.cpp
  33. test_eval.cpp
  34. test_exceptions.cpp
  35. test_inheritance.cpp
  36. test_issues.cpp
  37. test_keep_alive.cpp
  38. test_kwargs_and_defaults.cpp
  39. test_methods_and_attributes.cpp
  40. test_modules.cpp
  41. test_multiple_inheritance.cpp
  42. test_numpy_array.cpp
  43. test_numpy_dtypes.cpp
  44. test_numpy_vectorize.cpp
  45. test_opaque_types.cpp
  46. test_operator_overloading.cpp
  47. test_pickling.cpp
  48. test_python_types.cpp
  49. test_sequences_and_iterators.cpp
  50. test_smart_ptr.cpp
  51. test_stl_binders.cpp
  52. test_virtual_functions.cpp
  53. )
  54. # Invoking cmake with something like:
  55. # cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" ..
  56. # lets you override the tests that get compiled and run. You can restore to all tests with:
  57. # cmake -DPYBIND11_TEST_OVERRIDE= ..
  58. if (PYBIND11_TEST_OVERRIDE)
  59. set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
  60. endif()
  61. string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
  62. # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
  63. # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
  64. # skip message).
  65. list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
  66. if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
  67. # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
  68. # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
  69. # produces a fatal error if loaded from a pre-3.0 cmake.
  70. if (NOT CMAKE_VERSION VERSION_LESS 3.0)
  71. find_package(Eigen3 QUIET CONFIG)
  72. if (EIGEN3_FOUND)
  73. if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1)
  74. set(PYBIND11_EIGEN_VIA_TARGET 1)
  75. endif()
  76. endif()
  77. endif()
  78. if (NOT EIGEN3_FOUND)
  79. # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
  80. # tools/FindEigen3.cmake
  81. find_package(Eigen3 QUIET)
  82. endif()
  83. if(EIGEN3_FOUND)
  84. # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
  85. # rather than looking it up in the cmake script); older versions, and the
  86. # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
  87. if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
  88. set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
  89. endif()
  90. message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
  91. else()
  92. list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
  93. message(STATUS "Building tests WITHOUT Eigen")
  94. endif()
  95. endif()
  96. # Compile with compiler warnings turned on
  97. function(pybind11_enable_warnings target_name)
  98. if(MSVC)
  99. target_compile_options(${target_name} PRIVATE /W4)
  100. else()
  101. target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
  102. endif()
  103. if(PYBIND11_WERROR)
  104. if(MSVC)
  105. target_compile_options(${target_name} PRIVATE /WX)
  106. else()
  107. target_compile_options(${target_name} PRIVATE -Werror)
  108. endif()
  109. endif()
  110. endfunction()
  111. # Create the binding library
  112. pybind11_add_module(pybind11_tests THIN_LTO pybind11_tests.cpp
  113. ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
  114. pybind11_enable_warnings(pybind11_tests)
  115. if(EIGEN3_FOUND)
  116. if (PYBIND11_EIGEN_VIA_TARGET)
  117. target_link_libraries(pybind11_tests PRIVATE Eigen3::Eigen)
  118. else()
  119. target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
  120. endif()
  121. target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
  122. endif()
  123. set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
  124. # Always write the output file directly into the 'tests' directory (even on MSVC)
  125. if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  126. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
  127. foreach(config ${CMAKE_CONFIGURATION_TYPES})
  128. string(TOUPPER ${config} config)
  129. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
  130. endforeach()
  131. endif()
  132. # Make sure pytest is found or produce a fatal error
  133. if(NOT PYBIND11_PYTEST_FOUND)
  134. execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
  135. RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET)
  136. if(pytest_not_found)
  137. message(FATAL_ERROR "Running the tests requires pytest. Please install it manually"
  138. " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
  139. elseif(pytest_version VERSION_LESS 3.0)
  140. message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
  141. "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)")
  142. endif()
  143. set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
  144. endif()
  145. # A single command to compile and run the tests
  146. add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
  147. DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})
  148. if(PYBIND11_TEST_OVERRIDE)
  149. add_custom_command(TARGET pytest POST_BUILD
  150. COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
  151. endif()
  152. # Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
  153. add_custom_target(check DEPENDS pytest)
  154. # The remaining tests only apply when being built as part of the pybind11 project, but not if the
  155. # tests are being built independently.
  156. if (NOT PROJECT_NAME STREQUAL "pybind11")
  157. return()
  158. endif()
  159. # Add a post-build comment to show the .so size and, if a previous size, compare it:
  160. add_custom_command(TARGET pybind11_tests POST_BUILD
  161. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
  162. $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
  163. # Test CMake build using functions and targets from subdirectory or installed location
  164. add_custom_target(test_cmake_build)
  165. if(NOT CMAKE_VERSION VERSION_LESS 3.1)
  166. # 3.0 needed for interface library for subdirectory_target/installed_target
  167. # 3.1 needed for cmake -E env for testing
  168. include(CMakeParseArguments)
  169. function(pybind11_add_build_test name)
  170. cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN})
  171. set(build_options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/mock_install"
  172. "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
  173. "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
  174. "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}")
  175. if(NOT ARG_INSTALL)
  176. list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${PROJECT_SOURCE_DIR}")
  177. endif()
  178. add_custom_target(test_${name} ${CMAKE_CTEST_COMMAND}
  179. --quiet --output-log test_cmake_build/${name}.log
  180. --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_cmake_build/${name}"
  181. "${CMAKE_CURRENT_BINARY_DIR}/test_cmake_build/${name}"
  182. --build-config Release
  183. --build-noclean
  184. --build-generator ${CMAKE_GENERATOR}
  185. $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
  186. --build-makeprogram ${CMAKE_MAKE_PROGRAM}
  187. --build-target check
  188. --build-options ${build_options}
  189. )
  190. if(ARG_INSTALL)
  191. add_dependencies(test_${name} mock_install)
  192. endif()
  193. add_dependencies(test_cmake_build test_${name})
  194. endfunction()
  195. pybind11_add_build_test(subdirectory_function)
  196. pybind11_add_build_test(subdirectory_target)
  197. if(PYBIND11_INSTALL)
  198. add_custom_target(mock_install ${CMAKE_COMMAND}
  199. "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install"
  200. -P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
  201. )
  202. pybind11_add_build_test(installed_function INSTALL)
  203. pybind11_add_build_test(installed_target INSTALL)
  204. endif()
  205. endif()
  206. add_dependencies(check test_cmake_build)