CMake: How do I change properties on subdirectory project targets?

血红的双手。 提交于 2019-11-28 09:29:18

I've given your example a try and here are my two variants:

  1. Using the BUILDSYSTEM_TARGETS and SUBDIRECTORIES directory properties to evaluate a list of target names in the directory that "does not include any Imported Targets or Alias Targets":

    cmake_minimum_required(VERSION 3.7)
    
    project(AliasFolderSub)
    
    set_property(GLOBAL PROPERTY USE_FOLDERS TRUE)
    
    function(get_all_targets _result _dir)
        get_property(_subdirs DIRECTORY "${_dir}" PROPERTY SUBDIRECTORIES)
        foreach(_subdir IN LISTS _subdirs)
            get_all_targets(${_result} "${_subdir}")
        endforeach()
        get_property(_sub_targets DIRECTORY "${_dir}" PROPERTY BUILDSYSTEM_TARGETS)
        set(${_result} ${${_result}} ${_sub_targets} PARENT_SCOPE)
    endfunction()
    
    function(add_subdirectory_with_folder _folder_name _folder)
        add_subdirectory(${_folder} ${ARGN})
        get_all_targets(_targets "${_folder}")
        foreach(_target IN LISTS _targets)
            set_target_properties(
                ${_target}
                PROPERTIES FOLDER "${_folder_name}"
            )
        endforeach()
    endfunction()
    
    # External Libs
    add_subdirectory_with_folder("Poco" libs/poco)
    
  2. By transforming the FOLDER target property into something that is inherited from a directory property of the same name. This can be done using define_property() to redefine the FOLDER property as INHERITED:

    With the INHERITED option the get_property() command will chain up to the next higher scope when the requested property is not set in the scope given to the command. DIRECTORY scope chains to GLOBAL. TARGET, SOURCE, and TEST chain to DIRECTORY.

    cmake_minimum_required(VERSION 2.6)
    
    project(AliasFolderSub)
    
    set_property(GLOBAL PROPERTY USE_FOLDERS TRUE)
    define_property(
        TARGET
        PROPERTY FOLDER
        INHERITED
        BRIEF_DOCS "Set the folder name."
        FULL_DOCS  "Use to organize targets in an IDE."
    )
    
    function(add_subdirectory_with_folder _folder_name _folder)
        add_subdirectory(${_folder} ${ARGN})
        set_property(DIRECTORY "${_folder}" PROPERTY FOLDER "${_folder_name}")
    endfunction()
    
    # External Libs
    add_subdirectory_with_folder("Poco" libs/poco)
    

    𝓝𝓸𝓽𝓮: Using define_property() to redefine an existing property's scope is an undocumented behavior of CMake.

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!