Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake Nested Projects and Targets

i have a problem on building specific Visual Studio Projects from a CMake source tree. Immagine we have multiple targets in various subdirectories. In one sub-directory we initiate a new project (and solution). The projects in this project targets should be part of ALL_BUILD, but not in the parent ALL_BUILD.

In short:

project(Main)

add_executable(MainApplication ...)
target_link_libraries(MainApplication PRIVATE Library_A)

add_subdirectory(Library_A)
   add_library(Library_A STATIC ...)

add_subdirectory(Other_Related_Stuff_Using_MainApplication)
   project(OtherRelatedStuff)
   add_custom_target(OtherTarget ... DEPENDS MainApplication)

Now with a Visual Studio generator we get two Solution Files. That is right and correct. In both solutions we have these three projects (as OtherReleatedStuff depends on MainApplication beeing built and OtherRelatedStuff is part of the Main-Project).

Now: ALL_BUILD builds all projects.

When i set

set_target_properties(OtherTarget PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)

...in both solutions OtherTarget is disabled.

What i would like to do is:

Solution 1: In Main.Sln there should be not OtherTarget. In OtherRelatedStuff.sln there could be MainApplication, but should not build. Maybe there shouldn't be a MainApplication as well.

Solution 2: In Main.Sln, OtherTarget should never build (excluded from build). In OtherRelatedStuff.sln, OtherTarget should build, but the dependencies don't or should be even visible.

Is there a solution for it?

like image 328
roalter Avatar asked Nov 04 '15 12:11

roalter


1 Answers

All targets found in one CMakeLists.txt source tree will generate a Visual Studio project file. And - as you found out already - EXCLUDE_FROM_ALL and EXCLUDE_FROM_DEFAULT_BUILD are set per target/project or directory.

So you could split/link your projects with the use of ExternalProject_Add(). And since Other_Related_Stuff depends on MainApplication you should call ExternalProject_Add() in Other_Related_Stuff's CMake file.

So my recommendation would look like this:

CMakeLists.txt

project(Main)

add_subdirectory(Library_A)

add_executable(MainApplication ...)
target_link_libraries(MainApplication PRIVATE Library_A)

Other_Related_Stuff/CMakeLists.txt

project(OtherRelatedStuff)

ExternalProject_Add(
    MainApplication 
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/.. 
    PREFIX MainApplication
)   
add_custom_target(OtherTarget ... DEPENDS MainApplication)

So now your main CMakeLists.txt is Other_Related_Stuff/CMakeLists.txt and it does generate your second solution file in a MainApplication subdirectory.

Reference

  • CMake share library with multiple executables
like image 184
Florian Avatar answered Sep 19 '22 15:09

Florian