How to use CMake with components in custom directory

Chucky101
Posts: 2
Joined: Sun Jun 19, 2022 12:48 am

How to use CMake with components in custom directory

Postby Chucky101 » Wed Nov 09, 2022 10:34 pm

Problem Statement:
How does one, using the IDF SDk, create custom components that both require the use of the SDK components and cannot be in the a projects components directory.

Personal context:
-> I am extremely new to CMake and build systems
-> I have a very basic understanding of build systems
-> I have read the build system documentation at: [url][https://docs.espressif.com/projects/esp ... elists/url]
-> I have also read up on some general CMake documentation

What I have tried:
-> I have tried using the EXTRA_COMPONENT_DIRS variable but with no success
-> I tried to use pure CMake but then during the build stage, the component/library is unable to use the idf SDK

Common issues I have come across
-> XXX is not build by this project
-> No such file or directory for both custom components and IDF components

I am aware most of the information is vague/general but I would really appreciate if someone were able to give something like a minimum example that is able to address the problem in the problem statement.

Please don't refer me back the to build system documentation because I am pretty sure I am misunderstanding/missing something and I am not sure trying to parse it for a 15th time is going to help much :(

User avatar
gtjoseph
Posts: 92
Joined: Fri Oct 15, 2021 10:55 pm

Re: How to use CMake with components in custom directory

Postby gtjoseph » Mon Nov 14, 2022 11:11 am

There are a few ways to do this...

Let's say your directory structure looks like this (it's just an arbitrary structure)...

Code: Select all

mysource
  mycomponents
    mycomponent1
      CMakeLists.txt
      ..
    mycomponent2
      CMakeLists.txt
      ..
  myprojects
    myproject1    
      CMakeLists.txt
      ..
You could do this in your myproject1/CMakeLists.ttxt

Code: Select all

# It's important that the "set" come before "project"
set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../components")
project(myproject1)
If you only wanted to include mycomponent2, you could add...

Code: Select all

# It's important that the "set" come before "project"
set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../components")
set(COMPONENTS mycomponent2 <any esp-idf components you also need>)
project(myproject1)
An alternative is to symlink myproject1's "components" directory to mycomponents. Then no changes to CMakeLists.txt are required.

Code: Select all

mysource
  mycomponents
    mycomponent1
    mycomponent2
  myprojects
    myproject1
      components -> ../../components
Even another way is to symlink individual components...

Code: Select all

mysource
  mycomponents
    mycomponent1
    mycomponent1
  myprojects
    myproject1
      components
        mycomponent2 -> ../../mycomponents/mycomponent2
If none of those work, more detail would be required.

Who is online

Users browsing this forum: No registered users and 113 guests