1

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).

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I still don't see how that affects the dependencies of lua2c itself, unless CMake is optimizing it out, but this works - thanks!
@DrewMcGowen In your example, the command was executed during the lua2c build. So at this time the lua2c binary does not exist. That why you have to add dependencies somewhere to force the build of lua2c before anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.