/make/arduino-mk

To get this branch, use:
bzr branch http://bzr.ed.am/make/arduino-mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#_______________________________________________________________________________
#
#                         edam's arduino makefile
#_______________________________________________________________________________
#                                                                    version 0.1
#
# Copyright (c) 2011 Tim Marston <tim@ed.am>.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#_______________________________________________________________________________
#
#
# This is a general purpose makefile for use with Arduino (arduino.cc)
# hardware and software.  It works with the arduino-1.0 release and
# requires that to be downloaded separately.
#
# There are two ways to use this file, an automatic mode and a manual
# mode.  In automatic mode, you simply copy this makefile to your
# arduino project directory, rename it "Makefile" and type make.  The
# project directory is expected to contain an .ino or .pde file, which
# will automatically be used automatically, along with any other .c,
# .cc or .cpp files in the project directory and any subdirectory
# named "utility".  In this way, this makefile should act as a drop-in
# replacement for the Arduino IDE's build process and can build a
# project automatically from the files in a project directory.
#
# Alternatively, you can manually specify what files should be
# inclided in a build.  To use this makefile manually, you might be
# better to keep it somewhere and include it in your project's
# Makefile after having defined certain parameters that control the
# build.  As an example, consider the following Makefile:
#
#       TARGET = my_program
#       SOURCES = main.cc foo.cc
#		BOARD = nano
#		SERIALDEV = /dev/ttyACM0
#       include ~/src/arduino.mk
#
# In both manual and automatic modes, the standard Arduino main.cpp's
# main() is included, which expects to be able to call init() and
# loop() in your code.  The main difference is that, in manual mode,
# these would typically be placed in a .cc or .cpp file.
#
# When using manual mode, the following variables can be used:
#
# TARGET       The name of the target file.  This is typically the same name
#              as the project directory for an arduino project and, if
#              unspecified, that is used as a default.
#
# SOURCES      A list of all source files of whatever language. The language
#              type is determined by the file extension.
#
# BOARD        Specify a target board type.  These are defined in boards.txt,
#              which came with your arduino installation.  If unspecified,
#              a default is used.  (See below).
#
# SERIALDEV    The unix device of the device where the arduino can be found.
#              If unspecified, a default is used.  (See below).
#
# This general-purpose makefile also defines the following goals for use on the
# command line when you run make:
#
# all          This is the default if no goal is specified. It builds the
#              target and uploads it.
#
# target       Builds the target of your Makefile.
#
# upload       Uploads the target to an attached arduino.
#
# clean        Deletes temporary files.
#
# <file>       Builds the specified file, either an object file or the target,
#              from those that that would be built for the project.
#
#_______________________________________________________________________________
#                                                                       SETTINGS

# The full path to the arduino software, from arduino.cc
ARDUINODIR := $(wildcard ~/opt/arduino-1.0)

# The board name to build for and upload to. For a complete list of available
# boards, see hardware/arduino/boards.txt in your arduino software directory.
# Here is a list of available boards at time of writing:
#   uno         Arduino Uno
#   atmega328   Arduino Duemilanove w/ ATmega328
#   diecimila   Arduino Diecimila or Duemilanove w/ ATmega168
#   nano328     Arduino Nano w/ ATmega328
#   nano        Arduino Nano w/ ATmega168
#   mega2560    Arduino Mega 2560 or Mega ADK
#   mega        Arduino Mega (ATmega1280)
#   mini328     Arduino Mini w/ ATmega328
#   mini        Arduino Mini w/ ATmega168
#   fio         Arduino Fio
#   bt328       Arduino BT w/ ATmega328
#   bt          Arduino BT w/ ATmega168
#   lilypad328  LilyPad Arduino w/ ATmega328
#   lilypad     LilyPad Arduino w/ ATmega168
#   pro5v328    Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
#   pro5v       Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
#   pro328      Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
#   pro         Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
#   atmega168   Arduino NG or older w/ ATmega168
#   atmega8     Arduino NG or older w/ ATmega8
ifndef BOARD
BOARD := nano
endif

