Confused about CMake caching precedence

I got confused in cached CMake variables:

  • Case 1: CACHE + FORCE

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

  • Case 2: CACHE only

     set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "") 
    • First start of CMake: nothing is displayed in the CMakeCache.txt file. I want "myflags" appear for the first run.
    • Command line options: take precedence over "myflags" .

Am I drawing conclusions correctly? Or do "default variables" like CMAKE_CXX_FLAGS behave differently?

Is there a way to have "myflags" written in the CMakeCache.txt file the first time CMake is started (when CMake has not previously been run in this folder)?

I would like to set "myflags" the first time CMake is run in the cache, and then allow the user to override it using the command line.

If I use FORCE , the user cannot override it through the command line.

If I do not use FORCE , "myflags" not written to the cache file during the first run.

+6
source share
1 answer

This is consistent with the behavior described in the documentation:

Normally, set (... CACHE ...) creates cache variables, but does not change them. If FORCE is specified, the value of the cache variable is set, even if the variable is already in the cache. This should usually be avoided, as it will remove the user changing the value of the cache variables.

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

+11
source

Source: https://habr.com/ru/post/976118/


All Articles