# Manually simulating target namespaces for building multi-architecture repos

**URL:** https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397
**Category:** Usage
**Tags:** os:linux, gen:makefiles
**Created:** [August 6, 2024, 5:24pm UTC](https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397 "2024-08-06T17:24:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![buergi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@buergi](https://discourse.cmake.org/u/buergi)
#### Post date: [August 6, 2024, 5:24pm UTC](https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397/1 "2024-08-06T17:24:42Z")

</div>

This is an issue which I have since several years now and which always kept preventing me from transitioning to cmake for a larger STM32 project. I never found a reasonable solution so far, until I recently found a semi-acceptable work around, actually a very straight forward one.

The structure looks like this

```auto
shared
 | LibA
 | LibB
 | ... (about 30 libraries)
stm32f7xx
 | BoardA
 | BoardB
stm32f4xx
 | BoardC

```

I have everything set up and running, but I need to comment in/out the corresponding definitions

```auto
# only build stm32f7xx targets
set(MCU STM32F765xx)
add_definitions(-DSTM32F765xx)

# only build stm32f4xx targets
#set(MCU STM32F407xx)
#add_definitions(-DSTM32F407xx)

add_subdirectory(shared)
add_subdirectory(stm32f7xx)
add_subdirectory(stm32f4xx)

```

The `BoardX` projects are wrapped in a conditional clause to only build when the proper `MCU` is configured. So this way I can use `make BoardA BoardB` but not `make BoardC`.

I’ve tried a lot but I couldn’t find a proper way to build the shared directory twice without getting target name collisions. I tried around a lot with `ExternalProject_Add`, but I couldn’t get it to work. I tried using `execute_process` to call a secondary `cmake` instance, which I don’t like very much.

I think the only cmake feature which might help would be Project-level namespaces as proposed in [#22687](https://gitlab.kitware.com/cmake/cmake/-/issues/22687).  
So the only workaround I found so far is manually simulating namespaces, but it is a bit tedious. It effectively looks like this in EVERY project in any subdirectory, except the top level one.

```auto
project(${NS}LibA)
...
## dependencies
target_link_libraries(${PROJECT_NAME} ${NS}LibB)
...
add_library(${NS}Lib::LibA ALIAS ${PROJECT_NAME})

```

and then using it like this

```auto
set(NS STM32F4xx)
set(STM32_FAMILY STM32F4xx)
add_subdirectory(shared shared_stm32f4xx)

add_subdirectory(BoardA)

set(NS STM32F7xx)
set(STM32_FAMILY STM32F7xx)
add_subdirectory(shared shared_stm32f7xx)

add_subdirectory(BoardB)

```

This way I can just type

```auto
$ make BoardA BoardC
[33%] Built target STM32F4xxLibB
[66%] Built target STM32F4xxLibA
[100%] Built target BoardA
[33%] Built target STM32F7xxLibB
[66%] Built target STM32F7xxLibA
[100%] Built target BoardC

```

After month of procrastination because of this issue, I think I found an acceptable solution, although it requires to modify all CMakeLists it is something which can even be done automatically via Search&Replace. What do you think, any better suggestions how I could have solved this problem?

I really hope one day we get target namespaces, without knowning the details of the cmake code it looks to be something which would not require tremendous changes, but I know the devil is always in the detail.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [August 6, 2024, 9:12pm UTC](https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397/2 "2024-08-06T21:12:06Z")

</div>

IMHO you should use different build directories for each build.

Your way only works because the compiler binary happens to be the same.

You could use presets to make the build flavor easily selectable.

---

<div class="post-metadata">

### Author: ![buergi](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@buergi](https://discourse.cmake.org/u/buergi)
#### Post date: [August 8, 2024, 1:21pm UTC](https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397/3 "2024-08-08T13:21:45Z")

</div>

Thanks for your comment. I haven’t worked with presets so far, but after having played around with it a bit I like it very much. But unfortunately the best I could get is

```auto
cmake --preset=f4 && cmake --build --preset=f4 --target BoardA
cmake --preset=f7 && cmake --build --preset=f7 --target BoardC

```

While being by far shorter and better than I expected I still would prefer a way to build all boards at once, especially as I really like the total build progress percentage output of cmake.

So if there is any way to combine multiple builds I would be highly interested.

---

<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: [July 20, 2025, 11:54pm UTC](https://discourse.cmake.org/t/manually-simulating-target-namespaces-for-building-multi-architecture-repos/11397/4 "2025-07-20T23:54:29Z")

</div>

> [@buergi](#):
>
> So if there is any way to combine multiple builds I would be highly interested.

A superbuild that builds each project using [`ExternalProject`](https://cmake.org/cmake/help/latest/module/ExternalProject.html) would probably be the most straightforward way today.

CMake’s historical design decisions mean that a single build can only support a single toolchain per language, so “build target X for platform A and B” requires two build directories.
