简体   繁体   中英

add_custom_command from another target

I'm currently trying to setup a CMake project with two executables, one of which is a simple utility used to generate code for the other. Relevant bits of CMakeLists.txt:

add_executable(lua2c lua2c.c)
add_custom_command(OUTPUT lcode.c COMMAND lua2c lcode.lua lcode.c MAIN_DEPENDENCY lua2c)
...
add_executable(darpem ... lcode.c)

With this setup, target lua2c winds up with no dependencies, which causes cc to complain about no input files. If I remove the add_custom_command line, then lua2c is built properly, but obviously doesn't generate the file lcode.c . Is this possible in CMake? Would I need to add a subdirectory dependency of sorts?

Using CMake version 2.8.1 on Ubuntu 13.04, x86-64.

NOTE: For my particular case, because lua2c is simple enough, I can use a different language. I am, however, still curious as to how this might be possible (for more complex setups).

From the documentation :

Note that MAIN_DEPENDENCY is completely optional and is used as a suggestion to visual studio about where to hang the custom command.

Maybe this should solve your problem :

add_executable(lua2c lua2c.c)
add_custom_command(OUTPUT lcode.c COMMAND lua2c lcode.lua lcode.c DEPENDS lua2c)
#                                                                 ^^^^^^^
...
add_executable(darpem ... lcode.c)

Or if it doesn't work, this one should work :

add_executable(lua2c lua2c.c)
add_custom_command(TARGET lua2c
                    POST_BUILD
                    COMMAND lua2c lcode.lua lcode.c )

...
add_executable(darpem ... lcode.c)
add_dependencies( darpem lua2c )

It simply add a post build event after the build of lua2c . And it add lua2c as a dependency of darpem .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM