CMake system build for reusing components

adesandr
Posts: 22
Joined: Sat May 05, 2018 10:51 am

CMake system build for reusing components

Postby adesandr » Sat May 09, 2020 12:10 pm

Hello,

I going to you because i'm having a hard time solving an issue with the CMakeList build system.

Basically I want to share various components between various projects, without copy my components in each projects.

My directories are structured as below (extra_components is as the same level than project1/ ... projectN) :

MyProjets/
|---project1/
|---|---main/
|---|---|--- filemain.c
|---|---|--- filemain.h
|---|---|--- CMakeList.txt
|---|---CMakeList.txt
..........
|---projectN/
|...| same structure as project1
|...extra_components/
|...|... component1/
|...|... |... file1.c
|...|... |... file1.h
|---|----|--- CMakeList.txt
..............
|...|... componentN/
|...|... |... fileN.c
|...|... |... fileN.h
|---|----|--- CMakeList.txt

MyProjects/project1/main/CMakeList.txt which need component1 is :
  1. idf_component_register(SRCS "filemain.c"
  2.                        INCLUDE_DIRS ""  "${PROJECT_DIR}/../extra_components/component1")

MyProjets/projet1/CMakeList.txt is :
  1. cmake_minimum_required(VERSION 3.5)
  2. set(EXTRA_COMPONENT_DIRS "${PROJECT_DIR}/../extra_components")
  3. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
  4. project(project1)

unfortunately when i build project1, compilation works fine but component1 is not find by the linker, and is normal because when i check the components list and component path don't show any reference to component1.

I tryed to reference component1 using various CMake syntax like add_target_library, etc, with no success, component1 is never integrated neither in the components list nor the components path.

Thank you in advance for your help.

Regards.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: CMake system build for reusing components

Postby ESP_igrr » Sun May 10, 2020 9:57 am

Hi adesandr,

Instead of adding "${PROJECT_DIR}/../extra_components/component1" to the list of include directories in main/CMakeLists.txt, you need to add "component1" to the list of dependencies of the main component.

You can do this by adding REQUIRES component1 to the arguments of idf_component_register. If component A requires component B, then the build system will add public include paths of component B when compiling component A, and will also place component A before component B on the linker command line, ensuring that link-time dependencies are satisfied.

More about component requirements: https://docs.espressif.com/projects/esp ... quirements

adesandr
Posts: 22
Joined: Sat May 05, 2018 10:51 am

Re: CMake system build for reusing components

Postby adesandr » Sun May 10, 2020 3:43 pm

Hi ESP_igrr,

Many thank for your kind response.

I modified MyProject/project1/CMakeList.txt as follows :
  1. cmake_minimum_required(VERSION 3.5)
  2. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
  3. project(project1)
I modified MyProject/Project1/main/CMakeListe.txt as follows
  1. idf_component_register(SRCS "filemain.c"
  2.                        INCLUDE_DIRS "" "${PROJECT_DIR}/../Includes" "${PROJECT_DIR}/../extra_components/component1"
  3.                        REQUIRES component1)
when I launch idf.py reconfigure I obtain :

-- Building ESP-IDF components for target esp32
CMake Error at /home/adesandr/esp-idf/tools/cmake/build.cmake:185 (message):
Failed to resolve component 'component1

I tried also using in MyProject/Project1/main/CMakeListe.txt :
  1. idf_component_register(SRCS "filemain.c"
  2.                        INCLUDE_DIRS "" "${PROJECT_DIR}/../Includes" "${PROJECT_DIR}/../extra_components/component1"
  3.                        REQUIRES path_to_component1/component1)
I obtain the same error message, using a "path_to_component" in absolute or in relative.

If I put my extra_components directorie, inside project1, renaming the directorie in "components", the build works great, but I need to copy my component in each projects and it is not the target.

My extra_components directorie is as the same level than project1, so I can't understand how the CMakeList Wrapper can resolve giving him only component1 with the REQUIRES directive ?

Thank you for your help.

Regards.

Who is online

Users browsing this forum: No registered users and 414 guests