update project to CMake build system from GNU make build system

k.ifantidis
Posts: 33
Joined: Wed Mar 28, 2018 6:58 am

update project to CMake build system from GNU make build system

Postby k.ifantidis » Sat Aug 03, 2019 8:44 am

Hello there !

I've been using ESP32-WROOM-32D module with 4MB flash and esp-idf v3.2 for the last year. I would like to update my project to cmake based build system but I can not find how to add my project's custom components in the project using CMakeLists.txt files. Before trying to update my old project I've compiled, flashed and debugged successfully blink example from esp-idf examples with VScode.

My old project tree was like that:

Project (folder)
-->build (folder)
-->partition.csv
-->Makefile
-->sdkconfig
-->version.txt
-->main (folder)
-------->component.mk
-------->main.c
-------->include (folder)
------------->main_include.h
-->components (folder)
-------->component01 (folder)
------------->component.mk
------------->component01_source.c
------------->include (folder)
------------------>component01_include.h
-------->component02 (folder)
------------->component.mk
------------->component02_source.c
------------->include (folder)
------------------>component02_include.h

I've tried this one with Cmake build system (installed master branch with the help of ESP-Tools installer 2.0)

Project (folder)
-->build (folder)
-->partition.csv
-->CMakeLists.txt
-->sdkconfig
-->version.txt
-->main (folder)
-------->CMakeLists.txt
-------->main.c
-------->include (folder)
------------->main_include.h
-->components (folder)
-------->component01 (folder)
------------->CMakeLists.txt
------------->component01_source.c
------------->include (folder)
------------------>component01_include.h
-------->component02 (folder)
------------->CMakeLists.txt
------------->component02_source.c
------------->include (folder)
------------------>component02_include.h

But when compiling with VScode I get error for all of my components, like this one:

Code: Select all

[21/65] Building C object esp-idf/01_TIMER/CMakeFiles/__idf_01_TIMER.dir/timers_v1i1.c.obj
FAILED: esp-idf/01_TIMER/CMakeFiles/__idf_01_TIMER.dir/timers_v1i1.c.obj
C:\esp\esp-idf\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe  -Iconfig -I../components/01_TIMER/. -I../components/01_TIMER/include -I../../../../components/newlib/platform_include -I../../../../components/freertos/include -I../../../../components/heap/include -I../../../../components/log/include -I../../../../components/soc/esp32/include -I../../../../components/soc/include -I../../../../components/esp_rom/include -I../../../../components/esp_common/include -I../../../../components/xtensa/include -I../../../../components/xtensa/esp32/include -I../../../../components/esp32/include -I../../../../components/driver/include -I../../../../components/esp_ringbuf/include -I../../../../components/esp_event/include -I../../../../components/tcpip_adapter/include -I../../../../components/lwip/include/apps -I../../../../components/lwip/include/apps/sntp -I../../../../components/lwip/lwip/src/include -I../../../../components/lwip/port/esp32/include -I../../../../components/lwip/port/esp32/include/arch -I../../../../components/vfs/include -I../../../../components/esp_wifi/include -I../../../../components/esp_wifi/esp32/include -I../../../../components/esp_eth/include
-I../../../../components/efuse/include -I../../../../components/efuse/esp32/include -I../../../../components/app_trace/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.0-dev-1381-g6fe853a2c\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/01_TIMER/CMakeFiles/__idf_01_TIMER.dir/timers_v1i1.c.obj -MF esp-idf\01_TIMER\CMakeFiles\__idf_01_TIMER.dir\timers_v1i1.c.obj.d -o esp-idf/01_TIMER/CMakeFiles/__idf_01_TIMER.dir/timers_v1i1.c.obj   -c ../components/01_TIMER/timers_v1i1.c
../components/01_TIMER/timers_v1i1.c:38:10: fatal error: globals.h: No such file or directory
 #include "globals.h"
          ^~~~~~~~~~~
compilation terminated.
The include globals.h that my component needs is located inside project's main folder as a header file. But the compiler seems that can not find it. I suspect that I don't have correct syntax in my CMakeLists.txt files and some option is missing. I'll attach the CMakeLists.txt files that I've been using for this.

I've already read the Cmake build system from esp32readthedocs website but I can't understand yet the concept. My purpose is to make my code modular and not a single large file containing everything.

