/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-24 11:51:59 UTC
  • Revision ID: tim@ed.am-20120224115159-we23shz3vzsx7dsv
update version no.

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
#
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
#_______________________________________________________________________________
171
169
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
172
170
 
173
171
# automatically determine included libraries
174
 
LIBRARIES := $(filter $(notdir $(wildcard $(ARDUINODIR)/libraries/*)), \
 
172
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
 
173
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
175
174
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
176
175
 
177
176
endif
180
179
SERIALDEVGUESS := 0
181
180
ifeq "$(SERIALDEV)" ""
182
181
SERIALDEV := $(firstword $(wildcard \
183
 
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
 
182
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbmodem*))
184
183
SERIALDEVGUESS := 1
185
184
endif
186
185
 
192
191
AR := $(call findsoftware,avr-ar)
193
192
OBJCOPY := $(call findsoftware,avr-objcopy)
194
193
AVRDUDE := $(call findsoftware,avrdude)
195
 
AVRSIZE := $(call findsoftware,avr-size)
196
194
 
197
195
# files
198
196
TARGET := $(if $(TARGET),$(TARGET),a.out)
199
197
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))))
 
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)) ))))
208
208
ifeq "$(AVRDUDECONF)" ""
209
209
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
210
210
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
234
234
        $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
235
235
BOARD_UPLOAD_PROTOCOL := \
236
236
        $(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))
237
 
BOARD_USB_VID := \
238
 
        $(shell sed -ne "s/$(BOARD).build.vid=\(.*\)/\1/p" $(BOARDS_FILE))
239
 
BOARD_USB_PID := \
240
 
        $(shell sed -ne "s/$(BOARD).build.pid=\(.*\)/\1/p" $(BOARDS_FILE))
241
237
 
242
238
# invalid board?
243
239
ifeq "$(BOARD_BUILD_MCU)" ""
253
249
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
254
250
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
255
251
CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
256
 
CPPFLAGS += -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID)
257
 
CPPFLAGS += -I. -Iutil -Iutility -I $(ARDUINOCOREDIR)
258
 
CPPFLAGS += -I $(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
259
 
CPPFLAGS += $(addprefix -I $(ARDUINODIR)/libraries/, $(LIBRARIES))
260
 
CPPFLAGS += $(patsubst %, -I $(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
261
 
CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep
262
 
CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h
 
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))
263
256
AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV
264
257
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
265
258
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
266
259
LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
267
260
 
268
 
# figure out which arg to use with stty (for OS X, GNU and busybox stty)
269
 
STTYFARG := $(shell stty --help 2>&1 | \
270
 
        grep -q 'illegal option' && echo -f || echo -F)
271
 
 
272
 
# include dependencies
273
 
ifneq "$(MAKECMDGOALS)" "clean"
274
 
-include $(DEPFILES)
275
 
endif
 
261
# figure out which arg to use with stty
 
262
STTYFARG := $(shell stty --help > /dev/null 2>&1 && echo -F || echo -f)
276
263
 
277
264
# default rule
278
265
.DEFAULT_GOAL := all
280
267
#_______________________________________________________________________________
281
268
#                                                                          RULES
282
269
 
283
 
.PHONY: all target upload clean boards monitor size
 
270
.PHONY: all target upload clean boards monitor
284
271
 
285
 
all: target
 
272
all: target upload
286
273
 
287
274
target: $(TARGET).hex
288
275
 
289
 
upload: target
 
276
upload:
290
277
        @echo "\nUploading to board..."
291
278
        @test -n "$(SERIALDEV)" || { \
292
279
                echo "error: SERIALDEV could not be determined automatically." >&2; \
300
287
clean:
301
288
        rm -f $(OBJECTS)
302
289
        rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
303
 
        rm -rf .lib .dep
 
290
        rm -rf $(ARDUINOLIBTMP)
304
291
 
305
292
boards:
306
293
        @echo Available values for BOARD:
307
 
        @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \
308
 
                sed -Ee 's/([^.]+)\.name=(.*)/\1            \2/' \
309
 
                        -e 's/(.{12}) *(.*)/\1 \2/'
 
294
        @sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1            \2/;T' \
 
295
                -e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
310
296
 
311
297
monitor:
312
298
        @test -n "$(SERIALDEV)" || { \
320
306
                echo; }
321
307
        screen $(SERIALDEV)
322
308
 
323
 
size: $(TARGET).elf
324
 
        echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf
325
 
 
326
309
# building the target
327
310
 
328
311
$(TARGET).hex: $(TARGET).elf
331
314
.INTERMEDIATE: $(TARGET).elf
332
315
 
333
316
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
334
 
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@
335
 
 
336
 
%.o: %.c
337
 
        mkdir -p .dep/$(dir $<)
338
 
        $(COMPILE.c) $(CPPDEPFLAGS) -o $@ $<
339
 
 
340
 
%.o: %.cpp
341
 
        mkdir -p .dep/$(dir $<)
342
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
343
 
 
344
 
%.o: %.cc
345
 
        mkdir -p .dep/$(dir $<)
346
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
347
 
 
348
 
%.o: %.C
349
 
        mkdir -p .dep/$(dir $<)
350
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
 
317
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
351
318
 
352
319
%.o: %.ino
353
 
        mkdir -p .dep/$(dir $<)
354
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
320
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
355
321
 
356
322
%.o: %.pde
357
 
        mkdir -p .dep/$(dir $<)
358
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $<
 
323
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
359
324
 
360
325
# building the arduino library
361
326
 
362
327
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
363
328
        $(AR) rcs $@ $?
364
 
 
365
 
.lib/%.c.o: %.c
366
 
        mkdir -p $(dir $@)
367
 
        $(COMPILE.c) -o $@ $<
368
 
 
369
 
.lib/%.cpp.o: %.cpp
370
 
        mkdir -p $(dir $@)
371
 
        $(COMPILE.cpp) -o $@ $<
372
 
 
373
 
.lib/%.cc.o: %.cc
374
 
        mkdir -p $(dir $@)
375
 
        $(COMPILE.cpp) -o $@ $<
376
 
 
377
 
.lib/%.C.o: %.C
378
 
        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)
379
355
        $(COMPILE.cpp) -o $@ $<