• CMake 将所有 Target 的工程在 Visual Studio 中加到同一个文件夹


    我想要将所有 Target 的工程在 Visual Studio 中加到同一个文件夹

    一开始我想要重写 add_library 函数,来获取到那些 lib 添加了,进而获得所有的 library,存到一个 list 里面,最后对这个 list 遍历,设置 folder

    # 3RD_PARTY_LIB_LIST stores all lib targets that have folder properity
    set(3RD_PARTY_LIB_LIST "" CACHE INTERNAL "3RD_PARTY_LIB_LIST")
    
    function(add_library name)
        _add_library(${name} ${ARGN}) 
        # if not Meow Editor, it is 3rd party library
        if(NOT name STREQUAL ${RUNTIME_NAME})
            if(TARGET ${name})
                # alias target can not call set_target_properties
                # interface library doesn't have folder property
                # so exclude them
                get_property(ALIAS TARGET "${name}" PROPERTY ALIASED_TARGET)
                if("${ALIAS}" STREQUAL "")
                    get_target_property(3RD_PARTY_LIB_TYPE ${name} TYPE)
                    if(NOT ("${3RD_PARTY_LIB_TYPE}" STREQUAL "INTERFACE_LIBRARY"))
                        message(STATUS "Find 3rd lib: ${name}")
                        set(3RD_PARTY_LIB_LIST ${3RD_PARTY_LIB_LIST} ${name} CACHE INTERNAL "3RD_PARTY_LIB_LIST")
                    endif()
                endif()
            endif()
        endif()
    endfunction()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    # Set all 3rd party lib to one folder
    foreach(3RD_PARTY_LIB ${3RD_PARTY_LIB_LIST})
        # I don't know why one name has passed if(TARGET ${name}) check
        # but still may not find target now in this line
        # so give this dirty check
        if(TARGET ${3RD_PARTY_LIB})
            get_property(OLD_FOLDER TARGET "${3RD_PARTY_LIB}" PROPERTY FOLDER)
            set_target_properties(${3RD_PARTY_LIB} PROPERTIES FOLDER "3rdparty/${OLD_FOLDER}")
        endif()
    endforeach()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    之后我发现还有 utility 类型的工程,我就觉得不能单单看 library 了,应该是要有一个方法找到所有 target,然后从中排除掉不需要的 target,将剩下的所有 target 的 folder 属性更改

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    # Set all 3rd party project to one folder
    get_all_targets(ALL_TAR_LIST)
    foreach(TAR ${ALL_TAR_LIST})
        if("${TAR}" STREQUAL "${RUNTIME_NAME}" OR "${TAR}" STREQUAL "${EDITOR_NAME}")
            continue()
        endif()
    
        # alias target can not call set_target_properties
        # interface library doesn't have folder property
        # so exclude them
        get_property(ALIAS TARGET "${TAR}" PROPERTY ALIASED_TARGET)
        if(NOT ("${ALIAS}" STREQUAL ""))
            continue()
        endif()
        get_target_property(3RD_PARTY_LIB_TYPE ${TAR} TYPE)
        if("${3RD_PARTY_LIB_TYPE}" STREQUAL "INTERFACE_LIBRARY")
            continue()
        endif()
    
        message(STATUS "Found target: ${TAR}")
        get_property(OLD_FOLDER TARGET "${TAR}" PROPERTY FOLDER)
        set_target_properties(${TAR} PROPERTIES FOLDER "3rdparty/${OLD_FOLDER}")
    endforeach()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    面向对象重写理解 求值策略 -共享对象调用 面向对象原则
    速腾激光雷达 RS M1 slam 建图
    虹科 | 解决方案 | 汽车示波器 索赔管理方案
    【开发心得】Java ftp开发注意事项
    【数据结构】哈希表
    [网鼎杯 2018]Comment git泄露 / 恢复 二次注入 .DS_Store bash_history文件查看
    1、Elasticsearch 8.X 概述与安装
    分布式天花板?阿里百万架构师的ZK+Dubbo笔记,颠覆认知
    记一次 .NET 某电力系统 内存暴涨分析
    若依+lodop+jasperreports+ireport 设计打印票据格式(二)
  • 原文地址:https://blog.csdn.net/hijackedbycsdn/article/details/132796429