如何在cmake中打印目标的所有属性?

如何在cmake中打印目标的所有属性?,cmake,Cmake,使用以下cmake scrpt: get_cmake_property(_variableNames VARIABLES) foreach (_variableName ${_variableNames}) message(STATUS "${_variableName}=${${_variableName}}") endforeach() 我们可以打印CMake项目中的所有变量。那么我的问题是:是否有一个函数可以打印目标的所有属性 由于明显缺乏动态查询目标属性的能力,我在以下工作中取

使用以下cmake scrpt:

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

我们可以打印CMake项目中的所有变量。那么我的问题是:是否有一个函数可以打印目标的所有属性

由于明显缺乏动态查询目标属性的能力,我在以下工作中取得了有限的成功

我调用cmake命令列出所有属性,然后在目标上尝试每个属性

# Get all propreties that cmake supports
execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)

# Convert command output into a CMake list
STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")

function(print_properties)
    message ("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}")
endfunction(print_properties)

function(print_target_properties tgt)
    if(NOT TARGET ${tgt})
      message("There is no target named '${tgt}'")
      return()
    endif()

    foreach (prop ${CMAKE_PROPERTY_LIST})
        string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" prop ${prop})
    # Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i
    if(prop STREQUAL "LOCATION" OR prop MATCHES "^LOCATION_" OR prop MATCHES "_LOCATION$")
        continue()
    endif()
        # message ("Checking ${prop}")
        get_property(propval TARGET ${tgt} PROPERTY ${prop} SET)
        if (propval)
            get_target_property(propval ${tgt} ${prop})
            message ("${tgt} ${prop} = ${propval}")
        endif()
    endforeach(prop)
endfunction(print_target_properties)
#获取cmake支持的所有属性
执行\u进程(命令cmake--help属性列表输出\u变量cmake\u属性\u列表)
#将命令输出转换为CMake列表
字符串(正则表达式替换“\ \ \”“CMAKE\U属性\U列表”${CMAKE\U属性\U列表}”)
字符串(正则表达式替换“\n”“;“CMAKE_属性_列表”${CMAKE_属性_列表}”)
函数(打印属性)
消息(“CMAKE_属性_列表=${CMAKE_属性_列表}”)
endfunction(打印属性)
功能(打印目标属性tgt)
if(不是目标${tgt})
消息(“没有名为“${tgt}”的目标)
返回()
endif()
foreach(prop${CMAKE_PROPERTY_LIST})
字符串(替换“${CMAKE\U BUILD\U TYPE}”prop${prop})
#修理https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i
如果(道具STREQUAL“位置”或道具匹配“^LOCATION”或道具匹配“\U LOCATION$”)
继续()
endif()
#消息(“正在检查${prop}”)
get_属性(propval目标${tgt}属性${prop}集)
if(propval)
获取对象属性(propval${tgt}${prop})
消息(${tgt}${prop}=${propval}”)
endif()
endforeach(道具)
endfunction(打印对象属性)

这并不是一个真正的竞争答案,只是对答案的扩展(并由编辑)

如果目标是
接口库
,这将打印出关于白名单属性的大量错误。不幸的是,似乎没有一种动态查询白名单属性的好方法——通过检查源代码中的
cmTargetPropertyComputer::WhiteListedInterfaceProperty
我能够找到一个regexp,但它可能会随着版本的变化而变化(我正在查看cmake-3.11的源代码)。无论如何,这是我的修订版,支持
接口库
目标-我还删除了重复项,并在循环之外对位置属性进行了一次过滤

(我建议对原始问题进行编辑,但这是在这里,以防它不被接受…)

谢谢,还有

