CMakeLists.txt revision 11986
12350SN/Aif(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
28733SN/A  message(STATUS "Setting tests build type to MinSizeRel as none was specified")
38733SN/A  set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
48733SN/A  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
58733SN/A    "MinSizeRel" "RelWithDebInfo")
68733SN/Aendif()
78733SN/A
88733SN/A# Full set of test files (you can override these; see below)
98733SN/Aset(PYBIND11_TEST_FILES
108733SN/A  test_alias_initialization.cpp
118733SN/A  test_buffers.cpp
128733SN/A  test_callbacks.cpp
138733SN/A  test_chrono.cpp
142350SN/A  test_class_args.cpp
152350SN/A  test_constants_and_functions.cpp
162350SN/A  test_copy_move_policies.cpp
172350SN/A  test_docstring_options.cpp
182350SN/A  test_eigen.cpp
192350SN/A  test_enum.cpp
202350SN/A  test_eval.cpp
212350SN/A  test_exceptions.cpp
222350SN/A  test_inheritance.cpp
232350SN/A  test_issues.cpp
242350SN/A  test_keep_alive.cpp
252350SN/A  test_kwargs_and_defaults.cpp
262350SN/A  test_methods_and_attributes.cpp
272350SN/A  test_modules.cpp
282350SN/A  test_multiple_inheritance.cpp
292350SN/A  test_numpy_array.cpp
302350SN/A  test_numpy_dtypes.cpp
312350SN/A  test_numpy_vectorize.cpp
322350SN/A  test_opaque_types.cpp
332350SN/A  test_operator_overloading.cpp
342350SN/A  test_pickling.cpp
352350SN/A  test_python_types.cpp
362350SN/A  test_sequences_and_iterators.cpp
372350SN/A  test_smart_ptr.cpp
382350SN/A  test_stl_binders.cpp
392689SN/A  test_virtual_functions.cpp
402689SN/A)
412350SN/A
422315SN/A# Invoking cmake with something like:
4311793Sbrandon.potter@amd.com#     cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" ..
4411793Sbrandon.potter@amd.com# lets you override the tests that get compiled and run.  You can restore to all tests with:
452765SN/A#     cmake -DPYBIND11_TEST_OVERRIDE= ..
464762SN/Aif (PYBIND11_TEST_OVERRIDE)
472315SN/A  set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
482669SN/Aendif()
492669SN/A
502765SN/Astring(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
518733SN/A
522765SN/A# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
532315SN/A# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
542315SN/A# skip message).
552315SN/Alist(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
562315SN/Aif(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
574762SN/A  find_package(Eigen3 QUIET)
584762SN/A
592315SN/A  if(EIGEN3_FOUND)
602315SN/A    message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
614762SN/A  else()
628733SN/A    list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
632315SN/A    message(STATUS "Building tests WITHOUT Eigen")
642315SN/A  endif()
652315SN/Aendif()
662315SN/A
672315SN/A# Create the binding library
682356SN/Apybind11_add_module(pybind11_tests pybind11_tests.cpp
692732SN/A  ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
709793Sakash.bagdia@arm.com
718733SN/Apybind11_enable_warnings(pybind11_tests)
722315SN/A
732315SN/Aif(EIGEN3_FOUND)
742315SN/A  target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
752315SN/A  target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
762315SN/Aendif()
772315SN/A
782315SN/Aset(testdir ${PROJECT_SOURCE_DIR}/tests)
798887SN/A
802356SN/A# Always write the output file directly into the 'tests' directory (even on MSVC)
812367SN/Aif(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
822356SN/A  set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
832315SN/A  foreach(config ${CMAKE_CONFIGURATION_TYPES})
842315SN/A    string(TOUPPER ${config} config)
852315SN/A    set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
869384SAndreas.Sandberg@arm.com  endforeach()
875104SN/Aendif()
885110SN/A
892315SN/A# Make sure pytest is found or produce a fatal error
908733SN/Aif(NOT PYBIND11_PYTEST_FOUND)
912315SN/A  execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET
922315SN/A                  RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR)
932315SN/A  if(PYBIND11_EXEC_PYTHON_ERR)
942315SN/A    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