recursively adding all source files in components directory using cmake setup ( esp-idf stable 4.0 )

vinodstanur
Posts: 27
Joined: Thu Jun 08, 2017 12:59 pm

recursively adding all source files in components directory using cmake setup ( esp-idf stable 4.0 )

Postby vinodstanur » Tue Mar 17, 2020 10:14 am

Hello,

I am having too many recursive folders in my project and all are in components directory. In new stable 4.0 I am trying to make the CMakeLists.txt is too confusing and I am not used to cmake based build.

Could someone create an example for a CMakeLists.txt for recursive folder?

Say I have components and main folder. Inside components I have lcd folder, but no source only headers inside. Inside lcd I have drv, gui_lib etc and all are having c and h files. Inside gui_lib I have further deep directories which is having more source code. I am a bit confused how to make CMakeLists.txt for each one and what ever I do, it is always showing undefined reference a lot, seems like I am not doing it properly.

Is any way to tell the root level CMakeLists.txt to recursively add all *.c and all folders and include folders ? This way I don't want to do CMakeLists.txt inside each folder which is an overhead for me and at present not working also coz I am not sure how to do it properly.

vinodstanur
Posts: 27
Joined: Thu Jun 08, 2017 12:59 pm

Re: recursively adding all source files in components directory using cmake setup ( esp-idf stable 4.0 )

Postby vinodstanur » Tue Mar 31, 2020 6:27 pm

Any tips @idf experts :)

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: recursively adding all source files in components directory using cmake setup ( esp-idf stable 4.0 )

Postby ESP_Angus » Wed Apr 01, 2020 6:56 am

Hi vinodstanur,

The "recommended CMake Way" is to list all of the source files by name. They can all be given in a single CMakeLists.txt file, even when the files are in multiple subdirectories, but CMake recommends listing each file name individually and not using "globbing" techniques like "*".

In ESP-IDF build system, this means passing them in by the "SRCS" parameter of idf_component_register. Here's an example:
https://github.com/espressif/esp-idf/bl ... ts.txt#L26

(Note this uses a CMake variable called "srcs", this is optional you can also just list all of the source file relative paths one by one after SRCS.

If you're updating, you only need one CMakeLists.txt file in each place that you had a component.mk file in the old build system.

The reason for encouraging "source files not directories" is that CMake only re-runs when it is necessary, and if you add a new file to the build when using globbing then CMake will not automatically re-run - CMake needs to be run manually then, or "idf.py reconfigure" needs to be run. This can be confusing if you share the codebase with multiple people.

Despite all this, it is still possible to give a list SRC_DIRS instead of SRCS - similar to the GNU Make build system. CMake will use globbing to find all the source files. We do this for our internal unit test components:

https://github.com/espressif/esp-idf/bl ... sts.txt#L1

(Again, this component only has one source dir but you can put a list of multiple source directory paths here and it will work.)

For more information about the idf_component_register() function, see here:
https://docs.espressif.com/projects/esp ... t-commands

Who is online

Users browsing this forum: Bing [Bot], ESP_Dazz, Google [Bot], Majestic-12 [Bot], MicroController and 106 guests