-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolchain.cmake
176 lines (148 loc) · 4.71 KB
/
toolchain.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# find the toolchain root directory
INCLUDE(CMakeForceCompiler)
set(CMAKE_SYSTEM_NAME Generic)
# resolve board -> mcu if board from HAL
include(${CMAKE_CURRENT_LIST_DIR}/hal/boards/board.cmake)
# resolve mcu -> build variables
include(${CMAKE_CURRENT_LIST_DIR}/hal/mcu/mcu.cmake)
find_program(CC NAMES avr-gcc)
find_program(CXX NAMES avr-g++)
find_program(CMAKE_GCOV NAMES avr-gcov)
find_program(CMAKE_AR NAMES avr-gcc-ar)
find_program(CMAKE_RANLIB NAMES avr-gcc-ranlib)
find_program(CMAKE_OBJCOPY NAMES avr-objcopy)
find_program(CMAKE_OBJDUMP NAMES avr-objdump)
find_program(CMAKE_GCC_SIZE NAMES avr-size)
find_program(GDB NAMES avr-gdb-py)
find_program(CMAKE_MAKE_PROGRAM NAMES make)
find_program(AVRDUDE NAMES avrdude)
find_program(PICOCOM NAMES picocom)
find_program(SIMAVR NAMES simavr)
CMAKE_FORCE_C_COMPILER(${CC} GNU)
CMAKE_FORCE_CXX_COMPILER(${CXX} GNU)
message(STATUS "Using HAL from ${CMAKE_CURRENT_LIST_DIR}")
message(STATUS "Using C compiler from ${CMAKE_C_COMPILER}")
message(STATUS "Simavr: ${SIMAVR}")
message(STATUS "PicoCOM: ${PICOCOM}")
set(AVR_LINKER_LIBS "-lc -lm -lgcc")
set (CWARN "-Wall -Wstrict-prototypes -Wextra -Werror")
set (CXXWARN "-Wall -Wextra -Werror")
set (CTUNING "-Os -fomit-frame-pointer -ffunction-sections -fdata-sections -flto")
set (CWORKAROUNDS "-Wno-format")
set (CMCU "-mmcu=${GCC_TARGET} -DF_CPU=${F_CPU}L")
set (CMAKE_C_FLAGS "-std=gnu11 ${CMCU} ${CWARN} ${CTUNING} ${CWORKAROUNDS}" CACHE STRING "" FORCE)
set (CMAKE_CXX_FLAGS "-std=gnu++1y ${CMCU} -fno-exceptions ${CXXWARN} ${CTUNING} ${CWORKAROUNDS}" CACHE STRING "" FORCE)
set (CMAKE_CXX_FLAGS_DEBUG "-g" CACHE STRING "" FORCE)
set (CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG" CACHE STRING "" FORCE)
set (TERMINAL_PORT "" CACHE STRING "Port for terminal operations")
set (TERMINAL_BAUD "" CACHE STRING "Baudrate for terminal operations")
set (TERMINAL_PICOCOM_OPTIONS -l --imap lfcrlf --omap crlf CACHE STRING "Options for picocom")
macro(add_hal_executable target_name)
set(target ${target_name})
set(elf_file ${target_name}.elf)
set(map_file ${target_name}.map)
set(hex_file ${target_name}.hex)
set(eep_file ${target_name}.eep)
set(lst_file ${target_name}.lst)
# create elf file
add_executable(${target}
${ARGN}
)
target_link_libraries(${target} hal)
set_target_properties(${target} PROPERTIES OUTPUT_NAME ${elf_file})
set_target_properties(
${target}
PROPERTIES
LINK_FLAGS "-mmcu=${GCC_TARGET} -Wl,-Map,${map_file} -Wl,-u,vfprintf -lprintf_flt -lm ${AVR_LINKER_LIBS} -flto"
)
# generate the lst file
add_custom_command(
OUTPUT ${lst_file}
COMMAND
${CMAKE_OBJDUMP} -h -S ${elf_file} > ${lst_file}
DEPENDS ${target}
)
# create hex file
add_custom_command(
OUTPUT ${hex_file}
COMMAND
${CMAKE_OBJCOPY} -j .text -j .data -O ihex ${elf_file} ${hex_file}
DEPENDS ${target}
)
# create eep file
add_custom_command(
OUTPUT ${eep_file}
COMMAND
${CMAKE_OBJCOPY} -j .eeprom --no-change-warnings --change-section-lma .eeprom=0 -O ihex ${elf_file} ${eep_file}
DEPENDS ${target}
)
add_custom_command(
OUTPUT "print-size-${target}"
COMMAND
${CMAKE_GCC_SIZE} ${elf_file}
DEPENDS ${target}
)
# build the intel hex file for the device
add_custom_target(
${target_name}.build
ALL
DEPENDS ${hex_file} ${lst_file} ${eep_file} "print-size-${target}"
)
set_target_properties(
${target_name}.build
PROPERTIES
OUTPUT_NAME ${target}
)
# flash command
if(LOAD_EEPROM)
add_custom_command(
OUTPUT ${hex_file}.flash
COMMAND
${AVRDUDE} -c${AVRDUDE_TOOL} -p${AVRDUDE_TARGET} ${AVRDUDE_OPTIONS} -U flash:w:${hex_file} -U eeprom:w:${eep_file}
)
else()
add_custom_command(
OUTPUT ${hex_file}.flash
COMMAND
${AVRDUDE} -c${AVRDUDE_TOOL} -p${AVRDUDE_TARGET} ${AVRDUDE_OPTIONS} -U flash:w:${hex_file}
)
endif()
add_custom_target(
${target_name}.flash
DEPENDS ${target_name}.build ${hex_file}.flash
)
add_custom_target(${target_name}.sim
COMMAND ${SIMAVR} -m ${SIMAVR_TARGET} -f ${F_CPU} ${elf_file} -v -v -v
DEPENDS ${target_name}.build
)
add_custom_command(
OUTPUT "${target_name}.term_"
COMMAND
${PICOCOM} ${TERMINAL_PORT} -b${TERMINAL_BAUD} ${TERMINAL_PICOCOM_OPTIONS}
)
# terminal handling
add_custom_command(
OUTPUT "${target_name}.term_"
COMMAND
${PICOCOM} ${TERMINAL_PORT} -b${TERMINAL_BAUD} ${TERMINAL_PICOCOM_OPTIONS}
)
add_custom_target(
"${target_name}.term"
DEPENDS "${target_name}.term_"
)
add_custom_target(
"${target_name}.flash_and_term"
DEPENDS "${target_name}.build" "${hex_file}.flash" "${target_name}.term_"
)
endmacro(add_hal_executable)
if(NOT TARGET term)
add_custom_command(
OUTPUT "term_"
COMMAND
${PICOCOM} ${TERMINAL_PORT} -b${TERMINAL_BAUD} ${TERMINAL_PICOCOM_OPTIONS}
)
add_custom_target(
"term"
DEPENDS "term_"
)
endif(NOT TARGET term)