# The name of the serial device that the arduino is at. For example,
# /dev/ttyACM0 (Uno), or /dev/ttyUSB0 (Duemilanove)
ifndef SERIALDEV
SERIALDEV := /dev/ttyACM0
endif

#_______________________________________________________________________________
#

# auto mode?
INOFILE := $(wildcard *.ino *.pde)
ifdef INOFILE
ifneq ($(words $(INOFILE)), 1)
$(error There is more than one .pde or .ino file in the directory!)
endif
TARGET := $(basename $(INOFILE))
SOURCES := $(INOFILE) \
	$(wildcard *.c *.cc *.cpp $(addprefix utility/, *.c *.cc *.cpp))
endif

# files
OBJECTS := $(addsuffix .o, $(basename $(SOURCES)))
ifdef INOFILE
OBJECT += main.o
endif
ARDUINOSRCDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino
ARDUINOLIB := _arduino.a
ARDUINOLIBTMP := _arduino.a.tmp
ARDUINOSOURCES := $(wildcard $(addprefix $(ARDUINOSRCDIR)/, *.c *.cpp))
ARDUINOOBJECTS := $(addsuffix .o, $(addprefix $(ARDUINOLIBTMP)/, $(basename \
	$(subst $(ARDUINOSRCDIR)/,,$(ARDUINOSOURCES)))))

# obtain parameters from the arduino boards.txt file
BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt
BOARD_BUILD_MCU := \
	$(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_BUILD_FCPU := \
	$(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_BUILD_VARIANT := \
	$(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_UPLOAD_SPEED := \
	$(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE))
BOARD_UPLOAD_PROTOCOL := \
	$(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE))

# software
CC := avr-gcc
CXX := avr-g++
LD := avr-ld
AR := avr-ar
OBJCOPY := avr-objcopy
AVRDUDE := $(ARDUINODIR)/hardware/tools/avrdude

# flags
CPPFLAGS = -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections
CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -DF_CPU=$(BOARD_BUILD_FCPU)
CPPFLAGS += -I. -Iutility -I$(ARDUINOSRCDIR)
CPPFLAGS += -I$(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/
AVRDUDEFLAGS = -V -F -C $(ARDUINODIR)/hardware/tools/avrdude.conf
AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV)
AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED)

# checks
ifeq ($(TARGET),)
$(error No target defined!)
endif

# default rule
.DEFAULT_GOAL := all

#_______________________________________________________________________________
#                                                                          RULES

.PHONY:	all target upload clean

all: target upload

target: $(TARGET).elf

upload:
	stty -F $(SERIALDEV) hupcl
	$(OBJCOPY) -O ihex -R .eeprom $(TARGET).elf $(TARGET).hex
	$(AVRDUDE) $(ARVDUDEFLAGS) -U flash:w:$(TARGET).hex
	rm $(TARGET).hex

clean:
	rm -f $(OBJECTS)
	rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~
	rm -rf $(ARDUINOLIBTMP)

# building the target

$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS)
	$(CC) -Os -Wl,--gc-sections $(OBJECTS) $(ARDUINOLIB) -o $@

%.o: %.ino
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<

%.o: %.pde
	$(COMPILE.cpp) -o $@ -x c++ -include $(ARDUINOSRCDIR)/Arduino.h $<

# building the arduino library

$(ARDUINOLIB): $(ARDUINOOBJECTS)
	$(AR) rcs $@ $?
	rm -rf $(ARDUINOLIBTMP)

.INTERMEDIATE: $(ARDUINOOBJECTS)

$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.c
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.c) -o $@ $<

$(ARDUINOLIBTMP)/%.o: $(ARDUINOSRCDIR)/%.cpp
	@test -d $(ARDUINOLIBTMP) || mkdir $(ARDUINOLIBTMP)
	$(COMPILE.cpp) -o $@ $<