Best practice to manage collection of projects

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, 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

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


SUBDIRS := foo bar baz

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

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

See The Pitfalls and Benefits of GNU Make Parallelization | CMCrossroads 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?

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.