Search code examples
cmakecmake-languagecmakelists-options

CMake get a cache variables list from inside a CMakeLists.txt script


I'd like to know if there's a way to get a list of cached variables from inside a CMakeLists.txt script. I mean, having something like this:

cmake -S . -B build -DMY_VAR1=hello -DMY_VAR2=there 

and, within my CMakeLists.txt

option(MY_VAR3 "a cache setting" ON)

is there a way to get a list var MY_CACHE_LIST containing MY_VAR1, MY_VAR2 and MY_VAR3 and all other cache variables from inside the CMakeLists.txt script itself?

Thank you


Solution

  • List of cache variables is contained in the CACHE_VARIABLES property for a directory. It could be extracted as follows:

    get_directory_property(cache_vars CACHE_VARIABLES)
    message(STATUS "List of CACHE variables: ${cache_vars}")
    

    Aside from printing the list of these variables, by using name of each variable it is possible to extract its value and extract other properties listed under Properties on Cache Entries.