Skip to content

Commit 8c56d22

Browse files
committed
A way to register additional test targets.
1 parent 2cd32e5 commit 8c56d22

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

tests/CMakeLists.txt

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ macro(possibly_uninitialized)
4747
endforeach()
4848
endmacro()
4949

50+
# Function to add additional targets if any of the provided tests are found.
51+
# Needles; Specifies the test names to look for.
52+
# Additions; Specifies the additional test targets to add when any of the needles are found.
53+
macro(tests_extra_targets needles additions)
54+
# Add the index for this relation to the index extra targets map.
55+
list(LENGTH PYBIND11_TEST_EXTRA_TARGETS PYBIND11_TEST_EXTRA_TARGETS_LEN)
56+
list(APPEND PYBIND11_TEST_EXTRA_TARGETS ${PYBIND11_TEST_EXTRA_TARGETS_LEN})
57+
# Add the test names to look for, and the associated test target additions.
58+
set(PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${needles})
59+
set(PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${additions})
60+
endmacro()
61+
5062
# New Python support
5163
if(DEFINED Python_EXECUTABLE)
5264
set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}")
@@ -167,10 +179,11 @@ string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
167179
# Contains the set of test files that require pybind11_cross_module_tests to be
168180
# built; if none of these are built (i.e. because TEST_OVERRIDE is used and
169181
# doesn't include them) the second module doesn't get built.
170-
set(PYBIND11_CROSS_MODULE_TESTS test_exceptions.py test_local_bindings.py test_stl.py
171-
test_stl_binders.py)
182+
tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_stl_binders.py"
183+
"pybind11_cross_module_tests")
172184

173-
set(PYBIND11_CROSS_MODULE_GIL_TESTS test_gil_scoped.py)
185+
# And add additional targets for other tests.
186+
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
174187

175188
set(PYBIND11_EIGEN_REPO
176189
"https://gitlab.com/libeigen/eigen.git"
@@ -350,21 +363,17 @@ endfunction()
350363

351364
set(test_targets pybind11_tests)
352365

353-
# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
354-
foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
355-
list(FIND PYBIND11_PYTEST_FILES ${t} i)
356-
if(i GREATER -1)
357-
list(APPEND test_targets pybind11_cross_module_tests)
358-
break()
359-
endif()
360-
endforeach()
361-
362-
foreach(t ${PYBIND11_CROSS_MODULE_GIL_TESTS})
363-
list(FIND PYBIND11_PYTEST_FILES ${t} i)
364-
if(i GREATER -1)
365-
list(APPEND test_targets cross_module_gil_utils)
366-
break()
367-
endif()
366+
# Check if any tests need extra targets by iterating through the mappings registered.
367+
foreach(i ${PYBIND11_TEST_EXTRA_TARGETS})
368+
foreach(needle ${PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${i}})
369+
if(${needle} IN_LIST PYBIND11_PYTEST_FILES)
370+
# Add all the additional targets to the test list. List join in newer cmake.
371+
foreach(extra_target ${PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${i}})
372+
list(APPEND test_targets ${extra_target})
373+
endforeach()
374+
break() # Breaks out of the needle search, continues with the next mapping.
375+
endif()
376+
endforeach()
368377
endforeach()
369378

370379
# Support CUDA testing by forcing the target file to compile with NVCC

0 commit comments

Comments
 (0)