Building CMake project without idf.py

bluefarmer
Posts: 6
Joined: Tue Feb 11, 2020 5:00 pm

Building CMake project without idf.py

Postby bluefarmer » Tue Feb 11, 2020 6:35 pm

Hi,

I'm attempting to build a project using the esp-idf.
I've been using the idf.py script and the idf_component_register functions and that has worked however I'd like to switch to pure cmake to make debugging my own cmake files easier and to reduce the amount of 'magic' in the system a little bit.

I've tried using idf_build_process(esp32) instead of the script that seems to call cmake again in script mode which disallows certain cmake commands. My project structure is as follows:

-root
---esp-idf
---project
-----build
-----components
-------custom
---------custom.h/custom.c
---------CMakeLists.txt
-----main
-------main.c
-------CMakeLists.txt


Before running the cmake command I run the export.sh first to set any needed environment variables.
Then I cd into build and run cmake ..

Code: Select all

# root/project/CMakeLists.txt

cmake_minimum_required(VERSION 3.5...3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
idf_build_process(esp32)
project(hello-world)

Code: Select all

# root/project/main/CMakeLists.txt

#idf_component_register(
#    SRCS "main.c"
#    INCLUDE_DIRS "${COMPONENT_DIR}"
#)
add_executable(main.elf main.c)
target_link_libraries(main.elf PRIVATE custom)
idf_build_executable(main.elf)

Code: Select all

# root/project/components/custom/CMakeLists.txt

add_library(
        custom STATIC
        custom.c
        custom.h
)
target_include_directories(custom PUBLIC ${CMAKE_CURRENT_LIST_DIR})
I get an error stating that the add_library() command is not scriptable which I think is because the idf_build_process runs cmake again in scripting mode. When I uncomment idf_component_register and comment out add_executable it passes that stage of the build at least but I was hoping to not need idf_component_register and use straight CMake like add_library and add_executable instead.

Any hints regarding this and how to better structure a cmake idf project are highly appreciated.

Thanks.

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

Re: Building CMake project without idf.py

Postby ESP_Angus » Tue Feb 11, 2020 10:21 pm

Hi bluefarmer,

It should be possible to do something like what you want. I have a couple of pointers that should help you get things unstuck:

--

First is a clarification. You can still use the IDF build system and run "cmake" instead of "idf.py", the same way that you would to build a normal CMake project. idf.py is just a wrapper around cmake & make/ninja, plus some helpers for flashing. If you look at the logs from idf.py it prints the CMake commands that it runs in the background.

Some docs about running CMake directly (but still using IDF build system) can be found here:
https://docs.espressif.com/projects/esp ... e-directly

--

Making a "pure CMake" project that doesn't use the IDF build system to build your source files is also possible, but if you do this then you can't can't include "IDF_PATH/tools/cmake/project.cmake" into your project because this overrides CMake's normal "project" function. If you want the generic CMake behaviour with no IDF "magic" in your project then you need to include "/tools/cmake/idf.cmake" instead.

You'll also need to move the add_executable() and idf_build_executable() steps out of "main" component and up into the top-level component. This is similar to how you'd structure a CMake build process for a single executable, outside of IDF..

These may be the only things which are preventing the CMakeLists files you have from building correctly, but if you have more trouble then I suggest checking against the info here:

https://docs.espressif.com/projects/esp ... e-projects

and checking closely against the example here:

https://github.com/espressif/esp-idf/tr ... idf_as_lib

bluefarmer
Posts: 6
Joined: Tue Feb 11, 2020 5:00 pm

Re: Building CMake project without idf.py

Postby bluefarmer » Fri Feb 14, 2020 11:18 am

Including idf.cmake instead of project.cmake was my main problem.
Looking at those example projects and the build scripts helped me solve the other ones.

Thanks!

Who is online

Users browsing this forum: No registered users and 57 guests