AWS FreeRTOS 202107 - how to add and link custom components

The existing documentation provides some example that doesn’t work when using

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=freertos/tools/cmake/toolchains/xtensa-esp32.cmake -GNinja

(see Getting started with the Espressif ESP32-DevKitC and the ESP-WROVER-KIT - FreeRTOS)

# Add some extra components. IDF_EXTRA_COMPONENT_DIRS is a variable used by ESP-IDF
# to collect extra components.
get_filename_component(
    EXTRA_COMPONENT_DIRS
    "components/example_component" ABSOLUTE
)
list(APPEND IDF_EXTRA_COMPONENT_DIRS ${EXTRA_COMPONENT_DIRS}) 

IDF_EXTRA_COMPONENT_DIRS is not something the cmake scripts are aware of

Any idea about the best way to make it work?

My project structure:

my-app
-components
--component1
--component2
-freertos
-src
--main.c
-CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

project(adf-test)

# Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE adf-test)
set(IDF_EXECUTABLE_SRCS ${PROJECT_SOURCE_DIR}/src/main.c)

# Add FreeRTOS as a subdirectory. AFR_BOARD tells which board to target.
set(AFR_BOARD espressif.esp32_devkitc CACHE INTERNAL "")
add_subdirectory(freertos)

target_link_libraries(adf-test PRIVATE AFR::backoff_algorithm AFR::ble AFR::ble_hal AFR::common AFR::common_io AFR::core_mqtt AFR::crypto AFR::demo_base AFR::freertos AFR::pkcs11_mbedtls AFR::tls AFR::transport_interface_secure_sockets AFR::utils AFR::secure_sockets AFR::tls AFR::transport_interface_secure_sockets AFR::dev_mode_key_provisioning AFR::demo_numeric_comparison AFR::demo_core_mqtt)

Some remark:
in a project version that is using the esp-idf (non-aws) installation is working by using

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello-world)

Many thanks!

1 Like

Hey, could you try seeing if it works using the code from this Github: https://github.com/tgsong/amazon-freertos-examples ? If so, can borrow from that Cmake file.

Probably something like:

cmake_minimum_required(VERSION 3.13)

project(adf-test)

add_executable(adf-test src/main.c)

# Tell IDF build to link against this target.
set(IDF_PROJECT_EXECUTABLE adf-test)

# Add some extra components. IDF_EXTRA_COMPONENT_DIRS is an variable used by ESP-IDF
# to collect extra components.
get_filename_component(
    EXTRA_COMPONENT_DIRS
    "components/component1" ABSOLUTE
    "components/component2" ABSOLUTE
)
list(APPEND IDF_EXTRA_COMPONENT_DIRS ${EXTRA_COMPONENT_DIRS})

include_directories(BEFORE freertos-configs)

# Add freertos as an subdirectory. AFR_BOARD tells which board to target.
set(AFR_BOARD espressif.esp32_devkitc CACHE INTERNAL "")
add_subdirectory(freertos)

# Link against the mqtt demo so that we can use it. Dependencies of this demo are transitively
# linked.
target_link_libraries(
    adf-test
    PRIVATE
    AFR::demo_core_mqtt
    AFR::common_io
)

You might be missing some configs. What specific errors were you seeing?

The main problem is that IDF_EXTRA_COMPONENT_DIRS is not used by any of cmake files. At least not in the AWS Freertos 202107 version. So no effect if you try to make the build system aware of custom components like this.

I am able to compile components as static libraries and link them to the target.

Still wondering if any best practices to add custom components using idf as used to be (seeing the official documentation or the examples referred by you) in the previous versions.

If you take a look at the beta version of esp-aws-iot, all the demos contain several examples of how to register custom components for AWS libraries from the AWS IoT Embedded C SDK by making use of XyzFilePaths.cmake of each library. This might also be a better direction for your project instead of attempting to link AFR dependencies to a single executable.