Post

Set COMPONENTS to Reduce Build Time

This tip will help drastically reduce your build time, especially for simpler projects without a lot of IDF dependencies.

How to Use It

Add the following line to your top-level CMakeLists.txt file in the root of your project.

1
set(COMPONENTS main)

Without this line, by default, idf.py build will build all of the IDF components

Official documentation of this option can be found here.

You will then need to add REQUIRES and/or PRIV_REQUIRES entries to your main component CMakeLists.txt file to include any dependencies explicitly.

Example

Given the following extremely simple example we will compare build times on my machine (12th Gen Intel Core i7-1260P x 16 with 64GB RAM).

Root CMakeLists.txt

1
2
3
4
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 14)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp32_tip1)

main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <thread>
#include <chrono>

extern "C" void app_main(void)
{
  while (true) {
      printf("Welcome to Production ESP32 Tip #1\n");
      std::this_thread::sleep_for(std::chrono::seconds{5});
  }
}

Building this with time idf.py build produced the following results.

1
2
3
real    0m13.136s
user    1m29.214s
sys     0m26.995s

Updated Root CMakeLists.txt

1
2
3
4
5
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 14)
set(COMPONENTS main) # <----- Added this line
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp32_tip1)

Running the timed build again produced the following results.

1
2
3
real    0m7.628s
user    0m43.880s
sys     0m12.643s

We went from 13.1s build time to 7.6s, a 42% reduction. That is almost HALF as much time!!! As you can see this could save you a significant amount of time during development when you are building frequently.

An additional benefit you get from this change is a simplified sdkconfig file as it will exclude all of the default entries from components you aren’t using. In the example above the sdkconfig file went from 1803 lines of code to 1146, a 36% reduction in size.

Conclusion

This is a very simple change to your project to reduce build times. However, if your project has a lot of dependencies in the IDF you will need to figure those out so you can explicitly include the components needed. This can be complicated but well worth your time as it will help you be more explicit, and therefore deterministic, about your project dependencies.

If you try this tip I’d love to hear what kind of compile time reduction you experienced. Drop it in the comments.

Did you find this tip helpful? Join the community and get more content just like it in the weekly Production ESP32 newsletter. Concise, actionable content right in your inbox.

© Kevin Sidwar

Comments powered by Disqus.