Imagine a code generator which reads an input file (say a UML class diagram) and produces an arbitrary number of source files which I want to be handled in my project. (to draw a simple picture let's assume the code generator just produces .cpp
files).
The problem is now the number of files generated depends on the input file and thus is not known when writing the CMakeLists.txt
file or even in CMakes configure step. E.g.:
>>> code-gen uml.xml
generate class1.cpp..
generate class2.cpp..
generate class3.cpp..
What's the recommended way to handle generated files in such a case? You could use FILE(GLOB.. )
to collect the file names after running code-gen
the first time but this is discouraged because CMake would not know any files on the first run and later it would not recognize when the number of files changes.
I could think of some approaches but I don't know if CMake covers them, e.g.:
(somehow) define a dependency from an input file (
uml.xml
in my example) to a variable (list with generated file names)in case the code generator can be convinced to tell which files it generates the output of
code-gen
could be used to create a list of input file names. (would lead to similar problems but at least I would not have to useGLOB
which might collect old files)just define a custom target which runs the code generator and handles the output files without CMake (don't like this option)
Update: This question targets a similar problem but just asks how to glob generated files which does not address how to re-configure when the input file changes.