CMake with Arduino

Here's what I use:

It uses modern target-based CMake, using toolchain files to select different boards.
The CMakeLists file for a simple “blink” example looks like this:

The Arduino core files are compiled into a library in cmake/arduino_core.cmake.
The executable is converted into a .HEX file using cmake/arduino_hex.cmake, and this .HEX file is uploaded using avrdude in cmake/arduino_upload.cmake.

The board-specific options are in the toolchain files, e.g. cmake/toolchain/uno.toolchain.cmake. You can take these settings straight from ArduinoCore-avr/boards.txt.

It doesn't do anything fancy, but it works just fine, and it is simple enough to understand and modify yourself: it's just 160 lines of CMake scripting.

1 Like