0

I want to have one top level custom target, and nested submodules which are able to extend this already existed target by adding/binding custom_command to it. For now I face a problem: if add_custom_command(TARGET target_name ...) is used not in a file where the target is defined (target is defined on top level CMakeLists.txt), then this custom_command is simply ignored.

Details:

1) custom_target created on the top-level CMakelists.txt

cmake_minimum_required(VERSION 2.8)

add_custom_target(custom_tg
COMMAND ls > custom_target.txt
)

add_custom_command(TARGET custom_tg
COMMAND ls > custom_command1.txt
)

add_subdirectory(sub)

2) In subdirectory "sub" there is other CMakeLists.txt (module) which I was hoping is able to extend the existed target with custom command.

add_custom_command(TARGET custom_tg
COMMAND ls > custom_command2.txt 
)

3) When I create build directory and run

cmake .. && make custom_tg

the output files are custom_target.txt and custom_command1.txt, but there is no custom_command2.txt, and that's actually my problem is.

If I look into build/CMakeFiles/custom_tg.dir/build.make I see no mention about custom_command2.txt there. Here is content of build.make file: http://pastebin.com/zVVS4sYL.

Actually I can't find any mention about custom_command2.txt in tree of files generated by Cmake, and this looks weird to me.

Looking forward for you help.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153

1 Answers1

0

This is usually solved by constructing dependencies between targets. For each command in one of your subdirs you would first add the 'local' command/target and then make it a dependency for the top-level one. Thereby forcing cmake to execute all 'local' targets before it executes the top-level one.

renefritze
  • 2,167
  • 14
  • 25
  • sorry, sure it's accepted, i tried to rate it, but i got message "Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly displayed post score" – Oleksandr Pyvovarov May 23 '16 at 14:15
  • That's upvoting. Have a look at https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – renefritze May 23 '16 at 14:19