I have a CMake project with many subprojects. Each of them can use a function I provide to generate a small text file with some certain information (by calling add_custom_command). At the final step I'd like to combine all those files into one big text file.
I've created a custom command which searches for created files (all in one place) and merges them.
The problem is that I'd like to make this final step dependend on all of small steps being made in subprojects while I don't actually know how many files will be provided.
My final command looks like:
add_custom_command(OUTPUT combination.txt
COMMAND create combination.txt from all files from /path/)
and my create-small-text-file-for-each-subproject command looks like:
add_custom_command(OUTPUT /path/${sub_project_name}.txt
COMMAND create /path/${sub_project_name}.txt)
And when I create those small files I'd like to do something like to make "combination.txt" depend on /path/${sub_project_name}.txt
So I wish I could:
add_dependency(combination.txt /path/${sub_project_name}.txt)
However this only works for targets.
I've also tried to use set_source_files_properties with OBJECT_DEPENDS, but it seems to doesn't work (maybe its intend to be used with add_target's cpp files ?)
The last way to get it work I see is to use a cache variable which would accumulate all those small files paths and then use it like this:
add_custom_command(OUTPUT combination.txt
COMMAND create combination.txt from all files from /path/
DEPENDS ${all_small_files_list})
but this is the last thing I want to do.
Instead of using
add_custom_command
you could useadd_custom_target
with a correct dependency-definition (so that is it not built every time).Again: make sure you're using the
DEPENDS
argument ofadd_custom_target
to create a real dependency chain so that a project-target and thus the combination-target gets out of date.UPDATE: I was too premature. In fact cmake (at least up to 2.8.9) works as follows for dependencies: with a call to
add_dependencies
you cannot add a dependency which is theOUTPUT
of a custom command IOW a (generated) file. Withadd_dependencies
you can only addtarget
as created byadd_custom_target
. However in aadd_custom_target
you can depend on an output ofadd_custom_command
by using theDEPENDS
-directive. That said this makes it work:This will make the combination target always be regenerated as it has no
MAIN_DEPENDENCY
orDEPENDS
, but the usage ofadd_dependencies
is allowed.