Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
#
2
# Copyright (c) 2010 Michael Smith. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
6
# are met:
7
# 1. Redistributions of source code must retain the above copyright
8
#	  notice, this list of conditions and the following disclaimer.
9
# 2. Redistributions in binary form must reproduce the above copyright
10
#	  notice, this list of conditions and the following disclaimer in the
11
#	  documentation and/or other materials provided with the distribution.
12
#
13
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
# ARE DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
# SUCH DAMAGE.
24
#
25
 
26
#
27
# Build an Arduino sketch.
28
#
29
 
30
################################################################################
31
# Paths
32
#
33
 
34
#
35
# Save the system type for later use.
36
#
37
SYSTYPE			:=	$(shell uname)
38
 
39
# force LANG to C so awk works sanely on MacOS
40
export LANG=C
41
 
42
#
43
# Locate the sketch sources based on the initial Makefile's path
44
#
45
SRCROOT			:=	$(realpath $(dir $(firstword $(MAKEFILE_LIST))))
46
ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
47
  # Workaround a $(realpath ) bug on cygwin
48
  ifeq ($(SRCROOT),)
49
    SRCROOT	:=	$(shell cygpath -m ${CURDIR})
50
    $(warning your realpath function is not working)
51
    $(warning > setting SRCROOT to $(SRCROOT))
52
  endif
53
  # Correct the directory backslashes on cygwin
54
  ARDUINO		:=	$(subst \,/,$(ARDUINO))
55
endif
56
 
57
#
58
# We need to know the location of the sketchbook.  If it hasn't been overridden,
59
# try the parent of the current directory.  If there is no libraries directory
60
# there, assume that we are in a library's examples directory and try backing up
61
# further.
62
#
63
ifeq ($(SKETCHBOOK),)
64
  SKETCHBOOK		:=	$(shell cd $(SRCROOT)/.. && pwd)
65
  ifeq ($(wildcard $(SKETCHBOOK)/libraries),)
66
    SKETCHBOOK		:=	$(shell cd $(SRCROOT)/../.. && pwd)
67
  endif
68
  ifeq ($(wildcard $(SKETCHBOOK)/libraries),)
69
    SKETCHBOOK		:=	$(shell cd $(SRCROOT)/../../.. && pwd)
70
  endif
71
  ifeq ($(wildcard $(SKETCHBOOK)/libraries),)
72
    SKETCHBOOK		:=	$(shell cd $(SRCROOT)/../../../.. && pwd)
73
  endif
74
  ifeq ($(wildcard $(SKETCHBOOK)/libraries),)
75
    $(error ERROR: cannot determine sketchbook location - please specify on the commandline with SKETCHBOOK=<path>)
76
  endif
77
else
78
  ifeq ($(wildcard $(SKETCHBOOK)/libraries),)
79
    $(warning WARNING: sketchbook directory $(SKETCHBOOK) contains no libraries)
80
  endif
81
endif
82
ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
83
	# Convert cygwin path into a windows normal path
84
    SKETCHBOOK	:=	$(shell cygpath -d ${SKETCHBOOK})
85
    SKETCHBOOK	:=	$(subst \,/,$(SKETCHBOOK))
86
endif
87
 
88
#
89
# Work out the sketch name from the name of the source directory.
90
#
91
SKETCH			:=	$(lastword $(subst /, ,$(SRCROOT)))
92
# Workaround a $(lastword ) bug on cygwin
93
ifeq ($(SKETCH),)
94
  WORDLIST		:=	$(subst /, ,$(SRCROOT))
95
  SKETCH		:=	$(word $(words $(WORDLIST)),$(WORDLIST))
96
endif
97
 
98
#
99
# Work out where we are going to be building things
100
#
101
TMPDIR			?=	/tmp
102
BUILDROOT		:=	$(abspath $(TMPDIR)/$(SKETCH).build)
103
ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
104
  # Workaround a $(abspath ) bug on cygwin
105
  ifeq ($(BUILDROOT),)
106
    BUILDROOT	:=	C:$(TMPDIR)/$(SKETCH).build