#获取cmake支持的所有属性
执行\u进程(命令cmake--help属性列表输出\u变量cmake\u属性\u列表)
#将命令输出转换为CMake列表
字符串(正则表达式替换“\ \ \”“CMAKE\U属性\U列表”${CMAKE\U属性\U列表}”)
字符串(正则表达式替换“\n”“;“CMAKE_属性_列表”${CMAKE_属性_列表}”)
#修理https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i
列表(筛选器CMAKE_属性_列表排除正则表达式“^LOCATION$”^LOCATION|U LOCATION$”)
#出于某种原因,“类型”会出现两次——其他人也会出现?
列表(删除重复项CMAKE\U属性列表)
#通过从CMAKE_属性_列表中筛选来构建白名单,以防CMAKE
#另一个版本,我们的一个硬编码白名单属性
#根本不存在!
未设置(CMAKE\u白名单\u属性\u列表)
foreach(prop${CMAKE_PROPERTY_LIST})
如果(道具匹配“^(接口|[_a-z]|导入的(LIBNAME)|映射(u导入的)配置(u配置)| ^(兼容的|接口|编号|最大|编号|最小|字符串|导出|名称|导入的|全局| |配置| |(u LIBNAME))| |)|
列表(附加CMAKE_白名单_属性_列表${prop})
endif()
endforeach(道具)
函数(打印属性)
消息(“CMAKE_属性_列表=${CMAKE_属性_列表}”)
endfunction(打印属性)
功能(打印\白名单\属性)
消息(“CMAKE_白名单_属性_列表=${CMAKE_白名单_属性_列表}”)
endfunction(打印\白名单\属性)
功能(打印目标属性tgt)
if(不是目标${tgt})
消息(“没有名为“${tgt}”的目标)
返回()
endif()
获取\u目标\u属性(目标\u类型${tgt}类型)
if(目标类型STREQUAL“接口库”)
集合(属性列表${CMAKE\U白名单\U属性\U列表})
else()
集合(属性列表${CMAKE属性列表})
endif()
foreach(prop${prop_LIST})
字符串(替换“${CMAKE\U BUILD\U TYPE}”prop${prop})
#消息(“正在检查${prop}”)
get_属性(propval目标${tgt}属性${prop}集)
if(propval)
获取对象属性(propval${tgt}${prop})
消息(${tgt}${prop}=${propval}”)
endif()
endforeach(道具)
endfunction(打印对象属性)

改进了@AlwaysTraining和@PaulMolodowitch的答案。
解决中所述的
接口库
的问题

在功能上,我的实现最多与@PaulMolodowitch的实现相同。但它更简洁

## https://stackoverflow.com/questions/32183975/how-to-print-all-the-properties-of-a-target-in-cmake/56738858#56738858
## https://stackoverflow.com/a/56738858/3743145

## Get all properties that cmake supports
execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)
## Convert command output into a CMake list
STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")

list(REMOVE_DUPLICATES CMAKE_PROPERTY_LIST)

function(print_target_properties tgt)
    if(NOT TARGET ${tgt})
      message("There is no target named '${tgt}'")
      return()
    endif()

    foreach (prop ${CMAKE_PROPERTY_LIST})
        string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" prop ${prop})
        get_target_property(propval ${tgt} ${prop})
        if (propval)
            message ("${tgt} ${prop} = ${propval}")
        endif()
    endforeach(prop)
endfunction(print_target_properties)
##https://stackoverflow.com/questions/32183975/how-to-print-all-the-properties-of-a-target-in-cmake/56738858#56738858
## https://stackoverflow.com/a/56738858/3743145
##获取cmake支持的所有属性
执行\u进程(命令cmake--help属性列表输出\u变量cmake\u属性\u列表)
##将命令输出转换为CMake列表
字符串(正则表达式替换“\ \ \”“CMAKE\U属性\U列表”${CMAKE\U属性\U列表}”)
字符串(正则表达式替换“\n”“;“CMAKE_属性_列表”${CMAKE_属性_列表}”)
列表(删除重复项CMAKE\U属性列表)
功能(打印目标属性tgt)
if(不是目标${tgt})
消息(“没有名为“${tgt}”的目标)
返回()
endif()
foreach(prop${CMAKE_PROPERTY_LIST})
字符串(替换“${CMAKE\U BUILD\U TYPE}”prop${pro
## https://stackoverflow.com/questions/32183975/how-to-print-all-the-properties-of-a-target-in-cmake/56738858#56738858
## https://stackoverflow.com/a/56738858/3743145

## Get all properties that cmake supports
execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)
## Convert command output into a CMake list
STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")

list(REMOVE_DUPLICATES CMAKE_PROPERTY_LIST)

function(print_target_properties tgt)
    if(NOT TARGET ${tgt})
      message("There is no target named '${tgt}'")
      return()
    endif()

    foreach (prop ${CMAKE_PROPERTY_LIST})
        string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" prop ${prop})
        get_target_property(propval ${tgt} ${prop})
        if (propval)
            message ("${tgt} ${prop} = ${propval}")
        endif()
    endforeach(prop)
endfunction(print_target_properties)