0

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

3
  • 2
    There is a little sense to update CMakeCache.txt and do not run cmake. Just use cmake -D<var1>=<value1> -D<var2>=<value2> for run update variable and run cmake at the same time. Commented Jun 23, 2020 at 21:41
  • What you mean is something like KamilCuk is suggesting right? Commented Jun 26, 2020 at 15:47
  • Yes, his answer talks about the same. Commented Jun 26, 2020 at 15:52

1 Answer 1

1

Is there a proper way of doing this?

Just re-run cmake.

if # run cmake if Makefile does not exist [ ! -f "Makefile" ] || # or when variable differs ! grep -Fxq "SOME_VARIABLE:UNINITIALIZED=$SOME_VARIABLE" CMakeCache.txt then cmake .. "-DSOME_VARIABLE=$SOME_VARIABLE" fi 

better yet, don't bind to specific generator:

if # run cmake if Makefile does not exist [ ! -f "CMakeCache.txt" ] || # or when variable differs ! grep -Fxq "SOME_VARIABLE:UNINITIALIZED=$SOME_VARIABLE" CMakeCache.txt then cmake "-DSOME_VARIABLE=$SOME_VARIABLE" .. fi cmake --build . 
Sign up to request clarification or add additional context in comments.

1 Comment

Facepalm Don't know how did not remember this, much cleaner and very reasonable :) Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.