# CMake directory hierarchy question

**URL:** <https://discourse.cmake.org/t/cmake-directory-hierarchy-question/8477>\
**Category:** Code\
**Created:** [July 7, 2023, 1:12pm UTC](https://discourse.cmake.org/t/cmake-directory-hierarchy-question/8477 "2023-07-07T13:12:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marker5a](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@marker5a](https://discourse.cmake.org/u/marker5a)\
**Post date:** [July 7, 2023, 1:12pm UTC](https://discourse.cmake.org/t/cmake-directory-hierarchy-question/8477/1 "2023-07-07T13:12:11Z")

</div>

I have a project for an embedded target that contains source code for a bootloader and source code for two sets of user code for two different microcontrollers, call them mcu\_A and mcu\_B.

The folder structure is as follows:

```
root/
    CMakeLists.txt
    bootloader/
        CMakeLists.txt
        ...
    mcu_A/
        CMakeLists.txt
        ...
    mcu_B/
        CMakeLists.txt
        ...

```

Generally, the workflow for us currently (using homebrewed makefiles and not using CMake) is that we are working on some source code in either mcu\_A or mcu\_B and once we want to test out some new code, we call “make install”, which calls some post-processing targets before calling a bootloader that uploads the new firmware. One of the post process steps for instance is to combine the two hex files from mcu\_A and mcu\_B into a single hex file that is uploaded in the install step.

The nice thing about this setup is that you can call “make install” in either mcu\_A/ or mcu\_B/ and they call a Makefile in the top level that knows to depend on the mcu\_A and mcu\_B hex files, merges them, and then hands it off to the install target. We want to keep this structure.

**For a quick TLDR, this is my problem currently:**  
The problem that I am facing is that I can call upload in the toplevel, but that target is not made available to the mcu\_A/mcu\_B folders. I had thought about defining upload as a duplicate in the lower folders but when I tried that, it complains that upload is defined twice. Here are my CMakeLists files for my proof of concept:

root/CMakeLists.txt

> add\_subdirectory(mcu\_A)  
> add\_subdirectory(mcu\_B)
> 
> add\_custom\_target(upload COMMAND echo “Calling upload from top level” DEPENDS compile\_mcu\_A compile\_mcu\_B)

root/mcu\_A/CMakeLists.txt

> add\_custom\_target(compile\_mcu\_A ALL COMMAND echo “mcu\_A Code is being compiled”)

root/mcu\_B/CMakeLists.txt

> add\_custom\_target(compile\_mcu\_B ALL COMMAND echo “mcu\_B Code is being compiled”)

I am still fairly new to CMake, so I’m sure I am missing a key architectural concept that could be preventing this from working, but my hope is that it is do-able and like ant pointers

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [July 11, 2023, 6:18pm UTC](https://discourse.cmake.org/t/cmake-directory-hierarchy-question/8477/2 "2023-07-11T18:18:26Z")

</div>

What you’re trying to do, is based on the idea of using recursive makefiles.  
But that is a makefile generator specific detail. Other generators may do it differently. e.g. MSVC projects are per target (AFAIK) and for Ninja everything is in a single file.

I suggest to do an `upload_A` and an `upload_B` target. The `upload` could simply depend on them, but if you want to flash the combined hex file instead, I guess it’s a completely independent target.