107
    $(warning your abspath function is not working)
108
    $(warning > setting BUILDROOT to $(BUILDROOT))
109
  endif
110
endif
111
 
112
# Jump over the next makefile sections when runing a "make configure"
113
ifneq ($(MAKECMDGOALS),configure)
114
 
115
################################################################################
116
# Config options
117
#
118
# The Makefile calling us must specify BOARD
119
#
120
include $(SKETCHBOOK)/config.mk
121
ifeq ($(PORT),)
122
$(error ERROR: could not locate $(SKETCHBOOK)/config.mk, please run 'make configure' first)
123
endif
124
 
125
HARDWARE		?=	arduino
126
ifeq ($(BOARD),)
127
$(error ERROR: must set BOARD before including this file)
128
endif
129
 
130
#
131
# Find Arduino, if not explicitly specified
132
#
133
ifeq ($(ARDUINO),)
134
 
135
  #
136
  # List locations that might be valid ARDUINO settings
137
  #
138
  ifeq ($(SYSTYPE),Darwin)
139
    # use Spotlight to find Arduino.app
140
    ARDUINO_QUERY	=	'kMDItemKind == Application && kMDItemFSName == Arduino.app'
141
    ARDUINOS		:=	$(addsuffix /Contents/Resources/Java,$(shell mdfind -literal $(ARDUINO_QUERY)))
142
    ifeq ($(ARDUINOS),)
143
      $(error ERROR: Spotlight cannot find Arduino on your system.)
144
    endif
145
  endif
146
 
147
  ifeq ($(SYSTYPE),Linux)
148
    ARDUINO_SEARCHPATH	=	/usr/share/arduino* /usr/local/share/arduino*
149
    ARDUINOS		:=	$(wildcard $(ARDUINO_SEARCHPATH))
150
  endif
151
 
152
  ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
153
	# Most of the following commands are simply to deal with whitespaces in the path
154
	# Read the "Program Files" system directory from the windows registry
155
	PROGRAM_FILES		:=	$(shell cat /proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/ProgramFilesDir)
156
	# Convert the path delimiters to /
157
	PROGRAM_FILES		:=	$(shell cygpath -m ${PROGRAM_FILES})
158
	# Escape the space with a backslash
159
	PROGRAM_FILES		:=	$(shell echo $(PROGRAM_FILES) | sed s/\ /\\\\\ / )
160
	# Use DOS paths because they do not contain spaces
161
	PROGRAM_FILES		:=	$(shell cygpath -d ${PROGRAM_FILES})
162
	# Convert the path delimiters to /
163
	PROGRAM_FILES	:=	$(subst \,/,$(PROGRAM_FILES))
164
	# Search for an Arduino instalation in a couple of paths
165
	ARDUINO_SEARCHPATH	:=	c:/arduino* $(PROGRAM_FILES)/arduino*
166
    ARDUINOS		:=	$(wildcard $(ARDUINO_SEARCHPATH))
167
  endif
168
 
169
  #
170
  # Pick the first option if more than one candidate is found.
171
  #
172
  ARDUINO		:=	$(firstword $(ARDUINOS))
173
  ifeq ($(ARDUINO),)
174
    $(error ERROR: Cannot find Arduino on this system, please specify on the commandline with ARDUINO=<path> or on the config.mk file)
175
  endif
176
 
177
  ifneq ($(words $(ARDUINOS)),1)
178
    $(warning WARNING: More than one copy of Arduino was found, using $(ARDUINO))
179
  endif
180
 
181
endif
182
 
183
################################################################################
184
# Tools
185
#
186
 
187
#
188
# Decide where we are going to look for tools
189
#
190
ifeq ($(SYSTYPE),Darwin)
191
  # use the tools that come with Arduino
192
  TOOLPATH		:=	$(ARDUINOS)/hardware/tools/avr/bin
193
  # use BWK awk
194
  AWK			=	awk
195
endif
196
ifeq ($(SYSTYPE),Linux)
197
  # expect that tools are on the path
