CMakeLists.txt revision 11986
1695SN/Aif(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
29262Ssascha.bischoff@arm.com  message(STATUS "Setting tests build type to MinSizeRel as none was specified")
39262Ssascha.bischoff@arm.com  set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
49262Ssascha.bischoff@arm.com  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
59262Ssascha.bischoff@arm.com    "MinSizeRel" "RelWithDebInfo")
69262Ssascha.bischoff@arm.comendif()
79262Ssascha.bischoff@arm.com
89262Ssascha.bischoff@arm.com# Full set of test files (you can override these; see below)
99262Ssascha.bischoff@arm.comset(PYBIND11_TEST_FILES
109262Ssascha.bischoff@arm.com  test_alias_initialization.cpp
119262Ssascha.bischoff@arm.com  test_buffers.cpp
129262Ssascha.bischoff@arm.com  test_callbacks.cpp
139262Ssascha.bischoff@arm.com  test_chrono.cpp
141762SN/A  test_class_args.cpp
15695SN/A  test_constants_and_functions.cpp
16695SN/A  test_copy_move_policies.cpp
17695SN/A  test_docstring_options.cpp
18695SN/A  test_eigen.cpp
19695SN/A  test_enum.cpp
20695SN/A  test_eval.cpp
21695SN/A  test_exceptions.cpp
22695SN/A  test_inheritance.cpp
23695SN/A  test_issues.cpp
24695SN/A  test_keep_alive.cpp
25695SN/A  test_kwargs_and_defaults.cpp
26695SN/A  test_methods_and_attributes.cpp
27695SN/A  test_modules.cpp
28695SN/A  test_multiple_inheritance.cpp
29695SN/A  test_numpy_array.cpp
30695SN/A  test_numpy_dtypes.cpp
31695SN/A  test_numpy_vectorize.cpp
32695SN/A  test_opaque_types.cpp
33695SN/A  test_operator_overloading.cpp
34695SN/A  test_pickling.cpp
35695SN/A  test_python_types.cpp
36695SN/A  test_sequences_and_iterators.cpp
37695SN/A  test_smart_ptr.cpp
38695SN/A  test_stl_binders.cpp
392665Ssaidi@eecs.umich.edu  test_virtual_functions.cpp
402665Ssaidi@eecs.umich.edu)
419262Ssascha.bischoff@arm.com
42695SN/A# Invoking cmake with something like:
43695SN/A#     cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" ..
44695SN/A# lets you override the tests that get compiled and run.  You can restore to all tests with:
45695SN/A#     cmake -DPYBIND11_TEST_OVERRIDE= ..
46695SN/Aif (PYBIND11_TEST_OVERRIDE)
4711793Sbrandon.potter@amd.com  set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
4811793Sbrandon.potter@amd.comendif()
4911793Sbrandon.potter@amd.com
50729SN/Astring(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
51695SN/A
529554Sandreas.hansson@arm.com# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
539554Sandreas.hansson@arm.com# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
549554Sandreas.hansson@arm.com# skip message).
559554Sandreas.hansson@arm.comlist(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
569554Sandreas.hansson@arm.comif(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
579554Sandreas.hansson@arm.com  find_package(Eigen3 QUIET)
584078Sbinkertn@umich.edu
599262Ssascha.bischoff@arm.com  if(EIGEN3_FOUND)
609262Ssascha.bischoff@arm.com    message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
619262Ssascha.bischoff@arm.com  else()
629262Ssascha.bischoff@arm.com    list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
639262Ssascha.bischoff@arm.com    message(STATUS "Building tests WITHOUT Eigen")
649262Ssascha.bischoff@arm.com  endif()
659262Ssascha.bischoff@arm.comendif()
669262Ssascha.bischoff@arm.com
679262Ssascha.bischoff@arm.com# Create the binding library
689262Ssascha.bischoff@arm.compybind11_add_module(pybind11_tests pybind11_tests.cpp
699262Ssascha.bischoff@arm.com  ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
709262Ssascha.bischoff@arm.com
719262Ssascha.bischoff@arm.compybind11_enable_warnings(pybind11_tests)
729262Ssascha.bischoff@arm.com
739262Ssascha.bischoff@arm.comif(EIGEN3_FOUND)
749262Ssascha.bischoff@arm.com  target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
759262Ssascha.bischoff@arm.com  target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
769262Ssascha.bischoff@arm.comendif()
777823Ssteve.reinhardt@amd.com
787822Ssteve.reinhardt@amd.comset(testdir ${PROJECT_SOURCE_DIR}/tests)
79695SN/A
809262Ssascha.bischoff@arm.com# Always write the output file directly into the 'tests' directory (even on MSVC)
819262Ssascha.bischoff@arm.comif(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
829262Ssascha.bischoff@arm.com  set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
839262Ssascha.bischoff@arm.com  foreach(config ${CMAKE_CONFIGURATION_TYPES})
849262Ssascha.bischoff@arm.com    string(TOUPPER ${config} config)
859262Ssascha.bischoff@arm.com    set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
869450Ssascha.bischoff@arm.com  endforeach()
877811Ssteve.reinhardt@amd.comendif()
88695SN/A
89695SN/A# 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