/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-29 13:52:05 UTC
  • Revision ID: edam@waxworlds.org-20111229135205-ppyojdxqty9sdikj
initial commit

Show diffs side-by-side

added added

removed removed

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.  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
 
#
 
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
42
52
#               BOARD = nano
43
53
#               SERIALDEV = /dev/ttyACM0
44
54
#       include ~/src/arduino.mk
45
55
#
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:
 
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:
 
62
#
 
63
# TARGET       The name of the target file.  This is typically the same name
 
64
#              as the project directory for an arduino project and, if
 
65
#              unspecified, that is used as a default.
 
66
#
 
67
# SOURCES      A list of all source files of whatever language. The language
 
68
#              type is determined by the file extension.
68
69
#
69
70
# BOARD        Specify a target board type.  These are defined in boards.txt,
70
71
#              which came with your arduino installation.  If unspecified,
73
74
# SERIALDEV    The unix device of the device where the arduino can be found.
74
75
#              If unspecified, a default is used.  (See below).
75
76
#
76
 
# The following configuration parameters can be determined automatically:
77
 
#
78
 
# TARGET       The name of the target file.  This is typically the same name
79
 
#              as the project directory for an arduino project and, if
80
 
#              unspecified, that is used as a default.
81
 
#
82
 
# SOURCES      A list of all source files of whatever language. The language
83
 
#              type is determined by the file extension.
84
 
#
85
 
# LIBRARIES    A list of arduino libraries to include
86
 
#
87
77
# This general-purpose makefile also defines the following goals for use on the
88
78
# command line when you run make:
89
79
#
129
119
#   atmega168   Arduino NG or older w/ ATmega168
130
120
#   atmega8     Arduino NG or older w/ ATmega8
131
121
ifndef BOARD
132
 
BOARD := uno
 
122
BOARD := nano
133
123
endif
134
124
 
135
125
# The name of the serial device that the arduino is at. For example,
149
139
endif
150
140
TARGET := $(basename $(INOFILE))
151
141
SOURCES := $(INOFILE) \
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)))
 
142
        $(wildcard *.c *.cc *.cpp $(addprefix utility/, *.c *.cc *.cpp))
159
143
endif
160
144
 
161
145
# files
162
146
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
 
147
ifdef INOFILE
 
148
OBJECT += main.o
 
149
endif
163
150
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
164
151
ARDUINOLIB := _arduino.a
165
152
ARDUINOLIBTMP := _arduino.a.tmp
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))))))
 
153
ARDUINOSOURCES := $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp))
 
154
ARDUINOOBJECTS := $(addsuffix .o, $(addprefix $(ARDUINOLIBTMP)/, $(basename \
 
155
        $(subst $(ARDUINOSRCDIR)/,,$(ARDUINOSOURCES)))))
171
156
 
172
 
# obtain board parameters from the arduino boards.txt file
 
157
# obtain parameters from the arduino boards.txt file
173
158
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
174
159
BOARD_BUILD_MCU := \
175
160
        $(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
188
173
LD := avr-ld
189
174
AR := avr-ar
190
175
OBJCOPY := avr-objcopy
191
 
AVRDUDE := avrdude
192
 
#$(ARDUINODIR)/hardware/tools/avrdude
 
176
AVRDUDE := $(ARDUINODIR)/hardware/tools/avrdude
193
177
 
194
178
# flags
195
179
CPPFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
196
180
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
197
181
CPPFLAGS += -I. -Iutility -I$(ARDUINOSRCDIR)
198
182
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
199
 
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/,$(LIBRARIES))
200
 
AVRDUDEFLAGS = -C $(ARDUINODIR)/hardware/tools/avrdude.conf -DV
 
183
AVRDUDEFLAGS = -V -F -C $(ARDUINODIR)/hardware/tools/avrdude.conf
201
184
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
202
185
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
203
 
LINKFLAGS = -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
204
186
 
205
187
# checks
206
188
ifeq ($(TARGET),)
217
199
 
218
200
all: target upload
219
201
 
220
 
target: $(TARGET).hex
 
202
target: $(TARGET).elf
221
203
 
222
204
upload:
223
205
        stty -F $(SERIALDEV) hupcl
224
 
        $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i
 
206
        $(OBJCOPY) -O ihex -R .eeprom $(TARGET).elf $(TARGET).hex
 
207
        $(AVRDUDE) $(ARVDUDEFLAGS) -U flash:w:$(TARGET).hex
 
208
        rm $(TARGET).hex
225
209
 
226
210
clean:
227
211
        rm -f $(OBJECTS)
230
214
 
231
215
# building the target
232
216
 
233
 
$(TARGET).hex: $(TARGET).elf
234
 
        $(OBJCOPY) -O ihex -R .eeprom $< $@
235
 
 
236
 
.INTERMEDIATE: $(TARGET).elf
237
 
 
238
217
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
239
 
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
 
218
        $(CC) -Os -Wl,--gc-sections $(OBJECTS) $(ARDUINOLIB) -o $@
240
219
 
241
220
%.o: %.ino
242
221
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
246
225
 
247
226
# building the arduino library
248
227
 
249
 
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
 
228
$(ARDUINOLIB): $(ARDUINOOBJECTS)
250
229
        $(AR) rcs $@ $?
251
230
        rm -rf $(ARDUINOLIBTMP)
252
231
 
253
 
.INTERMEDIATE: $(ARDUINOLIBOBJS)
 
232
.INTERMEDIATE: $(ARDUINOOBJECTS)
254
233
 
255
234
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
256
235
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
259
238
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
260
239
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
261
240
        $(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 $@ $<