Best regards, K.Ifantidis.
Attachments
CMakeLists_timerComponent's_File.txt
(103 Bytes) Downloaded 729 times
CMakeLists_Project's_File.txt
(250 Bytes) Downloaded 715 times

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: update project to CMake build system from GNU make build system

Postby WiFive » Sat Aug 03, 2019 10:11 am

Try without the quotes around your list

k.ifantidis
Posts: 33
Joined: Wed Mar 28, 2018 6:58 am

Re: update project to CMake build system from GNU make build system

Postby k.ifantidis » Sat Aug 03, 2019 11:10 am

WiFive wrote:
Sat Aug 03, 2019 10:11 am
Try without the quotes around your list
I think you refer to those quotes right??
--> set(COMPONENT_SRCS "timers_v1i1.c") set(COMPONENT_ADD_INCLUDEDIRS ". include") register_component()

I've removed them but I've been receiving the same error log:

Code: Select all

Checking Python dependencies...
Python requirements from C:\esp\esp-idf\requirements.txt are satisfied.
Executing action: all (aliases: build)
Running ninja in directory c:\esp\esp-idf\examples\01_projects\prj_name_v5i2\build
Executing "ninja all"...
[1/65] Performing build step for 'bootloader'
ninja: no work to do.
[7/65] Building C object esp-idf/02_GPIO/CMakeFiles/__idf_02_GPIO.dir/gpio_v2i1.c.obj
FAILED: esp-idf/02_GPIO/CMakeFiles/__idf_02_GPIO.dir/gpio_v2i1.c.obj
C:\esp\esp-idf\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe  -Iconfig -I../components/02_GPIO/. -I../components/02_GPIO/include -I../../../../components/newlib/platform_include -I../../../../components/freertos/include -I../../../../components/heap/include -I../../../../components/log/include -I../../../../components/soc/esp32/include -I../../../../components/soc/include -I../../../../components/esp_rom/include -I../../../../components/esp_common/include -I../../../../components/xtensa/include -I../../../../components/xtensa/esp32/include -I../../../../components/esp32/include -I../../../../components/driver/include -I../../../../components/esp_ringbuf/include -I../../../../components/esp_event/include -I../../../../components/tcpip_adapter/include -I../../../../components/lwip/include/apps -I../../../../components/lwip/include/apps/sntp -I../../../../components/lwip/lwip/src/include -I../../../../components/lwip/port/esp32/include -I../../../../components/lwip/port/esp32/include/arch -I../../../../components/vfs/include -I../../../../components/esp_wifi/include -I../../../../components/esp_wifi/esp32/include -I../../../../components/esp_eth/include -I../../../../components/efuse/include -I../../../../components/efuse/esp32/include -I../../../../components/app_trace/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.0-dev-1381-g6fe853a2c\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/02_GPIO/CMakeFiles/__idf_02_GPIO.dir/gpio_v2i1.c.obj -MF esp-idf\02_GPIO\CMakeFiles\__idf_02_GPIO.dir\gpio_v2i1.c.obj.d -o esp-idf/02_GPIO/CMakeFiles/__idf_02_GPIO.dir/gpio_v2i1.c.obj   -c ../components/02_GPIO/gpio_v2i1.c
../components/02_GPIO/gpio_v2i1.c:26:10: fatal error: nvs_flash.h: No such file or directory
 #include "nvs_flash.h"
          ^~~~~~~~~~~~~
