CMakeLists.txt revision 12391
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 8cmake_minimum_required(VERSION 2.8.12) 9 10option(PYBIND11_WERROR "Report all warnings as errors" OFF) 11 12if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) 13 # We're being loaded directly, i.e. not via add_subdirectory, so make this 14 # work as its own project and load the pybind11Config to get the tools we need 15 project(pybind11_tests CXX) 16 17 find_package(pybind11 REQUIRED CONFIG) 18endif() 19 20if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 21 message(STATUS "Setting tests build type to MinSizeRel as none was specified") 22 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) 23 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" 24 "MinSizeRel" "RelWithDebInfo") 25endif() 26 27# Full set of test files (you can override these; see below) 28set(PYBIND11_TEST_FILES 29 test_buffers.cpp 30 test_builtin_casters.cpp 31 test_call_policies.cpp 32 test_callbacks.cpp 33 test_chrono.cpp 34 test_class.cpp 35 test_constants_and_functions.cpp 36 test_copy_move.cpp 37 test_docstring_options.cpp 38 test_eigen.cpp 39 test_enum.cpp 40 test_eval.cpp 41 test_exceptions.cpp 42 test_factory_constructors.cpp 43 test_iostream.cpp 44 test_kwargs_and_defaults.cpp 45 test_local_bindings.cpp 46 test_methods_and_attributes.cpp 47 test_modules.cpp 48 test_multiple_inheritance.cpp 49 test_numpy_array.cpp 50 test_numpy_dtypes.cpp 51 test_numpy_vectorize.cpp 52 test_opaque_types.cpp 53 test_operator_overloading.cpp 54 test_pickling.cpp 55 test_pytypes.cpp 56 test_sequences_and_iterators.cpp 57 test_smart_ptr.cpp 58 test_stl.cpp 59 test_stl_binders.cpp 60 test_virtual_functions.cpp 61) 62 63# Invoking cmake with something like: 64# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" .. 65# lets you override the tests that get compiled and run. You can restore to all tests with: 66# cmake -DPYBIND11_TEST_OVERRIDE= .. 67if (PYBIND11_TEST_OVERRIDE) 68 set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE}) 69endif() 70 71string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}") 72 73# Contains the set of test files that require pybind11_cross_module_tests to be 74# built; if none of these are built (i.e. because TEST_OVERRIDE is used and 75# doesn't include them) the second module doesn't get built. 76set(PYBIND11_CROSS_MODULE_TESTS 77 test_exceptions.py 78 test_local_bindings.py 79 test_stl.py 80 test_stl_binders.py 81) 82 83# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but 84# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" 85# skip message). 86list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I) 87if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) 88 # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake). 89 # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also 90 # produces a fatal error if loaded from a pre-3.0 cmake. 91 if (NOT CMAKE_VERSION VERSION_LESS 3.0) 92 find_package(Eigen3 QUIET CONFIG) 93 if (EIGEN3_FOUND) 94 if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1) 95 set(PYBIND11_EIGEN_VIA_TARGET 1) 96 endif() 97 endif() 98 endif() 99 if (NOT EIGEN3_FOUND) 100 # Couldn't load via target, so fall back to allowing module mode finding, which will pick up 101 # tools/FindEigen3.cmake 102 find_package(Eigen3 QUIET) 103 endif() 104 105 if(EIGEN3_FOUND) 106 # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed 107 # rather than looking it up in the cmake script); older versions, and the 108 # tools/FindEigen3.cmake, set EIGEN3_VERSION instead. 109 if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING) 110 set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING}) 111 endif() 112 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}") 113 else() 114 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) 115 message(STATUS "Building tests WITHOUT Eigen") 116 endif() 117endif() 118 119# Optional dependency for some tests (boost::variant is only supported with version >= 1.56) 120find_package(Boost 1.56) 121 122# Compile with compiler warnings turned on 123function(pybind11_enable_warnings target_name) 124 if(MSVC) 125 target_compile_options(${target_name} PRIVATE /W4) 126 else() 127 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual) 128 endif() 129 130 if(PYBIND11_WERROR) 131 if(MSVC) 132 target_compile_options(${target_name} PRIVATE /WX) 133 else() 134 target_compile_options(${target_name} PRIVATE -Werror) 135 endif() 136 endif() 137endfunction() 138 139set(test_targets pybind11_tests) 140 141# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it 142foreach(t ${PYBIND11_CROSS_MODULE_TESTS}) 143 list(FIND PYBIND11_PYTEST_FILES ${t} i) 144 if (i GREATER -1) 145 list(APPEND test_targets pybind11_cross_module_tests) 146 break() 147 endif() 148endforeach() 149 150set(testdir ${CMAKE_CURRENT_SOURCE_DIR}) 151foreach(target ${test_targets}) 152 set(test_files ${PYBIND11_TEST_FILES}) 153 if(NOT target STREQUAL "pybind11_tests") 154 set(test_files "") 155 endif() 156 157 # Create the binding library 158 pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS}) 159 pybind11_enable_warnings(${target}) 160 161 if(MSVC) 162 target_compile_options(${target} PRIVATE /utf-8) 163 endif() 164 165 if(EIGEN3_FOUND) 166 if (PYBIND11_EIGEN_VIA_TARGET) 167 target_link_libraries(${target} PRIVATE Eigen3::Eigen) 168 else() 169 target_include_directories(${target} PRIVATE ${EIGEN3_INCLUDE_DIR}) 170 endif() 171 target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN) 172 endif() 173 174 if(Boost_FOUND) 175 target_include_directories(${target} PRIVATE ${Boost_INCLUDE_DIRS}) 176 target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST) 177 endif() 178 179 # Always write the output file directly into the 'tests' directory (even on MSVC) 180 if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) 181 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir}) 182 foreach(config ${CMAKE_CONFIGURATION_TYPES}) 183 string(TOUPPER ${config} config) 184 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir}) 185 endforeach() 186 endif() 187endforeach() 188 189# Make sure pytest is found or produce a fatal error 190if(NOT PYBIND11_PYTEST_FOUND) 191 execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)" 192 RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET) 193 if(pytest_not_found) 194 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually" 195 " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)") 196 elseif(pytest_version VERSION_LESS 3.0) 197 message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}" 198 "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)") 199 endif() 200 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "") 201endif() 202 203if(CMAKE_VERSION VERSION_LESS 3.2) 204 set(PYBIND11_USES_TERMINAL "") 205else() 206 set(PYBIND11_USES_TERMINAL "USES_TERMINAL") 207endif() 208 209# A single command to compile and run the tests 210add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES} 211 DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL}) 212 213if(PYBIND11_TEST_OVERRIDE) 214 add_custom_command(TARGET pytest POST_BUILD 215 COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") 216endif() 217 218# Add a check target to run all the tests, starting with pytest (we add dependencies to this below) 219add_custom_target(check DEPENDS pytest) 220 221# The remaining tests only apply when being built as part of the pybind11 project, but not if the 222# tests are being built independently. 223if (NOT PROJECT_NAME STREQUAL "pybind11") 224 return() 225endif() 226 227# Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it: 228add_custom_command(TARGET pybind11_tests POST_BUILD 229 COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py 230 $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt) 231 232# Test embedding the interpreter. Provides the `cpptest` target. 233add_subdirectory(test_embed) 234 235# Test CMake build using functions and targets from subdirectory or installed location 236add_subdirectory(test_cmake_build) 237