CMakeLists.txt revision 11986:c12e4625ab56
112855Sgabeblack@google.comif(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 212855Sgabeblack@google.com message(STATUS "Setting tests build type to MinSizeRel as none was specified") 312855Sgabeblack@google.com set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) 412855Sgabeblack@google.com set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" 512855Sgabeblack@google.com "MinSizeRel" "RelWithDebInfo") 612855Sgabeblack@google.comendif() 712855Sgabeblack@google.com 812855Sgabeblack@google.com# Full set of test files (you can override these; see below) 912855Sgabeblack@google.comset(PYBIND11_TEST_FILES 1012855Sgabeblack@google.com test_alias_initialization.cpp 1112855Sgabeblack@google.com test_buffers.cpp 1212855Sgabeblack@google.com test_callbacks.cpp 1312855Sgabeblack@google.com test_chrono.cpp 1412855Sgabeblack@google.com test_class_args.cpp 1512855Sgabeblack@google.com test_constants_and_functions.cpp 1612855Sgabeblack@google.com test_copy_move_policies.cpp 1712855Sgabeblack@google.com test_docstring_options.cpp 1812855Sgabeblack@google.com test_eigen.cpp 1912855Sgabeblack@google.com test_enum.cpp 2012855Sgabeblack@google.com test_eval.cpp 2112855Sgabeblack@google.com test_exceptions.cpp 2212855Sgabeblack@google.com test_inheritance.cpp 2312855Sgabeblack@google.com test_issues.cpp 2412855Sgabeblack@google.com test_keep_alive.cpp 2512855Sgabeblack@google.com test_kwargs_and_defaults.cpp 2612855Sgabeblack@google.com test_methods_and_attributes.cpp 2712855Sgabeblack@google.com test_modules.cpp 2812855Sgabeblack@google.com test_multiple_inheritance.cpp 2912855Sgabeblack@google.com test_numpy_array.cpp 3012855Sgabeblack@google.com test_numpy_dtypes.cpp 3112855Sgabeblack@google.com test_numpy_vectorize.cpp 3212855Sgabeblack@google.com test_opaque_types.cpp 3312855Sgabeblack@google.com test_operator_overloading.cpp 3412855Sgabeblack@google.com test_pickling.cpp 3512855Sgabeblack@google.com test_python_types.cpp 3612855Sgabeblack@google.com test_sequences_and_iterators.cpp 3712855Sgabeblack@google.com test_smart_ptr.cpp 3812855Sgabeblack@google.com test_stl_binders.cpp 3912855Sgabeblack@google.com test_virtual_functions.cpp 4012855Sgabeblack@google.com) 4112855Sgabeblack@google.com 4212855Sgabeblack@google.com# Invoking cmake with something like: 4312855Sgabeblack@google.com# cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" .. 4412855Sgabeblack@google.com# lets you override the tests that get compiled and run. You can restore to all tests with: 4512855Sgabeblack@google.com# cmake -DPYBIND11_TEST_OVERRIDE= .. 4612855Sgabeblack@google.comif (PYBIND11_TEST_OVERRIDE) 4712855Sgabeblack@google.com set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE}) 4812855Sgabeblack@google.comendif() 4912855Sgabeblack@google.com 5012855Sgabeblack@google.comstring(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}") 5112855Sgabeblack@google.com 5212855Sgabeblack@google.com# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but 5312855Sgabeblack@google.com# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" 5412855Sgabeblack@google.com# skip message). 5512855Sgabeblack@google.comlist(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I) 5612855Sgabeblack@google.comif(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) 5712855Sgabeblack@google.com find_package(Eigen3 QUIET) 5812855Sgabeblack@google.com 5912855Sgabeblack@google.com if(EIGEN3_FOUND) 6012855Sgabeblack@google.com message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}") 6112855Sgabeblack@google.com else() 6212855Sgabeblack@google.com list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) 6312855Sgabeblack@google.com message(STATUS "Building tests WITHOUT Eigen") 6412855Sgabeblack@google.com endif() 6512855Sgabeblack@google.comendif() 6612855Sgabeblack@google.com 6712855Sgabeblack@google.com# Create the binding library 6812855Sgabeblack@google.compybind11_add_module(pybind11_tests pybind11_tests.cpp 6912855Sgabeblack@google.com ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS}) 7012855Sgabeblack@google.com 7112855Sgabeblack@google.compybind11_enable_warnings(pybind11_tests) 7212855Sgabeblack@google.com 7312855Sgabeblack@google.comif(EIGEN3_FOUND) 7412855Sgabeblack@google.com target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR}) 7512855Sgabeblack@google.com target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN) 7612855Sgabeblack@google.comendif() 7712855Sgabeblack@google.com 7812855Sgabeblack@google.comset(testdir ${PROJECT_SOURCE_DIR}/tests) 7912855Sgabeblack@google.com 8012855Sgabeblack@google.com# Always write the output file directly into the 'tests' directory (even on MSVC) 8112855Sgabeblack@google.comif(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) 8212855Sgabeblack@google.com set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir}) 8312855Sgabeblack@google.com foreach(config ${CMAKE_CONFIGURATION_TYPES}) 8412855Sgabeblack@google.com string(TOUPPER ${config} config) 8512855Sgabeblack@google.com set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir}) 8612855Sgabeblack@google.com endforeach() 8712855Sgabeblack@google.comendif() 8812855Sgabeblack@google.com 8912855Sgabeblack@google.com# Make sure pytest is found or produce a fatal error 9012855Sgabeblack@google.comif(NOT PYBIND11_PYTEST_FOUND) 9112855Sgabeblack@google.com execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET 9212855Sgabeblack@google.com RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR) 9312855Sgabeblack@google.com if(PYBIND11_EXEC_PYTHON_ERR) 9412855Sgabeblack@google.com message(FATAL_ERROR "Running the tests requires pytest. Please install it manually (try: ${PYTHON_EXECUTABLE} -m pip install pytest)") 9512855Sgabeblack@google.com endif() 9612855Sgabeblack@google.com set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "") 9712855Sgabeblack@google.comendif() 9812855Sgabeblack@google.com 9912855Sgabeblack@google.com# A single command to compile and run the tests 10012855Sgabeblack@google.comadd_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES} 10112855Sgabeblack@google.com DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir}) 10212855Sgabeblack@google.com 10312855Sgabeblack@google.comif(PYBIND11_TEST_OVERRIDE) 10412855Sgabeblack@google.com add_custom_command(TARGET pytest POST_BUILD 10512855Sgabeblack@google.com COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") 10612855Sgabeblack@google.comendif() 10712855Sgabeblack@google.com 10812855Sgabeblack@google.com# test use of installation 10912855Sgabeblack@google.comif(PYBIND11_INSTALL) 11012855Sgabeblack@google.com # 2.8.12 needed for test_installed_module 11112855Sgabeblack@google.com # 3.0 needed for interface library for test_installed_target 11212855Sgabeblack@google.com # 3.1 needed for cmake -E env for testing 11312855Sgabeblack@google.com if(NOT CMAKE_VERSION VERSION_LESS 3.1) 11412855Sgabeblack@google.com add_custom_target(test_installed_target 11512855Sgabeblack@google.com COMMAND ${CMAKE_COMMAND} 11612855Sgabeblack@google.com "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" 11712855Sgabeblack@google.com -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" 11812855Sgabeblack@google.com COMMAND ${CMAKE_CTEST_COMMAND} 11912855Sgabeblack@google.com --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_target" 12012855Sgabeblack@google.com "${CMAKE_CURRENT_BINARY_DIR}/test_installed_target" 12112855Sgabeblack@google.com --build-noclean 12212855Sgabeblack@google.com --build-generator ${CMAKE_GENERATOR} 12312855Sgabeblack@google.com $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} 12412855Sgabeblack@google.com --build-makeprogram ${CMAKE_MAKE_PROGRAM} 12512855Sgabeblack@google.com --build-target check 12612855Sgabeblack@google.com --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" 12712855Sgabeblack@google.com "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 12812855Sgabeblack@google.com "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" 12912855Sgabeblack@google.com "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}" 13012855Sgabeblack@google.com ) 13112855Sgabeblack@google.com add_custom_target(test_installed_module 13212855Sgabeblack@google.com COMMAND ${CMAKE_COMMAND} 13312855Sgabeblack@google.com "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" 13412855Sgabeblack@google.com -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" 13512855Sgabeblack@google.com COMMAND ${CMAKE_CTEST_COMMAND} 13612855Sgabeblack@google.com --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_module" 13712855Sgabeblack@google.com "${CMAKE_CURRENT_BINARY_DIR}/test_installed_module" 13812855Sgabeblack@google.com --build-noclean 13912855Sgabeblack@google.com --build-generator ${CMAKE_GENERATOR} 14012855Sgabeblack@google.com $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} 14112855Sgabeblack@google.com --build-makeprogram ${CMAKE_MAKE_PROGRAM} 14212855Sgabeblack@google.com --build-target check 14312855Sgabeblack@google.com --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" 14412855Sgabeblack@google.com "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" 14512855Sgabeblack@google.com "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" 14612855Sgabeblack@google.com "-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