/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-03-20 22:14:41 UTC
  • Revision ID: tim@ed.am-20120320221441-1w3uknfaki3u7ohu
build arduino static lib in .lib using full pathnames in object files (so files of the same name but in different locations can co-exist)

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.3dev
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
156
156
AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin
157
157
endif
158
158
 
159
 
# default path to find libraries
160
 
LIBRARIESPATH := libraries libs $(ARDUINODIR)/libraries
161
 
 
162
159
# auto mode?
163
160
INOFILE := $(wildcard *.ino *.pde)
164
161
ifdef INOFILE
169
166
# automatically determine sources and targeet
170
167
TARGET := $(basename $(INOFILE))
171
168
SOURCES := $(INOFILE) \
172
 
        $(wildcard *.c *.cc *.cpp *.C) \
173
 
        $(wildcard $(addprefix util/, *.c *.cc *.cpp *.C)) \
174
 
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp *.C))
 
169
        $(wildcard *.c *.cc *.cpp) \
 
170
        $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
 
171
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
175
172
 
176
173
# automatically determine included libraries
177
 
LIBRARIES := $(filter $(notdir $(wildcard $(addsuffix /*, $(LIBRARIESPATH)))), \
 
174
LIBRARIES := $(filter $(notdir $(wildcard $(ARDUINODIR)/libraries/*)), \
178
175
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
179
176
 
180
177
endif
183
180
SERIALDEVGUESS := 0
184
181
ifeq "$(SERIALDEV)" ""
185
182
SERIALDEV := $(firstword $(wildcard \
186
 
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
 
183
        /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbmodem*))
187
184
SERIALDEVGUESS := 1
188
185
endif
189
186
 
200
197
# files
201
198
TARGET := $(if $(TARGET),$(TARGET),a.out)
202
199
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
200
ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
210
 
 
211
 
# static arduino lib
212
201
ARDUINOLIB := .lib/arduino.a
213
 
ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(LIBRARIESDIRS), \
 
202
ARDUINOLIBLIBSDIR := $(ARDUINODIR)/libraries
 
203
ARDUINOLIBLIBSPATH := $(foreach lib, $(LIBRARIES), \
 
204
        $(ARDUINODIR)/libraries/$(lib)/ $(ARDUINODIR)/libraries/$(lib)/utility/ )
 
205
ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(ARDUINOLIBLIBSPATH), \
214
206
        $(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp))))
215
 
 
216
 
# avrdude confifuration
217
207
ifeq "$(AVRDUDECONF)" ""
218
208
ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude"
219
209
AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf
243
233
        $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
244
234
BOARD_UPLOAD_PROTOCOL := \
245
235
        $(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
236
 
251
237
# invalid board?
252
238
ifeq "$(BOARD_BUILD_MCU)" ""
258
244
endif
259
245
 
260
246
# flags
261
 
CPPFLAGS += -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
262
 
CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
263
 
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU)
264
 
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
 
247
CPPFLAGS_S := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
 
248
CPPFLAGS_S += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
 
249
CPPFLAGS_S += -mmcu=$(BOARD_BUILD_MCU)
 
250
CPPFLAGS_S += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST)
 
251
CPPFLAGS_S += -I. -Iutil -Iutility -I$(ARDUINOCOREDIR)
 
252
CPPFLAGS_S += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
 
253
CPPFLAGS_S += $(addprefix -I$(ARDUINODIR)/libraries/, $(LIBRARIES))
 
254
CPPFLAGS_S += $(patsubst %, -I$(ARDUINODIR)/libraries/%/utility, $(LIBRARIES))
 
255
CPPFLAGS = $(CPPFLAGS_S)
 
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
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 .lib
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)" || { \
339
317
.INTERMEDIATE: $(TARGET).elf
340
318
 
341
319
$(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 $@ $<
 
320
        $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -o $@
 
321
 
 
322
#%.o: %.c
 
323
#       $(COMPILE.c) -o $@ $<
359
324
 
360
325
%.o: %.ino
361
 
        mkdir -p .dep/$(dir $<)
362
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
326
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $<
363
327
 
364
328
%.o: %.pde
365
 
        mkdir -p .dep/$(dir $<)
366
 
        $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $<
 
329
        $(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $<
367
330
 
368
331
# building the arduino library
369
332