-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavr-arduino-toolchain.cmake
353 lines (313 loc) · 14.7 KB
/
avr-arduino-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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# AVR/Arduino CMake toolchain
#
# adds the toolchain and Arduino IDE libs
#
# @author Matthias L. Jugel <[email protected]>
#
# == LICENSE ==
# Copyright 2015 ubirch GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.0)
# find compiler and toolchain programs
find_program(AVRCPP avr-g++)
find_program(AVRC avr-gcc)
find_program(AVRAR avr-ar)
find_program(AVRSTRIP avr-strip)
find_program(OBJCOPY avr-objcopy)
find_program(OBJDUMP avr-objdump)
find_program(AVRSIZE avr-size)
find_program(AVRDUDE avrdude)
find_program(SCREEN screen)
find_program(SIMAVR simavr)
# toolchain settings
#set(CMAKE_SYSTEM_NAME Generic)
#set(CMAKE_CXX_COMPILER "${AVRCPP}")
#set(CMAKE_C_COMPILER "${AVRC}")
#set(CMAKE_AR "${AVRAR}")
#set(CMAKE_ASM_COMPILER "${AVRC}")
# toolchain settings
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CXX_COMPILER /home/mel/git/Arduino/build/linux/work/hardware/tools/avr/bin/avr-g++)
set(CMAKE_C_COMPILER /home/mel/git/Arduino/build/linux/work/hardware/tools/avr/bin/avr-gcc)
set(CMAKE_AR /home/mel/git/Arduino/build/linux/work/hardware/tools/avr/bin/avr-gcc-ar)
set(CMAKE_ASM_COMPILER /home/mel/git/Arduino/build/linux/work/hardware/tools/avr/bin/avr-gcc)
# Important project paths
set(BASE_PATH "${${PROJECT_NAME}_SOURCE_DIR}")
set(SRC_PATH "${BASE_PATH}/src")
set(LIB_PATH "${BASE_PATH}/lib")
# necessary settings for the chip we use
if (NOT DEFINED MCU)
set(MCU atmega2560)
endif ()
if (NOT DEFINED F_CPU)
set(F_CPU 16000000)
endif ()
if (NOT DEFINED BAUD)
set(BAUD 115200)
endif ()
if (NOT DEFINED PROGRAMMER)
set(PROGRAMMER arduino)
endif ()
if (NOT DEFINED MONITOR)
set(MONITOR ${SCREEN})
endif ()
if (NOT DEFINED MONITOR_ARGS)
set(MONITOR_ARGS ${SERIAL_DEV} ${BAUD})
endif ()
if (NOT DEFINED SIMAVR_ARGS)
set(SIMAVR_ARGS --gdb --mcu ${MCU} --freq ${F_CPU})
endif ()
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(CMAKE_C_FLAGS "-std=gnu11 -mcall-prologues -ffunction-sections -fdata-sections -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU} -fdiagnostics-color=always" CACHE STRING "")
set(CMAKE_CXX_FLAGS "-std=c++11 -felide-constructors -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU} -fdiagnostics-color=always" CACHE STRING "")
set(CMAKE_ASM_FLAGS "-x assembler-with-cpp -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU}" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS "-static -Wl,--relax -Wl,--gc-sections -Wl,-u,vfscanf -lscanf_min -Wl,-u,vfprintf -lprintf_min ${EXTRA_LIBS}" CACHE STRING "")
set(CMAKE_C_FLAGS_RELEASE "-std=gnu11 -mcall-prologues -ffunction-sections -fdata-sections -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU} -fdiagnostics-color=always" CACHE STRING "")
set(CMAKE_CXX_FLAGS_RELEASE "-std=c++11 -felide-constructors -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU} -fdiagnostics-color=always" CACHE STRING "")
set(CMAKE_ASM_FLAGS_RELEASE "-x assembler-with-cpp -O3 -Wall -Wno-unknown-pragmas -Wextra -MMD -mmcu=${MCU}" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-static -Wl,--relax -Wl,--gc-sections -Wl,-u,vfscanf -lscanf_min -Wl,-u,vfprintf -lprintf_min ${EXTRA_LIBS}" CACHE STRING "")
# some definitions that are common
add_definitions(-DMCU=${MCU})
add_definitions(-DF_CPU=${F_CPU})
add_definitions(-DBAUD=${BAUD})
add_definitions(-DARDUINO=10806)
add_definitions(-DARDUINO_AVR_MEGA2560)
add_definitions(-DARDUINO_ARCH_AVR)
add_definitions(-mmcu=${MCU})
add_compile_options(-gstabs)
add_compile_options(-funsigned-char)
add_compile_options(-funsigned-bitfields)
add_compile_options(-fpack-struct)
add_compile_options(-fshort-enums)
add_compile_options(-fno-exceptions)
add_compile_options(-ffunction-sections)
add_compile_options(-fdata-sections)
add_compile_options(-Os)
add_compile_options(-Wall)
add_compile_options(-Wno-unknown-pragmas)
add_compile_options(-Wextra)
add_compile_options(-MMD)
add_compile_options(-fdiagnostics-color=always)
add_compile_options(-fuse-linker-plugin)
add_compile_options(-flto)
add_compile_options(-Wl,--gc-sections)
add_compile_options(-std=gnu++11)
add_compile_options(-felide-constructors)
add_compile_options(-fpermissive)
add_compile_options(-fno-threadsafe-statics)
# we need a little function to add multiple targets
function(add_executable_avr NAME)
if (DEFINED MCU_REQUIRED AND (NOT (MCU STREQUAL "${MCU_REQUIRED}")))
message(STATUS "Ignoring target ${NAME} (required MCU '${MCU_REQUIRED}' != '${MCU}')")
else ()
add_executable(${NAME} ${ARGN})
set_target_properties(${NAME} PROPERTIES OUTPUT_NAME "${NAME}.elf")
set_target_properties(${NAME} PROPERTIES LINK_FLAGS " -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections,--relax -mmcu=atmega2560" )
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${NAME}.hex;${NAME}.eep;${NAME}.lst")
# generate the .hex file
add_custom_command(
OUTPUT ${NAME}.hex
COMMAND ${AVRSTRIP} "${NAME}.elf" -o "${NAME}_stripped.elf"
COMMAND ${OBJCOPY} -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "${NAME}_stripped.elf" "${NAME}_stripped.eep"
COMMAND ${OBJCOPY} -O ihex -R .eeprom "${NAME}_stripped.elf" "${NAME}.hex"
COMMAND ${AVRSTRIP} "${NAME}.hex"
COMMAND ${AVRSIZE} --mcu=${MCU} -C --format=avr "${NAME}_stripped.elf"
DEPENDS ${NAME})
add_custom_target(${NAME}-strip ALL DEPENDS ${NAME}.hex)
# add a reset command using avrdude (only if it does not yet exist)
if (NOT TARGET avr-reset)
add_custom_target(avr-reset COMMAND ${AVRDUDE} -c${PROGRAMMER} -p${MCU}
USES_TERMINAL)
endif ()
if (PROGRAMMER STREQUAL "usbasp")
set(AVRDUDE_ARGS -c${PROGRAMMER} -p${MCU} -Pusb)
add_custom_target(
${NAME}-flash
COMMAND ${AVRDUDE} ${AVRDUDE_ARGS} -v -U flash:w:${NAME}.hex
DEPENDS ${NAME}.hex
USES_TERMINAL)
elseif (PROGRAMMER STREQUAL "atmelice_isp")
add_custom_target(
${NAME}-flash
COMMAND avarice -4 -j usb --erase --program --file ${NAME}.hex
DEPENDS ${NAME}.hex
USES_TERMINAL)
else ()
set(AVRDUDE_ARGS -c${PROGRAMMER} -p${MCU} -P${SERIAL_DEV} -b${BAUD})
add_custom_target(
${NAME}-flash
COMMAND ${AVRDUDE} ${AVRDUDE_ARGS} -v -U flash:w:${NAME}.hex
DEPENDS ${NAME}.hex
USES_TERMINAL)
endif ()
# flash the produces binary
add_custom_target(
${NAME}-monitor
COMMAND ${MONITOR} ${MONITOR_ARGS}
USES_TERMINAL)
add_custom_target(
${NAME}-simulate
COMMAND ${SIMAVR} ${SIMAVR_ARGS} ${NAME}.elf
USES_TERMINAL
DEPENDS ${NAME})
endif ()
endfunction(add_executable_avr)
# add all the libraries as possible library dependencies
function(add_libraries LIB_PATH)
set(PROJECT_LIBS)
file(GLOB LIB_DIRS "${LIB_PATH}/*/CMakeLists.txt")
foreach (cmakedir ${LIB_DIRS})
get_filename_component(subdir ${cmakedir} PATH)
if (IS_DIRECTORY ${subdir})
get_filename_component(target ${subdir} NAME)
message(STATUS "Adding ubirch avr library: ${target}")
add_subdirectory(${subdir})
endif ()
endforeach ()
endfunction()
# add targets automatically from the src directory
function(add_sources SRC_PATH)
file(GLOB SRC_DIRS "${SRC_PATH}/*/CMakeLists.txt")
foreach (cmakedir ${SRC_DIRS})
get_filename_component(subdir ${cmakedir} PATH)
if (IS_DIRECTORY ${subdir})
get_filename_component(target ${subdir} NAME)
message(STATUS "Found ubirch avr target: ${target}")
add_subdirectory(${subdir})
endif ()
endforeach ()
endfunction()
# ====================================================================================================================
# ARDUINO SKETCH FUNCTIONALITY
# ====================================================================================================================
# set the arduino sdk path from an environment variable if given
if (DEFINED ENV{ARDUINO_SDK_PATH})
set(ARDUINO_SDK_PATH "$ENV{ARDUINO_SDK_PATH}" CACHE PATH "Arduino SDK Path" FORCE)
endif ()
set(ARDUINO_CORE_LIBS "" CACHE INTERNAL "arduino core libraries")
function(setup_arduino_core)
if (DEFINED ARDUINO_SDK_PATH AND IS_DIRECTORY ${ARDUINO_SDK_PATH} AND NOT (TARGET arduino-core))
# set the paths
set(ARDUINO_CORES_PATH ${ARDUINO_SDK_PATH}/hardware/arduino/avr/cores/arduino CACHE STRING "")
set(ARDUINO_VARIANTS_PATH ${ARDUINO_SDK_PATH}/hardware/arduino/avr/variants/standard CACHE STRING "")
set(ARDUINO_LIBRARIES_PATH ${ARDUINO_SDK_PATH}/hardware/arduino/avr/libraries CACHE STRING "")
# find arduino-core sources
file(GLOB_RECURSE CORE_SOURCES ${ARDUINO_CORES_PATH}/*.S ${ARDUINO_CORES_PATH}/*.c ${ARDUINO_CORES_PATH}/*.cpp)
set(ARDUINO_CORE_SRCS ${CORE_SOURCES} CACHE STRING "Arduino core library sources")
# setup the main arduino core target
# add_library(arduino-core ${ARDUINO_CORE_SRCS})
# target_include_directories(arduino-core PUBLIC ${ARDUINO_CORES_PATH})
# target_include_directories(arduino-core PUBLIC ${ARDUINO_VARIANTS_PATH})
# configure all the additional libraries in the core
file(GLOB CORE_DIRS ${ARDUINO_LIBRARIES_PATH}/*)
foreach (libdir ${CORE_DIRS})
get_filename_component(libname ${libdir} NAME)
if (IS_DIRECTORY ${libdir})
file(GLOB_RECURSE sources ${libdir}/*.cpp ${libdir}/*.S ${libdir}/*.c)
string(REGEX REPLACE "examples?/.*" "" sources "${sources}")
if (sources)
if (NOT TARGET ${libname})
message(STATUS "Adding Arduino Core library: ${libname}")
add_library(${libname} ${sources})
endif ()
target_link_libraries(${libname} arduino-core)
foreach (src ${sources})
get_filename_component(dir ${src} PATH)
target_include_directories(${libname} PUBLIC ${dir})
endforeach ()
list(APPEND ARDUINO_CORE_LIBS ${libname})
set(ARDUINO_CORE_LIBS ${ARDUINO_CORE_LIBS} CACHE INTERNAL "arduino core libraries")
else ()
include_directories(SYSTEM ${libdir})
endif ()
endif ()
endforeach ()
# add some of the default definitions (TODO: identify from Arduino IDE)
add_definitions(-DARDUINO=10605)
add_definitions(-DARDUINO_AVR_UNO)
add_definitions(-DARDUINO_ARCH_AVR)
endif ()
endfunction()
function(add_sketches SKETCHES_PATH)
setup_arduino_core()
if (TARGET arduino-core)
# finally add all the sketches
file(GLOB SRC_DIRS "${SKETCHES_PATH}/*/CMakeLists.txt")
foreach (cmakedir ${SRC_DIRS})
get_filename_component(subdir ${cmakedir} PATH)
if (IS_DIRECTORY ${subdir})
get_filename_component(target ${subdir} NAME)
message(STATUS "Found Arduino sketch: ${target}")
add_subdirectory(${subdir})
endif ()
endforeach ()
else ()
message(WARNING "No Arduino SDK found at '${ARDUINO_SDK_PATH}', can't compile sketches!")
endif ()
endfunction()
set(SKETCH_LIBS "" CACHE INTERNAL "arduino sketch libraries")
# add a sketch dependency by giving the target and a git url
function(target_sketch_library TARGET NAME URL)
setup_arduino_core()
if (TARGET arduino-core AND NOT TARGET ${NAME})
# try to install dependent libraries
find_package(Git)
if (GIT_FOUND)
# clone dependency into libraries
get_filename_component(MYLIBS "${CMAKE_CURRENT_SOURCE_DIR}/../libraries" REALPATH)
if (NOT EXISTS ${MYLIBS})
message(STATUS "creating library directory: ${MYLIBS}")
file(MAKE_DIRECTORY ${MYLIBS})
endif ()
if (NOT EXISTS ${MYLIBS}/${NAME})
message(STATUS "Installing ${NAME} (${URL})")
execute_process(
COMMAND ${GIT_EXECUTABLE} clone "${URL}" "${NAME}"
WORKING_DIRECTORY ${MYLIBS})
endif ()
# now add a library target
file(GLOB_RECURSE libsources FOLLOW_SYMLINKS ${MYLIBS}/${NAME}/*.S ${MYLIBS}/${NAME}/*.c ${MYLIBS}/${NAME}/*.cpp)
# go through list of sources and remove the examples
foreach (file ${libsources})
STRING(REGEX MATCH "/examples?/|/tests?/" example ${file})
if (NOT example)
list(APPEND sources ${file})
endif ()
endforeach ()
if (sources)
message(STATUS "Adding external library: ${NAME}")
add_library(${NAME} ${sources})
target_link_libraries(${NAME} ${ARDUINO_CORE_LIBS} ${SKETCH_LIBS})
list(APPEND SKETCH_LIBS ${NAME})
set(SKETCH_LIBS ${SKETCH_LIBS} CACHE INTERNAL "arduino sketch libraries")
foreach (src ${sources})
get_filename_component(dir ${src} PATH)
list(APPEND includes ${dir})
endforeach ()
list(REMOVE_DUPLICATES includes)
target_include_directories(${NAME} PUBLIC ${includes})
target_link_libraries(${TARGET} ${NAME})
else ()
include_directories(SYSTEM ${MYLIBS}/${NAME})
endif ()
else ()
message(FATAL_ERROR "Missing git, please install and try again!")
endif ()
else ()
target_link_libraries(${TARGET} ${NAME})
endif ()
endfunction(target_sketch_library)