/make/arduino-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/arduino-mk
1 by edam
initial commit
1
#_______________________________________________________________________________
2
#
3
#                         edam's arduino makefile
4
#_______________________________________________________________________________
5
#                                                                    version 0.1
6
#
7
# Copyright (c) 2011 Tim Marston <tim@ed.am>.
8
#
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:
15
#
16
# The above copyright notice and this permission notice shall be included in
17
# all copies or substantial portions of the Software.
18
#
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,
10 by edam
formatting, and changed default for ARDUINODIR
24
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
# SOFTWARE.
1 by edam
initial commit
26
#
27
#_______________________________________________________________________________
28
#
29
#
7 by edam
fixed comments for publishing
30
# This is a general purpose makefile for use with Arduino hardware and
10 by edam
formatting, and changed default for ARDUINODIR
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.
7 by edam
fixed comments for publishing
36
#
37
#   http://ed.am/dev/make/arduino-makefile
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
38
#
10 by edam
formatting, and changed default for ARDUINODIR
39
# To install, it is suggested that you keep arduino.mk somewhere and either
40
# symlink to it or include it in your make files.  I keep mine at
41
# ~/src/arduino.mk.  You will need to alter the value of the ARDUINODIR
42
# variable (below) to be the path where you have unpacked the arduino software
43
# from arduino.cc.
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
44
#
10 by edam
formatting, and changed default for ARDUINODIR
45
# This makefile can be used as a drop-in replacement for the Arduino IDE's
46
# build system.  Simply create a symlink to it under the name "Makefile" and
47
# run make (remembering to specify an ARDUINODIR and BOARD, as described
48
# below).  You would create the symlink like like so:
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
49
#
7 by edam
fixed comments for publishing
50
#   $ ln -s ~/src/arduino.mk Makefile
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
51
#
10 by edam
formatting, and changed default for ARDUINODIR
52
# The presence of a .ino or .pde file causes the arduino.mk to atuomatically
53
# determine va;ues for SOURCES, TARGET and LIBRARIES.  Any .c, .cc and .cpp
54
# files in the project directory (or a "util" or "utility" subdirectory) are
55
# automatically included in the build and are scanned for libraries that have
56
# been #included.
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
57
#
10 by edam
formatting, and changed default for ARDUINODIR
58
# Alternatively, if you want to manually specify build variables, create a
59
# Makefile that defines SOURCES and LIBRARARIES and then includes arduino.mk.
60
# (There is no need to define TARGET).  You will also be expected to provide a
61
# main() function, for example in main.cc, which may or may not duplicate the
62
# functionality of the default main() that calls init() and loop().  Here is an
63
# example Makefile:
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
64
#
7 by edam
fixed comments for publishing
65
#   SOURCES := main.cc foo.cc
66
#   LIBRARIES := EEPROM
67
#   include ~/src/arduino.mk
1 by edam
initial commit
68
#
10 by edam
formatting, and changed default for ARDUINODIR
69
# A complete list of all the settings you can use in your Makefile follows
70
# shortly.  It should be noted, however, that some variables are better
71
# specified in the environment (or on the command line) than in your Makefile.
72
# Specifically, the ARDUINODIR, BOARD and, if it is not automatically detected,
73
# SERIALDEV.
74
#
75
# When running make, you might want to specify the board and path to your
76
# installation of the arduino software, like this:
77
#
78
#   $ export ARDUINODIR=~/opt/arduino
7 by edam
fixed comments for publishing
79
#   $ export BOARD=pro5v
80
#   $ make
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
81
#
82
# For a list of available board types, run `make boards`.
83
# 
8 by edam
broke out ARDUINODIR, so it is not configured in the makefile
84
# Here is a complete list of configuration parameters:
85
#
10 by edam
formatting, and changed default for ARDUINODIR
86
# ARDUINODIR   The path where you have installed/unpacked the arduino software
87
#              (from http://arduino.cc/)
4 by edam
added included-library autodetection and updated comments
88
#
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
89
# BOARD        Specify a target board type.
4 by edam
added included-library autodetection and updated comments
90
#
10 by edam
formatting, and changed default for ARDUINODIR
91
# SERIALDEV    The unix device of the device where the arduino can be found.
92
#              If unspecified, an attempt is made to determine the name of a
93
#              connected arduino's serial device.
94
#
95
# TARGET       The name of the target file.  This is set automatically if a
96
#              .ino or .pde is found, but it is not neccesary to set it
97
#              otherwise.
98
#
99
# SOURCES      A list of all source files of whatever language.  The language
100
#              type is determined by the file extension.  This is set
101
#              automatically if a .ino or .pde is found.
102
#
103
# LIBRARIES    A list of arduino libraries to build and include.  This is set
104
#              automatically if a .ino or .pde is found.
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
105
#
106
# This general-purpose makefile also defines the following goals for
107
# use on the command line when you run make:
108
#
10 by edam
formatting, and changed default for ARDUINODIR
109
# all          This is the default if no goal is specified. It builds the
110
#              target and uploads it.
1 by edam
initial commit
111
#
112
# target       Builds the target of your Makefile.
113
#
114
# upload       Uploads the target to an attached arduino.
115
#
116
# clean        Deletes temporary files.
117
#
10 by edam
formatting, and changed default for ARDUINODIR
118
# boards       Display a list of available board names, so that you can set the
119
#              BOARD environment variable appropriately.
120
#
121
# monitor      Start `screen` on the serial device.  It is ment to be an
122
#              equivelant to the arduino serial monitor.
123
#
124
# <file>       Builds the specified file, either an object file or the target,
125
#              from those that that would be built for the project.
1 by edam
initial commit
126
#_______________________________________________________________________________
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
127
#
1 by edam
initial commit
128
129
# The full path to the arduino software, from arduino.cc
8 by edam
broke out ARDUINODIR, so it is not configured in the makefile
130
ifndef ARDUINODIR
10 by edam
formatting, and changed default for ARDUINODIR
131
ARDUINODIR := $(wildcard ~/opt/arduino)
8 by edam
broke out ARDUINODIR, so it is not configured in the makefile
132
endif
1 by edam
initial commit
133
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
134
# check arduino software
135
ifeq ($(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt), )
8 by edam
broke out ARDUINODIR, so it is not configured in the makefile
136
$(error ARDUINODIR is not set correctly; arduino software not found)
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
137
endif
1 by edam
initial commit
138
139
# auto mode?
140
INOFILE := $(wildcard *.ino *.pde)
141
ifdef INOFILE
142
ifneq ($(words $(INOFILE)), 1)
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
143
$(error There is more than one .pde or .ino file in this directory!)
1 by edam
initial commit
144
endif
145
TARGET := $(basename $(INOFILE))
146
SOURCES := $(INOFILE) \
4 by edam
added included-library autodetection and updated comments
147
	$(wildcard *.c *.cc *.cpp) \
