I have been fighting with this issue for a while but so far I haven't found a good answer, so here we are...
I have been trying to compile a project using a single bash script, my requirements for it are:
- allow quick iterative builds and
- automatically update the CMakeCache.txt variables (defined as bash variables).
(This project has multiple branches so the variables and the CMakeLists.txt files can change from branch to branch. I don't control the root CMakeLists.txt file so I can't change the way the variables are defined.)
The first issue I faced is when should I run cmake, which I didn't want to do very often since it takes some time. After reading this answer it was clear to me that it only needs to be run once, so I added a condition to check if the Makefile already exists. If it does, cmake is not called (directly).
The second issue, and the one this question relates to, is how to keep the CMakeCache.txt variables updated. I thought that cmake must provide some command to update only the variables, but I couldn't find such command. As far as I could understand the recommended way is to use the cmake GUI to change the CMakeCache.txt variables.
After some time I remembered I could do it with other bash commands, so at the moment I'm checking the value of the variable in CMakeCache.txt and if it is different from what I expected it is updated. This way the CMakeCache.txt is only changed if it is necessary. Here is how the script looks at the moment (cleaned up for clarity):
#!/bin/bash # variables SOME_VARIABLE="some_value" # clean build if [ "$1" = "-clean" ] then # Remove stuff fi # change directory mkdir build cd build # run cmake if Makefile does not exist if [ ! -f "Makefile" ]; then cmake .. \ -DSOME_VARIABLE=$SOME_VARIABLE fi # update cmake variables if ! grep -Fxq "SOME_VARIABLE:UNINITIALIZED=$SOME_VARIABLE" CMakeCache.txt ; then sed -i "s/SOME_VARIABLE:UNINITIALIZED=.*\n/SOME_VARIABLE:UNINITIALIZED=$SOME_VARIABLE/" CMakeCache.txt echo Updated variable fi # run make make Is there a proper way of doing this? This is working but I feel that it is a bit hacky and that there should be a better way to do this.
Thank you for your time, David
CMakeCache.txtand do not runcmake. Just usecmake -D<var1>=<value1> -D<var2>=<value2>for run update variable and runcmakeat the same time.