CMakeLists.txt revision 11986:c12e4625ab56
1if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 2 message(STATUS "Setting tests build type to MinSizeRel as none was specified") 3 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) 4 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" 5 "MinSizeRel" "RelWithDebInfo") 6endif() 7 8# Full set of test files (you can override these; see below) 9set(PYBIND11_TEST_FILES 10 test_alias_initialization.cpp 11 test_buffers.cpp 12 test_callbacks.cpp 13 test_chrono.cpp 14 test_class_args.cpp 15 test_constants_and_functions.cpp 16 test_copy_move_policies.cpp 17 test_docstring_options.cpp 18 test_eigen.cpp 19 test_enum.cpp 20 test_eval.cpp 21 test_exceptions.cpp 22 test_inheritance.cpp 23 test_issues.cpp 24 test_keep_alive.cpp 25 test_kwargs_and_defaults.cpp 26 test_methods_and_attributes.cpp 27 test_modules.cpp 28 test_multiple_inheritance.cpp 29 test_numpy_array.cpp 30 test_numpy_dtypes.cpp 31 test_numpy_vectorize.cpp 32 test_opaque_types.cpp 33 test_operator_overloading.cpp 34 test_pickling.cpp 35 test_python_types.cpp 36 test_sequences_and_iterators.cpp 37 test_smart_ptr.cpp 38 test_stl_binders.cpp 39 test_virtual_functions.cpp 40) 41 42# Invoking cmake with something like: 43# cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" .. 44# lets you override the tests that get compiled and run. You can restore to all tests with: 45# cmake -DPYBIND11_TEST_OVERRIDE= .. 46if (PYBIND11_TEST_OVERRIDE) 47 set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE}) 48endif() 49 50string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}") 51 52# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but 53# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" 54# skip message). 55list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I) 56if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) 57 find_package(Eigen3 QUIET) 58 59 if(EIGEN3_FOUND) 60 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}") 61 else() 62 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) 63 message(STATUS "Building tests WITHOUT Eigen") 64 endif() 65endif() 66 67# Create the binding library 68pybind11_add_module(pybind11_tests pybind11_tests.cpp 69 ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS}) 70 71pybind11_enable_warnings(pybind11_tests) 72 73if(EIGEN3_FOUND) 74 target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR}) 75 target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN) 76endif() 77 78set(testdir ${PROJECT_SOURCE_DIR}/tests) 79 80# Always write the output file directly into the 'tests' directory (even on MSVC) 81if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) 82 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir}) 83 foreach(config ${CMAKE_CONFIGURATION_TYPES}) 84 string(TOUPPER ${config} config) 85 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir}) 86 endforeach() 87endif() 88 89# Make sure pytest is found or produce a fatal error 90if(NOT PYBIND11_PYTEST_FOUND) 91 execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET 92 RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR) 93 if(PYBIND11_EXEC_PYTHON_ERR) 94 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually (try: ${PYTHON_EXECUTABLE} -m pip install pytest)") 95 endif() 96 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "") 97endif() 98 99# A single command to compile and run the tests 100add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES} 101 DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir}) 102 103if(PYBIND11_TEST_OVERRIDE) 104 add_custom_command(TARGET pytest POST_BUILD 105 COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") 106endif() 107 108# test use of installation 109if(PYBIND11_INSTALL) 110 # 2.8.12 needed for test_installed_module 111 # 3.0 needed for interface library for test_installed_target 112 # 3.1 needed for cmake -E env for testing 113 if(NOT CMAKE_VERSION VERSION_LESS 3.1) 114 add_custom_target(test_installed_target 115 COMMAND ${CMAKE_COMMAND} 116 "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" 117 -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" 118 COMMAND ${CMAKE_CTEST_COMMAND} 119 --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_target" 120 "${CMAKE_CURRENT_BINARY_DIR}/test_installed_target" 121 --build-noclean 122 --build-generator ${CMAKE_GENERATOR} 123 $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} 124 --build-makeprogram ${CMAKE_MAKE_PROGRAM} 125 --build-target check 126 --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" 127 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 128 "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" 129 "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}" 130 ) 131 add_custom_target(test_installed_module 132 COMMAND ${CMAKE_COMMAND} 133 "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" 134 -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" 135 COMMAND ${CMAKE_CTEST_COMMAND} 136 --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_module" 137 "${CMAKE_CURRENT_BINARY_DIR}/test_installed_module" 138 --build-noclean 139 --build-generator ${CMAKE_GENERATOR} 140 $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} 141 --build-makeprogram ${CMAKE_MAKE_PROGRAM} 142 --build-target check 143 --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" 144 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 145 "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" 146 "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}" 147 ) 148 else() 149 add_custom_target(test_installed_target) 150 add_custom_target(test_installed_module) 151 endif() 152 add_custom_target(test_install) 153 add_dependencies(test_install test_installed_target test_installed_module) 154endif() 155 156# And another to show the .so size and, if a previous size, compare it: 157add_custom_command(TARGET pybind11_tests POST_BUILD 158 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/libsize.py 159 $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt) 160