One major advantage of using cmake to build projects comes in the test cycle. The out-of-source tree builds, allow things like google-test to be used on small parts of logic within your own host and not on the target. This facilitates debugging and makes regression testing readily achievable.
Building firmware for the Arduino using the Arduino IDE/CLI with cmake is straightforward using the Arduino-CMake-Toolchain. The recent fork of the Arduino-CMake-Toolchain which allows ESP32 to work is here Arduino-CMake-Toolchain.
I am writing this to help provide the simple workarounds required to build projects on the ESP32 using the libraries downloadable from the Arduino Boards Manager.
This example builds two flavours. A basic blink firmware and a Wifi firmware.
It ran on Linux (Ubuntu) and assumed a cmake more recent than 3.14.0
Library Installation:-
Using the Arduino IDE, Tools, Board, Board Manager download Version 2.0.3 of the "esp32" by "Espressif Systems". 2.0.3 works ok.
This was targetted at a NodeMCU ESP32. Chip is ESP32-D0WD-V3 (revision 3)
The partition.csv was designed from the relevant build step which does the upload to various flash locations in
~/.arduino15/packages/esp32/hardware/esp32/2.0.3/platform.txt
The bootloader was chosen from information in
espressif32 nodemcu-32s
Tip: if the build is not happening, try starting with a fresh build directory, (preserving your BoardOptions.cmake).
Tip2: The first attempt with cmake and this Toolchain errors, but faithfully provides a fleshed out BoardOptions.cmake in the build directory, for you to uncomment your board.
Tip3: If the building is failing on weird things, this could be because of contributors to the ESP32 library are successfully writing code, which is slightly outside of the Espressif guidance on how to write code. For example #including from a file in a library, rather than #including the library's interface file. There are other areas where there are challenges for cmake. (The Arduino IDE is really forgiving when it hunts for libraries to include, so it will find libraries when the cmake-tool-chain does not.) Its worth looking through the issues raised on all the forks of the Arduino-CMake-Toolchain project for your symptom; as I've found simple workarounds to frustrating errors reported by cmake.
My thanks to a9183756-gh for making the toolchain and technyon for upgrading the toolchain.
Blink example
CMakeLists.txt
CMakeLists.txt (1.3 KB)
blink - example cpp
blink.cpp (502 Bytes)
partitions.csv
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs,data,nvs,0x9000,20K,
otadata,data,ota,0xe000,8K,
app0,app,ota_0,0x10000,1280K,
app1,app,ota_1,0x150000,1280K,
spiffs,data,spiffs,0x290000,1472K,
Wifi connect example
CMakeLists.txt
CMakeLists.txt (1.4 KB)
example cpp
connect.cpp (808 Bytes)
partitions.csv contained the same as above.