# Best practice to manage collection of projects

**URL:** https://discourse.cmake.org/t/best-practice-to-manage-collection-of-projects/8330
**Category:** Usage
**Tags:** gen:makefiles, gen:ninja, tool:cmake
**Created:** [June 14, 2023, 7:14am UTC](https://discourse.cmake.org/t/best-practice-to-manage-collection-of-projects/8330 "2023-06-14T07:14:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![menno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/menno/32/2928_2.png) [@menno](https://discourse.cmake.org/u/menno)
#### Post date: [June 14, 2023, 7:14am UTC](https://discourse.cmake.org/t/best-practice-to-manage-collection-of-projects/8330/1 "2023-06-14T07:14:58Z")

</div>

We have a number of different CMake projects, each representing a set of tests for our code. Each project uses the same build target names (`preprocess`, `compute`, `postprocess` and others) to perform said steps of the set of tests.

Now, I’m having some difficulty to properly tie this collection of projects into an overall suite of tests with CMake. I have explored the option to repeatedly use `add_subdirectory`, but then I get an error about duplicate target names and CMP0002. While this can be circumvented by setting [`ALLOW_DUPLICATE_CUSTOM_TARGETS`](https://cmake.org/cmake/help/latest/prop_gbl/ALLOW_DUPLICATE_CUSTOM_TARGETS.html), this does not work with other build tools then Makefiles. Furthermore, dependencies of sub-level targets with the same name on a top-level target is not handled correctly: if the target `preprocess` in subdirs Test1 Test2 and Test3 all depend on the top-level target `all_preprocess`, executing `make all_preprocess` will only make the last `preprocess` target added to its dependencies.

The other option is to sever the connection between the top-level directory and the sub-level directories, and have the top-level CMake project execute `cmake`, and each build target _as a loop over the sub-level directories_. This works as expected with both Makefiles and Ninja, but then I loose the ability to execute sub-level build steps in parallel.

To summarize: I’m looking for a way to build targets in sub-level directories _with the same name_ in each sub-directory  
a) with Makefiles and Ninja, and  
b) with sub-level directory parallelism, i.e. executing `Test1/preprocess`, `Test2/preprocess` and `Test3/preprocess` in parallel, when invoking the top-level target `all_preprocess`

Any hints how to do this, or where to find documentation, or examples of other similar projects are much appreciated

---

<div class="post-metadata">

### Author: ![menno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/menno/32/2928_2.png) [@menno](https://discourse.cmake.org/u/menno)
#### Post date: [June 14, 2023, 7:29am UTC](https://discourse.cmake.org/t/best-practice-to-manage-collection-of-projects/8330/2 "2023-06-14T07:29:54Z")

</div>

One thing I have found, for Makefiles, is this:

```auto

SUBDIRS := foo bar baz

.PHONY: all $(SUBDIRS)
all: $(SUBDIRS)

$(SUBDIRS):
    $(MAKE) --directory=$@

```

See [The Pitfalls and Benefits of GNU Make Parallelization | CMCrossroads](https://www.cmcrossroads.com/article/pitfalls-and-benefits-gnu-make-parallelization) about 2/3 of the length of the article, under _The right way to do recursive Make_

Is this something that can be achieved with CMake too, and if so, how?

---

<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: [June 17, 2023, 10:49pm UTC](https://discourse.cmake.org/t/best-practice-to-manage-collection-of-projects/8330/3 "2023-06-17T22:49:35Z")

</div>

I think that @craig.scott’s plans for namespacing projects is probably the long-term solution here. It would end up renaming the subdir targets of the same name automatically instead of being manually done (as it probably would need to be today anyways).

As for the `Makefile` snippet, CMake has no mechanism to provide its generated makefiles any custom code.
