gpt4 book ai didi

c++ - 如何以编程方式迭代所有 CMake 目标?

转载 作者:IT老高 更新时间:2023-10-28 23:00:56 37 4
gpt4 key购买 nike

有没有办法从顶层 CMakeLists.txt 中获取 CMake 项目的所有目标,即以编程方式迭代目标?

我想这样做的原因是对每个目标应用一些 XCode 特定的设置。 .

if (CMAKE_GENERATOR MATCHES "Xcode")
include(sanitize_xcode)
sanitize_xcode(myTarget)
endif()

仅供引用 - 清理模块看起来像这样。 .

macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
endmacro (set_xcode_property)

macro (sanitize_xcode TARGET)
set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=Debug] "YES")
set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=MinSizeRel] "NO")
set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=RelWithDebInfo] "YES")
set_xcode_property(${TARGET} GCC_GENERATE_DEBUGGING_SYMBOLS[variant=Release] "NO")

set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=Debug] "NO")
set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=MinSizeRel] "YES")
set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=RelWithDebInfo] "NO")
set_xcode_property(${TARGET} COPY_PHASE_STRIP[variant=Release] "YES")

set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=Debug] "0")
set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=MinSizeRel] "s")
set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=RelWithDebInfo] "3")
set_xcode_property(${TARGET} GCC_OPTIMIZATION_LEVEL[variant=Release] "3")

set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=Debug] "7.0")
set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=MinSizeRel] "7.0")
set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=RelWithDebInfo] "7.0")
set_xcode_property(${TARGET} IPHONEOS_DEPLOYMENT_TARGET[variant=Release] "7.0")
endmacro (sanitize_xcode)

最佳答案

更正弗洛里安的answer , BUILDSYSTEM_TARGETS不是真正的全局属性,而是 directory范围一。增强请求当前是 open请求真正的全局属性(property)。使用 SUBDIRECTORIES属性可以递归检索当前目录范围内的所有目标,函数如下:

function(get_all_targets var)
set(targets)
get_all_targets_recursive(targets ${CMAKE_CURRENT_SOURCE_DIR})
set(${var} ${targets} PARENT_SCOPE)
endfunction()

macro(get_all_targets_recursive targets dir)
get_property(subdirectories DIRECTORY ${dir} PROPERTY SUBDIRECTORIES)
foreach(subdir ${subdirectories})
get_all_targets_recursive(${targets} ${subdir})
endforeach()

get_property(current_targets DIRECTORY ${dir} PROPERTY BUILDSYSTEM_TARGETS)
list(APPEND ${targets} ${current_targets})
endmacro()

get_all_targets(all_targets)
message("All targets: ${all_targets}")

关于c++ - 如何以编程方式迭代所有 CMake 目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434946/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com