/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: Tim Marston
  • Date: 2012-03-12 18:05:15 UTC
  • Revision ID: tim@ed.am-20120312180515-33vfg8omarjitsm1
added 'size' goal

Show diffs side-by-side

added added

removed removed

2
2
#
3
3
#                         edam's Arduino makefile
4
4
#_______________________________________________________________________________
5
 
#                                                                 version 0.4dev
 
5
#                                                                 version 0.3dev
6
6
#
7
7
# Copyright (C) 2011, 1012 Tim Marston <tim@ed.am>.
8
8
#
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
#
171
171
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
172
172
 
173
173
# automatically determine included libraries
174
 
LIBRARIES := $(filter $(notdir $(wildcard $(ARDUINODIR)/libraries/*)), \
 
174
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
 
175
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
175
176
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
176
177
 
177
178
endif
180
181
SERIALDEVGUESS := 0
181
182
ifeq "$(SERIALDEV)" ""
182
183
SERIALDEV := $(firstword $(wildcard \
183
 
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
 
184
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbmodem*))
184
185
SERIALDEVGUESS := 1
185
186
endif
186
187
 
197
198
# files
198
199
TARGET := $(if $(TARGET),$(TARGET),a.out)
199
200
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
200
 
DEPFILES := $(patsubst %, .dep/%.dep, $(SOURCES))
201
 
ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
202
 
ARDUINOLIB := .lib/arduino.a
203
 
ARDUINOLIBLIBSDIR := $(ARDUINODIR)/libraries
204
 
ARDUINOLIBLIBSPATH := $(foreach lib, $(LIBRARIES), \
205
 
        $(ARDUINODIR)/libraries/$(lib)/ $(ARDUINODIR)/libraries/$(lib)/utility/ )
206
 
ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(ARDUINOLIBLIBSPATH), \
207
 
        $(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp))))
 
201
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
 
202
ARDUINOLIBSDIR := $(ARDUINODIR)/libraries
 
203
ARDUINOLIB := _arduino.a
 
204
ARDUINOLIBTMP := $(ARDUINOLIB).tmp
 
205
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
206
        $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
 
207
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
 
208
        $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
209
        $(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/, *.c *.cpp)) \
 
210
        $(wildcard $(addprefix $(ARDUINOLIBSDIR)/$(lib)/utility/, *.c *.cpp)) ))))
208
211
ifeq "$(AVRDUDECONF)" ""
209
212
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
210
213
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
249
252
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
250
253
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
251
254
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
252
 
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOCOREDIR)
 
255
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
253
256
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
254
257
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
255
258
CPPFLAGS += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
256
 
CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep
257
 
CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h
258
259
AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV
259
260
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
260
261
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
261
262
LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
262
263
 
263
 
# figure out which arg to use with stty (for OS X, GNU and busybox stty)
264
 
STTYFARG := $(shell stty --help 2>&1 | \
265
 
        grep -q 'illegal option' && echo -f || echo -F)
266
 
 
267
 
# include dependencies
268
 
ifneq "$(MAKECMDGOALS)" "clean"
269
 
-include $(DEPFILES)
270
 
endif
 
264
# figure out which arg to use with stty
 
265
STTYFARG := $(shell stty --help > /dev/null 2>&1 && echo -F || echo -f)
271
266
 
272
267
# default rule
273
268
.DEFAULT_GOAL := all
277
272
 
278
273
.PHONY: all target upload clean boards monitor size
279
274
 
280
 
all: target
 
275
all: target upload
281
276
 
282
277
target: $(TARGET).hex
283
278
 
284
 
upload: target
 
279
upload:
285
280
        @echo "\nUploading to board..."
286
281
        @test -n "$(SERIALDEV)" || { \
287
282
                echo "error: SERIALDEV could not be determined automatically." >&2; \
295
290
clean:
296
291
        rm -f $(OBJECTS)
297
292
        rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
298
 
        rm -rf .lib .dep
 
293
        rm -rf $(ARDUINOLIBTMP)
299
294
 
300
295
boards:
301
296
        @echo Available values for BOARD:
302
 
        @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \
303
 
                sed -Ee 's/([^.]+)\.name=(.*)/\1            \2/' \
304
 
                        -e 's/(.{12}) *(.*)/\1 \2/'
 
297
        @sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
 
298
                -e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
305
299
 
306
300
monitor:
307
301
        @test -n "$(SERIALDEV)" || { \
328
322
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
329
323
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
330
324
 
331
 
%.o: %.c
332
 
        mkdir -p .dep/$(dir $<)
333
 
        $(COMPILE.c) $(CPPDEPFLAGS) -o $@ $<
334
 
 
335
 
%.o: %.cpp
336
 
        mkdir -p .dep/$(dir $<)
337
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
338
 
 
339
 
%.o: %.cc
340
 
        mkdir -p .dep/$(dir $<)
341
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
342
 
 
343
 
%.o: %.C
344
 
        mkdir -p .dep/$(dir $<)
345
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
346
 
 
347
325
%.o: %.ino
348
 
        mkdir -p .dep/$(dir $<)
349
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
326
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
350
327
 
351
328
%.o: %.pde
352
 
        mkdir -p .dep/$(dir $<)
353
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $<
 
329
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
354
330
 
355
331
# building the arduino library
356
332
 
357
333
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
358
334
        $(AR) rcs $@ $?
359
 
 
360
 
.lib/%.c.o: %.c
361
 
        mkdir -p $(dir $@)
362
 
        $(COMPILE.c) -o $@ $<
363
 
 
364
 
.lib/%.cpp.o: %.cpp
365
 
        mkdir -p $(dir $@)
366
 
        $(COMPILE.cpp) -o $@ $<
367
 
 
368
 
.lib/%.cc.o: %.cc
369
 
        mkdir -p $(dir $@)
370
 
        $(COMPILE.cpp) -o $@ $<
371
 
 
372
 
.lib/%.C.o: %.C
373
 
        mkdir -p $(dir $@)
 
335
        rm -rf $(ARDUINOLIBTMP)
 
336
 
 
337
.INTERMEDIATE: $(ARDUINOLIBOBJS)
 
338
 
 
339
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
 
340
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
341
        $(COMPILE.c) -o $@ $<
 
342
 
 
343
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
 
344
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
345
        $(COMPILE.cpp) -o $@ $<
 
346
 
 
347
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
 
348
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
349
        $(COMPILE.c) -o $@ $<
 
350
 
 
351
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
 
352
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
353
        $(COMPILE.cpp) -o $@ $<
 
354
 
 
355
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.c
 
356
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
357
        $(COMPILE.c) -o $@ $<
 
358
 
 
359
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.cpp
 
360
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
374
361
        $(COMPILE.cpp) -o $@ $<