# Adding a SUBDIRECTORIES\_RECURSE directory property...

**URL:** https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036
**Category:** Development
**Created:** [May 2, 2023, 5:03pm UTC](https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036 "2023-05-02T17:03:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![neundorf](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neundorf/32/2075_2.png) [@neundorf](https://discourse.cmake.org/u/neundorf)
#### Post date: [May 2, 2023, 5:03pm UTC](https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036/1 "2023-05-02T17:03:30Z")

</div>

Hi,

I’m looking into adding a DIRECTORY property SUBDIRECTORIES\_RECURSE.  
Currently I implemented this in cmake script, but getting the SUBDIRECTORIES property and then iterating over them to get all their SUBDIRECTORIES feels a bit much for the cmake language. Also it seems to be a bit slow.  
I need this in order to collect all targets in all subdirs and then set some properties on each of them.

So I had a look at how the SUBDIRECTORIES property is implemented. So this is done in cmStateDirectory class. I have a bit a hard time figuring out how to add a recursive version there. I was assuming I would somehow see a vector of cmMakefile for each added subdirectory, but this is not the case.  
Should I get the SUBDIRECTORY property and then get the cmMakefile for each of the returns dirs ? Or is there a more direct way to collect all the subdirs ?

Thanks for pointers  
Alex

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [May 2, 2023, 6:12pm UTC](https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036/2 "2023-05-02T18:12:55Z")

</div>

The `cmState...` infrastructure comes from a partially completed attempt at abstracting the CMake code model representation away from the cmake language implementation in `cmMakefile`.

From a `cmStateDirectory` you can iterate over its `Children`, and in each of those `cmStateSnapshot` instances, call `.GetDirectory()` to get a `cmStateDirectory` instance for each one, and then recurse into that.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 3, 2023, 10:56pm UTC](https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036/3 "2023-05-03T22:56:38Z")

</div>

And something to be careful of in the implementation: it is possible to add the same source subdirectory more than once, with different binary subdirectories. I’ve seen that in real projects. It’s rare, but in very specific circumstances it can be a valid approach (but not one I’d generally recommend if you have an alternative way of accomplishing your goal).

---

<div class="post-metadata">

### Author: ![neundorf](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neundorf/32/2075_2.png) [@neundorf](https://discourse.cmake.org/u/neundorf)
#### Post date: [May 4, 2023, 9:05pm UTC](https://discourse.cmake.org/t/adding-a-subdirectories-recurse-directory-property/8036/4 "2023-05-04T21:05:18Z")

</div>

Here’s a first try: [https://gitlab.kitware.com/neundorf/cmake/-/commits/AddDirProp\_DIRECTORIES\_RECURSE](https://gitlab.kitware.com/neundorf/cmake/-/commits/AddDirProp_DIRECTORIES_RECURSE)
