The bogus cycle using add_custom_command and ALIAS
I want to build some executable with custom command. I've made a command, a target on it, and create executable target with "IMPORTED GLOBAL" to provide executable location via properties. By historical reasons I also need an another target name identical to the executable name, but I got a strange "strong component error".
The minimal reproducible sample:
project("cycle")
cmake_minimum_required(VERSION 3.20)
add_custom_command(
OUTPUT "out.exe"
COMMAND ${CMAKE_COMMAND} -E touch out.exe
)
add_custom_target(
"out.build"
DEPENDS "out.exe"
)
add_executable("hello" IMPORTED GLOBAL)
add_dependencies("hello" "out.build")
set_target_properties("hello" PROPERTIES "IMPORTED_LOCATION" "out.exe")
add_executable("out" ALIAS "hello") # alias with the same target name as an OUTPUT of the add_custom_command
Built with: "C:\Program Files\CMake3.20\bin\cmake.exe" ..\ -G"Ninja"
&& "C:\Program Files\CMake3.20\bin\cmake.exe" --build . --target out.build
Got an error:
CMake Error: The inter-target dependency graph contains the following strongly connected component (cycle):
"out.build" of type UTILITY
depends on "out.build" (strong)
The component contains at least one cycle consisting of strong dependencies (created by add_dependencies) that cannot be broken.
CMake Generate step failed. Build files cannot be regenerated correctly
If create an alias with any other name, it works like a charm, so I have an assumption that add_custom_command
create internal target with name of the output executable, which cause the cycle.