Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Print out all accessible variables in a script

Tags:

cmake

People also ask

How do I print a variable value in CMake?

Run CMake and have a look at the cache with the ccmake GUI tool. Then you'll get all the variables. Or run CMake with -LH then you will get all variables printed after configuration.

How can I see CMake variables?

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.

What is CMake Strequal?

According to the CMake documentation, the STREQUAL comparison is allowed to take either a VARIABLE or a STRING as either parameter. So, in this example below, the message does NOT print, which is broken: set( FUBARTEST "OK" ) if( FUBARTEST STREQUAL "OK" ) message( "It Worked" ) endif()

How do you clean CMake?

Simply delete the CMakeFiles/ directory inside your build directory. rm -rf CMakeFiles/ cmake --build . This causes CMake to rerun, and build system files are regenerated.


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 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.


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 ..


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.


None of the current answers allowed me to see the variables in my project subdirectory. Here's a solution:

function(print_directory_variables dir)
    # Dump variables:
    get_property(_variableNames DIRECTORY ${dir} PROPERTY VARIABLES)
    list (SORT _variableNames)
    foreach (_variableName ${_variableNames})
        get_directory_property(_variableValue DIRECTORY ${dir} DEFINITION ${_variableName})
        message(STATUS "DIR ${dir}: ${_variableName}=${_variableValue}")
    endforeach()
endfunction(print_directory_variables)

# for example
print_directory_variables(.)
print_directory_variables(ui/qt)