1

In our project, some modules are included only in specific configurations. Currently, we achieve this using an approach like this:

if (CMAKE_BUILD_TYPE STREQUAL "my_config")
    add_subdirectory(only_in_my_config)
endif()

However, the problem with this approach is that it doesn't play nicely with multi-config generators (it seems currently those are only Visual Studio and Xcode).

To properly support multi-config generators, AFAIK, we should use $<CONFIG:my_config> generator expression. However, those cannot be used with add_subdirectory.

How to work around this?

I guess we must add all subdirectories always. But then how to prevent their targets from being build in configurations other than my_config?

Adam Badura
  • 5,069
  • 1
  • 35
  • 70
  • 3
    Yes, you have to always call `add_subdirectory()`. I think what you are looking for is the [`EXCLUDE_FROM_DEFAULT_BUILD_`](https://cmake.org/cmake/help/latest/prop_tgt/EXCLUDE_FROM_DEFAULT_BUILD_CONFIG.html) target property. – Florian May 24 '18 at 10:36

1 Answers1

3

Turning my comment into an answer

Since CMake needs to go through all your CMakeLists.txt files during configuration step the answer is yes, you have to always call add_subdirectory().

I think what you are looking for is the EXCLUDE_FROM_DEFAULT_BUILD_<CONFIG> target property.

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Please, refer to my other question https://stackoverflow.com/questions/50505695/how-to-extract-parts-of-complex-configuration-with-config-generator-expression-i for a more complicated configuration setup. In such setup how to exclude targets for some sets of configurations? Like for all `A_*` configurations for example? Is there any approach for "hierarchy" of those settings or do you have to manually update those values for all `A_*` configurations? – Adam Badura May 25 '18 at 05:11
  • Another question is - can it somehow be done on directory level? So that I would not have to do it for all targets one by one, but instead do it once for all targets in a directory (including subdirectories)? – Adam Badura May 25 '18 at 05:14
  • 1
    @AdamBadura No, no wildcards for properties. But I answered your other question with a code example. Hope this helps. For the second part see [CMake: How do I change properties on subdirectory project targets?](https://stackoverflow.com/questions/45092198/cmake-how-do-i-change-properties-on-subdirectory-project-targets). Use the [`define_property()`](https://cmake.org/cmake/help/latest/command/define_property.html) command to extend the target properties scope. – Florian May 25 '18 at 20:34