/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: 2011-12-30 23:12:46 UTC
  • Revision ID: edam@waxworlds.org-20111230231246-zl8980dsrlfhcugf
added included-library autodetection and updated comments

Show diffs side-by-side

added added

removed removed

34
34
# more information and documentation on it's use.  The following text
35
35
# can only really be considered a reference to it's use.
36
36
#
37
 
# There are two ways to use this file, an automatic mode and a manual
38
 
# mode.  In automatic mode, you simply copy this makefile to your
39
 
# arduino project directory, rename it "Makefile" and type make.  The
40
 
# project directory is expected to contain an .ino or .pde file, which
41
 
# will automatically be used automatically, along with any other .c,
42
 
# .cc or .cpp files in the project directory and any subdirectory
43
 
# named "utility".  In this way, this makefile should act as a drop-in
44
 
# replacement for the Arduino IDE's build process and can build a
45
 
# project automatically from the files in a project directory.
46
 
#
47
 
# Alternatively, you can manually specify what files should be
48
 
# inclided in a build.  To use this makefile manually, you might be
49
 
# better to keep it somewhere and include it in your project's
50
 
# Makefile after having defined certain parameters that control the
51
 
# build.  As an example, consider the following Makefile:
52
 
#
53
 
#       TARGET = my_program
54
 
#       SOURCES = main.cc foo.cc
 
37
# To use this makefile, put a file named "Makefile" in your project
 
38
# directory.  Add all your project's settings to your Makefile and
 
39
# then include this file from it. For example, your Makefile might
 
40
# look something like this:
 
41
#
55
42
#               BOARD = nano
56
43
#               SERIALDEV = /dev/ttyACM0
57
44
#       include ~/src/arduino.mk
58
45
#
59
 
# In both manual and automatic modes, the standard Arduino main.cpp's
60
 
# main() is included, which expects to be able to call init() and
61
 
# loop() in your code.  The main difference is that, in manual mode,
62
 
# these would typically be placed in a .cc or .cpp file.
63
 
#
64
 
# When using manual mode, the following variables can be used:
 
46
# A complete list of all the settings you can use in your Makefile
 
47
# follows shortly. But it should be noted that there are two ways to
 
48
# use this file, an automatic mode and a manual mode.
 
49
#
 
50
# In automatic mode, you simply define a BOARD and SERIALDEV and then
 
51
# include this makefile.  The project directory must contain an .ino
 
52
# or .pde file, which will automatically be used, along with any other
 
53
# .c, .cc or .cpp files in the project directory.  When buliding, any
 
54
# arduino libraries that are #included in your code are detected and
 
55
# automatically linked against.  In this way, this makefile should act
 
56
# as a drop-in replacement for the Arduino IDE's build system.  You
 
57
# should be able to type "make" from the command line and achieve the
 
58
# same as building and uploading from the Arduino IDE.
 
59
#
 
60
# The alternative, manual mode, makes no assumptions.  If the
 
61
# directory doesn't contain an .ino or .pde file, you must also
 
62
# specify the SOURCES, TARGET and LIBRARIES manually in your Makefile.
 
63
# You will also be expected to provide a main() function, for example
 
64
# in main.cc, which may or may not duplicate the functionality of the
 
65
# default main() that calls init() and loop().
 
66
#
 
67
# Here is a list of all configuration parameters:
 
68
#
 
69
# BOARD        Specify a target board type.  These are defined in boards.txt,
 
70
#              which came with your arduino installation.  If unspecified,
 
71
#              a default is used.  (See below).
 
72
#
 
73
# SERIALDEV    The unix device of the device where the arduino can be found.
 
74
#              If unspecified, a default is used.  (See below).
 
75
#
 
76
# The following configuration parameters can be determined automatically:
65
77
#
66
78
# TARGET       The name of the target file.  This is typically the same name
67
79
#              as the project directory for an arduino project and, if
70
82
# SOURCES      A list of all source files of whatever language. The language
71
83
#              type is determined by the file extension.
72
84
#
73
 