198
  TOOLPATH		:=	$(subst :, ,$(PATH))
199
endif
200
ifeq ($(findstring CYGWIN, $(SYSTYPE)),CYGWIN)
201
  TOOLPATH		:=	$(ARDUINO)/hardware/tools/avr/bin
202
endif
203
 
204
ifeq ($(findstring CYGWIN, $(SYSTYPE)),)
205
FIND_TOOL		=	$(firstword $(wildcard $(addsuffix /$(1),$(TOOLPATH))))
206
else
207
FIND_TOOL		=	$(firstword $(wildcard $(addsuffix /$(1).exe,$(TOOLPATH))))
208
endif
209
CXX			:=	$(call FIND_TOOL,avr-g++)
210
CC			:=	$(call FIND_TOOL,avr-gcc)
211
AS			:=	$(call FIND_TOOL,avr-gcc)
212
AR			:=	$(call FIND_TOOL,avr-ar)
213
LD			:=	$(call FIND_TOOL,avr-gcc)
214
GDB			:=	$(call FIND_TOOL,avr-gdb)
215
AVRDUDE		:=	$(call FIND_TOOL,avrdude)
216
AVARICE		:=	$(call FIND_TOOL,avarice)
217
OBJCOPY			:=	$(call FIND_TOOL,avr-objcopy)
218
ifeq ($(CXX),)
219
$(error ERROR: cannot find the compiler tools anywhere on the path $(TOOLPATH))
220
endif
221
 
222
# Find awk
223
AWK			?=	gawk
224
ifeq ($(shell which $(AWK)),)
225
$(error ERROR: cannot find $(AWK) - you may need to install GNU awk)
226
endif
227
 
228
#
229
# Tool options
230
#
231
DEFINES			=	-DF_CPU=$(F_CPU) -DARDUINO=$(ARDUINO_VERS) $(EXTRAFLAGS)
232
OPTFLAGS		=	-Os -Wformat -Wall -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wformat=2 -Wno-reorder
233
DEPFLAGS		=	-MD -MT $@
234
 
235
# XXX warning options TBD
236
CXXOPTS			= 	-mcall-prologues -ffunction-sections -fdata-sections -fno-exceptions
237
COPTS			=	-mcall-prologues -ffunction-sections -fdata-sections
238
ASOPTS			=	-assembler-with-cpp
239
LISTOPTS		=	-adhlns=$(@:.o=.lst)
240
 
241
CXXFLAGS		=	-g -mmcu=$(MCU) $(DEFINES) -Wa,$(LISTOPTS) $(OPTFLAGS) $(DEPFLAGS) $(CXXOPTS)
242
CFLAGS			=	-g -mmcu=$(MCU) $(DEFINES) -Wa,$(LISTOPTS) $(OPTFLAGS) $(DEPFLAGS) $(COPTS)
243
ASFLAGS			=	-g -mmcu=$(MCU) $(DEFINES)     $(LISTOPTS) $(DEPFLAGS) $(ASOPTS)
244
LDFLAGS			=	-g -mmcu=$(MCU) $(OPTFLAGS) -Wl,--relax,--gc-sections -Wl,-Map -Wl,$(SKETCHMAP)
245
 
246
ifeq ($(BOARD),mega)
247
  LDFLAGS		=	-g -mmcu=$(MCU) $(OPTFLAGS) -Wl,--gc-sections -Wl,-Map -Wl,$(SKETCHMAP)
248
endif
249
 
250
 
251
 
252
LIBS			=	-lm
253
 
254
SRCSUFFIXES		=	*.cpp *.c *.S
255
 
256
ifeq ($(VERBOSE),)
257
v			=	@
258
else
259
v			=
260
endif
261
 
262
 
263
################################################################################
264
# Sketch
265
#
266
 
