1
#_______________________________________________________________________________
3
# edam's arduino makefile
4
#_______________________________________________________________________________
7
# Copyright (c) 2011 Tim Marston <tim@ed.am>.
9
# Permission is hereby granted, free of charge, to any person obtaining a copy
10
# of this software and associated documentation files (the "Software"), to deal
11
# in the Software without restriction, including without limitation the rights
12
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
# copies of the Software, and to permit persons to whom the Software is
14
# furnished to do so, subject to the following conditions:
16
# The above copyright notice and this permission notice shall be included in
17
# all copies or substantial portions of the Software.
19
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
#_______________________________________________________________________________
30
# This is a general purpose makefile for use with Arduino hardware and
31
# software. It works with the arduino-1.0 release and requires that software
32
# to be downloaded separately (see http://arduino.cc/). To download the latest
33
# version of this makefile, visit the following website, where you can also
34
# find more information and documentation on it's use. The following text can
35
# only really be considered a reference to it's use.
37
# http://ed.am/dev/make/arduino-mk
39
# This makefile can be used as a drop-in replacement for the Arduino IDE's
40
# build system. To use it, save arduino.mk somewhere (I keep mine at
41
# ~/src/arduino.mk) and create a symlink to it in your project directory named
42
# "Makefile". For example:
44
# $ ln -s ~/src/arduino.mk Makefile
46
# You also need to set up a couple of environment variables. ARDUINODIR should
47
# be set to the path where you unpacked the arduino software from arduino.cc
48
# (it defaults to ~/opt/arduino if unset). You might be best to set this in
49
# your ~/.profile by adding something like this:
51
# export ARDUINODIR=~/somewhere/arduino-1.0
53
# You will also need to set BOARD to the type of arduino you're using. This
54
# can be done when running make (or you could set a default in ~/.profile and
55
# override it as necessary). For example:
60
# You may also need to set SERIALDEV if it is not detected correctly.
62
# The presence of a .ino (or .pde) file causes the arduino.mk to automatically
63
# determine values for SOURCES, TARGET and LIBRARIES. Any .c, .cc and .cpp
64
# files in the project directory (or any "util" or "utility" subdirectories)
65
# are automatically included in the build and are scanned for Arduino libraries
66
# that have been #included. Note, there can only be one .ino (or .pde) file.
68
# Alternatively, if you want to manually specify build variables, create a
69
# Makefile that defines SOURCES and LIBRARIES and then includes arduino.mk.
70
# (There is no need to define TARGET). Here is an example Makefile:
72
# SOURCES := main.cc other.cc
74
# include ~/src/arduino.mk
76
# Here is a complete list of configuration parameters:
78
# ARDUINODIR The path where you have installed/unpacked the arduino software
79
# (from http://arduino.cc/)
81
# ARDUINOCONST The arduino software version, as an integer, used to define the
82
# ARDUINO version constant. This defaults to 100 if unset.
84
# AVRTOOLSPATH A space-separated list of directories to search in order when
85
# lookin for the avr build tools. This defaults to the system PATH
86
# followed by subdirectories in ARDUINODIR if unset.
88
# BOARD Specify a target board type. Run `make boards` to see available
91
# LIBRARIES A list of arduino libraries to build and include. This is set
92
# automatically if a .ino (or .pde) is found.
94
# SERIALDEV The unix device name of the serial device that is the arduino.
95
# If unspecified, an attempt is made to determine the name of a
96
# connected arduino's serial device.
98
# SOURCES A list of all source files of whatever language. The language
99
# type is determined by the file extension. This is set
100
# automatically if a .ino (or .pde) is found.
102
# TARGET The name of the target file. This is set automatically if a
103
# .ino (or .pde) is found, but it is not necessary to set it
106
# This makefile also defines the following goals for use on the command line
109
# all This is the default if no goal is specified. It builds the
110
# target and uploads it.
112
# target Builds the target.
114
# upload Uploads the last built target to an attached arduino.
116
# clean Deletes files created during the build.
118
# boards Display a list of available board names, so that you can set the
119
# BOARD environment variable appropriately.
121
# monitor Start `screen` on the serial device. This is meant to be an
122
# equivalent to the arduino serial monitor.
124
# <file> Builds the specified file, either an object file or the target,
125
# from those that that would be built for the project.
126
#_______________________________________________________________________________
129
# default arduino software directory, check software exists
131
ARDUINODIR := $(wildcard ~/opt/arduino)
133
ifeq ($(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt), )
134
$(error ARDUINODIR is not set correctly; arduino software not found)
137
# default arduino version
140
# default path for avr tools
142
AVRTOOLSPATH := $(subst :, , $(PATH))
143
AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools
144
AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin
148
INOFILE := $(wildcard *.ino *.pde)
150
ifneq ($(words $(INOFILE)), 1)
151
$(error There is more than one .pde or .ino file in this directory!)
154
# automatically determine sources and targeet
155
TARGET := $(basename $(INOFILE))
156
SOURCES := $(INOFILE) \
157
$(wildcard *.c *.cc *.cpp) \
158
$(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
159
$(wildcard $(addprefix utility/, *.c *.cc *.cpp))
161
# automatically determine included libraries
162
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
163
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
164
$(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
168
# no board? oh dear...
170
ifneq "$(MAKECMDGOALS)" "boards"
171
ifneq "$(MAKECMDGOALS)" "clean"
172
$(error BOARD is unset. Type 'make boards' to see possible values)
177
# no serial device? make a poor attempt to detect an arduino
178
ifeq ($(SERIALDEV), )
179
SERIALDEV := $(firstword $(wildcard /dev/ttyACM? /dev/ttyUSB?))
183
findsoftware = $(firstword $(wildcard $(addsuffix /$(1), $(AVRTOOLSPATH))))
184
CC := $(call findsoftware,avr-gcc)
185
CXX := $(call findsoftware,avr-g++)
186
LD := $(call findsoftware,avr-ld)
187
AR := $(call findsoftware,avr-ar)
188
OBJCOPY := $(call findsoftware,avr-objcopy)
189
AVRDUDE := $(call findsoftware,avrdude)
192
TARGET := $(if $(TARGET),$(TARGET),a.out)
193
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
194
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
195
ARDUINOLIBSDIR := $(ARDUINODIR)/libraries
196
ARDUINOLIB := _arduino.a
197
ARDUINOLIBTMP := $(ARDUINOLIB).tmp
198
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
199
$(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
200
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
201
$(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
202
$(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/, *.c *.cpp)) \
203
$(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/utility/, *.c *.cpp)) ))))
204
AVRDUDECONF := $(wildcard $(AVRDUDE).conf)
206
# obtain board parameters from the arduino boards.txt file
207
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
209
$(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
210
BOARD_BUILD_FCPU := \
211
$(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE))
212
BOARD_BUILD_VARIANT := \
213
$(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE))
214
BOARD_UPLOAD_SPEED := \
215
$(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
216
BOARD_UPLOAD_PROTOCOL := \
217
$(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))
220
CPPFLAGS := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
221
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
222
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
223
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
224
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
225
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
226
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
227
CPPFLAGS += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
228
AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV
229
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
230
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
231
LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
233
# figure out which arg to use with stty
234
STTYFARG := $(shell stty --help > /dev/null 2>&1 && echo -F || echo -f)
239
#_______________________________________________________________________________
242
.PHONY: all target upload clean boards monitor
246
target: $(TARGET).hex
249
@echo "\nUploading to board..."
250
@test -n "$(SERIALDEV)" || { \
251
echo "error: SERIALDEV could not be determined automatically." >&2; \
253
stty $(STTYFARG) $(SERIALDEV) hupcl
254
$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
258
rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
259
rm -rf $(ARDUINOLIBTMP)
262
@echo Available values for BOARD:
263
@sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1 \2/;T' \
264
-e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
267
@test -n "$(SERIALDEV)" || { \
268
echo "error: SERIALDEV could not be determined automatically." >&2; \
270
@test -n `which screen` || { \
271
echo "error: can't find GNU screen, you might need to install it." >&2 \
275
# building the target
277
$(TARGET).hex: $(TARGET).elf
278
$(OBJCOPY) -O ihex -R .eeprom $< $@
280
.INTERMEDIATE: $(TARGET).elf
282
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
283
$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
286
$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
289
$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
291
# building the arduino library
293
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
295
rm -rf $(ARDUINOLIBTMP)
297
.INTERMEDIATE: $(ARDUINOLIBOBJS)
299
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
300
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
301
$(COMPILE.c) -o $@ $<
303
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
304
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
305
$(COMPILE.cpp) -o $@ $<
307
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
308
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
309
$(COMPILE.c) -o $@ $<
311
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
312
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
313
$(COMPILE.cpp) -o $@ $<
315
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.c
316
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
317
$(COMPILE.c) -o $@ $<
319
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.cpp
320
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
321
$(COMPILE.cpp) -o $@ $<