0

cmake set value of variable for first run after command "project" prompted the present OP. From the tests mentioned there, and others, I guess that on first run CMakeCache.txt is created when executing command project.

Could someone confirm/correct, quoting a reputable source?

  • `CMakeLists.txt` is a sequence of commands. I guess (maybe wrong) file `CMakeCache.txt` is actually created on first run when executing command `project ...`. I guess I could put together tests, setting a wait time (if possible) at different points across `CMakeLists.txt`. – sancho.s ReinstateMonicaCellio Feb 15 '22 at 09:12

1 Answers1

1

I guess that on first run CMakeCache.txt is created when executing command project.

It is not.


The CMake cache, if it exists, is read on program start-up. Otherwise it initializes only the following variables:

  • CMake executables: CMAKE_COMMAND, CMAKE_CPACK_COMMAND, CMAKE_CTEST_COMMAND
  • CMake generator info: CMAKE_EXTRA_GENERATOR, CMAKE_GENERATOR, CMAKE_GENERATOR_INSTANCE, CMAKE_GENERATOR_PLATFORM, CMAKE_GENERATOR_TOOLSET
  • CMake installation info: CMAKE_HOME_DIRECTORY, CMAKE_ROOT

See this test:

$ cat CMakeLists.txt
get_property(vars DIRECTORY PROPERTY CACHE_VARIABLES)
message(STATUS "${vars}")

cmake_minimum_required(VERSION 3.22)
project(test NONE)
$ rm -rf build && cmake -S . -B build
-- CMAKE_COMMAND;CMAKE_CPACK_COMMAND;CMAKE_CTEST_COMMAND;CMAKE_EXTRA_GENERATOR;CMAKE_GENERATOR;CMAKE_GENERATOR_INSTANCE;CMAKE_GENERATOR_PLATFORM;CMAKE_GENERATOR_TOOLSET;CMAKE_HOME_DIRECTORY;CMAKE_ROOT
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

The project() command will, naturally, create many cache entries related to the language and project metadata. At a minimum, with no languages enabled, it sets these variables:

  • Platform information: CMAKE_PLATFORM_INFO_INITIALIZED, CMAKE_MAKE_PROGRAM, CMAKE_UNAME
  • Generator settings: CMAKE_VERBOSE_MAKEFILE, CMAKE_COLOR_MAKEFILE, CMAKE_EXPORT_COMPILE_COMMANDS
  • Installation settings: CMAKE_INSTALL_PREFIX, CMAKE_INSTALL_SO_NO_EXE, CMAKE_SKIP_INSTALL_RPATH, CMAKE_SKIP_RPATH
  • Project metadata: CMAKE_PROJECT_DESCRIPTION, CMAKE_PROJECT_HOMEPAGE_URL, CMAKE_PROJECT_NAME
  • Project-relative paths: ${PROJECT_NAME}_BINARY_DIR, ${PROJECT_NAME}_IS_TOP_LEVEL, ${PROJECT_NAME}_SOURCE_DIR

It will not however, write them to disk. Only when the program exits normally are the contents of the cache written out to disk. See this test:

$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.22)
project(test NONE)

get_property(vars DIRECTORY PROPERTY CACHE_VARIABLES)
message(STATUS "${vars}")

execute_process(COMMAND pkill cmake)

$ rm -rf build && cmake -S . -B build
-- CMAKE_COLOR_MAKEFILE;CMAKE_COMMAND;CMAKE_CPACK_COMMAND;CMAKE_CTEST_COMMAND;CMAKE_EXPORT_COMPILE_COMMANDS;CMAKE_EXTRA_GENERATOR;CMAKE_GENERATOR;CMAKE_GENERATOR_INSTANCE;CMAKE_GENERATOR_PLATFORM;CMAKE_GENERATOR_TOOLSET;CMAKE_HOME_DIRECTORY;CMAKE_INSTALL_PREFIX;CMAKE_INSTALL_SO_NO_EXE;CMAKE_MAKE_PROGRAM;CMAKE_PLATFORM_INFO_INITIALIZED;CMAKE_PROJECT_DESCRIPTION;CMAKE_PROJECT_HOMEPAGE_URL;CMAKE_PROJECT_NAME;CMAKE_ROOT;CMAKE_SKIP_INSTALL_RPATH;CMAKE_SKIP_RPATH;CMAKE_UNAME;CMAKE_VERBOSE_MAKEFILE;test_BINARY_DIR;test_IS_TOP_LEVEL;test_SOURCE_DIR
Terminated
$ stat build/CMakeCache.txt
stat: cannot stat 'build/CMakeCache.txt': No such file or directory

Note that replacing the pkill cmake command with message(FATAL_ERROR) does persist the cache. Hence, the cache usually resides in memory. You should not need to care about when exactly the cache is persisted.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86