# Hot to make add\_subdirectory() prevent install but still build as part of ALL

**URL:** <https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698>\
**Category:** Code\
**Created:** [August 8, 2023, 3:43pm UTC](https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698 "2023-08-08T15:43:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignus2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ignus2/32/3075_2.png) [@ignus2](https://discourse.cmake.org/u/ignus2)\
**Post date:** [August 8, 2023, 3:43pm UTC](https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698/1 "2023-08-08T15:43:15Z")

</div>

The “common wisdom” to prevent add\_subdirectory() from running install() rules from inside it is to use `add_subdirectory(dir EXCLUDE_FROM_ALL)`. This works fine, except it also removes all targets from building as part of ALL (naturally), except when they are explicitly referenced.

We have sub-projects, added with add\_subdirectory() and want to prevent install() rules from running in them, but we have to build the targets explicitly, because some are executables and modules (so not referenced anywhere), and install them in a custom way in the parent project. The trick we use is to create a dummy custom target part of ALL and make it depend on the subdir targets:

```auto
add_subdirectory(subproj EXCLUDE_FROM_ALL) # targets: subproj.target1, subproj.target2, etc.
add_custom_target(do_build_subproj ALL)
add_dependencies(do_build_subproj subproj.target1 subproj.target2)

```

Needless to say this is not ideal and just smells bad.  
Is there any easier way to make this happen?  
If not, would it make sense to make a feature request to support this use-case?

EDIT:  
Well one other way to do it would be to set the EXCLUDE\_FROM\_ALL target property to false for the subproj targets, like:

```auto
set_target_properties(subproj.target1 subproj.target2 PROPERTIES EXCLUDE_FROM_ALL 0)

```

but it’s still tedious to list the targets individually.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 8, 2023, 8:33pm UTC](https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698/2 "2023-08-08T20:33:12Z")

</div>

I don’t see a way to do this right now. I think components would be the easiest way right now, but some way to control `install(EXCLUDE_FROM_ALL)` from a scoped variable would probably be sufficient too.

---

<div class="post-metadata">

**Author:** ![ignus2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ignus2/32/3075_2.png) [@ignus2](https://discourse.cmake.org/u/ignus2)\
**Post date:** [August 8, 2023, 9:06pm UTC](https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698/3 "2023-08-08T21:06:30Z")

</div>

Thanks for the hint.  
BTW, I haven’t really seen it documented explicitly anywhere that `add_subdirectory(... EXCLUDE_FROM_ALL)` prevents install rules from running from the added dir.  
Or is it simply because of the side-effect that running install rules on excluded targets is undefined behavior, and hence they’re disabled?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [August 9, 2023, 11:28am UTC](https://discourse.cmake.org/t/hot-to-make-add-subdirectory-prevent-install-but-still-build-as-part-of-all/8698/4 "2023-08-09T11:28:51Z")

</div>

I’m not sure of the history there, sorry.
