gtest_discover_tests doesn't work with ALIAS targets
I have a slightly complicated CMake setup with two entry points (main CMakeLists.txt files). One builds and tests the code and the other is able to run tests against an installed tree of objects. This allows me to perform separated build and test runs in CI (where the build and test machine might not be the same).
As part of the ExternalTesting mode I find_package
on my install tree and re-export all the executables with their original names (minus the namespace).
get_directory_property(targets DIRECTORY "." IMPORTED_TARGETS)
foreach(target ${targets})
get_target_property(target_type ${target} TYPE)
# remove the namespace from the target name
string(REGEX REPLACE "^[^:]+::" "" target_name "${target}")
if (target_type MATCHES ".+_LIBRARY$")
add_library("${target_name}" ALIAS "${target}")
elseif (target_type STREQUAL "EXECUTABLE")
add_executable("${target_name}" ALIAS "${target}")
message("${target} -> ${target_name}")
endif()
endforeach()
When trying to use one of those executables with gtest_discover_tests
I have noticed that I get the following error.
include(GoogleTest)
gtest_discover_tests(my_test DISCOVERY_MODE PRE_TEST)
set_property can not be used on an ALIAS target.
See https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/GoogleTest.cmake#L468-472