compilation terminated.
[9/65] Building C object esp-idf/03_SOCKETS/CMakeFiles/__idf_03_SOCKETS.dir/sockets_v2i1.c.obj
FAILED: esp-idf/03_SOCKETS/CMakeFiles/__idf_03_SOCKETS.dir/sockets_v2i1.c.obj
C:\esp\esp-idf\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe  -Iconfig -I../components/03_SOCKETS/. -I../components/03_SOCKETS/include -I../../../../components/newlib/platform_include -I../../../../components/freertos/include -I../../../../components/heap/include -I../../../../components/log/include -I../../../../components/soc/esp32/include -I../../../../components/soc/include -I../../../../components/esp_rom/include -I../../../../components/esp_common/include -I../../../../components/xtensa/include -I../../../../components/xtensa/esp32/include -I../../../../components/esp32/include -I../../../../components/driver/include -I../../../../components/esp_ringbuf/include -I../../../../components/esp_event/include -I../../../../components/tcpip_adapter/include -I../../../../components/lwip/include/apps -I../../../../components/lwip/include/apps/sntp -I../../../../components/lwip/lwip/src/include -I../../../../components/lwip/port/esp32/include -I../../../../components/lwip/port/esp32/include/arch -I../../../../components/vfs/include -I../../../../components/esp_wifi/include -I../../../../components/esp_wifi/esp32/include -I../../../../components/esp_eth/include -I../../../../components/efuse/include -I../../../../components/efuse/esp32/include -I../../../../components/app_trace/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.0-dev-1381-g6fe853a2c\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/03_SOCKETS/CMakeFiles/__idf_03_SOCKETS.dir/sockets_v2i1.c.obj -MF esp-idf\03_SOCKETS\CMakeFiles\__idf_03_SOCKETS.dir\sockets_v2i1.c.obj.d -o esp-idf/03_SOCKETS/CMakeFiles/__idf_03_SOCKETS.dir/sockets_v2i1.c.obj   -c ../components/03_SOCKETS/sockets_v2i1.c
../components/03_SOCKETS/sockets_v2i1.c:32:10: fatal error: esp_ota_ops.h: No such file or directory
 #include "esp_ota_ops.h"
          ^~~~~~~~~~~~~~~
compilation terminated.
[10/65] Building C object esp-idf/03_UART/CMakeFiles/__idf_03_UART.dir/uart_v2i1.c.obj
FAILED: esp-idf/03_UART/CMakeFiles/__idf_03_UART.dir/uart_v2i1.c.obj
C:\esp\esp-idf\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe  -Iconfig -I../components/03_UART/. -I../components/03_UART/include -I../../../../components/newlib/platform_include -I../../../../components/freertos/include -I../../../../components/heap/include -I../../../../components/log/include -I../../../../components/soc/esp32/include -I../../../../components/soc/include -I../../../../components/esp_rom/include -I../../../../components/esp_common/include -I../../../../components/xtensa/include -I../../../../components/xtensa/esp32/include -I../../../../components/esp32/include -I../../../../components/driver/include -I../../../../components/esp_ringbuf/include -I../../../../components/esp_event/include -I../../../../components/tcpip_adapter/include -I../../../../components/lwip/include/apps -I../../../../components/lwip/include/apps/sntp -I../../../../components/lwip/lwip/src/include -I../../../../components/lwip/port/esp32/include -I../../../../components/lwip/port/esp32/include/arch -I../../../../components/vfs/include -I../../../../components/esp_wifi/include -I../../../../components/esp_wifi/esp32/include -I../../../../components/esp_eth/include -I../../../../components/efuse/include -I../../../../components/efuse/esp32/include -I../../../../components/app_trace/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.0-dev-1381-g6fe853a2c\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/03_UART/CMakeFiles/__idf_03_UART.dir/uart_v2i1.c.obj -MF esp-idf\03_UART\CMakeFiles\__idf_03_UART.dir\uart_v2i1.c.obj.d -o esp-idf/03_UART/CMakeFiles/__idf_03_UART.dir/uart_v2i1.c.obj   -c ../components/03_UART/uart_v2i1.c
../components/03_UART/uart_v2i1.c:23:10: fatal error: nvs_flash.h: No such file or directory
 #include "nvs_flash.h"
          ^~~~~~~~~~~~~
