Update README to no longer mention GCC4MBED.
[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#
172d42d9
AG
21# Updates:
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
068f6a12
AW
79CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c)
80ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
172d42d9
AG
81ifneq "$(OS)" "Windows_NT"
82ASRCS += $(wildcard $(SRC)/*.s $(SRC)/*/*.s $(SRC)/*/*/*.s $(SRC)/*/*/*/*.s $(SRC)/*/*/*/*/*.s)
83endif
068f6a12 84CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp)
4cff3ded 85
4cff3ded 86# List of the objects files to be compiled/assembled
172d42d9 87OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.s,$(OUTDIR)/%.o,$(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS))) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
8fcce42e 88
172d42d9
AG
89# Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
90OBJECTS += $(OUTDIR)/mbed_custom.o
8fcce42e
AG
91
92# List of the header dependency files, one per object file.
93DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
94
95# Linker script to be used. Indicates what code should be placed where in memory.
172d42d9 96LSCRIPT=$(MBED_DIR)/$(DEVICE)/GCC_ARM/$(DEVICE).ld
4cff3ded
AW
97
98# Location of external library and header dependencies.
172d42d9
AG
99MBED_DIR = $(BUILD_DIR)/../mbed/drop
100MRI_DIR = $(BUILD_DIR)/../mri
4cff3ded 101
068f6a12
AW
102# Include path which points to external library headers and to subdirectories of this project which contain headers.
103SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
104PROJINCS = $(sort $(dir $(SUBDIRS)))
172d42d9 105INCDIRS += $(SRC) $(PROJINCS) $(MRI_DIR) $(MBED_DIR) $(MBED_DIR)/$(DEVICE)
4cff3ded
AW
106
107# DEFINEs to be used when building C/C++ code
172d42d9
AG
108DEFINES += -DTARGET_$(DEVICE)
109DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
8fcce42e 110DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
383036c1 111DEFINES += -DWRITE_BUFFER_DISABLE=$(WRITE_BUFFER_DISABLE) -DSTACK_SIZE=$(STACK_SIZE)
4cff3ded 112
d9664db4
AG
113ifeq "$(OPTIMIZATION)" "0"
114DEFINES += -DDEBUG
115endif
116
4cff3ded 117# Libraries to be linked into final binary
172d42d9
AG
118MBED_LIBS = $(MBED_DIR)/$(DEVICE)/GCC_ARM/libmbed.a
119SYS_LIBS = -lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys
120LIBS = $(LIBS_PREFIX)
8fcce42e
AG
121
122ifeq "$(MRI_ENABLE)" "1"
172d42d9 123LIBS += $(MRI_DIR)/mri.ar
8fcce42e
AG
124endif
125
8fcce42e
AG
126LIBS += $(MBED_LIBS)
127LIBS += $(SYS_LIBS)
128LIBS += $(LIBS_SUFFIX)
4cff3ded 129
8fcce42e
AG
130# Compiler flags used to enable creation of header dependencies.
131DEPFLAGS = -MMD -MP
132
383036c1
AG
133# Setup wraps for newlib read/writes to redirect to MRI debugger.
134ifeq "$(MRI_ENABLE)" "1"
135MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
136else
137MRI_WRAPS=
138endif
139
140# Setup wraps to memory allocations routines if we want to tag heap allocations.
141HEAP_WRAPS=
142ifeq "$(HEAP_TAGS)" "1"
143HEAP_WRAPS=,--wrap=malloc,--wrap=realloc,--wrap=free
144DEFINES += -DHEAP_TAGS
145endif
146
8fcce42e 147# Compiler Options
172d42d9
AG
148GCFLAGS += -O$(OPTIMIZATION) -g3 $(DEVICE_CFLAGS)
149GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
58baeec1
MM
150GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
151GCFLAGS += $(DEFINES)
152GCFLAGS += $(DEPFLAGS)
153GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
154
172d42d9
AG
155GPFLAGS += $(GCFLAGS) -fno-rtti -std=gnu++11
156
157AS_GCFLAGS += -g3 $(DEVICE_FLAGS) -x assembler-with-cpp
158AS_GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
159AS_FLAGS += -g3 $(DEVICE_FLAGS)
58baeec1 160
172d42d9 161
8fcce42e 162# Linker Options.
383036c1
AG
163LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec
164LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty$(MRI_WRAPS)$(HEAP_WRAPS)
165LDFLAGS += -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
172d42d9
AG
166ifneq "$(NO_FLOAT_SCANF)" "1"
167LDFLAGS += -u _scanf_float
168endif
169ifneq "$(NO_FLOAT_PRINTF)" "1"
170LDFLAGS += -u _printf_float
171endif
4cff3ded 172
4cff3ded
AW
173
174# Compiler/Assembler/Linker Paths
8fcce42e 175GCC = arm-none-eabi-gcc
4cff3ded 176GPP = arm-none-eabi-g++
172d42d9 177AS = arm-none-eabi-as
4cff3ded
AW
178LD = arm-none-eabi-g++
179OBJCOPY = arm-none-eabi-objcopy
180OBJDUMP = arm-none-eabi-objdump
4cff3ded 181SIZE = arm-none-eabi-size
068f6a12 182
8fcce42e
AG
183# Some tools are different on Windows in comparison to Unix.
184ifeq "$(OS)" "Windows_NT"
185REMOVE = del
13e4a3f9 186SHELL=cmd.exe
8fcce42e
AG
187REMOVE_DIR = rd /s /q
188MKDIR = mkdir
189QUIET=>nul 2>nul & exit 0
172d42d9 190BLANK_LINE=echo -
8fcce42e
AG
191else
192REMOVE = rm
193REMOVE_DIR = rm -r -f
194MKDIR = mkdir -p
195QUIET=> /dev/null 2>&1 ; exit 0
172d42d9 196BLANK_LINE=echo
8fcce42e
AG
197endif
198
199# Create macro which will convert / to \ on Windows.
200ifeq "$(OS)" "Windows_NT"
201define convert-slash
202$(subst /,\,$1)
203endef
204else
205define convert-slash
206$1
207endef
068f6a12 208endif
4cff3ded
AW
209
210#########################################################################
172d42d9
AG
211.PHONY: all clean size
212
213all:: $(OUTDIR)/$(PROJECT).hex $(OUTDIR)/$(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm size
214
215$(OUTDIR)/$(PROJECT).bin: $(OUTDIR)/$(PROJECT).elf
216 @echo Extracting $@
217 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
218 $(Q) $(OBJCOPY) -O binary $< $@
219
220$(OUTDIR)/$(PROJECT).hex: $(OUTDIR)/$(PROJECT).elf
221 @echo Extracting $@
222 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
223 $(Q) $(OBJCOPY) -R .stack -O ihex $< $@
224
225$(OUTDIR)/$(PROJECT).disasm: $(OUTDIR)/$(PROJECT).elf
226 @echo Extracting disassembly to $@
227 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
228 $(Q) $(OBJDUMP) -d -f -M reg-names-std --demangle $< >$@
229
230$(OUTDIR)/$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
231 @echo Linking $@
232 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
233 $(Q) $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
234
235size: $(OUTDIR)/$(PROJECT).elf
236 $(Q) $(SIZE) $<
237 @$(BLANK_LINE)
4cff3ded 238
4cff3ded 239clean:
172d42d9
AG
240 @echo Cleaning up all build generated files
241 $(Q) $(REMOVE_DIR) $(OUTDIR) $(QUIET)
8fcce42e
AG
242
243-include $(DEPFILES)
068f6a12 244
4cff3ded
AW
245#########################################################################
246# Default rules to compile .c and .cpp file to .o
247# and assemble .s files to .o
248
383036c1 249$(OUTDIR)/mbed_custom.o : $(BUILD_DIR)/mbed_custom.cpp makefile
172d42d9
AG
250 @echo Compiling $<
251 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
383036c1 252 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
8fcce42e 253
64c668c2 254$(OUTDIR)/%.o : %.cpp makefile
172d42d9
AG
255 @echo Compiling $<
256 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
257 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
4cff3ded 258
64c668c2 259$(OUTDIR)/%.o : %.c makefile
172d42d9
AG
260 @echo Compiling $<
261 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
262 $(Q) $(GCC) $(GCFLAGS) -c $< -o $@
4cff3ded 263
64c668c2 264$(OUTDIR)/%.o : %.S makefile
172d42d9
AG
265 @echo Assembling $<
266 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
267 $(Q) $(GCC) $(AS_GCFLAGS) -c $< -o $@
4cff3ded 268
172d42d9
AG
269$(OUTDIR)/%.o : %.s makefile
270 @echo Assembling $<
271 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
272 $(Q) $(AS) $(AS_FLAGS) -o $@ $<
273
274#########################################################################