blob: aa2704b29f99fb4d322d71b4b70385d5768ab259 [file] [log] [blame]
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -05001# 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)
16
17 find_package(pybind11 REQUIRED CONFIG)
18endif()
19
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020020if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
21 message(STATUS "Setting tests build type to MinSizeRel as none was specified")
Wenzel Jakob7962f302016-09-17 12:58:18 +020022 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")
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020025endif()
26
Jason Rhinelander920e0e32016-11-12 19:10:53 -050027# Full set of test files (you can override these; see below)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020028set(PYBIND11_TEST_FILES
29 test_buffers.cpp
Dean Moldovan83e328f2017-06-09 00:44:49 +020030 test_builtin_casters.cpp
Dean Moldovan1ac19032017-03-16 11:22:26 +010031 test_call_policies.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020032 test_callbacks.cpp
Trent Houliston352149e2016-08-25 23:08:04 +100033 test_chrono.cpp
Dean Moldovan83e328f2017-06-09 00:44:49 +020034 test_class.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020035 test_constants_and_functions.cpp
Jason Rhinelander813d7e82017-05-14 15:57:26 -040036 test_copy_move.cpp
Alexander Stukowski9a110e62016-11-15 12:38:05 +010037 test_docstring_options.cpp
Jason Rhinelander52f4be82016-09-03 14:54:22 -040038 test_eigen.cpp
Dean Moldovana9a37b42016-08-13 00:57:24 +020039 test_enum.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020040 test_eval.cpp
41 test_exceptions.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020042 test_kwargs_and_defaults.cpp
Jason Rhinelander7437c692017-07-28 22:03:44 -040043 test_local_bindings.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020044 test_methods_and_attributes.cpp
45 test_modules.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020046 test_multiple_inheritance.cpp
Ivan Smirnov91b3d682016-08-29 02:41:05 +010047 test_numpy_array.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020048 test_numpy_dtypes.cpp
49 test_numpy_vectorize.cpp
50 test_opaque_types.cpp
51 test_operator_overloading.cpp
52 test_pickling.cpp
Dean Moldovan83e328f2017-06-09 00:44:49 +020053 test_pytypes.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020054 test_sequences_and_iterators.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020055 test_smart_ptr.cpp
Dean Moldovan83e328f2017-06-09 00:44:49 +020056 test_stl.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020057 test_stl_binders.cpp
58 test_virtual_functions.cpp
59)
60
Jason Rhinelander920e0e32016-11-12 19:10:53 -050061# Invoking cmake with something like:
Dean Moldovanbdfb50f2017-06-07 16:52:50 +020062# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" ..
Jason Rhinelander920e0e32016-11-12 19:10:53 -050063# lets you override the tests that get compiled and run. You can restore to all tests with:
64# cmake -DPYBIND11_TEST_OVERRIDE= ..
65if (PYBIND11_TEST_OVERRIDE)
66 set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
67endif()
68
Jason Rhinelander52f4be82016-09-03 14:54:22 -040069string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020070
Jason Rhinelander0bd59792017-07-28 20:57:57 -040071# Contains the set of test files that require pybind11_cross_module_tests to be
72# built; if none of these are built (i.e. because TEST_OVERRIDE is used and
73# doesn't include them) the second module doesn't get built.
74set(PYBIND11_CROSS_MODULE_TESTS
Jason Rhinelanderd5981722017-07-28 21:38:23 -040075 test_exceptions.py
Jason Rhinelander7437c692017-07-28 22:03:44 -040076 test_local_bindings.py
Jason Rhinelander0bd59792017-07-28 20:57:57 -040077)
78
Jason Rhinelander52f4be82016-09-03 14:54:22 -040079# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
80# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
81# skip message).
82list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
83if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -050084 # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
85 # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
86 # produces a fatal error if loaded from a pre-3.0 cmake.
87 if (NOT CMAKE_VERSION VERSION_LESS 3.0)
88 find_package(Eigen3 QUIET CONFIG)
89 if (EIGEN3_FOUND)
90 if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1)
91 set(PYBIND11_EIGEN_VIA_TARGET 1)
92 endif()
93 endif()
94 endif()
95 if (NOT EIGEN3_FOUND)
96 # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
97 # tools/FindEigen3.cmake
98 find_package(Eigen3 QUIET)
99 endif()
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400100
101 if(EIGEN3_FOUND)
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500102 # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
103 # rather than looking it up in the cmake script); older versions, and the
104 # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
105 if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
106 set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
107 endif()
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400108 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
109 else()
110 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
111 message(STATUS "Building tests WITHOUT Eigen")
112 endif()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113endif()
114
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500115# Compile with compiler warnings turned on
116function(pybind11_enable_warnings target_name)
117 if(MSVC)
118 target_compile_options(${target_name} PRIVATE /W4)
119 else()
120 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
121 endif()
122
123 if(PYBIND11_WERROR)
124 if(MSVC)
125 target_compile_options(${target_name} PRIVATE /WX)
126 else()
127 target_compile_options(${target_name} PRIVATE -Werror)
128 endif()
129 endif()
130endfunction()
131
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400132set(test_targets pybind11_tests)
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500133
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400134# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
135foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
136 list(FIND PYBIND11_PYTEST_FILES ${t} i)
137 if (i GREATER -1)
138 list(APPEND test_targets pybind11_cross_module_tests)
139 break()
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500140 endif()
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400141endforeach()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200142
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500143set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400144foreach(tgt ${test_targets})
145 set(test_files ${PYBIND11_TEST_FILES})
146 if(NOT tgt STREQUAL "pybind11_tests")
147 set(test_files "")
148 endif()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200149
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400150 # Create the binding library
151 pybind11_add_module(${tgt} THIN_LTO ${tgt}.cpp
152 ${test_files} ${PYBIND11_HEADERS})
153
154 pybind11_enable_warnings(${tgt})
155
156 if(MSVC)
157 target_compile_options(${tgt} PRIVATE /utf-8)
158 endif()
159
160 if(EIGEN3_FOUND)
161 if (PYBIND11_EIGEN_VIA_TARGET)
162 target_link_libraries(${tgt} PRIVATE Eigen3::Eigen)
163 else()
164 target_include_directories(${tgt} PRIVATE ${EIGEN3_INCLUDE_DIR})
165 endif()
166 target_compile_definitions(${tgt} PRIVATE -DPYBIND11_TEST_EIGEN)
167 endif()
168
169 # Always write the output file directly into the 'tests' directory (even on MSVC)
170 if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
171 set_target_properties(${tgt} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
172 foreach(config ${CMAKE_CONFIGURATION_TYPES})
173 string(TOUPPER ${config} config)
174 set_target_properties(${tgt} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
175 endforeach()
176 endif()
177endforeach()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200178
Jason Rhinelanderdd3d56a2016-08-26 17:11:40 -0400179# Make sure pytest is found or produce a fatal error
Dean Moldovan18319d52016-08-13 02:44:56 +0200180if(NOT PYBIND11_PYTEST_FOUND)
Dean Moldovand47febc2017-03-10 15:42:42 +0100181 execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
182 RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET)
183 if(pytest_not_found)
184 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually"
185 " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
186 elseif(pytest_version VERSION_LESS 3.0)
187 message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
188 "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)")
Dean Moldovan18319d52016-08-13 02:44:56 +0200189 endif()
Wenzel Jakobb55a5c52016-10-09 13:51:05 +0200190 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
Dean Moldovan18319d52016-08-13 02:44:56 +0200191endif()
192
Wenzel Jakob7653a112017-04-28 22:43:14 +0200193if(CMAKE_VERSION VERSION_LESS 3.2)
194 set(PYBIND11_USES_TERMINAL "")
195else()
196 set(PYBIND11_USES_TERMINAL "USES_TERMINAL")
197endif()
198
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200199# A single command to compile and run the tests
Dean Moldovand47febc2017-03-10 15:42:42 +0100200add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400201 DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL})
Jason Rhinelanderdc0b4bd2016-11-04 09:47:41 -0400202
Jason Rhinelander920e0e32016-11-12 19:10:53 -0500203if(PYBIND11_TEST_OVERRIDE)
204 add_custom_command(TARGET pytest POST_BUILD
205 COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
206endif()
207
Jason Rhinelander60d0e0d2017-02-24 17:07:53 -0500208# Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
209add_custom_target(check DEPENDS pytest)
210
211# The remaining tests only apply when being built as part of the pybind11 project, but not if the
212# tests are being built independently.
213if (NOT PROJECT_NAME STREQUAL "pybind11")
214 return()
215endif()
216
Jason Rhinelander0bd59792017-07-28 20:57:57 -0400217# Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it:
Jason Rhinelanderdc0b4bd2016-11-04 09:47:41 -0400218add_custom_command(TARGET pybind11_tests POST_BUILD
Matthew Woehlke5e92b3e2017-02-08 17:43:23 -0500219 COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
Dean Moldovanb0f38852016-12-14 01:43:39 +0100220 $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
221
Dean Moldovan9693a5c2017-03-23 17:27:32 +0100222# Test embedding the interpreter. Provides the `cpptest` target.
223add_subdirectory(test_embed)
224
Dean Moldovanb0f38852016-12-14 01:43:39 +0100225# Test CMake build using functions and targets from subdirectory or installed location
Dean Moldovanc67033a2017-06-25 15:16:34 +0200226add_subdirectory(test_cmake_build)