且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使所有项目在CMake Visual Studio依赖于一个项目

更新时间:2023-02-03 21:32:14

将我的评论变成答案



目前(根据​​CMake版本3.5.x),据我所知,没有CMake提供的全局目标列表全球财产)。



修改:现在已实现: BUILDSYSTEM_TARGETS 属性已随CMake 3.7发布



Posible解决方案


  1. 在你的情况下, t想要修改所有250个子项目的 CMakeLists.txt 文件 - 覆盖 add_executable() add_library() add_custom_target()会做到:

      cmake_minimum_required(VERSION 2.8)

    项目(DependsAllTest)

    宏(add_library _target)
    _add_library($ {_ target} $ {ARGN }
    set_property(GLOBAL APPEND PROPERTY GlobalTargetList $ {_ target})
    endmacro()

    宏(add_executable _target)
    _add_executable($ {_ target} $ {ARGN })
    set_property(GLOBAL APPEND PROPERTY GlobalTargetList $ {_ target})
    endmacro()

    宏(add_custom_target _target)
    _add_custom_target($ {_ target} $ {ARGN }
    set_property(GLOBAL APPEND PROPERTY GlobalTargetList $ {_ target})
    endmacro()

    add_subdirectory(MainProject)
    add_subdirectory(ProjectA)
    add_subdirectory ProjectB)
    add_subdirectory(ProjectC)
    add_subdirectory(ProjectD)

    get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
    message(STATUSGlobalTargetList:$ {_ allTargets} b $ b add_dependencies(MainProject $ {_ allTargets})

    果然,


  2. 如果你遵循的先决条件是 MainProject 总是第一个,你可以简化这一点:

      cmake_minimum_required(VERSION 2.8) 

    项目(DependsAllTest2)

    宏(add_library _target)
    _add_library($ {_ target} $ {ARGN})
    add_dependencies(MainProject $ {_ target }
    endmacro()

    宏(add_executable _target)
    _add_executable($ {_ target} $ {ARGN})
    add_dependencies(MainProject $ {_ target})
    endmacro()

    宏(add_custom_target _target)
    _add_custom_target($ {_ target} $ {ARGN})
    add_dependencies(MainProject $ {_ target})
    endmacro()

    add_subdirectory(MainProject)
    add_subdirectory(ProjectA)
    add_subdirectory(ProjectB)
    add_subdirectory(ProjectC)
    add_subdirectory b


参考


In my project, I have about 250 projects with one main project that uses most of the projects. It's important that all projects are up to date when the main project is run. So basically, Visual Studio should check for all 250 projects for changes when MainProject is compiled (and run). My CMakeLists.txt files look like this.

Root/CMakeLists.txt

....
add_subdirectory (MainProject)
add_subdirectory (ProjectA)
add_subdirectory (ProjectB)
add_subdirectory (ProjectC)
add_subdirectory (ProjectD)
....

Root/MainProject/CMakeLists.txt

....
add_executable (MainProject a.cpp b.cpp)
add_dependencies (MainProject ProjectA ProjectB ...)
....

Root/ProjectA/CMakeLists.txt

....
add_executable (ProjectA a.cpp b.cpp)
....

Obviously this is a very simplified example, but hopefully the idea is there. Basically, in order to make Visual Studio to check for dependencies for all 250 projects or so, I have to add all the other projects in the main project as dependencies. Now this is not an elegant solution at all, as add_dependencies in MainProject has a LOT of dependencies in it. It works, but is there anything more elegant for this problem?

Turning my comments into an answer

At the moment (as per CMake version 3.5.x), there is - as far as I know - no global target list provided by CMake itself (e.g. as a global property).

Edit: It is now implemented: Global BUILDSYSTEM_TARGETS property was released with CMake 3.7

Posible Solutions

  1. In your case - going by the assumption that you don't want to modify all 250 sub-project's CMakeLists.txt files - overwriting add_executable(), add_library() and add_custom_target() would do the trick:

    cmake_minimum_required(VERSION 2.8)
    
    project(DependsAllTest)
    
    macro(add_library _target)
        _add_library(${_target} ${ARGN})
        set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
    endmacro()
    
    macro(add_executable _target)
        _add_executable(${_target} ${ARGN})
        set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
    endmacro()
    
    macro(add_custom_target _target)
        _add_custom_target(${_target} ${ARGN})
        set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
    endmacro()
    
    add_subdirectory(MainProject)
    add_subdirectory(ProjectA)
    add_subdirectory(ProjectB)
    add_subdirectory(ProjectC)
    add_subdirectory(ProjectD)
    
    get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
    message(STATUS "GlobalTargetList: ${_allTargets}")
    add_dependencies(MainProject ${_allTargets})
    

    Sure enough, if you would do this from scratch I would - as @Lindydancer has suggested - use your own versions of those commands to allow global customizations.

  2. If you go by the prerequisite that MainProject is always first, you could simplify this a little:

    cmake_minimum_required(VERSION 2.8)
    
    project(DependsAllTest2)
    
    macro(add_library _target)
        _add_library(${_target} ${ARGN})
        add_dependencies(MainProject ${_target})
    endmacro()
    
    macro(add_executable _target)
        _add_executable(${_target} ${ARGN})
        add_dependencies(MainProject ${_target})
    endmacro()
    
    macro(add_custom_target _target)
        _add_custom_target(${_target} ${ARGN})
        add_dependencies(MainProject ${_target})
    endmacro()
    
    add_subdirectory(MainProject)
    add_subdirectory(ProjectA)
    add_subdirectory(ProjectB)
    add_subdirectory(ProjectC)
    add_subdirectory(ProjectD)
    

References