Skip to content

Commit

Permalink
cmake: Provide a way to deal with generated files in gtest_add_tests
Browse files Browse the repository at this point in the history
Generated files don't exist yet, so they can't just be passed to
file(READ). Unfortunately, the GENERATED property doesn't help
that much here, since it is only visible to lists in the same
directory as the relevant source file. Since we call gtest_add_tests
at the toplevel CMakeLists.txt, it won't be visible.

Generated files which form part of the tests but don't contain
any tests themselves should be put into SUPPORT_SOURCES before
core_add_test is called. In that case, they won't be considered
as part of the library.
  • Loading branch information
smspillaz committed May 2, 2016
1 parent ad7539d commit a0b9cee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion project/cmake/scripts/common/macros.cmake
Expand Up @@ -41,9 +41,12 @@ endfunction()

# Add a test library, and add sources to list for gtest integration macros
function(core_add_test_library name)
# Backup the old SOURCES variable, since we'll append SUPPORT_SOURCES to it
set(TEST_ONLY_SOURCES ${SOURCES})
set(SOURCES ${SOURCES} ${SUPPORT_SOURCES})
core_add_library(${name} NO_MAIN_DEPENDS)
set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL 1)
foreach(src ${SOURCES})
foreach(src ${TEST_ONLY_SOURCES})
# This will prepend CMAKE_CURRENT_SOURCE_DIR if the path is relative,
# otherwise use the absolute path.
get_filename_component(src_path "${src}" ABSOLUTE)
Expand Down
3 changes: 3 additions & 0 deletions project/cmake/scripts/common/projectmacros.cmake
Expand Up @@ -54,6 +54,9 @@ function(GTEST_ADD_TESTS executable extra_args)
message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
endif()
foreach(source ${ARGN})
# This assumes that every source file passed in exists. Consider using
# SUPPORT_SOURCES for source files which do not contain tests and might
# have to be generated.
file(READ "${source}" contents)
string(REGEX MATCHALL "TEST_?[F]?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
foreach(hit ${found_tests})
Expand Down

0 comments on commit a0b9cee

Please sign in to comment.