CMake Build System require the main module to be called "main"

ukrsms
Posts: 24
Joined: Tue Aug 20, 2019 11:30 am

CMake Build System require the main module to be called "main"

Postby ukrsms » Wed Sep 18, 2019 4:39 pm

I am trying to switch from the gnu make to CMake build system and faced with a problem: some header files are not visible from my "main.c" file. The key thing is that I've moved my project from ESP8266 and my "main" code lays within "user" dir.
After digging CMake related files I found that there are some places with the main component name hardcoded as "main"
in file expand_requirements.cmake (line 49):

Code: Select all

# Dummy register_component used to save requirements variables as global properties, for later expansion
#
# (expand_component_requirements() includes the component CMakeLists.txt, which then sets its component variables,
# calls this dummy macro, and immediately exits again.)
macro(register_component)
    if(COMPONENT STREQUAL main AND NOT COMPONENT_REQUIRES)
        set(main_component_requires ${COMPONENTS})
        list(REMOVE_ITEM main_component_requires "main")
As for me, the user should be able to name its "main" directory as he wants, but currently, it is impossible without IDF files to be edited.

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

Re: CMake Build System require the main module to be called "main"

Postby ESP_Angus » Thu Sep 19, 2019 4:48 am

Hi ukrsms,

You're right that the name "main" is hardcoded in the CMake build system. I'll see if the name can be changed.

However, there is only one difference between "main" and any other component in the project, which is that "main" doesn't need to declare it's dependencies with a "REQUIRES ...." list when registering the component.

As a workaround, if you're able to add that list of required components, then you can add the directory containing "user" to the EXTRA_COMPONENT_DIRS list for the project and the build should work with the current structure.

ukrsms
Posts: 24
Joined: Tue Aug 20, 2019 11:30 am

Re: CMake Build System require the main module to be called "main"

Postby ukrsms » Thu Sep 19, 2019 7:20 am

I am new to CMake, therefore, I am not sure enough how to use "REQUIRES ....".
However, at first I simply added "user" to the EXTRA_COMPONENT_DIRS, but during the building, some IDF header files included to my main.c were "not found".

For example, I had these files including in my main.c

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "main.h"
trying to build the project I got the error:

Code: Select all

../user/main.c:13:27: fatal error: esp_spi_flash.h: No such file or directory
After removing "#include "esp_spi_flash.h"" the project was built successfully.
However, the interesting thing, after I renamed the main dir to "main" the project was build Ok even with "esp_spi_flash.h" file included.

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

Re: CMake Build System require the main module to be called "main"

Postby ESP_Angus » Thu Sep 19, 2019 10:29 pm

Hi ukrsms,
ukrsms wrote:
Thu Sep 19, 2019 7:20 am
I am new to CMake, therefore, I am not sure enough how to use "REQUIRES ....".
The master branch build system docs has some additional details about this, but they're not in the v4.0 docs yet (they will be, it's the same in 4.0 and 4.1):
https://docs.espressif.com/projects/esp ... quirements

to summarise:

In the component CMakeLists.txt where you call idf_component_register you need to name which components your component will require. So in this case you'd add "idf_component_register(... REQUIRES spi_flash)" so that the "user" component could see the "spi_flash" component's headers.

The "main" component has special status because it automatically requires every other component in the build (as we assume that's where you'll put the startup code that pulls in everything else). This is why renaming "user" component to "main" fixed the header problem.

ukrsms
Posts: 24
Joined: Tue Aug 20, 2019 11:30 am

Re: CMake Build System require the main module to be called "main"

Postby ukrsms » Fri Sep 20, 2019 2:03 pm

Thank you, Angus, for your kindly reply.
In the component CMakeLists.txt where you call idf_component_register you need to name which components your component will require. So in this case you'd add "idf_component_register(... REQUIRES spi_flash)" so that the "user" component could see the "spi_flash" component's headers.
It really helped, however, because I use IDF ver 3.3 I added this line to CMakeLists.txt:

Code: Select all

set(COMPONENT_REQUIRES spi_flash)

Who is online

Users browsing this forum: Google [Bot] and 131 guests