Let's assume I have a script that generates a set of source files forming a target I want to link against in a CMakeLists.txt
. If the file names are known to the latter then the usual add_custom_target()
and add_custom_command()
commands will make it possible to use the generated files as target sources.
Let's assume, though, that only the generator script knows the file names and locations. How can a target library be generated so that the parent CMakeLists.txt
can link against it without its knowing the actual file names?
Note that the dependency topic isn't in this question's scope as the script knows itself when to regenerate or not. It's not the finest use of CMake, but it's sufficient in this use case.
Idea #1
The script also generates a generated.cmake
file included by the parent one using include(generated.cmake)
. Problem: CMake doesn't find generated.cmake
as it isn't existing at configuration time.
Idea #2
Similar to idea #1, but the script is called with the execute_process()
so that generated.cmake
is present at configuration time. Problem: The script is not called anymore at subsequent builds, thus ignoring possible changes to its input.
Idea #3
The script passes back a list of targets and files that is somehow considered by the parent CMakeLists.txt
. So far I couldn't find a way to do so.
execute_process()
command when a specific file changes. It gets me closer to what I'm aiming at, but I face the problem that I don't know all relevant input files (as the script parses the input files itself to derive other input files).execute_process()
and has the script "publish" its identified input files so that the parent CMake file can add them to the directory'sCMAKE_CONFIGURE_DEPENDS
property. I'll describe it thoroughly as possible answer as soon as it is finalized.add_executable
requires all source files to be specified at configuration step. As you don't know source file, generated by your script, you have no other way than call that script at configuration stage, and collect the generated files using, e.g.file(GLOB)
. So your "Idea #1" and "Idea #2" are just inevitable. This is described in that question: stackoverflow.com/questions/44076307/…. And dependency tracking for the generating script is just another problem, which is described by the question referenced by you.