1
+ #
2
+ # Copyright 2011 Alan Burlison,
[email protected] . All rights reserved.
3
+ # Use is subject to license terms.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice,
9
+ # this list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY ALAN BURLISON "AS IS" AND ANY EXPRESS OR IMPLIED
16
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
+ # EVENT SHALL ALAN BURLISON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21
+ # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24
+ # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ #
26
+ # Makefile for building Arduino projects outside of the Arduino environment
27
+ #
28
+ # This makefile should be included into a per-project Makefile of the following
29
+ # form:
30
+ #
31
+ # ----------
32
+ # BOARD = mega
33
+ # PORT = /dev/term/0
34
+ # THIRD_PARTY_HARDWARE = /Arduino/hardware/Sanguino
35
+ # INC_DIRS = ../common
36
+ # LIB_DIRS = ../libraries/Task ../../libraries/VirtualWire
37
+ # include ../../Makefile.master
38
+ # ----------
39
+ #
40
+ # Where:
41
+ # BOARD : Arduino board type, from boards.txt
42
+ # PORT : USB port
43
+ # THIRD_PARTY_HARDWARE : Path to third party hardware folder
44
+ # INC_DIRS : List pf directories containing header files
45
+ # LIB_DIRS : List of directories containing library source
46
+ #
47
+ # Before using this Makefile you can adjust the following macros to suit
48
+ # your environment, either by editing this file directly or by defining them in
49
+ # the Makefile that includes this one, in which case they will override the
50
+ # definitions below:
51
+ # ARD_REV : Arduino software revision, e.g. 0017, 0018
52
+ # ARD_HOME : installation directory of the Arduino software.
53
+ # ARD_BIN : location of compiler binaries
54
+ # AVRDUDE : location of avrdude executable
55
+ # AVRDUDE_CONF : location of avrdude configuration file
56
+ # PROGRAMMER : avrdude programmer type
57
+ # MON_SPEED : serial monitor speed
58
+ # EXTRA_FLAGS : any extra flags that should be passed to the compilers
59
+ #
60
+
61
+ # Global configuration.
62
+ ARD_REV ?= 105
63
+ ARD_HOME ?= /Applications/Arduino1.0.5.app/Contents/Resources/Java
64
+
65
+ ifneq ($(wildcard $(ARD_HOME)/hardware/tools/avr/bin/avrdude),)
66
+ ARD_BIN ?= $(ARD_HOME)/hardware/tools/avr/bin
67
+ AVRDUDE ?= $(ARD_BIN)/avrdude
68
+ AVRDUDE_CONF ?= $(ARD_HOME)/hardware/tools/avr/etc/avrdude.conf
69
+ endif
70
+
71
+ ifneq ($(wildcard $(ARD_HOME)/hardware/tools/avrdude),)
72
+ ARD_BIN ?= $(ARD_HOME)/hardware/tools/avr/bin
73
+ AVRDUDE ?= $(ARD_HOME)/hardware/tools/avrdude
74
+ AVRDUDE_CONF ?= $(ARD_HOME)/hardware/tools/avrdude.conf
75
+ endif
76
+
77
+ ifneq ($(wildcard $(ARD_HOME)/build/linux/work/hardware/tools/avrdude),)
78
+ ARD_BIN ?= $(ARD_HOME)/build/linux/work/hardware/tools/avr/bin
79
+ AVRDUDE ?= $(ARD_HOME)/build/linux/work/hardware/tools/avrdude
80
+ AVRDUDE_CONF ?= $(ARD_HOME)/build/linux/work/hardware/tools/avrdude.conf
81
+ endif
82
+
83
+ PROGRAMMER ?= arduino
84
+ MON_SPEED ?= 115200
85
+ GCS = -Wl,--gc-sections
86
+ #EXTRA_FLAGS =
87
+ ### Nothing below here should require editing. ###
88
+
89
+ # Check for the required definitions.
90
+ ifndef BOARD
91
+ $(error $$(BOARD) not defined)
92
+ endif
93
+ ifndef PORT
94
+ $(error $$(PORT) not defined)
95
+ endif
96
+
97
+ # Version-specific settings
98
+ ifdef THIRD_PARTY_HARDWARE
99
+ ARD_BOARDS = $(THIRD_PARTY_HARDWARE)/boards.txt
100
+ CORE = $(shell sed -n 's/$(BOARD)\.build\.core=\(.*\)/\1/p' < $(ARD_BOARDS))
101
+ ARD_SRC_DIR = $(THIRD_PARTY_HARDWARE)/cores/$(CORE)
102
+ ARD_MAIN = $(ARD_SRC_DIR)/main.cpp
103
+ LIB_DIRS += $(THIRD_PARTY_HARDWARE)/variants/$(shell sed -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
104
+ else
105
+
106
+ ifneq ($(wildcard $(ARD_HOME)/hardware/arduino/boards.txt),)
107
+ ARD_BOARDS = $(ARD_HOME)/hardware/arduino/boards.txt
108
+ ARD_SRC_DIR = $(ARD_HOME)/hardware/arduino/cores/arduino
109
+ ARD_MAIN = $(ARD_SRC_DIR)/main.cpp
110
+ LIB_DIRS += $(ARD_HOME)/hardware/arduino/variants/$(shell sed -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))
111
+ endif
112
+
113
+ ifneq ($(wildcard $(ARD_HOME)/hardware/boards.txt),)
114
+ ARD_BOARDS = $(ARD_HOME)/hardware/boards.txt
115
+ ARD_SRC_DIR = $(ARD_HOME)/hardware/cores/arduino
116
+ ARD_MAIN = $(ARD_SRC_DIR)/main.cxx
117
+ endif
118
+
119
+ endif
120
+
121
+ ifndef ARD_BOARDS
122
+ $(error can not locate arduino tools)
123
+ endif
124
+
125
+ # Platform-specific settings.
126
+ PLATFORM = $(shell uname -s)
127
+ ifeq "$(PLATFORM)" "SunOS"
128
+ define run-monitor
129
+ gnome-terminal -t '$(BOARD) $(PORT)' \
130
+ -e 'env -i tip -$(MON_SPEED) $(PORT)' &
131
+ endef
132
+ define kill-monitor
133
+ - pkill -f 'tip.*$(PORT)'
134
+ endef
135
+ else ifeq "$(PLATFORM)" "Linux"
136
+ define run-monitor
137
+ screen $(PORT) $(MON_SPEED)
138
+ endef
139
+ define kill-monitor
140
+ - pkill -f 'screen.*$(PORT)'
141
+ endef
142
+ else ifeq "$(PLATFORM)" "Darwin"
143
+ define run-monitor
144
+ screen $(PORT) $(MON_SPEED)
145
+ endef
146
+ define kill-monitor
147
+ - pkill -f 'screen.*$(PORT)'
148
+ endef
149
+ else
150
+ $(error Unknown platform $(PLATFORM))
151
+ endif
152
+
153
+ # Standard macros.
154
+ SKETCH = $(notdir $(CURDIR))
155
+ BUILD_DIR = build
156
+ VPATH = $(LIB_DIRS)
157
+
158
+ # Macros derived from boards.txt
159
+ MCU := $(shell sed -n 's/$(BOARD)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
160
+ F_CPU := $(shell sed -n 's/$(BOARD)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
161
+ UPLOAD_SPEED := \
162
+ $(shell sed -n 's/$(BOARD)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
163
+
164
+ # Build tools.
165
+ CC = $(ARD_BIN)/avr-gcc
166
+ CXX = $(ARD_BIN)/avr-g++
167
+ CXXFILT = $(ARD_BIN)/avr-c++filt
168
+ OBJCOPY = $(ARD_BIN)/avr-objcopy
169
+ OBJDUMP = $(ARD_BIN)/avr-objdump
170
+ AR = $(ARD_BIN)/avr-ar
171
+ SIZE = $(ARD_BIN)/avr-size
172
+ NM = $(ARD_BIN)/avr-nm
173
+ MKDIR = mkdir -p
174
+ RM = rm -rf
175
+ MV = mv -f
176
+
177
+ # Compiler flags.
178
+ INC_FLAGS = \
179
+ $(addprefix -I,$(LIB_DIRS)) $(addprefix -I,$(INC_DIRS)) -I$(ARD_SRC_DIR)
180
+
181
+ ARD_FLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -DARDUINO=$(ARD_REV)
182
+
183
+ C_CXX_FLAGS = -g -w -Wa,-adhlns=$(BUILD_DIR)/$*.lst $(EXTRA_FLAGS)
184
+
185
+ C_FLAGS = \
186
+ -std=gnu99 -Wstrict-prototypes -Wno-old-style-declaration $(C_CXX_FLAGS)
187
+
188
+ CXX_FLAGS = \
189
+ $(C_CXX_FLAGS)
190
+
191
+ # Optimiser flags.
192
+ OPT_FLAGS = -Os -fno-exceptions -ffunction-sections -fdata-sections -MMD
193
+
194
+ # Build parameters.
195
+ IMAGE = $(BUILD_DIR)/$(SKETCH)
196
+ ARD_C_SRC = $(wildcard $(ARD_SRC_DIR)/*.c)
197
+ ARD_CXX_SRC = $(wildcard $(ARD_SRC_DIR)/*.cpp)
198
+ ARD_C_OBJ = $(patsubst %.c,%.o,$(notdir $(ARD_C_SRC)))
199
+ ARD_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(ARD_CXX_SRC)))
200
+ ARD_LIB = arduino
201
+ ARD_AR = $(BUILD_DIR)/lib$(ARD_LIB).a
202
+ ARD_AR_OBJ = $(ARD_AR)($(ARD_C_OBJ) $(ARD_CXX_OBJ))
203
+ ARD_LD_FLAG = -l$(ARD_LIB)
204
+
205
+ # Sketch libraries.
206
+ LIB_C_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.c))
207
+ LIB_CXX_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.cpp))
208
+ LIB_SRC = $(LIB_C_SRC) $(LIB_CXX_SRC)
209
+ ifneq "$(strip $(LIB_C_SRC) $(LIB_CXX_SRC))" ""
210
+ LIB_C_OBJ = $(patsubst %.c,%.o,$(notdir $(LIB_C_SRC)))
211
+ LIB_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(LIB_CXX_SRC)))
212
+ LIB_LIB = library
213
+ LIB_AR = $(BUILD_DIR)/lib$(LIB_LIB).a
214
+ LIB_AR_OBJ = $(LIB_AR)($(LIB_C_OBJ) $(LIB_CXX_OBJ))
215
+ LIB_LD_FLAG = -l$(LIB_LIB)
216
+ endif
217
+
218
+ # Sketch PDE source.
219
+ SKT_PDE_SRC = $(wildcard *.ino)
220
+ ifneq "$(strip $(SKT_PDE_SRC))" ""
221
+ SKT_PDE_OBJ = $(BUILD_DIR)/$(SKETCH)_ino.o
222
+ endif
223
+
224
+ # C and C++ source.
225
+ SKT_C_SRC = $(wildcard *.c)
226
+ SKT_CXX_SRC = $(wildcard *.cpp)
227
+ ifneq "$(strip $(SKT_C_SRC) $(SKT_CXX_SRC))" ""
228
+ SKT_C_OBJ = $(patsubst %.c,%.o,$(SKT_C_SRC))
229
+ SKT_CXX_OBJ = $(patsubst %.cpp,%.o,$(SKT_CXX_SRC))
230
+ SKT_LIB = sketch
231
+ SKT_AR = $(BUILD_DIR)/lib$(SKT_LIB).a
232
+ SKT_AR_OBJ = $(SKT_AR)/($(SKT_C_OBJ) $(SKT_CXX_OBJ))
233
+ SKT_LD_FLAG = -l$(SKT_LIB)
234
+ endif
235
+
236
+ # Common rule bodies.
237
+ define run-cc
238
+ $(CC) -c $(C_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
239
+ -MD -MT '$@($%)' -MF $(@D)/.$(@F)_$*.dep $< -o $(BUILD_DIR)/$%
240
+ @ $(AR) rc $@ $(BUILD_DIR)/$%
241
+ @ $(RM) $(BUILD_DIR)/$%
242
+ @ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
243
+ @ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
244
+ endef
245
+
246
+ define run-cxx
247
+ $(CXX) -c $(CXX_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
248
+ -MD -MT '$@($%)' -MF $(@D)/.$(@F)_$*.dep $< -o $(BUILD_DIR)/$%
249
+ @ $(AR) rc $@ $(BUILD_DIR)/$%
250
+ @ $(RM) $(BUILD_DIR)/$%
251
+ @ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
252
+ @ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
253
+ endef
254
+
255
+ # Rules.
256
+ .PHONY : all clean upload monitor upload_monitor upmonitor
257
+
258
+ all : $(BUILD_DIR) $(IMAGE).hex
259
+
260
+ clean :
261
+ $(RM) $(BUILD_DIR)
262
+
263
+ $(BUILD_DIR) :
264
+ $(MKDIR) $@
265
+
266
+ $(SKT_PDE_OBJ) : $(SKT_PDE_SRC)
267
+ echo '#include <Arduino.h>' > $(BUILD_DIR)/$(SKETCH)_ino.cpp
268
+ cat $(SKT_PDE_SRC) >> $(BUILD_DIR)/$(SKETCH)_ino.cpp
269
+ cd $(BUILD_DIR) && $(CXX) -c $(subst build/,,$(CXX_FLAGS)) \
270
+ $(OPT_FLAGS) $(ARD_FLAGS) -I.. \
271
+ $(patsubst -I..%,-I../..%,$(INC_FLAGS)) \
272
+ $(SKETCH)_ino.cpp -o $(@F)
273
+
274
+ (%.o) : $(ARD_SRC_DIR)/%.c
275
+ $(run-cc)
276
+
277
+ (%.o) : $(ARD_SRC_DIR)/%.cpp
278
+ $(run-cxx)
279
+
280
+ (%.o) : %.c
281
+ $(run-cc)
282
+
283
+ (%.o) : %.cpp
284
+ $(run-cxx)
285
+
286
+ $(BUILD_DIR)/%.d : %.c
287
+ $(run-cc-d)
288
+
289
+ $(BUILD_DIR)/%.d : %.cpp
290
+ $(run-cxx-d)
291
+
292
+ #-Wl,--gc-sections
293
+ # $(AR) d $(BUILD_DIR)/lib$(LIB_LIB).a malloc.o
294
+
295
+ $(IMAGE).hex : $(ARD_AR_OBJ) $(LIB_AR_OBJ) $(SKT_AR_OBJ) $(SKT_PDE_OBJ)
296
+ $(CC) -Os $(GCS) -mmcu=$(MCU) -L$(BUILD_DIR) $(SKT_PDE_OBJ) \
297
+ $(SKT_LD_FLAG) $(LIB_LD_FLAG) $(ARD_LD_FLAG) -lm -o $(IMAGE).elf
298
+ $(OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load \
299
+ --no-change-warnings --change-section-lma .eeprom=0 $(IMAGE).elf \
300
+ $(IMAGE).eep
301
+ $(OBJCOPY) -O ihex -R .eeprom $(IMAGE).elf $(IMAGE).hex
302
+ $(OBJDUMP) -h -S $(IMAGE).elf | $(CXXFILT) -t > $(IMAGE).lst
303
+ $(SIZE) $(IMAGE).elf
304
+ $(SIZE) -C --mcu=$(MCU) $(IMAGE).elf
305
+
306
+ upload : all
307
+ $(kill-monitor)
308
+ - $(AVRDUDE) -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) \
309
+ -b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i
310
+
311
+ monitor :
312
+ $(kill-monitor)
313
+ $(run-monitor)
314
+
315
+ upload_monitor : upload monitor
316
+
317
+ upmonitor: upload_monitor
318
+
319
+ -include $(wildcard $(BUILD_DIR)/.*.dep))
0 commit comments