3
3
# edam's Arduino makefile
4
4
#_______________________________________________________________________________
7
# Copyright (C) 2011, 2012 Tim Marston <tim@ed.am>.
7
# Copyright (C) 2011, 1012 Tim Marston <tim@ed.am>.
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
171
171
$(wildcard $(addprefix utility/, *.c *.cc *.cpp))
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)))
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
234
237
$(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
235
238
BOARD_UPLOAD_PROTOCOL := \
236
239
$(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))
238
$(shell sed -ne "s/$(BOARD).build.vid=\(.*\)/\1/p" $(BOARDS_FILE))
240
$(shell sed -ne "s/$(BOARD).build.pid=\(.*\)/\1/p" $(BOARDS_FILE))
243
242
ifeq "$(BOARD_BUILD_MCU)" ""
253
252
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
254
253
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
255
254
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
255
CPPFLAGS += -I. -Iutil -Iutility -I$(ARDUINOSRCDIR)
256
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
257
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
258
CPPFLAGS += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
263
259
AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV
264
260
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
265
261
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
266
262
LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU)
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)
272
# include dependencies
273
ifneq "$(MAKECMDGOALS)" "clean"
264
# figure out which arg to use with stty
265
STTYFARG := $(shell stty --help > /dev/null 2>&1 && echo -F || echo -f)
278
268
.DEFAULT_GOAL := all
302
292
rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
293
rm -rf $(ARDUINOLIBTMP)
306
296
@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/'
297
@sed -ne '/^#/d;s/^\(.*\).name=\(.*\)/\1 \2/;T' \
298
-e 's/\(.\{12\}\) *\(.*\)/\1 \2/;p' $(BOARDS_FILE)
312
301
@test -n "$(SERIALDEV)" || { \
331
320
.INTERMEDIATE: $(TARGET).elf
333
322
$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
334
$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@
337
mkdir -p .dep/$(dir $<)
338
$(COMPILE.c) $(CPPDEPFLAGS) -o $@ $<
341
mkdir -p .dep/$(dir $<)
342
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
345
mkdir -p .dep/$(dir $<)
346
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
349
mkdir -p .dep/$(dir $<)
350
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $<
323
$(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
353
mkdir -p .dep/$(dir $<)
354
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
326
$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
357
mkdir -p .dep/$(dir $<)
358
$(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $<
329
$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<
360
331
# building the arduino library
362
333
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
367
$(COMPILE.c) -o $@ $<
371
$(COMPILE.cpp) -o $@ $<
375
$(COMPILE.cpp) -o $@ $<
335
rm -rf $(ARDUINOLIBTMP)
337
.INTERMEDIATE: $(ARDUINOLIBOBJS)
339
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
340
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
341
$(COMPILE.c) -o $@ $<
343
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
344
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
345
$(COMPILE.cpp) -o $@ $<
347
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
348
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
349
$(COMPILE.c) -o $@ $<
351
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
352
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
353
$(COMPILE.cpp) -o $@ $<
355
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.c
356
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
357
$(COMPILE.c) -o $@ $<
359
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/utility/%.cpp
360
@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
379
361
$(COMPILE.cpp) -o $@ $<