267
# Sketch source files
268
SKETCHPDESRCS		:=	$(wildcard $(SRCROOT)/*.pde $(SRCROOT)/*.ino)
269
SKETCHSRCS		:=	$(wildcard $(addprefix $(SRCROOT)/,$(SRCSUFFIXES)))
270
SKETCHPDE		:=	$(wildcard $(SRCROOT)/$(SKETCH).pde $(SRCROOT)/$(SKETCH).ino)
271
SKETCHCPP		:=	$(BUILDROOT)/$(SKETCH).cpp
272
ifneq ($(words $(SKETCHPDE)),1)
273
$(error ERROR: sketch $(SKETCH) must contain exactly one of $(SKETCH).pde or $(SKETCH).ino)
274
endif
275
 
276
# Sketch object files
277
SKETCHOBJS		:=	$(subst $(SRCROOT),$(BUILDROOT),$(SKETCHSRCS)) $(SKETCHCPP)
278
SKETCHOBJS		:=	$(addsuffix .o,$(basename $(SKETCHOBJS)))
279
 
280
# List of input files to the sketch.cpp file in the order they should
281
# be appended to create it
282
SKETCHCPP_SRC		:=	$(SKETCHPDE) $(sort $(filter-out $(SKETCHPDE),$(SKETCHPDESRCS)))
283
 
284
################################################################################
285
# Libraries
286
#
287
# Pick libraries to add to the include path and to link with based on
288
# #include directives in the sketchfiles.
289
#
290
# For example:
291
#
292
#   #include <Foo.h>
293
#
294
# implies that there might be a Foo library.
295
#
296
# Note that the # and $ require special treatment to avoid upsetting
297
# make.
298
#
299
SEXPR			=	's/^[[:space:]]*\#include[[:space:]][<\"]([^>\"./]+).*$$/\1/p'
300
ifeq ($(SYSTYPE),Darwin)
301
  LIBTOKENS        :=    $(sort $(shell cat $(SKETCHPDESRCS) $(SKETCHSRCS) | sed -nEe $(SEXPR)))
302
else
303
  LIBTOKENS        :=    $(sort $(shell cat $(SKETCHPDESRCS) $(SKETCHSRCS) | sed -nre $(SEXPR)))
304
endif
305
 
306
#
307
# Find sketchbook libraries referenced by the sketch.
308
#
309
# Include paths for sketch libraries
310
#
311
SKETCHLIBS		:=	$(wildcard $(addprefix $(SKETCHBOOK)/libraries/,$(LIBTOKENS)))
312
SKETCHLIBNAMES		:=	$(notdir $(SKETCHLIBS))
313
SKETCHLIBSRCDIRS	:=	$(SKETCHLIBS) $(addsuffix /utility,$(SKETCHLIBS))
314
SKETCHLIBSRCS		:=	$(wildcard $(foreach suffix,$(SRCSUFFIXES),$(addsuffix /$(suffix),$(SKETCHLIBSRCDIRS))))
315
SKETCHLIBOBJS		:=	$(addsuffix .o,$(basename $(subst $(SKETCHBOOK),$(BUILDROOT),$(SKETCHLIBSRCS))))
316
SKETCHLIBINCLUDES	:=	$(addprefix -I,$(SKETCHLIBS))
317
 
318
#
319
# Find Arduino libraries referenced by the sketch.  Exclude any that
320
# are overloaded by versions in the sketchbook.
321
#
322
ARDUINOLIBS		:=	$(wildcard $(addprefix $(ARDUINO)/libraries/,$(filter-out $(SKETCHLIBNAMES),$(LIBTOKENS))))
323
ARDUINOLIBNAMES		:=	$(notdir $(ARDUINOLIBS))
324
ARDUINOLIBSRCDIRS	:=	$(ARDUINOLIBS) $(addsuffix /utility,$(ARDUINOLIBS))
325
ARDUINOLIBSRCS		:=	$(wildcard $(foreach suffix,$(SRCSUFFIXES),$(addsuffix /$(suffix),$(ARDUINOLIBSRCDIRS))))
326
ARDUINOLIBOBJS		:=	$(addsuffix .o,$(basename $(subst $(ARDUINO),$(BUILDROOT),$(ARDUINOLIBSRCS))))
327
ARDUINOLIBINCLUDES	:=	$(addprefix -I,$(ARDUINOLIBS))
328
 
329
# Library object files
330
LIBOBJS			:=	$(SKETCHLIBOBJS) $(ARDUINOLIBOBJS)
331
 
332
################################################################################
333
# *duino core
334
#
335
 
336
# Pull the Arduino version
337
ARDUINO_VERS	:=	$(shell $(SKETCHBOOK)/Tools/scripts/arduino_version.sh $(ARDUINO))
338
 
339
# Find the hardware directory to use
340
HARDWARE_DIR		:=	$(firstword $(wildcard $(SKETCHBOOK)/hardware/$(HARDWARE) \
341
							$(ARDUINO)/hardware/$(HARDWARE)))
342
ifeq ($(HARDWARE_DIR),)
343
$(error ERROR: hardware directory for $(HARDWARE) not found)
344
endif
345
 
346
# Find the boards.txt that we are using
347
BOARDFILE		:=	$(wildcard $(HARDWARE_DIR)/boards.txt)
348
ifeq ($(BOARDFILE),)
349
$(error ERROR: could not locate boards.txt for hardware $(HARDWARE))
350
endif
351
 
352
# Extract needed build parameters from the boardfile
353
MCU			:=	$(shell grep $(BOARD).build.mcu $(BOARDFILE) | cut -d = -f 2)
354
F_CPU			:=	$(shell grep $(BOARD).build.f_cpu $(BOARDFILE) | cut -d = -f 2)
355
HARDWARE_CORE		:=	$(shell grep $(BOARD).build.core $(BOARDFILE) | cut -d = -f 2)
356
UPLOAD_SPEED		:=	$(shell grep $(BOARD).upload.speed $(BOARDFILE) | cut -d = -f 2)
357
 
358
ifeq ($(UPLOAD_PROTOCOL),)
359
  UPLOAD_PROTOCOL	:=	$(shell grep $(BOARD).upload.protocol $(BOARDFILE) | cut -d = -f 2)
360
endif
361
 
362
# Adding override for mega since boards.txt uses stk500 instead of
363
# arduino on 22 release
364
ifeq ($(BOARD),mega)
365
  UPLOAD_PROTOCOL	:=	arduino
366
endif
367
 
368
ifeq ($(MCU),)
369
$(error ERROR: Could not locate board $(BOARD) in $(BOARDFILE))
370
endif
371
 
372
# Hardware source files
373
CORESRC_DIR		=	$(HARDWARE_DIR)/cores/$(HARDWARE_CORE)
374
CORESRC_PATTERNS	=	$(foreach suffix,/*.cpp /*.c /*.S,$(addsuffix $(suffix),$(CORESRC_DIR)))
375
CORESRCS		:=	$(wildcard $(CORESRC_PATTERNS))
376
 
377
# Include spec for core includes
378
COREINCLUDES		=	-I$(CORESRC_DIR) -I$(HARDWARE_DIR)/variants/mega
379
 
380
# Hardware object files
381
CORELIBOBJS		:=	$(subst $(CORESRC_DIR),$(BUILDROOT)/$(HARDWARE),$(CORESRCS))
382
CORELIBOBJS		:=	$(addsuffix .o,$(basename $(CORELIBOBJS)))
383
 
384
################################################################################
385
# Built products
386
#
387
 
388
# The ELF file
389
SKETCHELF		=	$(BUILDROOT)/$(SKETCH).elf
390
 
391
# HEX file
392
SKETCHHEX		=	$(BUILDROOT)/$(SKETCH).hex
393
 
394
# EEP file
395
SKETCHEEP		=	$(BUILDROOT)/$(SKETCH).eep
396
 
397
# Map file
398
SKETCHMAP		=	$(BUILDROOT)/$(SKETCH).map
399
 
400
# The core library
401
CORELIB			=	$(BUILDROOT)/$(HARDWARE)/core.a
402
 
403
# All of the objects that may be built
404
ALLOBJS			=	$(SKETCHOBJS) $(LIBOBJS) $(CORELIBOBJS)
405
 
406
# All of the dependency files that may be generated
407
ALLDEPS			=	$(ALLOBJS:%.o=%.d)
408
endif
409
 
410
################################################################################
411
# Targets
412
#
413
 
414
all:	$(SKETCHELF) $(SKETCHEEP) $(SKETCHHEX)
415
 
416
.PHONY: upload
417
upload: $(SKETCHHEX)
418
	$(AVRDUDE) -c $(UPLOAD_PROTOCOL) -p $(MCU) -P $(PORT) -b$(UPLOAD_SPEED) -U flash:w:$(SKETCHHEX):i
419
 
420
configure:
421
	$(warning WARNING - A $(SKETCHBOOK)/config.mk file has been written)
422
	$(warning Please edit the file to match your system configuration, if you use a different board or port)
423
	@echo \# Select \'mega\' for the original APM, or \'mega2560\' for the V2 APM. > $(SKETCHBOOK)/config.mk
424
	@echo BOARD=mega2560     >> $(SKETCHBOOK)/config.mk
425
	@echo \# The communication port used to communicate with the APM. >> $(SKETCHBOOK)/config.mk
426
ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
427
	@echo PORT=com3 >> $(SKETCHBOOK)/config.mk
428
else
429
	@echo PORT=/dev/ttyUSB0 >> $(SKETCHBOOK)/config.mk
430
endif
431
 
432
debug:
433
	$(AVARICE) --mkII --capture --jtag usb :4242 & \
434
	gnome-terminal -x $(GDB) $(SKETCHELF) & \
435
	echo -e '\n\nat the gdb prompt type "target remote localhost:4242"'
436
 
437
# this allows you to flash your image via JTAG for when you
438
# have completely broken your USB
439
jtag-program:
440
	$(AVARICE) --mkII --jtag usb --erase --program --file $(SKETCHELF)
441
 
442
clean:
443
ifneq ($(findstring CYGWIN, $(SYSTYPE)),)
444
	@del /S $(BUILDROOT)
445
else
446
	@rm -fr $(BUILDROOT)
447
endif
448
 
449
################################################################################
450
# Rules
451
#
452
 
453
# fetch dependency info from a previous build if any of it exists
454
-include $(ALLDEPS)
455
 
456
# common header for rules, prints what is being built
457
define RULEHDR
458
	@echo %% $(subst $(BUILDROOT)/,,$@)
459
	@mkdir -p $(dir $@)
460
endef
461
 
462
# Link the final object
463
$(SKETCHELF):	$(SKETCHOBJS) $(LIBOBJS) $(CORELIB)
464
	$(RULEHDR)
465
	$(v)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
466
 
467
# Create the hex file
468
$(SKETCHHEX):	$(SKETCHELF)
469
	$(RULEHDR)
470
	$(v)$(OBJCOPY) -O ihex -R .eeprom $< $@
471
 
472
# Create the eep file
473
$(SKETCHEEP):	$(SKETCHELF)
474
	$(RULEHDR)
475
	$(v)$(OBJCOPY) -O ihex -j.eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $< $@
476
 
477
#
478
# Build sketch objects
479
#
480
SKETCH_INCLUDES	=	$(SKETCHLIBINCLUDES) $(ARDUINOLIBINCLUDES) $(COREINCLUDES)
481
 
482
$(BUILDROOT)/%.o: $(BUILDROOT)/%.cpp
483
	$(RULEHDR)
484
	$(v)$(CXX) $(CXXFLAGS) -c -o $@ $< -I$(SRCROOT) $(SKETCH_INCLUDES)
485
 
486
$(BUILDROOT)/%.o: $(SRCROOT)/%.cpp
487
	$(RULEHDR)
488
	$(v)$(CXX) $(CXXFLAGS) -c -o $@ $< $(SKETCH_INCLUDES)
489
 
490
$(BUILDROOT)/%.o: $(SRCROOT)/%.c
491
	$(RULEHDR)
492
	$(v)$(CC) $(CFLAGS) -c -o $@ $< $(SKETCH_INCLUDES)
493
 
494
$(BUILDROOT)/%.o: $(SRCROOT)/%.S
495
	$(RULEHDR)
496
	$(v)$(AS) $(ASFLAGS) -c -o $@ $< $(SKETCH_INCLUDES)
497
 
498
#
499
# Build library objects from sources in the sketchbook
500
#
501
SLIB_INCLUDES	=	-I$(dir $<)/utility $(SKETCHLIBINCLUDES) $(ARDUINOLIBINCLUDES) $(COREINCLUDES)
502
 
503
$(BUILDROOT)/libraries/%.o: $(SKETCHBOOK)/libraries/%.cpp
504
	$(RULEHDR)
505
	$(v)$(CXX) $(CXXFLAGS) -c -o $@ $< $(SLIB_INCLUDES)
506
 
507
$(BUILDROOT)/libraries/%.o: $(SKETCHBOOK)/libraries/%.c
508
	$(RULEHDR)
509
	$(v)$(CC) $(CFLAGS) -c -o $@ $< $(SLIB_INCLUDES)
510
 
511
$(BUILDROOT)/libraries/%.o: $(SKETCHBOOK)/libraries/%.S
512
	$(RULEHDR)
513
	$(v)$(AS) $(ASFLAGS) -c -o $@ $< $(SLIB_INCLUDES)
514
 
515
#
516
# Build library objects from Ardiuno library sources
517
#
518
ALIB_INCLUDES	=	-I$(dir $<)/utility $(ARDUINOLIBINCLUDES) $(COREINCLUDES)
519
 
520
$(BUILDROOT)/libraries/%.o: $(ARDUINO)/libraries/%.cpp
521
	$(RULEHDR)
522
	$(v)$(CXX) $(CXXFLAGS) -c -o $@ $< $(ALIB_INCLUDES)
523
 
524
$(BUILDROOT)/libraries/%.o: $(ARDUINO)/libraries/%.c
525
	$(RULEHDR)
526
	$(v)$(CC) $(CFLAGS) -c -o $@ $< $(ALIB_INCLUDES)
527
 
528
$(BUILDROOT)/libraries/%.o: $(ARDUINO)/libraries/%.S
529
	$(RULEHDR)
530
	$(v)$(AS) $(ASFLAGS) -c -o $@ $< $(ALIB_INCLUDES)
531
 
532
#
533
# Build objects from the hardware core
534
#
535
$(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.cpp
536
	$(RULEHDR)
537
	$(v)$(CXX) $(filter-out -W%,$(CXXFLAGS)) -c -o $@ $< $(COREINCLUDES)
538
 
539
$(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.c
540
	@mkdir -p $(dir $@)
541
	$(v)$(CC) $(filter-out -W%,$(CFLAGS)) -c -o $@ $< $(COREINCLUDES)
542
 
543
$(BUILDROOT)/$(HARDWARE)/%.o: $(CORESRC_DIR)/%.S
544
	$(RULEHDR)
545
	$(v)$(AS) $(ASFLAGS) -c -o $@ $< $(COREINCLUDES)
546
 
547
#
548
# Build the core library
549
#
550
$(CORELIB): $(CORELIBOBJS)
551
	$(RULEHDR)
552
	$(v)$(AR) -rcs $@ $^
553
 
554
#
555
# Build the sketch.cpp file
556
#
557
# This process strives to be as faithful to the Arduino implementation as
558
# possible.  Conceptually, the process is as follows:
559
#
560
# * All of the .pde/.ino files are concatenated, starting with the file named
561
#   for the sketch and followed by the others in alphabetical order.
562
# * An insertion point is created in the concatenated file at
563
#   the first statement that isn't a preprocessor directive or comment.
564
# * An include of "WProgram.h" is inserted at the insertion point.
565
# * The file following the insertion point is scanned for function definitions
566
#   and prototypes for these functions are inserted at the insertion point.
567
#
568
# In addition, we add #line directives wherever the originating file changes
569
# to help backtrack from compiler messages and in the debugger.
570
#
571
$(SKETCHCPP):	$(SKETCHCPP_SRC)
572
	$(RULEHDR)
573
	$(v)$(AWK) -v mode=header '$(SKETCH_SPLITTER)'   $(SKETCHCPP_SRC) >  $@
574
	$(v)echo "#line 1 \"autogenerated\""                              >> $@
575
	$(v)echo "#if defined(ARDUINO) && ARDUINO >= 100"                 >> $@
576
	$(v)echo "#include \"Arduino.h\""                                 >> $@
577
	$(v)echo "#else"                                                  >> $@
578
	$(v)echo "#include \"WProgram.h\""                                >> $@
579
	$(v)echo "#endif"                                                 >> $@
580
	$(v)$(AWK)                '$(SKETCH_PROTOTYPER)' $(SKETCHCPP_SRC) >> $@
581
	$(v)$(AWK) -v mode=body   '$(SKETCH_SPLITTER)'   $(SKETCHCPP_SRC) >> $@
582
 
583
# delete the sketch.cpp file if a processing error occurs
584
.DELETE_ON_ERROR: $(SKETCHCPP)
585
 
586
#
587
# The sketch splitter is an awk script used to split off the
588
# header and body of the concatenated .pde/.ino files.  It also
589
# inserts #line directives to help in backtracking from compiler
590
# and debugger messages to the original source file.
591
#
592
# Note that # and $ require special treatment here to avoid upsetting
593
# make.
594
#
595
# This script requires BWK or GNU awk.
596
#
597
define SKETCH_SPLITTER
598
  BEGIN { 							\
599
    scanning = 1; 						\
600
    printing = (mode ~ "header") ? 1 : 0;			\
601
  }								\
602
  { toggles = 1 }						\
603
  (FNR == 1) && printing { 					\
604
    printf "#line %d \"%s\"\n", FNR, FILENAME;			\
605
  }								\
606
  /^[[:space:]]*\/\*/,/\*\// { 					\
607
    toggles = 0;						\
608
  }								\
