251

I'm wondering if there is a way to print out all accessible variables in CMake. I'm not interested in the CMake variables - as in the --help-variables option. I'm talking about my variables that I defined, or the variables defined by included scripts.

I'm currently including:

INCLUDE (${CMAKE_ROOT}/Modules/CMakeBackwardCompatibilityCXX.cmake)

And I was hoping that I could just print out all the variables that are here, instead of having to go through all the files and read what was available - I may find some variables I didn't know about that may be useful. It would be good to aid learning & discovery. It is strictly for debugging/development.

This is similar to the question in Print all local variables accessible to the current scope in Lua, but for CMake!

Has anyone done this?

squareskittles
  • 11,997
  • 7
  • 31
  • 48
Michael
  • 3,094
  • 2
  • 16
  • 12

7 Answers7

434

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")
squareskittles
  • 11,997
  • 7
  • 31
  • 48
sakra
  • 53,539
  • 13
  • 152
  • 136
  • That's a very useful snippet. Why doesn't it print out **ENV** variables which I set on command line? – IgorGanapolsky Nov 30 '16 at 21:47
  • 2
    @Geremia you can copy this code block to file **myfile.txt** and run : **cmake -P myfile.txt** – Idok Feb 03 '17 at 23:18
  • 2
    `VARIABLES` lists only "variables defined in the current directory." https://cmake.org/cmake/help/latest/prop_dir/VARIABLES.html#prop_dir:VARIABLES – CivFan Oct 04 '17 at 21:45
  • 1
    I had to remove the `STATUS` from the `message` command for the output to be visible. – luator Feb 01 '18 at 12:28
  • Didn't work for me. all I got was variables starting with CMAKE_* – C Johnson Oct 26 '18 at 21:40
  • The last suggestion: "execute_process()" is not helpful when using CMAKE under eclipse on MAC for these reasons (1) Eclipse manages the command line to CMAKE, and (2) MACOS strips and resets ENV variables when you launch an "installed app" like eclipse that is installed for example in /Applications/Eclipse.app – user3696153 Dec 17 '19 at 04:48
  • Note that in newer versions of cmake, `VARIABLES` is a `DIRECTORY` property rather than a global property: so `get_cmake_property` should be replaced with `get_directory_property`. – Seth Johnson May 16 '20 at 17:38
185

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.

Community
  • 1
  • 1
jtsagata
  • 2,085
  • 1
  • 12
  • 7
9

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

  • 3
    Useful tool :) However, the questions asks to print out all variables.. but this will only reveal cached variables. – OLL Jul 27 '17 at 16:42
1

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.

Willem Hengeveld
  • 2,569
  • 19
  • 19
0

based on @sakra

function(dump_cmake_variables)
    get_cmake_property(_variableNames VARIABLES)
    list (SORT _variableNames)
    foreach (_variableName ${_variableNames})
        if (ARGV0)
            unset(MATCHED)

            #case sensitive match
            # string(REGEX MATCH ${ARGV0} MATCHED ${_variableName})
            #
            #case insenstitive match
            string( TOLOWER "${ARGV0}" ARGV0_lower )
            string( TOLOWER "${_variableName}" _variableName_lower )
            string(REGEX MATCH ${ARGV0_lower} MATCHED ${_variableName_lower})

            if (NOT MATCHED)
                continue()
            endif()
        endif()
        message(STATUS "${_variableName}=${${_variableName}}")
    endforeach()
endfunction()

dump_cmake_variables("^Boost")

variable names are case sensitive

btw if you are interested in boost, it is Boost_INCLUDE_DIRS not BOOST_INCLUDE_DIRS, and it is Boost_LIBRARIES not BOOST_LIBRARIES, and by mistake I had BOOST_LIBRARIES instead of Boost_LIBRARIES, https://cmake.org/cmake/help/v3.0/module/FindBoost.html , better example for boost:

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS RANDOM)
include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(myfile PRIVATE
 ${Boost_LIBRARIES}
)
Shimon Doodkin
  • 3,676
  • 29
  • 33
0

You can use message :

message([STATUS] "SUB_SOURCES : ${SUB_SOURCES}")
Furlings
  • 27
  • 8
0

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)
bukzor
  • 34,859
  • 10
  • 67
  • 104