After running the cmake
command once to generate a build system, when, if ever, should I rerun the cmake
command?
The generated build systems can detect changes in the associated CMakeLists.txt
files and behave accordingly. You can see the logic for doing so in generated Makefiles. The exact rules for when this will happen successfully are mysterious to me.
When should I rerun cmake
? Does the answer depend on the generator used?
This blog post (under heading: "Invoking CMake multiple times") points out the confusion over this issue and states that the answer is actually 'never', regardless of generator, but I find that surprising. Is it true?
The answer is simple: The
cmake
binary of course needs to re-run each time you make changes to any build setting, but you wont need to do it by design; hence "never" is correct regarding commands you have to issue.The build targets created by cmake automatically include checks for each file subsequently [=starting from the main CMakeLists.txt file] involved or included generating the current set of Makefiles/VS projects/whatever. When invoking
make
(assuming unix here) this automatically triggers a previous execution ofcmake
if necessary; so your generated projects include logic to invoke cmake itself! As all command-line parameters initially passed (e.g.cmake -DCMAKE_BUILD_TYPE=RELEASE ..
will be stored in theCMakeCache.txt
, you dont need to re-specify any of those on subsequent invocations, which is why the projects also can just runcmake
and know it still does what you intended.Some more detail: CMake generates book-keeping files containing all files that were involved in Makefile/Project generation, see e.g. these sample contents of my
<binary-dir>/CMakeFiles/Makefile.cmake
file using MSYS makefiles:Any modification to any of these files will trigger another cmake run whenever you choose to start a build of a target. I honestly dont know how fine-grained those dependencies tracking goes in CMake, i.e. if a target will just be build if any changes somewhere else wont affect the target's compilation. I wouldn't expect it as this can get messy quite quickly, and repeated CMake runs (correctly using the Cache capabilities) are very fast anyways.
The only case where you need to re-run
cmake
is when you change the compiler after you started aproject(MyProject)
; but even this case is handled by newer CMake versions automatically now (with some yelling :-)).additional comment responding to comments:
There are cases where you will need to manually re-run cmake, and that is whenever you write your configure scripts so badly that cmake cannot possibly detect files/dependencies you're creating. A typical scenario would be that your first cmake run creates files using e.g.
execute_process
and you would then include them usingfile(GLOB ..)
. This is BAD style and the CMake Docs for file explicitly sayBtw this comment also sheds light on the above explained self-invocation by the generated build system :-)
The "proper" way to treat this kind of situations where you create source files during configure time is to use
add_custom_command(OUTPUT ...)
, so that CMake is "aware" of a file being generated and tracks changes correctly. If for some reason you can't/won't useadd_custom_command
, you can still let CMake know of your file generation using the source file property GENERATED. Any source file with this flag set can be hard-coded into target source files and CMake wont complain about missing files at configure time (and expects this file to be generated some time during the (first!) cmake run.