Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Add unit test source file guard for core tests (#36972)

Pawel Raasza4eb78aa4b9f
diff --git a/cmake/developer_package/add_target_helpers.cmake b/cmake/developer_package/add_target_helpers.cmake
index cc535a9d0d9..3ad93c3dc15 100644
--- a/cmake/developer_package/add_target_helpers.cmake
+++ b/cmake/developer_package/add_target_helpers.cmake
@@ -236,7 +236,7 @@ function(ov_add_test_target_per_source)
endif()
foreach(_src IN LISTS ARG_SOURCES)
- # Use the subdirectory-qualified stem to avoid collisions when two source files have the same basename
+ # Use the subdirectory-qualified stem to avoid collisions when two source files have the same basename
# but live in different subdirectories.
file(RELATIVE_PATH _rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_src}")
get_filename_component(_stem_dir "${_rel}" DIRECTORY)
@@ -276,36 +276,141 @@ function(ov_add_test_target_per_source)
endfunction()
#[[
-Wrapper function over ov_add_target that also registers the executable as a CTest test.
-All ov_add_target parameters (ROOT, SOURCES, LINK_LIBRARIES, INCLUDES, DEFINES,
-DEPENDENCIES, EXCLUDED_SOURCE_PATHS, etc.) are forwarded transparently.
+Wrapper over ov_add_target that builds a test executable and registers it as a CTest test.
+All ov_add_target parameters (SOURCES, ROOT, LINK_LIBRARIES, INCLUDES, DEFINES, DEPENDENCIES,
+EXCLUDED_SOURCE_PATHS, etc.) are forwarded transparently.
-Usage:
+SOURCE LISTING — preferred vs. legacy
+--------------------------------------
+SOURCES (preferred)
+ Pass an explicit list of source files. CMake sees the exact file set at configure time, incremental builds are
+ reliable, and there are no hidden glob-related staleness issues.
+ When SOURCES is used, pass CHECK_SOURCES_LISTED (see below) to guard against accidentally unlisted files.
+
+ROOT (legacy / deprecated)
+ Pass a directory root; the function will GLOB_RECURSE for *.cpp under that directory.
+ New test targets should NOT use ROOT. The automatic completeness check is skipped in this mode, since GLOB
+ already guarantees every file on disk is picked up.
+
+Migration pattern — converting ROOT to SOURCES:
+ 1. Replace ROOT ${CMAKE_CURRENT_SOURCE_DIR} with an explicit SOURCES list (or include() a sources.cmake).
+ 2. Remove ADDITIONAL_SOURCE_DIRS and EXCLUDED_SOURCE_PATHS (handle in the SOURCES list).
+ See src/core/tests/CMakeLists.txt for a worked example.
+
+Automatic completeness check (opt-in)
+---------------------------------------
+The completeness check itself (every *.cpp under a scanned directory must be part of the target's SOURCES) is
+implemented as an internal helper, private to ov_add_test_target - there is no standalone function to call from a
+CMakeLists.txt. It is enabled via:
+
+ CHECK_SOURCES_LISTED Opt-in: run the check (requires SOURCES, not ROOT). The check is registered via
+ cmake_language(DEFER CALL ...) to run at the end of the current directory's
+ CMakeLists.txt processing, so EXCLUDE_TARGETS may safely name targets that do
+ not exist yet at the point ov_add_test_target() is called (e.g. targets created
+ by an add_subdirectory() that happens later in the same file) - see
+ src/core/tests/CMakeLists.txt for such a case.
+
+Extra parameters tune the check (meaningful only together with CHECK_SOURCES_LISTED; passing any of them
+without it has no effect and triggers a warning):
+
+ CHECK_SOURCES_DIRECTORY <dir> Directory to scan (default: CMAKE_CURRENT_SOURCE_DIR)
+ CHECK_SOURCES_EXTENSIONS <ext1> [<ext2>] Extensions to scan (default: cpp)
+ CHECK_SOURCES_EXCLUDE_FILES <file1> [<file2>] Files never matching the target's raw SOURCES property
+ (e.g. listed behind a generator expression)
+ CHECK_SOURCES_EXCLUDE_DIRECTORIES <dir1> [<dir2>] Directories skipped entirely by the scan
+ CHECK_SOURCES_EXCLUDE_TARGETS <tgt1> [<tgt2>] Other targets whose SOURCES should count as "listed" too
+ (e.g. a mock/benchmark target built from files that live
+ next to the main target's sources)
+
+The recommended pattern is to keep the exclusion list next to the source list it applies to, as a dedicated
+variable in the target's sources.cmake, e.g.:
+
+ # sources.cmake
+ set(MY_TESTS_SRCS
+ ${CMAKE_CURRENT_LIST_DIR}/foo_test.cpp
+ $<$<BOOL:${ENABLE_DEBUG_CAPS}>:${CMAKE_CURRENT_LIST_DIR}/debug_only_test.cpp>
+ )
+ # Files above listed behind a generator expression - excluded from the completeness check
+ set(MY_TESTS_CHECK_SOURCES_EXCLUDE_FILES
+ ${CMAKE_CURRENT_LIST_DIR}/debug_only_test.cpp
+ )
+
+ # CMakeLists.txt
+ include(sources.cmake)
+ ov_add_test_target(
+ NAME my_unit_tests
+ SOURCES
+ ${MY_TESTS_SRCS}
+ INCLUDES
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ LINK_LIBRARIES
+ common_test_utils
+ openvino::runtime
+ DEPENDENCIES
+ openvino_template_extension
+ GTEST_DISCOVER
+ CHECK_SOURCES_LISTED
+ CHECK_SOURCES_EXCLUDE_FILES
+ ${MY_TESTS_CHECK_SOURCES_EXCLUDE_FILES}
+ LABELS
+ OV UNIT
+ )
+
+Legacy example (ROOT — avoid for new targets):
ov_add_test_target(
- NAME <target-name>
- LABELS <label1> <label2>
- GTEST_DISCOVER # register per-TEST() CTest entries (requires GTest)
- TESTS_PER_SOURCE # delegate to ov_add_test_target_per_source (local dev only)
- COMPONENT <cpack-component> # default: tests
+ NAME my_unit_tests
+ ROOT ${CMAKE_CURRENT_SOURCE_DIR}
+ EXCLUDED_SOURCE_PATHS
+ ${CMAKE_CURRENT_SOURCE_DIR}/skip_this/
+ INCLUDES
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ LINK_LIBRARIES
+ common_test_utils
+ openvino::runtime
+ DEPENDENCIES
+ openvino_template_extension
+ GTEST_DISCOVER
+ LABELS
+ OV UNIT
)
-Note: LABELS must be the last keyword argument when ROOT or other ov_add_target
- pass-through args are used, as it consumes all following tokens.
+Test-specific parameters (in addition to all ov_add_target parameters):
+ NAME <target-name> (required) — also becomes the CTest test name
+ LABELS <label1> <label2> CTest LABELS property; must come after all pass-through args
+ GTEST_DISCOVER Register per-TEST() CTest entries via gtest_discover_tests
+ TESTS_PER_SOURCE Create one executable per source file (local dev only, not CI)
+ COMPONENT <cpack-component> CPack install component (default: tests)
+ See "Automatic completeness check (opt-in)" above for CHECK_SOURCES_LISTED, CHECK_SOURCES_DIRECTORY,
+ CHECK_SOURCES_EXTENSIONS, CHECK_SOURCES_EXCLUDE_FILES, CHECK_SOURCES_EXCLUDE_DIRECTORIES and
+ CHECK_SOURCES_EXCLUDE_TARGETS.
+
+Note: LABELS, CHECK_SOURCES_LISTED, CHECK_SOURCES_DIRECTORY, CHECK_SOURCES_EXTENSIONS,
+ CHECK_SOURCES_EXCLUDE_FILES, CHECK_SOURCES_EXCLUDE_DIRECTORIES and CHECK_SOURCES_EXCLUDE_TARGETS must all come
+ after every ov_add_target pass-through argument (SOURCES/ROOT, LINK_LIBRARIES, DEPENDENCIES, INCLUDES, etc.).
+ Each of these keywords greedily consumes every token that follows until the next keyword *recognized by
+ ov_add_test_target itself* - a pass-through argument like LINK_LIBRARIES placed after one of them would
+ silently be swallowed and never reach ov_add_target.
#]]
function(ov_add_test_target)
set(options
GTEST_DISCOVER # Register per-TEST() CTest entries via gtest_discover_tests (requires GTest)
TESTS_PER_SOURCE # Create one executable per source file for local dev (not for CI)
+ CHECK_SOURCES_LISTED # Opt-in: run the completeness check, deferred to the end of the directory scope (requires SOURCES)
)
set(oneValueRequiredArgs
NAME
)
set(oneValueOptionalArgs
COMPONENT
- )
+ CHECK_SOURCES_DIRECTORY # Directory scanned by the automatic completeness check (default: CMAKE_CURRENT_SOURCE_DIR)
+ )
set(multiValueArgs
LABELS
- )
+ CHECK_SOURCES_EXTENSIONS # Extensions scanned by the automatic completeness check (default: cpp)
+ CHECK_SOURCES_EXCLUDE_FILES # Files never matching the target's raw SOURCES property
+ CHECK_SOURCES_EXCLUDE_DIRECTORIES # Directories skipped entirely by the completeness check
+ CHECK_SOURCES_EXCLUDE_TARGETS # Other targets whose SOURCES should count as "listed" too
+ )
cmake_parse_arguments(ARG "${options}" "${oneValueRequiredArgs};${oneValueOptionalArgs}" "${multiValueArgs}" ${ARGN} )
if (NOT DEFINED ARG_COMPONENT)
set(ARG_COMPONENT tests)
@@ -313,6 +418,45 @@ function(ov_add_test_target)
ov_add_target(TYPE EXECUTABLE NAME ${ARG_NAME} ${ARG_UNPARSED_ARGUMENTS})
+ if(NOT ARG_CHECK_SOURCES_LISTED)
+ if(DEFINED ARG_CHECK_SOURCES_DIRECTORY OR ARG_CHECK_SOURCES_EXTENSIONS OR ARG_CHECK_SOURCES_EXCLUDE_FILES
+ OR ARG_CHECK_SOURCES_EXCLUDE_DIRECTORIES OR ARG_CHECK_SOURCES_EXCLUDE_TARGETS)
+ message(WARNING "ov_add_test_target(${ARG_NAME}): CHECK_SOURCES_* arguments were passed without "
+ "CHECK_SOURCES_LISTED - they have no effect.")
+ endif()
+ else()
+ # Peek at SOURCES/ROOT without consuming them, so they are still forwarded to ov_add_target above as-is.
+ cmake_parse_arguments(_ov_test_check "" "ROOT" "SOURCES" ${ARG_UNPARSED_ARGUMENTS})
+
+ if(_ov_test_check_ROOT)
+ message(SEND_ERROR "ov_add_test_target(${ARG_NAME}): CHECK_SOURCES_LISTED "
+ "is not available for ROOT-based (legacy) targets - ROOT-based targets already glob every file "
+ "on disk. Migrate to SOURCES to use the completeness check.")
+ elseif(NOT _ov_test_check_SOURCES)
+ message(SEND_ERROR "ov_add_test_target(${ARG_NAME}): CHECK_SOURCES_LISTED "
+ "requires SOURCES (ROOT-based targets already glob every file on disk).")
+ else()
+ if(NOT ARG_CHECK_SOURCES_DIRECTORY)
+ set(ARG_CHECK_SOURCES_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+ endif()
+
+ # cmake_language(DEFER CALL ...) only evaluates variable references in its <args> at the time the
+ # deferred call actually runs (end of the current directory's CMakeLists.txt) - by then this
+ # function's local ARG_* variables no longer exist. Wrapping the registration in
+ # cmake_language(EVAL CODE ...) bakes the current values in now as literal bracket-quoted arguments,
+ # instead of having them re-evaluated (as empty) later.
+ cmake_language(EVAL CODE "
+ cmake_language(DEFER DIRECTORY [[${CMAKE_CURRENT_SOURCE_DIR}]] CALL _ov_check_all_sources_listed
+ TARGET [[${ARG_NAME}]]
+ DIRECTORY [[${ARG_CHECK_SOURCES_DIRECTORY}]]
+ EXTENSIONS [[${ARG_CHECK_SOURCES_EXTENSIONS}]]
+ EXCLUDE_FILES [[${ARG_CHECK_SOURCES_EXCLUDE_FILES}]]
+ EXCLUDE_DIRECTORIES [[${ARG_CHECK_SOURCES_EXCLUDE_DIRECTORIES}]]
+ EXCLUDE_TARGETS [[${ARG_CHECK_SOURCES_EXCLUDE_TARGETS}]])
+ ")
+ endif()
+ endif()
+
if(EMSCRIPTEN)
set(JS_BIN_NAME "${ARG_NAME}.js")
set(JS_APP_NAME "${ARG_NAME}_js.js")
@@ -363,3 +507,90 @@ function(ov_add_test_target)
COMPONENT ${ARG_COMPONENT}
EXCLUDE_FROM_ALL)
endfunction()
+
+#[=[
+Validates that every source file matching the given extensions that exists under DIRECTORY is also listed in the SOURCES
+property of TARGET. Emits SEND_ERROR for each unlisted file so the developer sees all problems before configure stops.
+
+This is an internal helper, private to ov_add_test_target - it is not meant to be called directly from a
+CMakeLists.txt.
+
+Usage:
+_ov_check_all_sources_listed(
+ TARGET <target-name>
+ DIRECTORY <root-directory-to-scan>
+ [EXTENSIONS <ext1> [<ext2> ...]] # defaults to "cpp"
+)
+]=]
+function(_ov_check_all_sources_listed)
+ cmake_parse_arguments(_ov_casl "" "TARGET;DIRECTORY" "EXTENSIONS;EXCLUDE_DIRECTORIES;EXCLUDE_TARGETS;EXCLUDE_FILES" ${ARGN})
+
+ if(NOT _ov_casl_TARGET)
+ message(FATAL_ERROR "_ov_check_all_sources_listed: TARGET is required")
+ endif()
+
+ if(NOT _ov_casl_DIRECTORY)
+ message(FATAL_ERROR "_ov_check_all_sources_listed: DIRECTORY is required")
+ endif()
+
+ if(NOT _ov_casl_EXTENSIONS)
+ set(_ov_casl_EXTENSIONS cpp)
+ endif()
+
+ get_target_property(_ov_casl_registered_sources ${_ov_casl_TARGET} SOURCES)
+
+ # Collect sources from all excluded targets
+ set(_ov_casl_excluded_sources)
+
+ foreach(_ov_casl_excl_target IN LISTS _ov_casl_EXCLUDE_TARGETS)
+ if(TARGET ${_ov_casl_excl_target})
+ get_target_property(_ov_casl_excl_target_sources ${_ov_casl_excl_target} SOURCES)
+
+ if(_ov_casl_excl_target_sources)
+ list(APPEND _ov_casl_excluded_sources ${_ov_casl_excl_target_sources})
+ endif()
+ endif()
+ endforeach()
+
+ set(_ov_casl_glob_patterns)
+
+ foreach(_ov_casl_ext IN LISTS _ov_casl_EXTENSIONS)
+ list(APPEND _ov_casl_glob_patterns "${_ov_casl_DIRECTORY}/*.${_ov_casl_ext}")
+ endforeach()
+
+ file(GLOB_RECURSE _ov_casl_disk_files CONFIGURE_DEPENDS ${_ov_casl_glob_patterns})
+
+ foreach(_ov_casl_file IN LISTS _ov_casl_disk_files)
+ # Skip individually excluded files
+ if(_ov_casl_file IN_LIST _ov_casl_EXCLUDE_FILES)
+ continue()
+ endif()
+
+ # Skip files that belong to an excluded target
+ if(_ov_casl_file IN_LIST _ov_casl_excluded_sources)
+ continue()
+ endif()
+
+ # Skip files under any excluded directory
+ set(_ov_casl_excluded FALSE)
+
+ foreach(_ov_casl_excl_dir IN LISTS _ov_casl_EXCLUDE_DIRECTORIES)
+ cmake_path(IS_PREFIX _ov_casl_excl_dir "${_ov_casl_file}" NORMALIZE _ov_casl_is_prefix)
+
+ if(_ov_casl_is_prefix)
+ set(_ov_casl_excluded TRUE)
+ break()
+ endif()
+ endforeach()
+
+ if(_ov_casl_excluded)
+ continue()
+ endif()
+
+ if(NOT _ov_casl_file IN_LIST _ov_casl_registered_sources)
+ message(SEND_ERROR
+ "[${_ov_casl_TARGET}] '${_ov_casl_file}' exists on disk but is not listed in target sources and "
+ "will not be compiled. Add it to the appropriate CMakeLists.txt or sources.cmake.")
+ endif()
+ endforeach()
+endfunction()
diff --git a/src/core/tests/CMakeLists.txt b/src/core/tests/CMakeLists.txt
index f5a7cfa4082..a585e377832 100644
--- a/src/core/tests/CMakeLists.txt
+++ b/src/core/tests/CMakeLists.txt
@@ -98,6 +98,7 @@ set(OV_CORE_TESTS_SRCS
)
include(conditional_compilation/sources.cmake)
+include(frontend/sources.cmake)
include(pass/sources.cmake)
include(reference/sources.cmake)
include(type_prop/sources.cmake)
@@ -109,6 +110,7 @@ ov_add_test_target(
SOURCES
${OV_CORE_TESTS_SRCS}
${OV_CORE_TESTS_CC_SRCS}
+ ${OV_CORE_TESTS_FRONTEND_SRCS}
${OV_CORE_TESTS_PASS_SRCS}
${OV_CORE_TESTS_REFERENCE_SRCS}
${OV_CORE_TESTS_TYPE_PROP_SRCS}
@@ -123,7 +125,7 @@ ov_add_test_target(
# process models
test_model_zoo
LINK_LIBRARIES
- common_test_utils
+ common_test_utils
openvino::reference
openvino::util
openvino::shape_inference
@@ -136,6 +138,12 @@ ov_add_test_target(
TESTS_PER_SOURCE
LABELS
OV UNIT CORE
+ CHECK_SOURCES_LISTED
+ CHECK_SOURCES_EXCLUDE_TARGETS
+ openvino_mock1_frontend
+ ov_file_load_benchmark
+ CHECK_SOURCES_EXCLUDE_FILES
+ ${CMAKE_CURRENT_SOURCE_DIR}/dnnl.cpp
)
target_include_directories(${TARGET_NAME} PRIVATE $<TARGET_PROPERTY:openvino_core_obj,SOURCE_DIR>/src
diff --git a/src/core/tests/frontend/sources.cmake b/src/core/tests/frontend/sources.cmake
new file mode 100644
index 00000000000..12f105edcab
--- /dev/null
+++ b/src/core/tests/frontend/sources.cmake
@@ -0,0 +1,5 @@
+set(OV_CORE_TESTS_FRONTEND_SRCS
+ ${CMAKE_CURRENT_LIST_DIR}/decoder_transformation_extension.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/frontend_manager.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/progress_reporter.cpp
+)