compilation terminated.
[11/65] Building C object esp-idf/04_ACTOR/CMakeFiles/__idf_04_ACTOR.dir/actor_v2i1.c.obj
FAILED: esp-idf/04_ACTOR/CMakeFiles/__idf_04_ACTOR.dir/actor_v2i1.c.obj
C:\esp\esp-idf\.espressif\tools\xtensa-esp32-elf\esp32-2019r1-8.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-gcc.exe  -Iconfig -I../components/04_ACTOR/. -I../components/04_ACTOR/include -I../../../../components/newlib/platform_include -I../../../../components/freertos/include -I../../../../components/heap/include -I../../../../components/log/include -I../../../../components/soc/esp32/include -I../../../../components/soc/include -I../../../../components/esp_rom/include -I../../../../components/esp_common/include -I../../../../components/xtensa/include -I../../../../components/xtensa/esp32/include -I../../../../components/esp32/include -I../../../../components/driver/include -I../../../../components/esp_ringbuf/include -I../../../../components/esp_event/include -I../../../../components/tcpip_adapter/include -I../../../../components/lwip/include/apps -I../../../../components/lwip/include/apps/sntp -I../../../../components/lwip/lwip/src/include -I../../../../components/lwip/port/esp32/include -I../../../../components/lwip/port/esp32/include/arch -I../../../../components/vfs/include -I../../../../components/esp_wifi/include -I../../../../components/esp_wifi/esp32/include -I../../../../components/esp_eth/include
-I../../../../components/efuse/include -I../../../../components/efuse/esp32/include -I../../../../components/app_trace/include -mlongcalls -Wno-frame-address   -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -nostdlib -Wall -Werror=all -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -std=gnu99 -Wno-old-style-declaration -D_GNU_SOURCE -DIDF_VER=\"v4.0-dev-1381-g6fe853a2c\" -DGCC_NOT_5_2_0 -DESP_PLATFORM -MD -MT esp-idf/04_ACTOR/CMakeFiles/__idf_04_ACTOR.dir/actor_v2i1.c.obj -MF esp-idf\04_ACTOR\CMakeFiles\__idf_04_ACTOR.dir\actor_v2i1.c.obj.d -o esp-idf/04_ACTOR/CMakeFiles/__idf_04_ACTOR.dir/actor_v2i1.c.obj   -c ../components/04_ACTOR/actor_v2i1.c
../components/04_ACTOR/actor_v2i1.c:25:10: fatal error: nvs_flash.h: No such file or directory
 #include "nvs_flash.h"
          ^~~~~~~~~~~~~
compilation terminated.
[16/65] Building C object esp-idf/openssl/CMakeFiles/__idf_openssl.dir/library/ssl_lib.c.obj
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
The terminal process terminated with exit code: 2

Terminal will be reused by tasks, press any key to close it.
It seems that the compiler can not find some basic esp-idf's components header files like "nvs_flash.h" and some others.

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

Re: update project to CMake build system from GNU make build system

Postby ESP_Angus » Mon Aug 05, 2019 12:32 am

One significant difference with the CMake build system in IDF V4.0 is that components need to declare what other components they depend on. Otherwise the component's source files won't see their header files.

So, for example, if "timerComponent" includes headers from the components "main", "nvs_flash" and "app_update" (for OTA) then:

Code: Select all

set(COMPONENT_SRCS "timers_v1i1.c")
set(COMPONENT_ADD_INCLUDEDIRS ". include")
set(COMPONENT_PRIV_REQUIRES main nvs_flash app_update)
register_component()
(Note: COMPONENT_PRIV_REQUIRES is for "private" requirements where the source files in the component use the header, but it's not needed for the public interface of the ocmponent. If the "timerComponent" has header files that include headers from other components headers then add those component to the COMPONENT_REQUIRES variable, instead.

BTW, in IDF V4.0 we introduced a new way to register components in CMake. The above will still work, but the new way (which only works in V4.0 not in v3.x) is:

Code: Select all

idf_component_register(SRCS "timers_v1i1.c"
                       INCLUDE_DIRS "." "include"
                       PRIV_REQUIRES main nvs_flash app_update
                       )
(Just like COMPONENT_REQUIRES above, use the REQUIRES keyword to register public header dependencies in this case.)

We know this is an extra step and a "gotcha" for people writing components, which is annoying. The upside is that it makes the compile and link process more predictable and the linking step faster (as we know what order to pass components to the linker now). And tracking component dependencies also opens the door for the ESP-IDF "package manager" which we want to introduce.

k.ifantidis
Posts: 33
Joined: Wed Mar 28, 2018 6:58 am

Re: update project to CMake build system from GNU make build system

Postby k.ifantidis » Wed Aug 07, 2019 7:25 am

Hello Angus.

Thank you for your response. So... If I create a component and it has wifi, OTA, nvs and FAT I have to do two things in order my new component to work.
1) Include the necessary headers of wifi, OTA, nvs and FAT in the new component's source file
2) Create the CMakeLists file that has inside all the dependencies that are needed, even ESP_IDF components

If I just want to add some extra source and header files in my project, what should I do? Do I have to add them in my main's folder and just use the code bellow in order to include them in my project ?

Code: Select all

idf_component_register(SRCS "main.c" "timers_v1i1.c" "fat_custom_v1i1.c" 
                       INCLUDE_DIRS "." "include"
                       PRIV_REQUIRES esp_vfs_fat nvs_flash app_update
                       )
Or I have to create them as components and treat them like components??

Best regards, K.Ifantidis

Who is online

Users browsing this forum: Bing [Bot] and 211 guests