Home > Enterprise >  Cmake: how to delete old variables with a new build
Cmake: how to delete old variables with a new build

Time:02-27

I am trying to configure my app with cmake, which depends on

cmake -DVERSION_TO_BUILD ../

In my CMakeList I wrote that checker

if (NOT VERSION_TO_BUILD)
    message(FATAL_ERROR "Please, set VERSION_TO_BUILD")
endif()

In first configure all works normally, but in the next time when I try to reconfigre like

cmake ../

When I configure my app without -DVERSION_TO_BUILD=1, cmake throws an error (as expected). After that, I configure an app with -DVERSION_TO_BUILD=1 and it does not give an error (as expected). However, when I go back to the first option without -DVERSION_TO_BUILD=1 it does not give an error. I checked it by calling message and found that it exists.
I found out that cmake saved VERSION_TO_BUILD variable. So my checker doesn't work correctly.

Of course, I can't use set in CMakeList because VERSION_TO_BUILD is external dependency.
How can I fix this so that I don't delete the cmake folder?

CodePudding user response:

Passing a -D option during the configuration of your cmake project sets a cache variable. The cache is persisted for builds and future reconfiguration. Note that cmake does not treat cache variables that were persisted and cache variables that are passed via command line any different during reconfiguration of a project.

In fact after editing the project files building the project may actually cause the version of cmake that causes a reconfiguration of the project without passing any additional information (i.e. cmake <path to build dir>) to be run automatically.

It would be preferrable to simply set up different build directories for both configurations, assuming there are no conflicting files written e.g. to the source tree, if you do this.

Other than that you can simply reconfigure the project explicitly deleting a cache entry using the -U option

cmake -U VERSION_TO_BUILD <path to build dir>

CodePudding user response:

However, when I go back to the first option without -DVERSION_TO_BUILD=1 it does not give an error.

This is the expected behaviour, documented in the manual. cmake .. reconfigures the targets with exactly the same arguments that are used previously. You should explicitly change the arguments if you want update the targets.

cmake -DVERSION_TO_BUILD=NO ..

You may also remove CMakeCache.txt if you want to reset the arguments to their default values.

CodePudding user response:

If you for some reason need that VERSION_TO_BUILD variable to be specified each time you configure; and want an error if it's not specified via -D..., then you can remove it at the end of the config run; use the unset command for that:

In CMakeLists.txt:

# ... all other CMake code
UNSET(VERSION_TO_BUILD)

The "real" solution would probably be to find a non-saving way of passing in the information to CMake, but I don't currently know of any way to pass a variable into CMake which is not cached. Maybe the folks at the CMake Discourse could tell you one.

I do suspect, however, that the behavior you intend is currently not really something that CMake supports directly. From my experience, the mindset for CMake is that a build directory typically represents a specific build configuration; if anything about that configuration should change, a new build directory should be created.

  • Related