/make/arduino-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/arduino-mk

« back to all changes in this revision

Viewing changes to arduino.mk

  • Committer: edam
  • Date: 2011-12-30 23:12:46 UTC
  • Revision ID: edam@waxworlds.org-20111230231246-zl8980dsrlfhcugf
added included-library autodetection and updated comments

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#
30
30
# This is a general purpose makefile for use with Arduino (arduino.cc)
31
31
# hardware and software.  It works with the arduino-1.0 release and
32
 
# requires that to be downloaded separately.
33
 
#
34
 
# There are two ways to use this file, an automatic mode and a manual
35
 
# mode.  In automatic mode, you simply copy this makefile to your
36
 
# arduino project directory, rename it "Makefile" and type make.  The
37
 
# project directory is expected to contain an .ino or .pde file, which
38
 
# will automatically be used automatically, along with any other .c,
39
 
# .cc or .cpp files in the project directory and any subdirectory
40
 
# named "utility".  In this way, this makefile should act as a drop-in
41
 
# replacement for the Arduino IDE's build process and can build a
42
 
# project automatically from the files in a project directory.
43
 
#
44
 
# Alternatively, you can manually specify what files should be
45
 
# inclided in a build.  To use this makefile manually, you might be
46
 
# better to keep it somewhere and include it in your project's
47
 
# Makefile after having defined certain parameters that control the
48
 
# build.  As an example, consider the following Makefile:
49
 
#
50
 
#       TARGET = my_program
51
 
#       SOURCES = main.cc foo.cc
 
32
# requires that to be downloaded separately.  It can be downloaded
 
33
# from http://ed.am/dev/make/arduino-makefile where you can also find
 
34
# more information and documentation on it's use.  The following text
 
35
# can only really be considered a reference to it's use.
 
36
#
 
37
# To use this makefile, put a file named "Makefile" in your project
 
38
# directory.  Add all your project's settings to your Makefile and
 
39
# then include this file from it. For example, your Makefile might
 
40
# look something like this:
 
41
#
52
42
#               BOARD = nano
53
43
#               SERIALDEV = /dev/ttyACM0
54
44
#       include ~/src/arduino.mk
55
45
#
56
 
# In both manual and automatic modes, the standard Arduino main.cpp's
57
 
# main() is included, which expects to be able to call init() and
58
 
# loop() in your code.  The main difference is that, in manual mode,
59
 
# these would typically be placed in a .cc or .cpp file.
60
 
#
61
 
# When using manual mode, the following variables can be used:
 
46
# A complete list of all the settings you can use in your Makefile
 
47
# follows shortly. But it should be noted that there are two ways to
 
48
# use this file, an automatic mode and a manual mode.
 
49
#
 
50
# In automatic mode, you simply define a BOARD and SERIALDEV and then
 
51
# include this makefile.  The project directory must contain an .ino
 
52
# or .pde file, which will automatically be used, along with any other
 
53
# .c, .cc or .cpp files in the project directory.  When buliding, any
 
54
# arduino libraries that are #included in your code are detected and
 
55
# automatically linked against.  In this way, this makefile should act
 
56
# as a drop-in replacement for the Arduino IDE's build system.  You
 
57
# should be able to type "make" from the command line and achieve the
 
58
# same as building and uploading from the Arduino IDE.
 
59
#
 
60
# The alternative, manual mode, makes no assumptions.  If the
 
61
# directory doesn't contain an .ino or .pde file, you must also
 
62
# specify the SOURCES, TARGET and LIBRARIES manually in your Makefile.
 
63
# You will also be expected to provide a main() function, for example
 
64
# in main.cc, which may or may not duplicate the functionality of the
 
65
# default main() that calls init() and loop().
 
66
#
 
67
# Here is a list of all configuration parameters:
 
68
#
 
69
# BOARD        Specify a target board type.  These are defined in boards.txt,
 
70
#              which came with your arduino installation.  If unspecified,
 
71
#              a default is used.  (See below).
 
72
#
 
73
# SERIALDEV    The unix device of the device where the arduino can be found.
 
74
#              If unspecified, a default is used.  (See below).
 
75
#
 
76
# The following configuration parameters can be determined automatically:
62
77
#
63
78
# TARGET       The name of the target file.  This is typically the same name
64
79
#              as the project directory for an arduino project and, if
67
82
# SOURCES      A list of all source files of whatever language. The language
68
83
#              type is determined by the file extension.
69
84
#
70
 
# BOARD        Specify a target board type.  These are defined in boards.txt,
71
 
#              which came with your arduino installation.  If unspecified,
72
 
#              a default is used.  (See below).
73
 
#
74
 
# SERIALDEV    The unix device of the device where the arduino can be found.
75
 
#              If unspecified, a default is used.  (See below).
 
