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