/make/arduino-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/arduino-mk
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
#_______________________________________________________________________________
#
#                         edam's arduino makefile
#_______________________________________________________________________________
#                                                                    version 0.1
#
# Copyright (c) 2011 Tim Marston <tim@ed.am>.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#_______________________________________________________________________________
#
#
# This is a general purpose makefile for use with Arduino hardware and
# software.  It works with the arduino-1.0 release and requires that
# software to be downloaded separately (see http://arduino.cc/).  To
# download the latest version of this makefile, visit the following
# website, where you can also find more information and documentation
# on it's use.  The following text can only really be considered a
# reference to it's use.
#
#   http://ed.am/dev/make/arduino-makefile
#
# To install, it is suggested that you keep arduino.mk somewhere and
# either symlink to it or include it in your make files.  I keep mine
# at ~/src/arduino.mk.  You will need to alter the value of the
# ARDUINODIR variable (below) to be the path where you have unpacked
# the arduino software from arduino.cc.
#
# This makefile can be used as a drop-in replacement for the Arduino
# IDE's build system.  Simply create a symlink to it under the name
# "Makefile" and run make as described below (remembering to specify a
# BOARD).  You would create the symlink like like so:
#
#   $ ln -s ~/src/arduino.mk Makefile
#
# The presence of a .ino or .pde file causes the arduino.mk to
# atuomatically determine va;ues for SOURCES, TARGET and LIBRARIES.
# Any .c, .cc and .cpp files in the project directory (or a "util" or
# "utility" subdirectory) are automatically included in the build and
# are scanned for libraries that have been #included.
#
# Alternatively, if you want to manually specify build variables,
# create a Makefile that defines SOURCES and LIBRARARIES and then
# includes arduino.mk.  (There is no need to define TARGET).  You will
# also be expected to provide a main() function, for example in
# main.cc, which may or may not duplicate the functionality of the
# default main() that calls init() and loop().  Here is an example
# Makefile:
#
#   SOURCES := main.cc foo.cc
#   LIBRARIES := EEPROM
#   include ~/src/arduino.mk
#
# A complete list of all the settings you can use in your Makefile
# follows shortly.  It should be noted, however, that some variables
# are better specified in the environment or on the command line than
# in a Makefile.  Specifically, the BOARD and SERIALDEV (if it is not
# automatically detected).
#
# When running make, you might want to specify the board type:
#
#   $ BOARD=pro5v make
#
# Or in the environment:
#
#   $ export BOARD=pro5v
#   $ make
#
# For a list of available board types, run `make boards`.
# 
# Here is a list of all configuration parameters:
#
# BOARD        Specify a target board type.
#
# SERIALDEV    The unix device of the device where the arduino can be
#              found.  If unspecified, an attempt is made to determine
#              the name of a connected arduino's serial device.
#
# The following configuration parameters can be determined automatically:
#
# TARGET       The name of the target file.  This need not be set if it
#              is not determined automatically.
#
# SOURCES      A list of all source files of whatever language.  The
#              language type is determined by the file extension.
#
# LIBRARIES    A list of arduino libraries to build and include.
#
# This general-purpose makefile also defines the following goals for
# use on the command line when you run make:
#
# all          This is the default if no goal is specified. It builds
#              the target and uploads it.
#
# target       Builds the target of your Makefile.
#
# upload       Uploads the target to an attached arduino.
#
# clean        Deletes temporary files.
#
# <file>       Builds the specified file, either an object file or the
#              target, from those that that would be built for the
#              project.
#
#_______________________________________________________________________________
#

# The full path to the arduino software, from arduino.cc
ARDUINODIR := $(wildcard ~/opt/arduino-1.0)

# check arduino software
ifeq ($(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt), )
$(error ARDUINODIR is not set correctly at the top of arduino.mk)
endif

# auto mode?
INOFILE := $(wildcard *.ino *.pde)
ifdef INOFILE
ifneq ($(words $(INOFILE)), 1)
$(error There is more than one .pde or .ino file in this directory!)
endif
TARGET := $(basename $(INOFILE))
SOURCES := $(INOFILE) \
	$(wildcard *.c *.cc *.cpp) \
	$(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
	$(wildcard $(addprefix utility/, *.c *.cc *.cpp))
# automatically determine included libraries
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
	$(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
endif

# no target? use default
ifndef TARGET
TARGET := a.out
endif

# no serial device? attempt to detect an arduino
ifndef SERIALDEV
SERIALDEV := $(firstword $(wildcard /dev/ttyACM? /dev/ttyUSB?))
endif

# no board? oh dear...
ifndef BOARD
ifneq "$(MAKECMDGOALS)" "boards"
ifneq "$(MAKECMDGOALS)" "clean"
$(error BOARD is unset.  Type 'make boards' to see possible values)
endif
endif
endif

# files
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
ARDUINOLIB := _arduino.a
ARDUINOLIBTMP := _arduino.a.tmp
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
	$(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
	$(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
	$(wildcard $(addprefix $(ARDUINODIR)/libraries/$(lib)/, *.c *.cpp))))))

# obtain board parameters from the arduino boards.txt file
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
BOARD_BUILD_MCU := \
	$(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_BUILD_FCPU := \
	$(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_BUILD_VARIANT := \
	$(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_UPLOAD_SPEED := \
	$(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_UPLOAD_PROTOCOL := \
	$(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))

# software
CC := avr-gcc
CXX := avr-g++
LD := avr-ld
AR := avr-ar
OBJCOPY := avr-objcopy
AVRDUDE := avrdude

# flags
CPPFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/,$(LIBRARIES))
AVRDUDEFLAGS = -C $(ARDUINODIR)/hardware/tools/avrdude.conf -DV
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
LINKFLAGS = -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)

# default rule
.DEFAULT_GOAL := all

#_______________________________________________________________________________
#                                                                          RULES

.PHONY:	all target upload clean boards

all: target upload

target: $(TARGET).hex

upload:
	stty -F $(SERIALDEV) hupcl
	$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i

clean:
	rm -f $(OBJECTS)
	rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
	rm -rf $(ARDUINOLIBTMP)

boards:
	@echo Available values for BOARD:
	@sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
		-e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)

# building the target

$(TARGET).hex: $(TARGET).elf
	$(OBJCOPY) -O ihex -R .eeprom $< $@

.INTERMEDIATE: $(TARGET).elf

$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
	$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@

%.o: %.ino
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<

%.o: %.pde
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<

# building the arduino library

$(ARDUINOLIB): $(ARDUINOLIBOBJS)
	$(AR) rcs $@ $?
	rm -rf $(ARDUINOLIBTMP)

.INTERMEDIATE: $(ARDUINOLIBOBJS)

$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.c) -o $@ $<

$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.cpp) -o $@ $<

$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.c) -o $@ $<

$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.cpp) -o $@ $<