2

I would like to have a command or option to list all the modified cache variables of the current build configuration. While cmake -L[AH] is nice, it is also quite overwhelming and doesn't show which are non-default values.

There seems to be a variable property MODIFIED that sounds exactly like what I'm looking for - but the documentation is not very reassuring:

Internal management property. Do not set or get. This is an internal cache entry property managed by CMake to track interactive user modification of entries. Ignore it.

This question also didn't help: CMAKE: Print out all accessible variables in a script

mattmilten
  • 6,242
  • 3
  • 35
  • 65
  • You can then only check what's changed inside the script itself (something similar to [this](https://stackoverflow.com/questions/42741501/how-to-know-library-variable-names-for-cmakelists/42752036#42752036)). – Florian Nov 23 '17 at 13:53
  • I mainly want to check the top level user-modified variables. For example `CMAKE_BUILD_TYPE`. If this is `Release` by default then I want to see an info message when the current built is configured with `Debug` - without seeing all the other 20 available variables that were not modified. – mattmilten Nov 23 '17 at 15:40
  • Why don't you just add if statements to run code based on the variables value? You know what the default value is, if the variable is not the default value it must've been set to something else by the user. Why do you need to check if it's been modified when you know the default value? Either it's default or it's modified... – Simon Hyll Nov 24 '17 at 05:50

1 Answers1

1

There are so many ways you could change or initialize variables in CMake (command line, environment variables, script files, etc.) that you won't be able to cover them all.

I just came up with the following script that covers the command line switches. Put the following file in your CMake project's root folder and you get the modified variables printed:

PreLoad.cmake

set(_file "${CMAKE_BINARY_DIR}/UserModifiedVars.txt")

get_directory_property(_vars CACHE_VARIABLES)
list(FIND _vars "CMAKE_BACKWARDS_COMPATIBILITY" _idx)

if (_idx EQUAL -1)
    list(REMOVE_ITEM _vars "CMAKE_COMMAND" "CMAKE_CPACK_COMMAND" "CMAKE_CTEST_COMMAND" "CMAKE_ROOT")
    file(WRITE "${_file}" "${_vars}")
else()
    file(READ "${_file}" _vars)
endif()

foreach(_var IN LISTS _vars)
    message(STATUS "User modified ${_var} = ${${_var}}")
endforeach()

This will load before anything else and therefore can relatively easily identify the user modified variables and store them into a file for later reference.

The CMAKE_BACKWARDS_COMPATIBILITY is a cached variable set by CMake at the end of a configuration run and therefor is used here to identify an already configured CMake project.

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149