获取具有指定前缀的变量列表 [英] Get a list of variables with a specified prefix

查看:213
本文介绍了获取具有指定前缀的变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取具有指定前缀的用户定义变量的列表?例如:

  set(vars_MyVar1 something)
set(vars_MyVar2 something)
getListOfVarsStartingWith(vars_)

解决方案>

函数 getListOfVarsStartingWith 可以用以下方式编写:

  function(getListOfVarsStartingWith _prefix _varResult)
get_cmake_property(_vars VARIABLES)
string(REGEX MATCHALL(^ |;)$ {_ prefix} [A-Za-z0-9 _] *_matchedVars$ { _vars})
set($ {_ varResult} $ {_ matchedVars} PARENT_SCOPE)
endfunction()


b $ b

函数使用CMake函数 string(REGEX MATCHALL )来计算所有匹配的变量名,而不使用循环。这里是一个用法示例:

  set(vars_MyVar1 something)
set(vars_MyVar2 something)
getListOfVarsStartingWith(vars_matchedVars)
foreach _var IN LISTS matchedVars)
消息($ {_ var} = $ {$ {_ var}})
endforeach()

如果搜索只返回缓存变量,请使用以下函数:

 函数(getListOfVarsStartingWith _prefix _varResult)
get_cmake_property(_vars CACHE_VARIABLES)
string(REGEX MATCHALL(^ |;)$ {_ prefix} [A-Za-z0-9 _] *_matchedVars$ {_ vars}
set(_resultVars)
foreach(_variable $ {_ matchedVars})
get_property(_type CACHE$ {_ variable}PROPERTY TYPE)
if {_type}STREQUALSTATIC)
list(APPEND _resultVars$ {_ variable})
endif()
endforeach()
set($ {_ varResult} _resultVars} PARENT_SCOPE)
endfunction()

此函数查询 CACHE_VARIABLES 属性,并确保类型 STATIC 的缓存变量,由CMake内部使用,不会返回。


Is there a way to get a list of user-defined variables that have a specified prefix? For example:

set(vars_MyVar1 something)
set(vars_MyVar2 something)
getListOfVarsStartingWith(vars_)

?

解决方案

The function getListOfVarsStartingWith can be written in the following way:

function (getListOfVarsStartingWith _prefix _varResult)
    get_cmake_property(_vars VARIABLES)
    string (REGEX MATCHALL "(^|;)${_prefix}[A-Za-z0-9_]*" _matchedVars "${_vars}")
    set (${_varResult} ${_matchedVars} PARENT_SCOPE)
endfunction()

The functions uses the CMake function string(REGEX MATCHALL to compute all matched variable names without a loop. Here is a usage example:

set(vars_MyVar1 something)
set(vars_MyVar2 something)
getListOfVarsStartingWith("vars_" matchedVars)
foreach (_var IN LISTS matchedVars)
    message("${_var}=${${_var}}")
endforeach()

If the search should only return cache variables, use the following function:

function (getListOfVarsStartingWith _prefix _varResult)
    get_cmake_property(_vars CACHE_VARIABLES)
    string (REGEX MATCHALL "(^|;)${_prefix}[A-Za-z0-9_]*" _matchedVars "${_vars}")
    set (_resultVars "")
    foreach (_variable ${_matchedVars})
        get_property(_type CACHE "${_variable}" PROPERTY TYPE)
        if (NOT "${_type}" STREQUAL "STATIC") 
            list (APPEND _resultVars "${_variable}")
        endif()
    endforeach()
    set (${_varResult} ${_resultVars} PARENT_SCOPE)
endfunction()

This function queries the CACHE_VARIABLES property and also makes sure that cache variables of type STATIC, which are used by CMake internally, are not returned.

这篇关于获取具有指定前缀的变量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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