CMake:打印出脚本中所有可访问的变量 [英] CMake: Print out all accessible variables in a script

查看:36
本文介绍了CMake:打印出脚本中所有可访问的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法打印出 CMake 中所有可访问的变量.我对 CMake 变量不感兴趣——就像在 --help-variables 选项中一样.我说的是我定义的变量,或者包含脚本定义的变量.

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.

我目前包括:

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.

这类似于打印 Lua 中当前作用域可访问的所有局部变量,但对于 CMake!

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

有人这样做过吗?

推荐答案

使用 get_cmake_property 函数,以下循环将打印出所有定义的 CMake 变量及其值:

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()

要打印环境变量,请使用 CMake 的 命令模式:

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

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

这篇关于CMake:打印出脚本中所有可访问的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