148
	$(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
149
	$(wildcard $(addprefix utility/, *.c *.cc *.cpp))
150
# automatically determine included libraries
151
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
152
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
153
	$(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
1 by edam
initial commit
154
endif
155
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
156
# no target? use default
157
ifndef TARGET
158
TARGET := a.out
159
endif
160
161
# no serial device? attempt to detect an arduino
162
ifndef SERIALDEV
163
SERIALDEV := $(firstword $(wildcard /dev/ttyACM? /dev/ttyUSB?))
164
endif
165
166
# no board? oh dear...
167
ifndef BOARD
168
ifneq "$(MAKECMDGOALS)" "boards"
169
ifneq "$(MAKECMDGOALS)" "clean"
170
$(error BOARD is unset.  Type 'make boards' to see possible values)
171
endif
172
endif
173
endif
174
1 by edam
initial commit
175
# files
176
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
177
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
178
ARDUINOLIB := _arduino.a
179
ARDUINOLIBTMP := _arduino.a.tmp
4 by edam
added included-library autodetection and updated comments
180
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
181
	$(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
182
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
183
	$(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
184
	$(wildcard $(addprefix $(ARDUINODIR)/libraries/$(lib)/, *.c *.cpp))))))
1 by edam
initial commit
185
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
186
# obtain board parameters from the arduino boards.txt file
1 by edam
initial commit
187
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
188
BOARD_BUILD_MCU := \
189
	$(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
190
BOARD_BUILD_FCPU := \
191
	$(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE))
