What's the recommended directory structure for reusable components?

a2800276
Posts: 78
Joined: Sat Jan 23, 2016 1:59 pm

What's the recommended directory structure for reusable components?

Postby a2800276 » Wed Mar 29, 2023 12:43 pm

We're currently organizing some of the boilerplate code we use frequently into idf components to facilitate reuse. Functionality such as network setup boilerplate, sensor drivers, basic infrastructure for ota device configuration, etc.

The component template generated by `idf.py create_component` seems to cater more to components that are used to structure single projects. E.g. out-of-the-box, the generated component can not even be compiled to an *.a archive for syntax checks, no test structure etc. is created, etc.

The following seems to me to be a common way for components to be structured abd is how I'm setting up our components currently :

Code: Select all

.
├── CMakeLists.txt
├── example
│   ├── CMakeLists.txt
│   ├── main
│   │   ├── CMakeLists.txt
│   │   └── example.c
│   └── sdkconfig
├── include
│   └── my_component.h
├── README.md
└── src
    └── my_component.c
It differs from the `create-component` template in that:

- the c implementation files are moved to a `src` directory instead of being in the top-level directory of the component
- I create an `example` (and/or `test`) project which demonstrates usage and from which I compile the component during development

Any thoughts on this? Did I skip an import part of the docs that explained how this should be structured? How is everyone else constructing their components?

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: What's the recommended directory structure for reusable components?

Postby ESP_Sprite » Thu Mar 30, 2023 6:46 am

Speaking from the ESP-IDF side, we don't have a prescriptive component layout, especially since a fair amount of components also 'wrap' existing projects. Instead, we made our build system flexible enough so you can adjust it to any component layout you may prefer. So from that PoV, yours is as good as any.

a2800276
Posts: 78
Joined: Sat Jan 23, 2016 1:59 pm

Re: What's the recommended directory structure for reusable components?

Postby a2800276 » Thu Mar 30, 2023 8:40 am

Quick additional question: is the an idf.py command I'm missing to compile the component to a static archive? Or does that always have to occur in the context of compiling a project. (Sorry if this is obvious to people who put in the effort to understand the cmake process, I realize I'm being lazy :)

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: What's the recommended directory structure for reusable components?

Postby ESP_Sprite » Fri Mar 31, 2023 12:23 am

I don't think idf.py has specialized commands for that, but the underlying cmake build system probably has. No idea what that would be, though, I'm no cmake expert either.

Who is online

Users browsing this forum: No registered users and 105 guests