Generator expression TARGET_NAME doesn't fail if target is not presented
Problem
-
TARGET_FILE
generator expression checks targets existance on configure+generate stage (before the real build is started) and failes, if the target is not presented. -
TARGET_NAME
generator expression have no similar check, but it should. Looks like it just replace the value instead of real checking. If we need just not interpret value as a possible target name, I prefer to rename it to theRAW_STRING
or similar. -
TARGET_NAME_IF_EXISTS
only returns the empty string instead of error generation - nice check only API.
Why we need this check
We're using this just to check that target is really presented in the end of the configure stage.
We have a very big project with lots of options. We have some cases, when the target name is used & we hope it exist, but it's not the truth. And want to trigger this problem on configure stage instead of sometimes on linkage stage. Sometimes it may be used something else from the system (such as executable, library, etc.) instead of our target - and this behavior leads to the execution problems, that's much worse.
Example
Files to reproduce the problem:
- CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(prj1)
add_executable(
${PROJECT_NAME}
main.c
)
set(importedTargetName MyMath)
target_link_libraries(
${PROJECT_NAME}
PUBLIC
## No error on configure stage - OK
# "${importedTargetName}"
## No error on configure stage - NOT OK
# "$<TARGET_NAME:${importedTargetName}>"
## Error on configure stage - OK
"$<TARGET_FILE:${importedTargetName}>"
)
- main.c
int main()
{
return 0;
}