Depending on an executable
I am trying to create a generator in C++ by compiling an executable that outputs the generated code in stdout, which is then written to a file that can be used by a different target.
So basically: generator.cpp
-> generator.exe
-> result.cpp
-> result.lib
I've already ran into a lot of roadblocks, and tried a bunch of different methods to compile and run the generator. In the end it seems like the least problematic way to do this is though targets, even if this will probably create a lot of them. And I'm close, if it wasn't for this one error:
CUSTOMBUILD : CMake error : The inter-target dependency graph contains the following strongly connected component (cycle):
"common" of type STATIC_LIBRARY
depends on "charly_build_Visual_Studio_16_2019_Win64_src_lib_common_myenum.cpp" (strong)
"charly_build_Visual_Studio_16_2019_Win64_src_lib_common_myenum.cpp" of type EXECUTABLE
depends on "common" (weak)
At least one of these targets is not a STATIC_LIBRARY. Cyclic dependencies are allowed only among static libraries.
CMake Generate step failed. Build files cannot be regenerated correctly.
It looks like I can't depend on an executable. The error states that it is a cyclic dependency, even though it is not. Is there a reason why a target is not allowed to depend on an executable? Or did I just hit unintended behavior with my uncommon use case?
Here is the code I used to create the generator target:
function(charly_generate target source destination)
# Create destination file
if(NOT EXISTS "${destination}")
file(TOUCH "${destination}")
endif()
# Add source and destination to target
target_sources("${target}" PRIVATE "${source}" "${destination}")
set_source_files_properties("${source}" PROPERTIES HEADER_FILE_ONLY ON)
# Generate unique name for the generator target
file(RELATIVE_PATH generator_target "${CMAKE_SOURCE_DIR}" "${destination}")
string(REPLACE "/" "_" generator_target "${generator_target}")
string(REPLACE " " "_" generator_target "${generator_target}")
set(generator_target "charly_${generator_target}")
# Add generator executable
add_executable("${generator_target}" "${source}")
target_link_libraries("${generator_target}" PRIVATE "${target}")
set_target_properties("${generator_target}" PROPERTIES FOLDER "Charly")
# Call generator executable from custom command
add_custom_command(OUTPUT "${destination}" COMMAND "$<TARGET_FILE:${generator_target}>")
endfunction()
I am currently using cmake 3.15.0 with Visual Studio 2019