How to chain custom_targets from different directories in CMake without global variables?

By | January 28, 2018

According to this tutorial (Part 5):

CMake: dependencies between targets and files and custom commands

File-level dependencies of custom targets are not propagated

So when I have two custom targets in chain, second custom command should depend both on first custom target and first custom command output

But what if I have custom targets in different sub-directories. How can I get path to files generated by first target?

project
├── gen1
│   └── CMakeLists.txt
├── gen2
│   └── CMakeLists.txt
└── CMakeLists.txt

gen1 CMakeLists.txt:

set(SPECIAL_TEXT foo)    
add_custom_command(
    OUTPUT gen1
    COMMAND cmake -E echo ${SPECIAL_TEXT} > gen1
    )

add_custom_target(
    gen1-wrapper
    DEPENDS gen1
    )

gen2 CMakeLists.txt:

add_custom_command(
    OUTPUT gen2
    DEPENDS gen1-wrapper **????????**
    COMMAND cmake -E copy gen1 gen2
    )

add_custom_target(
    all-generated ALL
    DEPENDS gen2
    )

top CMakeList.txt

add_subdirectory(gen2)
add_subdirectory(gen1)

Is it possible to propagate dependencies without introducing global variables?