CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. cmake_minimum_required(VERSION 3.4)
  8. # The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with
  9. # some versions of VS that have a patched CMake 3.11. This forces us to emulate
  10. # the behavior using the following workaround:
  11. if(${CMAKE_VERSION} VERSION_LESS 3.18)
  12. cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  13. else()
  14. cmake_policy(VERSION 3.18)
  15. endif()
  16. # Only needed for CMake < 3.5 support
  17. include(CMakeParseArguments)
  18. # Filter out items; print an optional message if any items filtered
  19. #
  20. # Usage:
  21. # pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "")
  22. #
  23. macro(pybind11_filter_tests LISTNAME)
  24. cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN})
  25. set(PYBIND11_FILTER_TESTS_FOUND OFF)
  26. foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS)
  27. list(FIND ${LISTNAME} ${filename} _FILE_FOUND)
  28. if(_FILE_FOUND GREATER -1)
  29. list(REMOVE_AT ${LISTNAME} ${_FILE_FOUND})
  30. set(PYBIND11_FILTER_TESTS_FOUND ON)
  31. endif()
  32. endforeach()
  33. if(PYBIND11_FILTER_TESTS_FOUND AND ARG_MESSAGE)
  34. message(STATUS "${ARG_MESSAGE}")
  35. endif()
  36. endmacro()
  37. macro(possibly_uninitialized)
  38. foreach(VARNAME ${ARGN})
  39. if(NOT DEFINED "${VARNAME}")
  40. set("${VARNAME}" "")
  41. endif()
  42. endforeach()
  43. endmacro()
  44. # New Python support
  45. if(DEFINED Python_EXECUTABLE)
  46. set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}")
  47. set(PYTHON_VERSION "${Python_VERSION}")
  48. endif()
  49. # There's no harm in including a project in a project
  50. project(pybind11_tests CXX)
  51. # Access FindCatch and more
  52. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools")
  53. option(PYBIND11_WERROR "Report all warnings as errors" OFF)
  54. option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF)
  55. option(PYBIND11_CUDA_TESTS "Enable building CUDA tests (requires CMake 3.12+)" OFF)
  56. set(PYBIND11_TEST_OVERRIDE
  57. ""
  58. CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests")
  59. set(PYBIND11_TEST_FILTER
  60. ""
  61. CACHE STRING "Tests from ;-separated list of *.cpp files will be removed from all tests")
  62. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  63. # We're being loaded directly, i.e. not via add_subdirectory, so make this
  64. # work as its own project and load the pybind11Config to get the tools we need
  65. find_package(pybind11 REQUIRED CONFIG)
  66. endif()
  67. if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
  68. message(STATUS "Setting tests build type to MinSizeRel as none was specified")
  69. set(CMAKE_BUILD_TYPE
  70. MinSizeRel
  71. CACHE STRING "Choose the type of build." FORCE)
  72. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel"
  73. "RelWithDebInfo")
  74. endif()
  75. if(PYBIND11_CUDA_TESTS)
  76. enable_language(CUDA)
  77. if(DEFINED CMAKE_CXX_STANDARD)
  78. set(CMAKE_CUDA_STANDARD ${CMAKE_CXX_STANDARD})
  79. endif()
  80. set(CMAKE_CUDA_STANDARD_REQUIRED ON)
  81. endif()
  82. # Full set of test files (you can override these; see below)
  83. set(PYBIND11_TEST_FILES
  84. test_async.cpp
  85. test_buffers.cpp
  86. test_builtin_casters.cpp
  87. test_call_policies.cpp
  88. test_callbacks.cpp
  89. test_chrono.cpp
  90. test_class.cpp
  91. test_constants_and_functions.cpp
  92. test_copy_move.cpp
  93. test_custom_type_casters.cpp
  94. test_docstring_options.cpp
  95. test_eigen.cpp
  96. test_enum.cpp
  97. test_eval.cpp
  98. test_exceptions.cpp
  99. test_factory_constructors.cpp
  100. test_gil_scoped.cpp
  101. test_iostream.cpp
  102. test_kwargs_and_defaults.cpp
  103. test_local_bindings.cpp
  104. test_methods_and_attributes.cpp
  105. test_modules.cpp
  106. test_multiple_inheritance.cpp
  107. test_numpy_array.cpp
  108. test_numpy_dtypes.cpp
  109. test_numpy_vectorize.cpp
  110. test_opaque_types.cpp
  111. test_operator_overloading.cpp
  112. test_pickling.cpp
  113. test_pytypes.cpp
  114. test_sequences_and_iterators.cpp
  115. test_smart_ptr.cpp
  116. test_stl.cpp
  117. test_stl_binders.cpp
  118. test_tagbased_polymorphic.cpp
  119. test_union.cpp
  120. test_virtual_functions.cpp)
  121. # Invoking cmake with something like:
  122. # cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
  123. # lets you override the tests that get compiled and run. You can restore to all tests with:
  124. # cmake -DPYBIND11_TEST_OVERRIDE= ..
  125. if(PYBIND11_TEST_OVERRIDE)
  126. set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
  127. endif()
  128. # You can also filter tests:
  129. if(PYBIND11_TEST_FILTER)
  130. pybind11_filter_tests(PYBIND11_TEST_FILES ${PYBIND11_TEST_FILTER})
  131. endif()
  132. if(PYTHON_VERSION VERSION_LESS 3.5)
  133. pybind11_filter_tests(PYBIND11_TEST_FILES test_async.cpp MESSAGE
  134. "Skipping test_async on Python 2")
  135. endif()
  136. # Skip tests for CUDA check:
  137. # /pybind11/tests/test_constants_and_functions.cpp(125):
  138. # error: incompatible exception specifications
  139. if(PYBIND11_CUDA_TESTS)
  140. pybind11_filter_tests(
  141. PYBIND11_TEST_FILES test_constants_and_functions.cpp MESSAGE
  142. "Skipping test_constants_and_functions due to incompatible exception specifications")
  143. endif()
  144. string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
  145. # Contains the set of test files that require pybind11_cross_module_tests to be
  146. # built; if none of these are built (i.e. because TEST_OVERRIDE is used and
  147. # doesn't include them) the second module doesn't get built.
  148. set(PYBIND11_CROSS_MODULE_TESTS test_exceptions.py test_local_bindings.py test_stl.py
  149. test_stl_binders.py)
  150. set(PYBIND11_CROSS_MODULE_GIL_TESTS test_gil_scoped.py)
  151. # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
  152. # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
  153. # skip message).
  154. list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
  155. if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
  156. # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
  157. # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
  158. # produces a fatal error if loaded from a pre-3.0 cmake.
  159. if(DOWNLOAD_EIGEN)
  160. if(CMAKE_VERSION VERSION_LESS 3.11)
  161. message(FATAL_ERROR "CMake 3.11+ required when using DOWNLOAD_EIGEN")
  162. endif()
  163. set(EIGEN3_VERSION_STRING "3.3.8")
  164. include(FetchContent)
  165. FetchContent_Declare(
  166. eigen
  167. GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
  168. GIT_TAG ${EIGEN3_VERSION_STRING})
  169. FetchContent_GetProperties(eigen)
  170. if(NOT eigen_POPULATED)
  171. message(STATUS "Downloading Eigen")
  172. FetchContent_Populate(eigen)
  173. endif()
  174. set(EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR})
  175. set(EIGEN3_FOUND TRUE)
  176. else()
  177. find_package(Eigen3 3.2.7 QUIET CONFIG)
  178. if(NOT EIGEN3_FOUND)
  179. # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
  180. # tools/FindEigen3.cmake
  181. find_package(Eigen3 3.2.7 QUIET)
  182. endif()
  183. endif()
  184. if(EIGEN3_FOUND)
  185. if(NOT TARGET Eigen3::Eigen)
  186. add_library(Eigen3::Eigen IMPORTED INTERFACE)
  187. set_property(TARGET Eigen3::Eigen PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  188. "${EIGEN3_INCLUDE_DIR}")
  189. endif()
  190. # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
  191. # rather than looking it up in the cmake script); older versions, and the
  192. # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
  193. if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
  194. set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
  195. endif()
  196. message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
  197. else()
  198. list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
  199. message(
  200. STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download")
  201. endif()
  202. endif()
  203. # Optional dependency for some tests (boost::variant is only supported with version >= 1.56)
  204. find_package(Boost 1.56)
  205. if(Boost_FOUND)
  206. if(NOT TARGET Boost::headers)
  207. add_library(Boost::headers IMPORTED INTERFACE)
  208. if(TARGET Boost::boost)
  209. # Classic FindBoost
  210. set_property(TARGET Boost::boost PROPERTY INTERFACE_LINK_LIBRARIES Boost::boost)
  211. else()
  212. # Very old FindBoost, or newer Boost than CMake in older CMakes
  213. set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  214. ${Boost_INCLUDE_DIRS})
  215. endif()
  216. endif()
  217. endif()
  218. # Compile with compiler warnings turned on
  219. function(pybind11_enable_warnings target_name)
  220. if(MSVC)
  221. target_compile_options(${target_name} PRIVATE /W4)
  222. elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS)
  223. target_compile_options(
  224. ${target_name}
  225. PRIVATE -Wall
  226. -Wextra
  227. -Wconversion
  228. -Wcast-qual
  229. -Wdeprecated
  230. -Wundef
  231. -Wnon-virtual-dtor)
  232. endif()
  233. if(PYBIND11_WERROR)
  234. if(MSVC)
  235. target_compile_options(${target_name} PRIVATE /WX)
  236. elseif(PYBIND11_CUDA_TESTS)
  237. target_compile_options(${target_name} PRIVATE "SHELL:-Werror all-warnings")
  238. elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)")
  239. target_compile_options(${target_name} PRIVATE -Werror)
  240. endif()
  241. endif()
  242. # Needs to be readded since the ordering requires these to be after the ones above
  243. if(CMAKE_CXX_STANDARD
  244. AND CMAKE_CXX_COMPILER_ID MATCHES "Clang"
  245. AND PYTHON_VERSION VERSION_LESS 3.0)
  246. if(CMAKE_CXX_STANDARD LESS 17)
  247. target_compile_options(${target_name} PUBLIC -Wno-deprecated-register)
  248. else()
  249. target_compile_options(${target_name} PUBLIC -Wno-register)
  250. endif()
  251. endif()
  252. endfunction()
  253. set(test_targets pybind11_tests)
  254. # Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
  255. foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
  256. list(FIND PYBIND11_PYTEST_FILES ${t} i)
  257. if(i GREATER -1)
  258. list(APPEND test_targets pybind11_cross_module_tests)
  259. break()
  260. endif()
  261. endforeach()
  262. foreach(t ${PYBIND11_CROSS_MODULE_GIL_TESTS})
  263. list(FIND PYBIND11_PYTEST_FILES ${t} i)
  264. if(i GREATER -1)
  265. list(APPEND test_targets cross_module_gil_utils)
  266. break()
  267. endif()
  268. endforeach()
  269. # Support CUDA testing by forcing the target file to compile with NVCC
  270. if(PYBIND11_CUDA_TESTS)
  271. set_property(SOURCE ${PYBIND11_TEST_FILES} PROPERTY LANGUAGE CUDA)
  272. endif()
  273. foreach(target ${test_targets})
  274. set(test_files ${PYBIND11_TEST_FILES})
  275. if(NOT "${target}" STREQUAL "pybind11_tests")
  276. set(test_files "")
  277. endif()
  278. # Support CUDA testing by forcing the target file to compile with NVCC
  279. if(PYBIND11_CUDA_TESTS)
  280. set_property(SOURCE ${target}.cpp PROPERTY LANGUAGE CUDA)
  281. endif()
  282. # Create the binding library
  283. pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
  284. pybind11_enable_warnings(${target})
  285. if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
  286. get_property(
  287. suffix
  288. TARGET ${target}
  289. PROPERTY SUFFIX)
  290. set(source_output "${CMAKE_CURRENT_SOURCE_DIR}/${target}${suffix}")
  291. if(suffix AND EXISTS "${source_output}")
  292. message(WARNING "Output file also in source directory; "
  293. "please remove to avoid confusion: ${source_output}")
  294. endif()
  295. endif()
  296. if(MSVC)
  297. target_compile_options(${target} PRIVATE /utf-8)
  298. endif()
  299. if(EIGEN3_FOUND)
  300. target_link_libraries(${target} PRIVATE Eigen3::Eigen)
  301. target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN)
  302. endif()
  303. if(Boost_FOUND)
  304. target_link_libraries(${target} PRIVATE Boost::headers)
  305. target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST)
  306. endif()
  307. # Always write the output file directly into the 'tests' directory (even on MSVC)
  308. if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  309. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
  310. "${CMAKE_CURRENT_BINARY_DIR}")
  311. if(DEFINED CMAKE_CONFIGURATION_TYPES)
  312. foreach(config ${CMAKE_CONFIGURATION_TYPES})
  313. string(TOUPPER ${config} config)
  314. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
  315. "${CMAKE_CURRENT_BINARY_DIR}")
  316. endforeach()
  317. endif()
  318. endif()
  319. endforeach()
  320. # Make sure pytest is found or produce a warning
  321. pybind11_find_import(pytest VERSION 3.1)
  322. if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
  323. # This is not used later in the build, so it's okay to regenerate each time.
  324. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
  325. COPYONLY)
  326. file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
  327. "\ntestpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"")
  328. endif()
  329. # cmake 3.12 added list(transform <list> prepend
  330. # but we can't use it yet
  331. string(REPLACE "test_" "${CMAKE_CURRENT_SOURCE_DIR}/test_" PYBIND11_ABS_PYTEST_FILES
  332. "${PYBIND11_PYTEST_FILES}")
  333. set(PYBIND11_TEST_PREFIX_COMMAND
  334. ""
  335. CACHE STRING "Put this before pytest, use for checkers and such")
  336. # A single command to compile and run the tests
  337. add_custom_target(
  338. pytest
  339. COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest
  340. ${PYBIND11_ABS_PYTEST_FILES}
  341. DEPENDS ${test_targets}
  342. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  343. USES_TERMINAL)
  344. if(PYBIND11_TEST_OVERRIDE)
  345. add_custom_command(
  346. TARGET pytest
  347. POST_BUILD
  348. COMMAND ${CMAKE_COMMAND} -E echo
  349. "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
  350. endif()
  351. # cmake-format: off
  352. add_custom_target(
  353. memcheck
  354. COMMAND
  355. PYTHONMALLOC=malloc
  356. valgrind
  357. --leak-check=full
  358. --show-leak-kinds=definite,indirect
  359. --errors-for-leak-kinds=definite,indirect
  360. --error-exitcode=1
  361. --read-var-info=yes
  362. --track-origins=yes
  363. --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-python.supp"
  364. --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-numpy-scipy.supp"
  365. --gen-suppressions=all
  366. ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_ABS_PYTEST_FILES}
  367. DEPENDS ${test_targets}
  368. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  369. USES_TERMINAL)
  370. # cmake-format: on
  371. # Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
  372. add_custom_target(check DEPENDS pytest)
  373. # The remaining tests only apply when being built as part of the pybind11 project, but not if the
  374. # tests are being built independently.
  375. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  376. return()
  377. endif()
  378. # Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it:
  379. add_custom_command(
  380. TARGET pybind11_tests
  381. POST_BUILD
  382. COMMAND
  383. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/libsize.py
  384. $<TARGET_FILE:pybind11_tests>
  385. ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
  386. if(NOT PYBIND11_CUDA_TESTS)
  387. # Test embedding the interpreter. Provides the `cpptest` target.
  388. add_subdirectory(test_embed)
  389. # Test CMake build using functions and targets from subdirectory or installed location
  390. add_subdirectory(test_cmake_build)
  391. endif()