使 CMake Visual Studio 中的所有项目都依赖于一个项目 [英] Making all projects in CMake Visual Studio depend on one project

查看:29
本文介绍了使 CMake Visual Studio 中的所有项目都依赖于一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有大约 250 个项目,其中一个主要项目使用了大部分项目.当主项目运行时,所有项目都是最新的,这一点很重要.所以基本上,Visual Studio 应该在编译(和运行) MainProject 时检查所有 250 个项目的更改.我的 CMakeLists.txt 文件看起来像这样.

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)
....

显然这是一个非常简化的例子,但希望这个想法是存在的.基本上,为了让 Visual Studio 检查所有 250 个左右项目的依赖项,我必须将主项目中的所有其他项目添加为依赖项.现在这根本不是一个优雅的解决方案,因为 MainProject 中的 add_dependencies 有很多依赖项.它有效,但有没有更优雅的方法解决这个问题?

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?

推荐答案

将我的评论变成答案

目前(根据​​ CMake 3.5.x 版),据我所知,CMake 本身没有提供全局目标列表(例如作为全局属性).

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).

现在已实现:全局 BUILDSYSTEM_TARGETS 属性随 CMake 3.7 一起发布

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

可能的解决方案

  1. 在您的情况下 - 假设您不想修改所有 250 个子项目的 CMakeLists.txt 文件 - 覆盖 add_executable(), add_library()add_custom_target() 可以解决问题:

  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})

果然,如果您从头开始执行此操作,我会 - 正如@Lindydancer 所建议的那样 - 使用您自己的这些命令版本以允许全局自定义.

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.

如果你遵循 MainProject 总是第一的先决条件,你可以稍微简化一下:

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)

参考资料

这篇关于使 CMake Visual Studio 中的所有项目都依赖于一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