CMakeLists.txt (11986:c12e4625ab56) CMakeLists.txt (12037:d28054ac6ec9)
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)
16
17 find_package(pybind11 REQUIRED CONFIG)
18endif()
19
1if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
2 message(STATUS "Setting tests build type to MinSizeRel as none was specified")
3 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
4 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
5 "MinSizeRel" "RelWithDebInfo")
6endif()
7
8# Full set of test files (you can override these; see below)

--- 40 unchanged lines hidden (view full) ---

49
50string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
51
52# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
53# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
54# skip message).
55list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
56if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
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)

--- 40 unchanged lines hidden (view full) ---

68
69string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
70
71# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
72# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
73# skip message).
74list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
75if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
57 find_package(Eigen3 QUIET)
76 # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
77 # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
78 # produces a fatal error if loaded from a pre-3.0 cmake.
79 if (NOT CMAKE_VERSION VERSION_LESS 3.0)
80 find_package(Eigen3 QUIET CONFIG)
81 if (EIGEN3_FOUND)
82 if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1)
83 set(PYBIND11_EIGEN_VIA_TARGET 1)
84 endif()
85 endif()
86 endif()
87 if (NOT EIGEN3_FOUND)
88 # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
89 # tools/FindEigen3.cmake
90 find_package(Eigen3 QUIET)
91 endif()
58
59 if(EIGEN3_FOUND)
92
93 if(EIGEN3_FOUND)
94 # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed
95 # rather than looking it up in the cmake script); older versions, and the
96 # tools/FindEigen3.cmake, set EIGEN3_VERSION instead.
97 if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING)
98 set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
99 endif()
60 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
61 else()
62 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
63 message(STATUS "Building tests WITHOUT Eigen")
64 endif()
65endif()
66
100 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
101 else()
102 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
103 message(STATUS "Building tests WITHOUT Eigen")
104 endif()
105endif()
106
107# Compile with compiler warnings turned on
108function(pybind11_enable_warnings target_name)
109 if(MSVC)
110 target_compile_options(${target_name} PRIVATE /W4)
111 else()
112 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
113 endif()
114
115 if(PYBIND11_WERROR)
116 if(MSVC)
117 target_compile_options(${target_name} PRIVATE /WX)
118 else()
119 target_compile_options(${target_name} PRIVATE -Werror)
120 endif()
121 endif()
122endfunction()
123
124
67# Create the binding library
125# Create the binding library
68pybind11_add_module(pybind11_tests pybind11_tests.cpp
126pybind11_add_module(pybind11_tests THIN_LTO pybind11_tests.cpp
69 ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
70
71pybind11_enable_warnings(pybind11_tests)
72
73if(EIGEN3_FOUND)
127 ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
128
129pybind11_enable_warnings(pybind11_tests)
130
131if(EIGEN3_FOUND)
74 target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
132 if (PYBIND11_EIGEN_VIA_TARGET)
133 target_link_libraries(pybind11_tests PRIVATE Eigen3::Eigen)
134 else()
135 target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
136 endif()
75 target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
76endif()
77
137 target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
138endif()
139
78set(testdir ${PROJECT_SOURCE_DIR}/tests)
140set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
79
80# Always write the output file directly into the 'tests' directory (even on MSVC)
81if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
82 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
83 foreach(config ${CMAKE_CONFIGURATION_TYPES})
84 string(TOUPPER ${config} config)
85 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
86 endforeach()
87endif()
88
89# Make sure pytest is found or produce a fatal error
90if(NOT PYBIND11_PYTEST_FOUND)
141
142# Always write the output file directly into the 'tests' directory (even on MSVC)
143if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
144 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
145 foreach(config ${CMAKE_CONFIGURATION_TYPES})
146 string(TOUPPER ${config} config)
147 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
148 endforeach()
149endif()
150
151# Make sure pytest is found or produce a fatal error
152if(NOT PYBIND11_PYTEST_FOUND)
91 execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET
92 RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR)
93 if(PYBIND11_EXEC_PYTHON_ERR)
94 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
153 execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import pytest; print(pytest.__version__)"
154 RESULT_VARIABLE pytest_not_found OUTPUT_VARIABLE pytest_version ERROR_QUIET)
155 if(pytest_not_found)
156 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually"
157 " (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
158 elseif(pytest_version VERSION_LESS 3.0)
159 message(FATAL_ERROR "Running the tests requires pytest >= 3.0. Found: ${pytest_version}"
160 "Please update it (try: ${PYTHON_EXECUTABLE} -m pip install -U pytest)")
95 endif()
96 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
97endif()
98
99# A single command to compile and run the tests
161 endif()
162 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
163endif()
164
165# A single command to compile and run the tests
100add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES}
166add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${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
167 DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})
168
169if(PYBIND11_TEST_OVERRIDE)
170 add_custom_command(TARGET pytest POST_BUILD
171 COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
172endif()
173
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
174# Add a check target to run all the tests, starting with pytest (we add dependencies to this below)
175add_custom_target(check DEPENDS pytest)
176
177# The remaining tests only apply when being built as part of the pybind11 project, but not if the
178# tests are being built independently.
179if (NOT PROJECT_NAME STREQUAL "pybind11")
180 return()
181endif()
182
183# Add a post-build comment to show the .so size and, if a previous size, compare it:
184add_custom_command(TARGET pybind11_tests POST_BUILD
185 COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
186 $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
187
188# Test CMake build using functions and targets from subdirectory or installed location
189add_custom_target(test_cmake_build)
190if(NOT CMAKE_VERSION VERSION_LESS 3.1)
191 # 3.0 needed for interface library for subdirectory_target/installed_target
112 # 3.1 needed for cmake -E env for testing
192 # 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}"
193
194 include(CMakeParseArguments)
195 function(pybind11_add_build_test name)
196 cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN})
197
198 set(build_options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/mock_install"
199 "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
200 "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
201 "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}")
202 if(NOT ARG_INSTALL)
203 list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${PROJECT_SOURCE_DIR}")
204 endif()
205
206 add_custom_target(test_${name} ${CMAKE_CTEST_COMMAND}
207 --quiet --output-log test_cmake_build/${name}.log
208 --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_cmake_build/${name}"
209 "${CMAKE_CURRENT_BINARY_DIR}/test_cmake_build/${name}"
210 --build-config Release
211 --build-noclean
212 --build-generator ${CMAKE_GENERATOR}
213 $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
214 --build-makeprogram ${CMAKE_MAKE_PROGRAM}
215 --build-target check
216 --build-options ${build_options}
130 )
217 )
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}"
218 if(ARG_INSTALL)
219 add_dependencies(test_${name} mock_install)
220 endif()
221 add_dependencies(test_cmake_build test_${name})
222 endfunction()
223
224 pybind11_add_build_test(subdirectory_function)
225 pybind11_add_build_test(subdirectory_target)
226
227 if(PYBIND11_INSTALL)
228 add_custom_target(mock_install ${CMAKE_COMMAND}
229 "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install"
230 -P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
147 )
231 )
148 else()
149 add_custom_target(test_installed_target)
150 add_custom_target(test_installed_module)
232
233 pybind11_add_build_test(installed_function INSTALL)
234 pybind11_add_build_test(installed_target INSTALL)
151 endif()
235 endif()
152 add_custom_target(test_install)
153 add_dependencies(test_install test_installed_target test_installed_module)
154endif()
155
236endif()
237
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)
238add_dependencies(check test_cmake_build)