arduino-cmake/CMakeLists.txt

82 lines
2.2 KiB
CMake

cmake_minimum_required(VERSION 3.15)
# Project name
project("Blink")
# Product filename
set(PRODUCT_NAME blink)
set(F_CPU 16000000UL)
set(MCU atmega328p)
set(BAUD 9600)
set(PROG_TYPE arduino)
set(PROG_PORT /dev/ttyACM0)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CXX_COMPILER avr-g++)
set(CMAKE_C_COMPILER avr-gcc)
set(CMAKE_ASM_COMPILER avr-gcc)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_definitions(
-DF_CPU=${F_CPU}
-DBAUD=${BAUD}
)
include_directories(SYSTEM /usr/avr/include)
set(CMAKE_EXE_LINKER_FLAGS -mmcu=${MCU})
add_compile_options(
-mmcu=${MCU} # MCU
-std=gnu++17
-Os # optimize
-Wall # enable warnings
-Wno-main
-Wundef
-pedantic
-Werror
-Wfatal-errors
-Wl,--relax,--gc-sections
-g
-gdwarf-2
-funsigned-char # a few optimizations
-funsigned-bitfields
-fpack-struct
-fshort-enums
-ffunction-sections
-fdata-sections
-fno-split-wide-types
-fno-tree-scev-cprop
)
# Create one target
add_executable(${PRODUCT_NAME}
src/blink.cpp
)
# Rename the output to .elf as we will create multiple files
set_target_properties(${PRODUCT_NAME} PROPERTIES OUTPUT_NAME ${PRODUCT_NAME}.elf)
# Strip binary for upload
add_custom_target(strip ALL avr-strip ${PRODUCT_NAME}.elf DEPENDS ${PRODUCT_NAME})
# Transform binary into hex file, we ignore the eeprom segments in the step
add_custom_target(hex ALL avr-objcopy -R .eeprom -O ihex ${PRODUCT_NAME}.elf ${PRODUCT_NAME}.hex DEPENDS strip)
# Transform binary into hex file, this is the eeprom part (empty if you don't
# use eeprom static variables)
add_custom_target(eeprom avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex ${PRODUCT_NAME}.elf ${PRODUCT_NAME}.eep DEPENDS strip)
# Upload the firmware with avrdude
add_custom_target(upload avrdude -c ${PROG_TYPE} -P ${PROG_PORT} -p ${MCU} -U flash:w:${PRODUCT_NAME}.hex DEPENDS hex)
# Upload the eeprom with avrdude
add_custom_target(upload_eeprom avrdude -c ${PROG_TYPE} -P ${PROG_PORT} -p ${MCU} -U eeprom:w:${PRODUCT_NAME}.eep DEPENDS eeprom)
# Clean extra files
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PRODUCT_NAME}.hex;${PRODUCT_NAME}.eeprom;${PRODUCT_NAME}.lst")