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.

Added a new global property XBMC_GENERATED_TEST_FILES, which should
be appended to every time a new generated source file is added to a
test. gtest_add_tests will skip files in this list.
  • Loading branch information
smspillaz committed Apr 28, 2016
1 parent ad7539d commit 78e9ef6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions project/cmake/scripts/common/projectmacros.cmake
Expand Up @@ -50,10 +50,32 @@ endfunction()
# Copied from FindGTest.cmake
# Thanks to Daniel Blezek <blezek@gmail.com> for the GTEST_ADD_TESTS code
function(GTEST_ADD_TESTS executable extra_args)
get_property(XBMC_GENERATED_TEST_FILES GLOBAL PROPERTY XBMC_GENERATED_TEST_FILES)

if(NOT ARGN)
message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
endif()
foreach(source ${ARGN})
get_property(GENERATED SOURCE "${source}" PROPERTY GENERATED)
get_property(SKIP_SCAN_TESTS SOURCE "${source}" PROPERTY SKIP_SCAN_TESTS)
if (NOT EXISTS "${source}")
# We can't rely on the GENERATED property here since it is likely that
# the file was added in a separate subdirectory. According to the CMake
# documentation, properties on source files can only be read by other
# listfiles in the same directory. This is an issue with
# set_source_file_properties itself.
#
# https://cmake.org/cmake/help/v3.0/command/set_source_files_properties.html
list(FIND XBMC_GENERATED_TEST_FILES "${source}" INDEX)
if ("${INDEX}" EQUAL -1)
message(FATAL_ERROR "Cannot scan ${source} for tests, as it does not exist")
else()
# This is a generated source file, don't read it. Note that this means
# that you can't have tests generated by the build system, though
# supporting code can be generated.
continue()
endif()
endif()
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 78e9ef6

Please sign in to comment.