if(_VOID_TESTING_INCLUDED) return() endif() set(_VOID_TESTING_INCLUDED TRUE) set(VOID_TESTING_SCANNER "${CMAKE_CURRENT_LIST_DIR}/VoidTestScanner.cmake" CACHE INTERNAL "Path to VoidTestScanner.cmake") # Returns TRUE in if transitively links Void::Testing. function(_void_testing_links_testing target out_var) set(${out_var} FALSE PARENT_SCOPE) set(_visited "") set(_queue "${target}") while(_queue) list(POP_FRONT _queue _current) if(_current IN_LIST _visited) continue() endif() list(APPEND _visited "${_current}") if(_current STREQUAL "Void::Testing" OR _current STREQUAL "Void_Testing") set(${out_var} TRUE PARENT_SCOPE) return() endif() if(NOT TARGET ${_current}) continue() endif() get_target_property(_libs ${_current} LINK_LIBRARIES) if(_libs) list(APPEND _queue ${_libs}) endif() get_target_property(_iflibs ${_current} INTERFACE_LINK_LIBRARIES) if(_iflibs) list(APPEND _queue ${_iflibs}) endif() endwhile() endfunction() # Apply the scan/HEADER_FILE_ONLY rewrite to . Idempotent. function(_void_testing_apply_scan target) if(NOT TARGET ${target}) return() endif() get_target_property(_done ${target} VOID_TESTING_SCANNED) if(_done) return() endif() set_target_properties(${target} PROPERTIES VOID_TESTING_SCANNED TRUE) get_target_property(_target_dir ${target} BINARY_DIR) set(_gen_dir "${_target_dir}/void_testing_generated") file(MAKE_DIRECTORY "${_gen_dir}") get_target_property(_sources ${target} SOURCES) set(_generated) foreach(_src ${_sources}) if(NOT _src MATCHES "\\.cpp$") continue() endif() if(_src MATCHES "\\.void_tests\\.cpp$") continue() endif() get_filename_component(_abs ${_src} ABSOLUTE) get_filename_component(_stem ${_src} NAME_WE) set(_gen "${_gen_dir}/${_stem}.void_tests.cpp") add_custom_command( OUTPUT ${_gen} COMMAND ${CMAKE_COMMAND} -DINPUT_FILE=${_abs} -DOUTPUT_FILE=${_gen} -DSUITE_NAME=${_stem} -P ${VOID_TESTING_SCANNER} DEPENDS ${_abs} ${VOID_TESTING_SCANNER} COMMENT "Scanning ${_src} for Void::Testing tests" ) list(APPEND _generated ${_gen}) set_source_files_properties(${_src} PROPERTIES HEADER_FILE_ONLY TRUE) set_source_files_properties(${_gen} PROPERTIES GENERATED TRUE) endforeach() if(_generated) target_sources(${target} PRIVATE ${_generated}) endif() if(NOT TEST ${target}) add_test(NAME ${target} COMMAND ${target}) endif() endfunction() # Public entry point — explicit opt-in. Idempotent with the auto-scan sweep. function(void_add_tests target) if(NOT TARGET ${target}) message(FATAL_ERROR "void_add_tests: '${target}' is not a target") endif() target_link_libraries(${target} PRIVATE Void::Testing) _void_testing_apply_scan(${target}) endfunction() # Sweep: visit every executable in and its subdirectories. If the # executable transitively links Void::Testing and has not opted out via # VOID_TESTING_NO_AUTO_SCAN, apply the scan rewrite. function(_void_testing_sweep_directory dir) get_property(_targets DIRECTORY "${dir}" PROPERTY BUILDSYSTEM_TARGETS) foreach(_target IN LISTS _targets) if(NOT TARGET ${_target}) continue() endif() get_target_property(_type ${_target} TYPE) if(NOT _type STREQUAL "EXECUTABLE") continue() endif() get_target_property(_optout ${_target} VOID_TESTING_NO_AUTO_SCAN) if(_optout) continue() endif() _void_testing_links_testing(${_target} _links) if(_links) _void_testing_apply_scan(${_target}) endif() endforeach() get_property(_subdirs DIRECTORY "${dir}" PROPERTY SUBDIRECTORIES) foreach(_subdir IN LISTS _subdirs) _void_testing_sweep_directory("${_subdir}") endforeach() endfunction() # Schedule the sweep at the end of the include-site directory so the # generated custom commands and target_sources() additions land in the # same directory scope as the test targets that consume them. if(NOT _VOID_TESTING_SWEEP_REGISTERED) set(_VOID_TESTING_SWEEP_REGISTERED TRUE) cmake_language(DEFER DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" CALL _void_testing_sweep_directory "${CMAKE_CURRENT_SOURCE_DIR}") endif()