How to force recompile of application with cmake
c++, cmake
Solution
Well, you should introduce an artificial dependency between generator compilation target and source generation target.
You should add the following to the CMakeLists.txt from the mentioned SO question, as suggested in the answer by Ronny Andersson on that page.
- use MAIN_DEPENDENCY argument in add_custom_command to introduce dependency between your "top-level" sources and the generated files.
- use DEPENDS arguments to introduce other dependencies such as dependency on the target which builds the generator (if creation of generator is added like `add_binary(generator_name generator_source1... generator_source2...)` then use `generator_name` as dependency name)
- Mark generated sources as, well, generated with the argument `PROPERTIES GENERATED 1` in `add_custom_command`.
These three arguments make a proper dependency chain in CMake-generated Makefile's. See the text at the bottom of `SWIG_ADD_SOURCE_TO_MODULE` in `/usr/share/CMake/Modules/UseSWIG.cmake` (or wherever your CMake distribution internals reside) to see what's done to solve basically the same task as yours.
Problem
I am using a source file generator application to generate the source files of an other application which is compiled with CMake (noth of the in one project). the basic setup is like: ``` a. generator b. something_else The generator is a few CPP files linked together The something_else is: a. a few "normal" CPP files b. generated CPP/h files which I have to link in. These CPP files are generated by the generator ``` The generator is configured in the configure phase with some choices, depending on these options the content of the generated files is different. The issue was presented here: CMake compile time generation of source (together with the solution). Now, when I make the generator is compiled and executed, then the something_else is compiled and linked. However, now I have the following issue: If I modify only the sources of the generator and execute a make in the build directory the generator executable is recompiled and linked, however it is not executed, nor is the something_else recompiled and linked so I have to `make clean` and `make` again in order to have the generator executed. And the question is: is it possible to have the cmake re-run the generator in case the sources of it are modified? If yes, how?