xis
xis

Reputation: 24850

Calling an executable generate during make in CMake

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

Answers (1)

superquadratic
superquadratic

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.cppto be re-generated when you make changes to the compiler.

Upvotes: 2

Related Questions