CMake: Print out all accessible variables in a script

Another way is to simply use:

cmake -LAH

From the manpage:

-L[A][H]

List non-advanced cached variables.

List cache variables will run CMake and list all the variables from the CMake cache that are not marked as INTERNAL or ADVANCED. This will effectively display current CMake settings [...].

If A is specified, then it will display also advanced variables.

If H is specified, it will also display help for each variable.


Using the get_cmake_property function, the following loop will print out all CMake variables defined and their values:

get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

This can also be embedded in a convenience function which can optionally use a regular expression to print only a subset of variables with matching names

function(dump_cmake_variables)
    get_cmake_property(_variableNames VARIABLES)
    list (SORT _variableNames)
    foreach (_variableName ${_variableNames})
        if (ARGV0)
            unset(MATCHED)
            string(REGEX MATCH ${ARGV0} MATCHED ${_variableName})
            if (NOT MATCHED)
                continue()
            endif()
        endif()
        message(STATUS "${_variableName}=${${_variableName}}")
    endforeach()
endfunction()

To print environment variables, use CMake's command mode:

execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "environment")

Another way to view all cmake's internal variables, is by executing cmake with the --trace-expand option.

This will give you a trace of all .cmake files executed and variables set on each line.


ccmake is a good interactive option to interactively inspect cached variables (option( or set( CACHE:

sudo apt-get install cmake-curses-gui
mkdir build
cd build
cmake ..
ccmake ..

Tags:

Cmake