/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: 2012-02-21 15:43:30 UTC
  • Revision ID: tim@ed.am-20120221154330-se8hali569whg99p
typos/spelling

Show diffs side-by-side

added added

removed removed

2
2
#
3
3
#                         edam's Arduino makefile
4
4
#_______________________________________________________________________________
5
 
#                                                                 version 0.5dev
 
5
#                                                                    version 0.3
6
6
#
7
 
# Copyright (C) 2011, 2012 Tim Marston <tim@ed.am>.
 
7
# Copyright (C) 2011, 1012 Tim Marston <tim@ed.am>.
8
8
#
9
9
# Permission is hereby granted, free of charge, to any person obtaining a copy
10
10
# of this software and associated documentation files (the "Software"), to deal
117
117
# when you run make:
118
118
#
119
119
# all          This is the default if no goal is specified.  It builds the
120
 
#              target.
 
120
#              target and uploads it.
121
121
#
122
122
# target       Builds the target.
123
123
#
131
131
# monitor      Start `screen` on the serial device.  This is meant to be an
132
132
#              equivalent to the Arduino serial monitor.
133
133
#
134
 
# size         Displays size information about the bulit target.
135
 
#
136
134
# <file>       Builds the specified file, either an object file or the target,
137
135
#              from those that that would be built for the project.
138
136
#_______________________________________________________________________________
156
154
AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin
157
155
endif
158
156
 
159
 
# default path to find libraries
160
 
LIBRARIESPATH := libraries libs $(ARDUINODIR)/libraries
161
 
 
162
157
# auto mode?
163
158
INOFILE := $(wildcard *.ino *.pde)
164
159
ifdef INOFILE
169
164
# automatically determine sources and targeet
170
165
TARGET := $(basename $(INOFILE))
171
166
SOURCES := $(INOFILE) \
172
 
        $(wildcard *.c *.cc *.cpp *.C) \
173
 
        $(wildcard $(addprefix util/, *.c *.cc *.cpp *.C)) \
174
 
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp *.C))
 
167
        $(wildcard *.c *.cc *.cpp) \
 
168
        $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
 
169
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
175
170
 
176
171
# automatically determine included libraries
177
 