192
BOARD_BUILD_VARIANT := \
193
	$(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE))
194
BOARD_UPLOAD_SPEED := \
195
	$(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
196
BOARD_UPLOAD_PROTOCOL := \
197
	$(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))
198
199
# software
200
CC := avr-gcc
201
CXX := avr-g++
202
LD := avr-ld
203
AR := avr-ar
204
OBJCOPY := avr-objcopy
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
205
AVRDUDE := avrdude
1 by edam
initial commit
206
207
# flags
208
CPPFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
209
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
210
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
1 by edam
initial commit
211
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
9 by edam
added "monitor" goal; documented boards and monitor goals
212
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
213
CPPFLAGS += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
214
AVRDUDEFLAGS = -C $(ARDUINODIR)/hardware/tools/avrdude.conf -DV
1 by edam
initial commit
215
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
216
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
217
LINKFLAGS = -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
1 by edam
initial commit
218
219
# default rule
220
.DEFAULT_GOAL := all
221
222
#_______________________________________________________________________________
223
#                                                                          RULES
224
9 by edam
added "monitor" goal; documented boards and monitor goals
225
.PHONY:	all target upload clean boards monitor
1 by edam
initial commit
226
227
all: target upload
228
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
229
target: $(TARGET).hex
1 by edam
initial commit
230
231
upload:
232
	stty -F $(SERIALDEV) hupcl
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
233
	$(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
1 by edam
initial commit
234
235
clean:
236
	rm -f $(OBJECTS)
237
	rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
238
	rm -rf $(ARDUINOLIBTMP)
239
5 by edam
updated comments; added checks for missing or misconfigured variables; use a default TARGET; attempt to autodetect SERIALDEV; added 'boards' goal; fixed use of 'util' project subdirectory.
240
boards:
241
	@echo Available values for BOARD:
242
	@sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
243
		-e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
244
9 by edam
added "monitor" goal; documented boards and monitor goals
245
monitor:
246
	screen $(SERIALDEV)
247
1 by edam
initial commit
248
# building the target
249
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
250
$(TARGET).hex: $(TARGET).elf
251
	$(OBJCOPY) -O ihex -R .eeprom $< $@
252
253
.INTERMEDIATE: $(TARGET).elf
254
1 by edam
initial commit
255
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
2 by edam
fixed build (missing library), tested upload, and made the .elf file an intermediate file (the .hex file is now the target)
256
	$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
1 by edam
initial commit
257
258
%.o: %.ino
259
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
260
261
%.o: %.pde
262
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
263
264
# building the arduino library
265
4 by edam
added included-library autodetection and updated comments
266
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
1 by edam
initial commit
267
	$(AR) rcs $@ $?
268
	rm -rf $(ARDUINOLIBTMP)
269
4 by edam
added included-library autodetection and updated comments
270
.INTERMEDIATE: $(ARDUINOLIBOBJS)
1 by edam
initial commit
271
272
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
273
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
274
	$(COMPILE.c) -o $@ $<
275
276
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
277
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
278
	$(COMPILE.cpp) -o $@ $<
4 by edam
added included-library autodetection and updated comments
279
280
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
281
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
282
	$(COMPILE.c) -o $@ $<
283
284
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
285
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
286
	$(COMPILE.cpp) -o $@ $<