# BOARD        Specify a target board type.  These are defined in boards.txt,
74
 
#              which came with your arduino installation.  If unspecified,
75
 
#              a default is used.  (See below).
76
 
#
77
 
# SERIALDEV    The unix device of the device where the arduino can be found.
78
 
#              If unspecified, a default is used.  (See below).
 
85
# LIBRARIES    A list of arduino libraries to include
79
86
#
80
87
# This general-purpose makefile also defines the following goals for use on the
81
88
# command line when you run make:
122
129
#   atmega168   Arduino NG or older w/ ATmega168
123
130
#   atmega8     Arduino NG or older w/ ATmega8
124
131
ifndef BOARD
125
 
BOARD := nano
 
132
BOARD := uno
126
133
endif
127
134
 
128
135
# The name of the serial device that the arduino is at. For example,
142
149
endif
143
150
TARGET := $(basename $(INOFILE))
144
151
SOURCES := $(INOFILE) \
145
 
        $(wildcard *.c *.cc *.cpp $(addprefix utility/, *.c *.cc *.cpp))
 
152
        $(wildcard *.c *.cc *.cpp) \
 
153
        $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \
 
154
        $(wildcard $(addprefix utility/, *.c *.cc *.cpp))
 
155
# automatically determine included libraries
 
156
ARDUINOLIBSAVAIL := $(notdir $(wildcard $(ARDUINODIR)/libraries/*))
 
157
LIBRARIES := $(filter $(ARDUINOLIBSAVAIL), \
 
158
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES)))
146
159
endif
147
160
 
148
161
# files
149
162
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
150
 
ifdef INOFILE
151
 
OBJECT += main.o
152
 
endif
153
163
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
154
164
ARDUINOLIB := _arduino.a
155
165
ARDUINOLIBTMP := _arduino.a.tmp
156
 
ARDUINOSOURCES := $(wildcard $(ARDUINOSRCDIR)/*.c $(ARDUINOSRCDIR)/*.cpp)
157
 
ARDUINOOBJECTS := $(addsuffix .o, $(addprefix $(ARDUINOLIBTMP)/, $(basename \
158
 
        $(notdir $(ARDUINOSOURCES)))))
 
166
ARDUINOLIBOBJS := $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
167
        $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp)))))
 
168
ARDUINOLIBOBJS += $(foreach lib, $(LIBRARIES), \
 
169
        $(patsubst %, $(ARDUINOLIBTMP)/%.o, $(basename $(notdir \
 
170
        $(wildcard $(addprefix $(ARDUINODIR)/libraries/$(lib)/, *.c *.cpp))))))
159
171
 
160
172
# obtain board parameters from the arduino boards.txt file
161
173
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
184
196
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
185
197
CPPFLAGS += -I. -Iutility -I$(ARDUINOSRCDIR)
186
198
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
 
199
CPPFLAGS += $(addprefix -I$(ARDUINODIR)/libraries/,$(LIBRARIES))
187
200
AVRDUDEFLAGS = -C $(ARDUINODIR)/hardware/tools/avrdude.conf -DV
188
201
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
189
202
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)
233
246
 
234
247
# building the arduino library
235
248
 
236
 
$(ARDUINOLIB): $(ARDUINOOBJECTS)
 
249
$(ARDUINOLIB): $(ARDUINOLIBOBJS)
237
250
        $(AR) rcs $@ $?
238
251
        rm -rf $(ARDUINOLIBTMP)
239
252
 
240
 
.INTERMEDIATE: $(ARDUINOOBJECTS)
 
253
.INTERMEDIATE: $(ARDUINOLIBOBJS)
241
254
 
242
255
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
243
256
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
246
259
$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
247
260
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
248
261
        $(COMPILE.cpp) -o $@ $<
 
262
 
 
263
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.c
 
264
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
265
        $(COMPILE.c) -o $@ $<
 
266
 
 
267
$(ARDUINOLIBTMP)/%.o: $(ARDUINODIR)/libraries/*/%.cpp
 
268
        @test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
 
269
        $(COMPILE.cpp) -o $@ $<