Reputation: 24850
I have a code needs complicated parameter input. So my design is to write a compiler that compiles a simpler input to C++ code and then compile the C++ code into the big project. Now the problem is the compiler itself is written in C/BISON and I need to compile it before generating C++ code. Since both part of the code is made using CMake, is it possible to let CMake have a 2-steps compile, i.e. compile the compiler, and call the compiler to generate C++ code, and then compile the generated C++ code?
Upvotes: 1
Views: 176
Reputation: 310
Yes, it is possible. You can do something like this:
add_executable(compiler compiler.c)
add_custom_command(OUTPUT complicated.cpp COMMAND compiler DEPENDS compiler.c)
add_executable(main_program complicated.cpp)
This builds your compiler
from compiler.c
, then adds a custom command that uses the compiler
to generate complicated.cpp
. Finally, the main_program
is built from the generated code.
add_custom_command
has some more optional parameters, e.g. to supply command line arguments to compiler
. Adding the compiler source files as dependencies of the custom command (i.e. DEPENDS compiler.c
) is needed if you want complicated.cpp
to be re-generated when you make changes to the compiler.
Upvotes: 2