简体   繁体   中英

Making all projects in CMake Visual Studio depend on one project

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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM