/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-11-28 21:52:52 UTC
  • Revision ID: tim@ed.am-20121128215252-5ml7w5vtfljskurt
added support for third party libraries and fixed support for .C c++ files

Show diffs side-by-side

added added

removed removed

2
2
#
3
3
#                         edam's Arduino makefile
4
4
#_______________________________________________________________________________
5
 
#                                                                    version 0.3
 
5
#                                                                 version 0.5dev
6
6
#
7
 
# Copyright (C) 2011, 1012 Tim Marston <tim@ed.am>.
 
7
# Copyright (C) 2011, 2012 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 and uploads it.
 
120
#              target.
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
#
134
136
# <file>       Builds the specified file, either an object file or the target,
135
137
#              from those that that would be built for the project.
136
138
#_______________________________________________________________________________
154
156
AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin
155
157
endif
156
158
 
 
159
# default path to find libraries
 
160
LIBRARIESPATH := libraries libs $(ARDUINODIR)/libraries
 
161
 
157
162
# auto mode?
158
163
INOFILE := $(wildcard *.ino *.pde)
159
164
ifdef INOFILE
164
169
# automatically determine sources and targeet
165
170
TARGET := $(basename $(INOFILE))
166
171
SOURCES := $(INOFILE) \
167
 
        $(wildcard *.c *.cc *.cpp) \
168
 
        $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
169
 
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
 
172
        $(wildcard *.c *.cc *.cpp *.C) \
 
173
        $(wildcard $(addprefix util/, *.c *.cc *.cpp *.C)) \
 
174
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp *.C))
170
175
 
171
176
# automatically determine included libraries
172
 
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
173
 
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
 
177
LIBRARIES := $(filter $(notdir $(wildcard $(addsuffix /*, $(LIBRARIESPATH)))), \
174
178
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
175
179
 
176
180
endif
179
183
SERIALDEVGUESS := 0
180
184
ifeq "$(SERIALDEV)" ""
181
185
SERIALDEV := $(firstword $(wildcard \
182
 
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbmodem*))
 
186
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
183
187
SERIALDEVGUESS := 1
184
188
endif
185
189
 
191
195
AR := $(call findsoftware,avr-ar)
192
196
OBJCOPY := $(call findsoftware,avr-objcopy)
193
197
AVRDUDE := $(call findsoftware,avrdude)
 
198
AVRSIZE := $(call findsoftware,avr-size)
194
199
 
195
200
# files
196
201
TARGET := $(if $(TARGET),$(TARGET),a.out)
197
202
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
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)) ))))
 
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
208
217
ifeq "$(AVRDUDECONF)" ""
209
218
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
210
219
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
234
243
        $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
235
244
BOARD_UPLOAD_PROTOCOL := \
236
245
        $(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))
237
250
 
238
251
# invalid board?
239
252
ifeq "$(BOARD_BUILD_MCU)" ""
245
258
endif
246
259
 
247
260
# flags
248
 
CPPFLAGS := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
 
261
CPPFLAGS += -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
249
262
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
250
263
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
251
264
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
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
 
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
257
272
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
258
273
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
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)
 
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
263
284
 
264
285
# default rule
265
286
.DEFAULT_GOAL := all
267
288
#_______________________________________________________________________________
268
289
#                                                                          RULES
269
290
 
270
 
.PHONY: all target upload clean boards monitor
 
291
.PHONY: all target upload clean boards monitor size
271
292
 
272
 
all: target upload
 
293
all: target
273
294
 
274
295
target: $(TARGET).hex
275
296
 
276
 
upload:
 
297
upload: target
277
298
        @echo "\nUploading to board..."
278
299
        @test -n "$(SERIALDEV)" || { \
279
300
                echo "error: SERIALDEV could not be determined automatically." >&2; \
287
308
clean:
288
309
        rm -f $(OBJECTS)
289
310
        rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
290
 
        rm -rf $(ARDUINOLIBTMP)
 
311
        rm -rf .lib .dep
291
312
 
292
313
boards:
293
314
        @echo Available values for BOARD:
294
 
        @sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
295
 
                -e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
 
315
        @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \
 
316
                sed -Ee 's/([^.]+)\.name=(.*)/\1            \2/' \
 
317
                        -e 's/(.{12}) *(.*)/\1 \2/'
296
318
 
297
319
monitor:
298
320
        @test -n "$(SERIALDEV)" || { \
306
328
                echo; }
307
329
        screen $(SERIALDEV)
308
330
 
 
331
size: $(TARGET).elf
 
332
        echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf
 
333
 
309
334
# building the target
310
335
 
311
336
$(TARGET).hex: $(TARGET).elf
314
339
.INTERMEDIATE: $(TARGET).elf
315
340
 
316
341
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
317
 
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
 
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 $@ $<
318
359
 
319
360
%.o: %.ino
320
 
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
 
361
        mkdir -p .dep/$(dir $<)
 
362
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
321
363
 
322
364
%.o: %.pde
323
 
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
 
365
        mkdir -p .dep/$(dir $<)
 
366
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
324
367
 
325
368
# building the arduino library
326
369
 
327
370
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
328
371
        $(AR) rcs $@ $?
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)
 
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 $@)
355
387
        $(COMPILE.cpp) -o $@ $<