Compile Arduino AVR programs using CMake.
Note that I've (undefined0) modified this to work with CPM!
Usage example is at undefined06855/CanSat at arduino/cansat/sat-v2/CMakeLists.txt
-
Clone this repository and open a terminal in the
Arduino-AVR-CMakefolder. -
Ensure that the Arduino AVR core is installed correctly, this guide assumes that the core is in
~/.arduino15/packages/arduino/hardware/avr/1.8.6, that the toolchain is in~/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7, and that avrdude is in~/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17. If this is not the case, change it now incmake/toolchain/avr.toolchain.cmake. -
Configure the project using CMake by running the following command:
cmake -S. -Bbuild \ -D ARDUINO_PORT=/dev/ttyACM0 \ -D CMAKE_TOOLCHAIN_FILE=cmake/toolchain/uno.toolchain.cmake \ -D CMAKE_BUILD_TYPE=MinSizeRelCustomize the port, toolchain file, and build type for your specific configuration.
On windows you will need to use a generator other than Visual Studio such Ninja:
cmake -S. -Bbuild \ -D ARDUINO_PORT=COM1 \ -D CMAKE_TOOLCHAIN_FILE=cmake/toolchain/uno.toolchain.cmake \ -D CMAKE_BUILD_TYPE=MinSizeRel \ -G Ninja -
Finally, build and upload the example “blink” program:
cmake --build build -j -t upload-blink
To compile the program without uploading, you can use
cmake --build build -j -t blink
If you're using an Arduino with a native USB interface (e.g. Leonardo), you'll have to press the reset button before uploading. You could automate this by opening its serial port at 1200 baud as part of the upload process.
In VSCode, you can select the board you want to use using the
CMake: Select a Kit command from the CMake Tools extension.
Then select the right board as shown in the following image:

To build the sketch, you can either press F7 or click the Build
button in the bottom ribbon or in the CMake side panel. To upload, you can use
the upload-blink utility in the CMake side panel:
