Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / build / common.mk
CommitLineData
4cff3ded
AW
1#Copyright (C) 2011 by Sagar G V
2#
3#Permission is hereby granted, free of charge, to any person obtaining a copy
4#of this software and associated documentation files (the "Software"), to deal
5#in the Software without restriction, including without limitation the rights
6#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7#copies of the Software, and to permit persons to whom the Software is
8#furnished to do so, subject to the following conditions:
9#
10#The above copyright notice and this permission notice shall be included in
11#all copies or substantial portions of the Software.
12#
13#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19#THE SOFTWARE.
20#
dc735ba6 21# Updates:
172d42d9 22# Arthur Wolf & Adam Green in 2011 - 2012 - Updated to work with mbed.
4cff3ded 23###############################################################################
6c79da43
AG
24# Check for undefined variables.
25ifndef PROJECT
26$(error makefile must set PROJECT variable.)
13e4a3f9
AW
27endif
28
172d42d9
AG
29ifndef BUILD_DIR
30$(error makefile must set BUILD_DIR.)
31endif
32
33# Set VERBOSE make variable to 1 to output all tool commands.
34VERBOSE?=0
35ifeq "$(VERBOSE)" "0"
36Q=@
37else
38Q=
4cff3ded
AW
39endif
40
6c79da43
AG
41
42# Default variables.
43SRC ?= .
172d42d9 44BUILD_TYPE ?= Release
6c79da43
AG
45MRI_BREAK_ON_INIT ?= 1
46MRI_UART ?= MRI_UART_MBED_USB
383036c1
AG
47HEAP_TAGS ?= 0
48WRITE_BUFFER_DISABLE ?= 0
49STACK_SIZE ?= 0
6c79da43
AG
50
51
172d42d9
AG
52# Configure MRI variables based on BUILD_TYPE build type variable.
53ifeq "$(BUILD_TYPE)" "Release"
8fcce42e 54OPTIMIZATION ?= 2
6c79da43
AG
55MRI_ENABLE = 0
56MRI_SEMIHOST_STDIO ?= 0
57endif
58
59
172d42d9 60ifeq "$(BUILD_TYPE)" "Debug"
6c79da43 61OPTIMIZATION = 0
172d42d9 62MRI_ENABLE ?= 1
6c79da43
AG
63MRI_SEMIHOST_STDIO ?= 1
64endif
65
66
172d42d9 67ifeq "$(BUILD_TYPE)" "Checked"
8fcce42e 68OPTIMIZATION ?= 2
6c79da43
AG
69MRI_ENABLE = 1
70MRI_SEMIHOST_STDIO ?= 1
71endif
72
73MRI_INIT_PARAMETERS=$(MRI_UART)
74
8fcce42e 75# Output Object Directory
3d11c05c 76OUTDIR=../$(DEVICE)
8fcce42e 77
4cff3ded 78# List of sources to be compiled/assembled
d4ee6ee2 79CSRCS1 = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c $(SRC)/*/*/*/*/*/*.c)
df930a5f 80# Totally exclude network if NONETWORK is defined
d4ee6ee2 81ifeq "$(NONETWORK)" "1"
93ea6adb 82CSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CSRCS1))
d4ee6ee2
JM
83DEFINES += -DNONETWORK
84else
93ea6adb 85CSRCS2 = $(CSRCS1)
d4ee6ee2
JM
86endif
87
93ea6adb
JM
88# do not compile the src/testframework as that can only be done with rake
89CSRCS = $(filter-out $(SRC)/testframework/%,$(CSRCS2))
90
718eaf93
JM
91ifeq "$(DISABLEMSD)" "1"
92DEFINES += -DDISABLEMSD
93endif
94
068f6a12 95ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
172d42d9
AG
96ifneq "$(OS)" "Windows_NT"
97ASRCS += $(wildcard $(SRC)/*.s $(SRC)/*/*.s $(SRC)/*/*/*.s $(SRC)/*/*/*/*.s $(SRC)/*/*/*/*/*.s)
98endif
d4ee6ee2
JM
99CPPSRCS1 = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp $(SRC)/*/*/*/*/*/*.cpp)
100ifeq "$(NONETWORK)" "1"
df930a5f 101CPPSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CPPSRCS1))
d4ee6ee2 102else
df930a5f 103CPPSRCS2 = $(CPPSRCS1)
d4ee6ee2 104endif
4cff3ded 105
df930a5f
JM
106# Totally exclude any modules listed in EXCLUDE_MODULES
107# uppercase function
108uc = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))
109EXL = $(patsubst %,$(SRC)/modules/%/%,$(EXCLUDED_MODULES))
93ea6adb 110CPPSRCS3 = $(filter-out $(EXL),$(CPPSRCS2))
df930a5f
JM
111DEFINES += $(call uc, $(subst /,_,$(patsubst %,-DNO_%,$(EXCLUDED_MODULES))))
112
93ea6adb
JM
113# do not compile the src/testframework as that can only be done with rake
114CPPSRCS = $(filter-out $(SRC)/testframework/%,$(CPPSRCS3))
115
4cff3ded 116# List of the objects files to be compiled/assembled
172d42d9 117OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.s,$(OUTDIR)/%.o,$(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS))) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
8fcce42e 118
172d42d9
AG
119# Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
120OBJECTS += $(OUTDIR)/mbed_custom.o
8fcce42e 121
33110301
L
122OBJECTS += $(OUTDIR)/configdefault.o
123
8fcce42e
AG
124# List of the header dependency files, one per object file.
125DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
126
127# Linker script to be used. Indicates what code should be placed where in memory.
172d42d9 128LSCRIPT=$(MBED_DIR)/$(DEVICE)/GCC_ARM/$(DEVICE).ld
4cff3ded
AW
129
130# Location of external library and header dependencies.
172d42d9
AG
131MBED_DIR = $(BUILD_DIR)/../mbed/drop
132MRI_DIR = $(BUILD_DIR)/../mri
4cff3ded 133
068f6a12
AW
134# Include path which points to external library headers and to subdirectories of this project which contain headers.
135SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
136PROJINCS = $(sort $(dir $(SUBDIRS)))
172d42d9 137INCDIRS += $(SRC) $(PROJINCS) $(MRI_DIR) $(MBED_DIR) $(MBED_DIR)/$(DEVICE)
4cff3ded
AW
138
139# DEFINEs to be used when building C/C++ code
172d42d9 140DEFINES += -DTARGET_$(DEVICE)
dc735ba6 141DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
8fcce42e 142DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
383036c1 143DEFINES += -DWRITE_BUFFER_DISABLE=$(WRITE_BUFFER_DISABLE) -DSTACK_SIZE=$(STACK_SIZE)
4cff3ded 144
d9664db4
AG
145ifeq "$(OPTIMIZATION)" "0"
146DEFINES += -DDEBUG
147endif
148
4cff3ded 149# Libraries to be linked into final binary
172d42d9 150MBED_LIBS = $(MBED_DIR)/$(DEVICE)/GCC_ARM/libmbed.a
3b02cd71
JM
151#SYS_LIBS = -lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys
152SYS_LIBS = -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys
dc735ba6 153LIBS = $(LIBS_PREFIX)
8fcce42e
AG
154
155ifeq "$(MRI_ENABLE)" "1"
172d42d9 156LIBS += $(MRI_DIR)/mri.ar
8fcce42e
AG
157endif
158
8fcce42e
AG
159LIBS += $(MBED_LIBS)
160LIBS += $(SYS_LIBS)
161LIBS += $(LIBS_SUFFIX)
4cff3ded 162
8fcce42e
AG
163# Compiler flags used to enable creation of header dependencies.
164DEPFLAGS = -MMD -MP
165
dc735ba6 166# Setup wraps for newlib read/writes to redirect to MRI debugger.
383036c1
AG
167ifeq "$(MRI_ENABLE)" "1"
168MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
169else
170MRI_WRAPS=
171endif
172
173# Setup wraps to memory allocations routines if we want to tag heap allocations.
383036c1 174ifeq "$(HEAP_TAGS)" "1"
383036c1
AG
175DEFINES += -DHEAP_TAGS
176endif
177
8fcce42e 178# Compiler Options
172d42d9
AG
179GCFLAGS += -O$(OPTIMIZATION) -g3 $(DEVICE_CFLAGS)
180GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
58baeec1
MM
181GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
182GCFLAGS += $(DEFINES)
183GCFLAGS += $(DEPFLAGS)
184GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
185
172d42d9
AG
186GPFLAGS += $(GCFLAGS) -fno-rtti -std=gnu++11
187
188AS_GCFLAGS += -g3 $(DEVICE_FLAGS) -x assembler-with-cpp
189AS_GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
190AS_FLAGS += -g3 $(DEVICE_FLAGS)
58baeec1 191
172d42d9 192
8fcce42e 193# Linker Options.
dc735ba6 194LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec
b4e5c830 195LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free$(MRI_WRAPS)
383036c1 196LDFLAGS += -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
172d42d9
AG
197ifneq "$(NO_FLOAT_SCANF)" "1"
198LDFLAGS += -u _scanf_float
199endif
200ifneq "$(NO_FLOAT_PRINTF)" "1"
201LDFLAGS += -u _printf_float
202endif
4cff3ded 203
4cff3ded
AW
204
205# Compiler/Assembler/Linker Paths
8fcce42e 206GCC = arm-none-eabi-gcc
4cff3ded 207GPP = arm-none-eabi-g++
172d42d9 208AS = arm-none-eabi-as
4cff3ded
AW
209LD = arm-none-eabi-g++
210OBJCOPY = arm-none-eabi-objcopy
211OBJDUMP = arm-none-eabi-objdump
4cff3ded 212SIZE = arm-none-eabi-size
068f6a12 213
8fcce42e
AG
214# Some tools are different on Windows in comparison to Unix.
215ifeq "$(OS)" "Windows_NT"
216REMOVE = del
13e4a3f9 217SHELL=cmd.exe
8fcce42e
AG
218REMOVE_DIR = rd /s /q
219MKDIR = mkdir
220QUIET=>nul 2>nul & exit 0
172d42d9 221BLANK_LINE=echo -
8fcce42e
AG
222else
223REMOVE = rm
224REMOVE_DIR = rm -r -f
225MKDIR = mkdir -p
226QUIET=> /dev/null 2>&1 ; exit 0
172d42d9 227BLANK_LINE=echo
8fcce42e
AG
228endif
229
230# Create macro which will convert / to \ on Windows.
231ifeq "$(OS)" "Windows_NT"
232define convert-slash
233$(subst /,\,$1)
234endef
235else
236define convert-slash
237$1
238endef
068f6a12 239endif
4cff3ded
AW
240
241#########################################################################
172d42d9
AG
242.PHONY: all clean size
243
244all:: $(OUTDIR)/$(PROJECT).hex $(OUTDIR)/$(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm size
245
246$(OUTDIR)/$(PROJECT).bin: $(OUTDIR)/$(PROJECT).elf
247 @echo Extracting $@
248 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
249 $(Q) $(OBJCOPY) -O binary $< $@
250
251$(OUTDIR)/$(PROJECT).hex: $(OUTDIR)/$(PROJECT).elf
252 @echo Extracting $@
253 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
254 $(Q) $(OBJCOPY) -R .stack -O ihex $< $@
dc735ba6 255
172d42d9
AG
256$(OUTDIR)/$(PROJECT).disasm: $(OUTDIR)/$(PROJECT).elf
257 @echo Extracting disassembly to $@
258 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
259 $(Q) $(OBJDUMP) -d -f -M reg-names-std --demangle $< >$@
dc735ba6 260
172d42d9
AG
261$(OUTDIR)/$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
262 @echo Linking $@
263 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
264 $(Q) $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
265
266size: $(OUTDIR)/$(PROJECT).elf
267 $(Q) $(SIZE) $<
268 @$(BLANK_LINE)
4cff3ded 269
4cff3ded 270clean:
172d42d9
AG
271 @echo Cleaning up all build generated files
272 $(Q) $(REMOVE_DIR) $(OUTDIR) $(QUIET)
8fcce42e
AG
273
274-include $(DEPFILES)
068f6a12 275
4cff3ded
AW
276#########################################################################
277# Default rules to compile .c and .cpp file to .o
278# and assemble .s files to .o
279
383036c1 280$(OUTDIR)/mbed_custom.o : $(BUILD_DIR)/mbed_custom.cpp makefile
172d42d9
AG
281 @echo Compiling $<
282 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
383036c1 283 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
8fcce42e 284
64c668c2 285$(OUTDIR)/%.o : %.cpp makefile
172d42d9
AG
286 @echo Compiling $<
287 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
288 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
4cff3ded 289
64c668c2 290$(OUTDIR)/%.o : %.c makefile
172d42d9
AG
291 @echo Compiling $<
292 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
293 $(Q) $(GCC) $(GCFLAGS) -c $< -o $@
4cff3ded 294
64c668c2 295$(OUTDIR)/%.o : %.S makefile
172d42d9
AG
296 @echo Assembling $<
297 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
298 $(Q) $(GCC) $(AS_GCFLAGS) -c $< -o $@
4cff3ded 299
172d42d9
AG
300$(OUTDIR)/%.o : %.s makefile
301 @echo Assembling $<
302 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
303 $(Q) $(AS) $(AS_FLAGS) -o $@ $<
304
33110301 305$(OUTDIR)/configdefault.o : config.default
dc735ba6 306 $(Q) $(OBJCOPY) -I binary -O elf32-littlearm -B arm --readonly-text --rename-section .data=.rodata.configdefault $< $@
33110301
L
307
308#########################################################################