609
  /^[[:space:]]*$$/ || /^[[:space:]]*\/\/.*/ || /^\#.*$$/ { 	\
610
    toggles = 0;						\
611
  }								\
612
  scanning && toggles { 					\
613
    scanning = 0; 						\
614
    printing = !printing;					\
615
    if (printing) { 						\
616
      printf "#line %d \"%s\"\n", FNR, FILENAME;		\
617
    }								\
618
  }								\
619
  printing
620
endef
621
 
622
#
623
# The prototype scanner is an awk script used to generate function
624
# prototypes from the concantenated .pde/.ino files.
625
#
626
# Function definitions are expected to follow the form
627
#
628
#   <newline><type>[<qualifier>...]<name>([<arguments>]){
629
#
630
# with whitespace permitted between the various elements.  The pattern
631
# is assembled from separate subpatterns to make adjustments easier.
632
#
633
# Note that $ requires special treatment here to avoid upsetting make,
634
# and backslashes are doubled in the partial patterns to satisfy
635
# escaping rules.
636
#
637
# This script requires BWK or GNU awk.
638
#
639
define SKETCH_PROTOTYPER
640
  BEGIN {								\
641
    RS="{";								\
642
    type       = "((\\n)|(^))[[:space:]]*[[:alnum:]_]+[[:space:]]+";	\
643
    qualifiers = "([[:alnum:]_\\*&]+[[:space:]]*)*";			\
644
    name       = "[[:alnum:]_]+[[:space:]]*";				\
645
    args       = "\\([[:space:][:alnum:]_,&\\*\\[\\]]*\\)";		\
646
    bodycuddle = "[[:space:]]*$$";					\
647
    pattern    = type qualifiers name args bodycuddle;			\
648
  }									\
649
  match($$0, pattern) {							\
650
    proto = substr($$0, RSTART, RLENGTH);				\
651
    gsub("\n", " ", proto);						\
652
    printf "%s;\n", proto;						\
653
  }
654
endef