86 lines
2.2 KiB
CMake
86 lines
2.2 KiB
CMake
|
# give info about target
|
||
|
SET(CMAKE_SYSTEM_NAME Generic)
|
||
|
SET(CMAKE_SYSTEM_PROCESSOR avr)
|
||
|
|
||
|
# find tools
|
||
|
find_program(AVR_CC avr-gcc)
|
||
|
find_program(AVR_CXX avr-g++)
|
||
|
find_program(AVR_OBJCOPY avr-objcopy)
|
||
|
find_program(AVR_SIZE_TOOL avr-size)
|
||
|
|
||
|
# set compiler (globally)
|
||
|
SET(CMAKE_C_COMPILER ${AVR_CC})
|
||
|
SET(CMAKE_CXX_COMPILER ${AVR_CXX})
|
||
|
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
|
||
|
|
||
|
add_definitions(-mmcu=${AVR_MCU} -DF_CPU=8000000 --std=c99)
|
||
|
|
||
|
function(add_avr_executable EXECUTABLE_NAME)
|
||
|
|
||
|
if(NOT ARGN)
|
||
|
message(FATAL_ERROR "No source files given for ${EXECUTABLE_NAME}.")
|
||
|
endif(NOT ARGN)
|
||
|
|
||
|
# set file names
|
||
|
set(elf_file ${EXECUTABLE_NAME})
|
||
|
set(hex_file ${EXECUTABLE_NAME}.hex)
|
||
|
set(lst_file ${EXECUTABLE_NAME}.lst)
|
||
|
set(map_file ${EXECUTABLE_NAME}.map)
|
||
|
set(eeprom_image ${EXECUTABLE_NAME}_eeprom.hex)
|
||
|
|
||
|
# elf file
|
||
|
add_executable(${elf_file} EXCLUDE_FROM_ALL ${ARGN})
|
||
|
|
||
|
set_target_properties(
|
||
|
${elf_file}
|
||
|
PROPERTIES
|
||
|
COMPILE_FLAGS "-mmcu=${AVR_MCU}"
|
||
|
LINK_FLAGS "-mmcu=${AVR_MCU} -Wl,--gc-sections -mrelax -Wl,-Map,${map_file}"
|
||
|
)
|
||
|
|
||
|
add_custom_command(
|
||
|
OUTPUT ${hex_file}
|
||
|
COMMAND
|
||
|
${AVR_OBJCOPY} -j .text -j .data -O ihex ${elf_file} ${hex_file}
|
||
|
COMMAND
|
||
|
${AVR_SIZE_TOOL} ${AVR_SIZE_ARGS} ${elf_file}
|
||
|
DEPENDS ${elf_file}
|
||
|
)
|
||
|
|
||
|
add_custom_command(
|
||
|
OUTPUT ${lst_file}
|
||
|
COMMAND
|
||
|
${AVR_OBJDUMP} -d ${elf_file} > ${lst_file}
|
||
|
DEPENDS ${elf_file}
|
||
|
)
|
||
|
|
||
|
# eeprom
|
||
|
add_custom_command(
|
||
|
OUTPUT ${eeprom_image}
|
||
|
COMMAND
|
||
|
${AVR_OBJCOPY} -j .eeprom --set-section-flags=.eeprom=alloc,load
|
||
|
--change-section-lma .eeprom=0 --no-change-warnings
|
||
|
-O ihex ${elf_file} ${eeprom_image}
|
||
|
DEPENDS ${elf_file}
|
||
|
)
|
||
|
|
||
|
add_custom_target(
|
||
|
build_${EXECUTABLE_NAME}
|
||
|
ALL
|
||
|
DEPENDS ${hex_file} ${lst_file} ${eeprom_image}
|
||
|
)
|
||
|
|
||
|
set_target_properties(
|
||
|
build_${EXECUTABLE_NAME}
|
||
|
PROPERTIES
|
||
|
OUTPUT_NAME "${elf_file}"
|
||
|
)
|
||
|
|
||
|
# clean
|
||
|
get_directory_property(clean_files ADDITIONAL_MAKE_CLEAN_FILES)
|
||
|
set_directory_properties(
|
||
|
PROPERTIES
|
||
|
ADDITIONAL_MAKE_CLEAN_FILES "${map_file}"
|
||
|
)
|
||
|
endfunction(add_avr_executable)
|