Skip to content

Commit 7170f87

Browse files
mhxbuddly27
authored andcommitted
Avoid unnecessary rebuilds of test targets
Currently, all targets created by `pytest_discover_tests` will cause a rebuild *every time* `make` / `ninja` is called, even if no files have changed since the last build. Quoting the CMake documentation for `add_custom_target`: > The target has no output file and is always considered out of > date even if the commands try to create a file with the name of > the target. Use the add_custom_command() command to generate a > file with dependencies. This change should be functionally equivalent, but avoids the indefinite rebuilds because the target will only check for the presence of `${_tests_file}` rather than unconditionally building it.
1 parent 959fad8 commit 7170f87

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

cmake/FindPytest.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
105105
set(_include_file "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_include.cmake")
106106
set(_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_tests.cmake")
107107

108-
# Create a custom target to run the tests.
109-
add_custom_target(
110-
${NAME} ALL VERBATIM
111-
BYPRODUCTS "${_tests_file}"
108+
add_custom_command(
109+
VERBATIM
110+
OUTPUT "${_tests_file}"
112111
DEPENDS ${_DEPENDS}
113112
COMMAND ${CMAKE_COMMAND}
114113
-D "PYTEST_EXECUTABLE=${PYTEST_EXECUTABLE}"
@@ -127,6 +126,9 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
127126
-D "CTEST_FILE=${_tests_file}"
128127
-P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PytestAddTests.cmake")
129128

129+
# Create a custom target to run the tests.
130+
add_custom_target(${NAME} ALL DEPENDS ${_tests_file})
131+
130132
file(WRITE "${_include_file}"
131133
"if(EXISTS \"${_tests_file}\")\n"
132134
" include(\"${_tests_file}\")\n"

0 commit comments

Comments
 (0)