CMakeLists.txt - How to add component by absolute path in PRIV_REQUIRES?

brianpdx
Posts: 10
Joined: Fri Jan 29, 2021 6:07 am

CMakeLists.txt - How to add component by absolute path in PRIV_REQUIRES?

Postby brianpdx » Sun Dec 11, 2022 9:56 pm

I have a project with a structure like:

Code: Select all

- my_project/
             - CMakeLists.txt
             - components/
             	 - foo/
             	 	- CMakeLists.txt
                        - foo.c
I want the foo subcomponent to require (via PRIV_REQUIRES) a component via absolute path, i.e. I don't want to add the path to foo's required component at the project level via EXTRA_COMPONENT_DIRS.

Use case:

Let's say for example I want to include the protocols_examples_common component from the esp-idf examples:

The solution that works (that I'd like to avoid) is:

project/CMakeLists.txt:

Code: Select all

cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS
    "$ENV{IDF_PATH}/examples/common_components/protocol_examples_common"
)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_project)
foo/CMakeLists.txt:

Code: Select all

idf_component_register(
    SRCS "foo.c"
    INCLUDE_DIRS "."
    PRIV_REQUIRES protocol_examples_common
)
The above configuration will build correctly because the foo component can find protocol_examples_common.

What I'd like to do however is not add the path to protocol_examples_common to EXTRA_COMPONENT_DIRS, and specify an absolute path for the component in foo/CMakeLists.txt, for example:

project/CMakeLists.txt:

Code: Select all

cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_project)
foo/CMakeLists.txt:

Code: Select all

idf_component_register(
    SRCS "foo.c"
    INCLUDE_DIRS "."
    PRIV_REQUIRES
    	"$ENV{IDF_PATH}/examples/common_components/protocol_examples_common"
)
But using the above configuration causes cmake to complain:

Code: Select all

Failed to resolve component
  '/Users/brian/.espressif/esp-idf/examples/common_components/protocol_examples_common'.
Is there a way to write foo/CMakeLists.txt so that it can indicate it needs protocol_examples_common without having to update EXTRA_COMPONENT_DIRS in the project's CMakeLists.txt?

Who is online

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