Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about CMake's cached variable setting priority

Tags:

caching

cmake

I'm confused about CMake's cached variables:

  • Case 1: CACHE + FORCE

    set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "" FORCE)
    
    • First CMake run: "myflags" appears in the CMakeCache.txt file as intended.
    • Command line options: command line options do not override "myflags" - seems like FORCE has higher priority than command line -D...="..." arguments. This is not desired - I would like command line arguments to override "myflags".

  • Case 2: only CACHE

    set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "")
    
    • First CMake run: nothing appears in the CMakeCache.txt file. I want"myflags" to appear for the first run.
    • Command line options: command line have priority over "myflags".

Am I correct about my conclusions? Or do "default variables" such as CMAKE_CXX_FLAGS behave differently?

Is there a way to have "myflags" written in the CMakeCache.txt file during the first CMake run (when CMake wasn't run previously in this folder)?

I'd like to set "myflags" during the first CMake run in the cache, then allow the user to override it using the command line.

If I use FORCE, the user can't override it via command line.

If I don't use FORCE, "myflags" isn't written in the cache file during the first run.

like image 782
Vittorio Romeo Avatar asked Oct 01 '14 16:10

Vittorio Romeo


People also ask

How do I change CMake cache entry?

In a CMake script you can only change existing Cache entries if you use the set(... CACHE ... FORCE) syntax. This behavior is made use of e.g. by CMake itself, because it normally does not force Cache entries itself and therefore you can pre-define it with another value.

How do you set a variable in CMake?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

What generates CMakeCache txt?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.

Where is CMake cache stored?

CMake caches variables and settings in the CMakeCache. txt file. When you load a project for the first time, this file is generated in the build directory (cmake-build-debug or cmake-build-release by default) according to the contents of CMakeLists. txt.


1 Answers

This is consistent with the behaviour explained in the documentation:

Normally, set(...CACHE...) creates cache variables, but does not modify them. If FORCE is specified, the value of the cache variable is set, even if the variable is already in the cache. This should normally be avoided, as it will remove any changes to the cache variable’s value by the user.

CMAKE_CXX_FLAGS is present in the cache even without set(CMAKE_CXX_FLAGS ... CACHE ...) in your CMakeLists.txt, because CMAKE_CXX_FLAGS variable is already set during compiler flags initialization.

like image 143
Peter Avatar answered Sep 28 '22 06:09

Peter