LIBRARIES := $(filter $(notdir $(wildcard $(addsuffix /*, $(LIBRARIESPATH)))), \
 
172
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
 
173
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
178
174
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
179
175
 
180
176
endif
183
179
SERIALDEVGUESS := 0
184
180
ifeq "$(SERIALDEV)" ""
185
181
SERIALDEV := $(firstword $(wildcard \
186
 
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
 
182
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbmodem*))
187
183
SERIALDEVGUESS := 1
188
184
endif
189
185
 
195
191
AR := $(call findsoftware,avr-ar)
196
192
OBJCOPY := $(call findsoftware,avr-objcopy)
197
193
AVRDUDE := $(call findsoftware,avrdude)
198
 
AVRSIZE := $(call findsoftware,avr-size)
199
194
 
200
195
# files
201
196
TARGET := $(if $(TARGET),$(TARGET),a.out)
202
197
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
203
 
DEPFILES := $(patsubst %, .dep/%.dep, $(SOURCES))
204
 
 
205
 
# directories
206
 
LIBRARIESDIRS := $(foreach lib, $(LIBRARIES), \
207
 
        $(firstword $(wildcard $(addsuffix /$(lib), $(LIBRARIESPATH)))))
208
 
LIBRARIESDIRS += $(addsuffix /utility, $(LIBRARIESDIRS))
209
 
ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
210
 
 
211
 
# static arduino lib
212
 
ARDUINOLIB := .lib/arduino.a
213
 
ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(LIBRARIESDIRS), \
214
 
        $(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp))))
215
 
 
216
 
# avrdude confifuration
 
198
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
 
199
ARDUINOLIBSDIR := $(ARDUINODIR)/libraries
 
200
ARDUINOLIB := _arduino.a
 
201
ARDUINOLIBTMP := $(ARDUINOLIB).tmp
 
202
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
203
        $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
 
204
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
 
205
        $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
206
        $(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/, *.c *.cpp)) \
 
207
        $(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/utility/, *.c *.cpp)) ))))
217
208
ifeq "$(AVRDUDECONF)" ""
218
209
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
219
210
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
243
234
        $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
244
235
BOARD_UPLOAD_PROTOCOL := \
245
236
        $(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))
246
 
BOARD_USB_VID := \
247
 
        $(shell sed -ne "s/$(BOARD).build.vid=\(.*\)/\1/p" $(BOARDS_FILE))
248
 
BOARD_USB_PID := \
249
 
        $(shell sed -ne "s/$(BOARD).build.pid=\(.*\)/\1/p" $(BOARDS_FILE))
250
237
 
251
238
# invalid board?
252
239
ifeq "$(BOARD_BUILD_MCU)" ""
258
245
endif
259
246
 
260
247
# flags
261
 
CPPFLAGS += -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
 
248
CPPFLAGS := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
262
249
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
263
250
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
264
251
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
265
 
CPPFLAGS += -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID)
266
 
CPPFLAGS += -I. -Iutil -Iutility -I $(ARDUINOCOREDIR)
267
 
CPPFLAGS += -I $(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
268
 
CPPFLAGS += $(addprefix -I , $(LIBRARIESDIRS))
269
 
CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep
270
 
CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h
271
 
AVRDUDEFLAGS += $(addprefix -C , $(AVRDUDECONF)) -DV
 
252
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
 
253
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
 
254
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
 
255
CPPFLAGS += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
 
256
AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV
272
257
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
273
258
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
274
 
LINKFLAGS += -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
275
 
 
276
 
# figure out which arg to use with stty (for OS X, GNU and busybox stty)
277
 
STTYFARG := $(shell stty --help 2>&1 | \
278
 
        grep -q 'illegal option' && echo -f || echo -F)
279
 
 
280
 
# include dependencies
281
 
ifneq "$(MAKECMDGOALS)" "clean"
282
 
-include $(DEPFILES)
283
 
endif
 
259
LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
 
260
 
 
261
# figure out which arg to use with stty
 
262
STTYFARG := $(shell stty --help > /dev/null 2>&1 && echo -F || echo -f)
284
263
 
285
264
# default rule
286
265
.DEFAULT_GOAL := all
288
267
#_______________________________________________________________________________
289
268
#                                                                          RULES
290
269
 
291
 
.PHONY: all target upload clean boards monitor size
 
270
.PHONY: all target upload clean boards monitor
292
271
 
293
 
all: target
 
272
all: target upload
294
273
 
295
274
target: $(TARGET).hex
296
275
 
297
 
upload: target
 
276
upload:
298
277
        @echo "\nUploading to board..."
299
278
        @test -n "$(SERIALDEV)" || { \
300
279
                echo "error: SERIALDEV could not be determined automatically." >&2; \
308
287
clean:
309
288
        rm -f $(OBJECTS)
310
289
        rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
311
 
        rm -rf .lib .dep
 
290
        rm -rf $(ARDUINOLIBTMP)
312
291
 
313
292
boards:
314
293
        @echo Available values for BOARD:
315
 
        @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \
316
 
                sed -Ee 's/([^.]+)\.name=(.*)/\1            \2/' \
317
 
                        -e 's/(.{12}) *(.*)/\1 \2/'
 
294
        @sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
 
295
                -e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
318
296
 
319
297
monitor:
320
298
        @test -n "$(SERIALDEV)" || { \
328
306
                echo; }
329
307
        screen $(SERIALDEV)
330
308
 
331
 
size: $(TARGET).elf
332
 
        echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf
333
 
 
334
309
# building the target
335
310
 
336
311
$(TARGET).hex: $(TARGET).elf
339
314
.INTERMEDIATE: $(TARGET).elf
340
315
 
341
316
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
342
 
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@
343
 
 
344
 
%.o: %.c
345
 
        mkdir -p .dep/$(dir $<)
346
 
        $(COMPILE.c) $(CPPDEPFLAGS) -o $@ $<
347
 
 
348
 
%.o: %.cpp
349
 
        mkdir -p .dep/$(dir $<)
350
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
351
 
 
352
 
%.o: %.cc
353
 
        mkdir -p .dep/$(dir $<)
354
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
355
 
 
356
 
%.o: %.C
357
 
        mkdir -p .dep/$(dir $<)
358
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
 
317
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
359
318
 
360
319
%.o: %.ino
361
 
        mkdir -p .dep/$(dir $<)
362
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
320
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
363
321
 
364
322
%.o: %.pde
365
 
        mkdir -p .dep/$(dir $<)
366
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
323
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
367
324
 
368
325
# building the arduino library
369
326
 
370
327
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
371
328
        $(AR) rcs $@ $?
372
 
 
373
 
.lib/%.c.o: %.c
374
 
        mkdir -p $(dir $@)
375
 
        $(COMPILE.c) -o $@ $<
376
 
 
377
 
.lib/%.cpp.o: %.cpp
378
 
        mkdir -p $(dir $@)
379
 
        $(COMPILE.cpp) -o $@ $<
380
 
 
381
 
.lib/%.cc.o: %.cc
382
 
        mkdir -p $(dir $@)
383
 
        $(COMPILE.cpp) -o $@ $<
384
 
 
385
 
.lib/%.C.o: %.C
386
 
        mkdir -p $(dir $@)
 
329
        rm -rf $(ARDUINOLIBTMP)
 
330
 
 
331
.INTERMEDIATE: $(ARDUINOLIBOBJS)
 
332
 
 
333
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
 
334
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
335
        $(COMPILE.c) -o $@ $<
 
336
 
 
337
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
 
338
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
339
        $(COMPILE.cpp) -o $@ $<
 
340
 
 
341
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
 
342
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
343
        $(COMPILE.c) -o $@ $<
 
344
 
 
345
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
 
346
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
347
        $(COMPILE.cpp) -o $@ $<
 
348
 
 
349
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.c
 
350
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
351
        $(COMPILE.c) -o $@ $<
 
352
 
 
353
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.cpp
 
354
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
387
355
        $(COMPILE.cpp) -o $@ $<