85
# LIBRARIES    A list of arduino libraries to include
76
86
#
77
87
# This general-purpose makefile also defines the following goals for use on the
78
88
# command line when you run make:
119
129
#   atmega168   Arduino NG or older w/ ATmega168
120
130
#   atmega8     Arduino NG or older w/ ATmega8
121
131
ifndef BOARD
122
 
BOARD := nano
 
132
BOARD := uno
123
133
endif
124
134
 
125
135
# The name of the serial device that the arduino is at. For example,
139
149
endif
140
150
TARGET := $(basename $(INOFILE))
141
151
SOURCES := $(INOFILE) \
142
 
        $(wildcard *.c *.cc *.cpp $(addprefix utility/, *.c *.cc *.cpp))
 
152
        $(wildcard *.c *.cc *.cpp) \
 
153
        $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
 
154
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
 
155
# automatically determine included libraries
 
156
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
 
157
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
 
158
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
143
159
endif
144
160
 
145
161
# files
146
162
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
147
 
ifdef INOFILE
148
 
OBJECT += main.o
149
 
endif
150
163
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
151
164
ARDUINOLIB := _arduino.a
152
165
ARDUINOLIBTMP := _arduino.a.tmp
153
 
ARDUINOSOURCES := $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp))
154
 
ARDUINOOBJECTS := $(addsuffix .o, $(addprefix $(ARDUINOLIBTMP)/, $(basename \
155
 
        $(subst $(ARDUINOSRCDIR)/,,$(ARDUINOSOURCES)))))
 
166
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
167
        $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
 
168
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
 
169
        $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
170
        $(wildcard $(addprefix $(ARDUINODIR)/libraries/$(lib)/, *.c *.cpp))))))
156
171
 
157
 
# obtain parameters from the arduino boards.txt file
 
172
# obtain board parameters from the arduino boards.txt file
158
173
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
159
174
BOARD_BUILD_MCU := \
160
175
        $(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
173
188
LD := avr-ld
174
189
AR := avr-ar
175
190
OBJCOPY := avr-objcopy
176
 
AVRDUDE := $(ARDUINODIR)/hardware/tools/avrdude
 
191
AVRDUDE := avrdude
 
192
#$(ARDUINODIR)/hardware/tools/avrdude
177
193
 
178
194
# flags
179
195
CPPFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
180
196
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
181
197
CPPFLAGS += -I. -Iutility -I$(ARDUINOSRCDIR)
182
198
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
183
 
AVRDUDEFLAGS = -V -F -C $(ARDUINODIR)/hardware/tools/avrdude.conf
 
199
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/,$(LIBRARIES))
 
200
AVRDUDEFLAGS = -C $(ARDUINODIR)/hardware/tools/avrdude.conf -DV
184
201
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
185
202
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
 
203
LINKFLAGS = -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
186
204
 
187
205
# checks
188
206
ifeq ($(TARGET),)
199
217
 
200
218
all: target upload
201
219
 
202
 
target: $(TARGET).elf
 
220
target: $(TARGET).hex
203
221
 
204
222
upload:
205
223
        stty -F $(SERIALDEV) hupcl
206
 
        $(OBJCOPY) -O ihex -R .eeprom $(TARGET).elf $(TARGET).hex
207
 
        $(AVRDUDE) $(ARVDUDEFLAGS) -U flash:w:$(TARGET).hex
208
 
        rm $(TARGET).hex
 
224
        $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
209
225
 
210
226
clean:
211
227
        rm -f $(OBJECTS)
214
230
 
215
231
# building the target
216
232
 
 
233
$(TARGET).hex: $(TARGET).elf
 
234
        $(OBJCOPY) -O ihex -R .eeprom $< $@
 
235
 
 
236
.INTERMEDIATE: $(TARGET).elf
 
237
 
217
238
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
218
 
        $(CC) -Os -Wl,--gc-sections $(OBJECTS) $(ARDUINOLIB) -o $@
 
239
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
219
240
 
220
241
%.o: %.ino
221
242
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
225
246
 
226
247
# building the arduino library
227
248
 
228
 
$(ARDUINOLIB): $(ARDUINOOBJECTS)
 
249
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
229
250
        $(AR) rcs $@ $?
230
251
        rm -rf $(ARDUINOLIBTMP)
231
252
 
232
 
.INTERMEDIATE: $(ARDUINOOBJECTS)
 
253
.INTERMEDIATE: $(ARDUINOLIBOBJS)
233
254
 
234
255
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
235
256
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
238
259
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
239
260
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
240
261
        $(COMPILE.cpp) -o $@ $<
 
262
 
 
263
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
 
264
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
265
        $(COMPILE.c) -o $@ $<
 
266
 
 
267
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
 
268
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
269
        $(COMPILE.cpp) -o $@ $<