使所有项目在CMake Visual Studio依赖于一个项目 [英] Making all projects in CMake Visual Studio depend on one project
问题描述
在我的项目中,我有大约250个项目,一个主要项目使用大多数项目。重要的是,当主项目运行时,所有项目都是最新的。所以基本上,Visual Studio应该检查所有250个项目的变化,当MainProject编译(和运行)。我的CMakeLists.txt文件如下所示。
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有很多依赖关系。
将我的评论变成答案
目前(根据CMake版本3.5.x),据我所知,没有CMake提供的全局目标列表全球财产)。
修改:现在已实现: BUILDSYSTEM_TARGETS
属性已随CMake 3.7发布
Posible解决方案
-
在你的情况下, 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})
果然,
-
如果你遵循的先决条件是
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
In your case - going by the assumption that you don't want to modify all 250 sub-project's
CMakeLists.txt
files - overwritingadd_executable()
,add_library()
andadd_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.
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
这篇关于使所有项目在CMake Visual Studio依赖于一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!