Switch back to ugly size output.
[clinton/Smoothieware.git] / gcc4mbed / build / gcc4mbed.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#
58baeec1 21# Updates:
4cff3ded
AW
22# Arthur Wolf & Adam Green in 2011 - Updated to work with mbed.
23###############################################################################
24# USAGE:
25# Variables that must be defined in including makefile.
26# PROJECT: Name to be given to the output binary for this project.
27# SRC: The root directory for the sources of your project.
28# GCC4MED_DIR: The root directory for where the gcc4mbed sources are located
29# in your project. This should point to the parent directory
30# of the build directory which contains this gcc4mbed.mk file.
31# LIBS_PREFIX: List of library/object files to prepend to mbed.ar capi.ar libs.
32# LIBS_SUFFIX: List of library/object files to append to mbed.ar capi.ar libs.
6c79da43
AG
33# GCC4MBED_TYPE: Type of build to produce. Allowed values are:
34# Debug - Build for debugging. Disables optimizations and
58baeec1 35# links in debug MRI runtime. Best debugging
6c79da43
AG
36# experience.
37# Release - Build for release with no debug support.
38# Checked - Release build with debug support. Due to
39# optimizations, debug experience won't be as good
40# as Debug but might be needed when bugs don't
41# reproduce in Debug builds.
42# default: Release
43# MRI_BREAK_ON_INIT: Should the program halt before calling into main(),
44# allowing the developer time to set breakpoints in main()
45# or in code run from within global constructors.
46# default: 1 - break on init.
47# MRI_SEMIHOST_STDIO: Set to non-zero value to allow debug monitor to use
48# semi-host calls to redirect stdin/stdout/stderr to the
49# gdb console.
50# default: 1 for Debug/Checked builds and 0 for Release.
51# MRI_UART: Select the UART to be used by the debugger. See mri.h for
52# allowed values.
53# default: MRI_UART_MBED_USB - Use USB based UART on the mbed.
4cff3ded
AW
54# Example makefile:
55# PROJECT=HelloWorld
56# SRC=.
57# GCC4MBED_DIR=../..
58# LIBS_PREFIX=../agutil/agutil.ar
59# LIBS_SUFFIX=
60#
61# include ../../build/gcc4mbed.mk
58baeec1 62#
4cff3ded
AW
63###############################################################################
64
6c79da43
AG
65# Check for undefined variables.
66ifndef PROJECT
67$(error makefile must set PROJECT variable.)
13e4a3f9
AW
68endif
69
6c79da43
AG
70ifndef GCC4MBED_DIR
71$(error makefile must set GCC4MBED_DIR.)
4cff3ded
AW
72endif
73
6c79da43
AG
74
75# Default variables.
76SRC ?= .
6c79da43
AG
77GCC4MBED_TYPE ?= Release
78MRI_BREAK_ON_INIT ?= 1
79MRI_UART ?= MRI_UART_MBED_USB
80
81
82# Configure MRI variables based on GCC4MBED_TYPE build type variable.
83ifeq "$(GCC4MBED_TYPE)" "Release"
8fcce42e 84OPTIMIZATION ?= 2
6c79da43
AG
85MRI_ENABLE = 0
86MRI_SEMIHOST_STDIO ?= 0
87endif
88
89
90ifeq "$(GCC4MBED_TYPE)" "Debug"
91OPTIMIZATION = 0
92MRI_ENABLE = 1
93MRI_SEMIHOST_STDIO ?= 1
94endif
95
96
97ifeq "$(GCC4MBED_TYPE)" "Checked"
8fcce42e 98OPTIMIZATION ?= 2
6c79da43
AG
99MRI_ENABLE = 1
100MRI_SEMIHOST_STDIO ?= 1
101endif
102
103MRI_INIT_PARAMETERS=$(MRI_UART)
104
105
8fcce42e
AG
106# Output Object Directory
107OUTDIR=LPC176x
108
4cff3ded 109# List of sources to be compiled/assembled
068f6a12
AW
110CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c)
111ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
112CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp)
4cff3ded 113
4cff3ded 114# List of the objects files to be compiled/assembled
8fcce42e
AG
115OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS)) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
116
117# Add in the GCC4MBED stubs which allow hooking in the MRI debug monitor.
118OBJECTS += $(OUTDIR)/gcc4mbed.o
119
120# List of the header dependency files, one per object file.
121DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
122
123# Linker script to be used. Indicates what code should be placed where in memory.
4cff3ded
AW
124LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld
125
126# Location of external library and header dependencies.
127EXTERNAL_DIR = $(GCC4MBED_DIR)/external
128
068f6a12
AW
129# Include path which points to external library headers and to subdirectories of this project which contain headers.
130SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
131PROJINCS = $(sort $(dir $(SUBDIRS)))
8fcce42e 132INCDIRS += $(PROJINCS) $(GCC4MBED_DIR)/mri $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768
4cff3ded
AW
133
134# DEFINEs to be used when building C/C++ code
d9664db4 135DEFINES += -DTARGET_LPC1768 -D__LPC17XX__
58baeec1 136DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
8fcce42e 137DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
4cff3ded 138
d9664db4
AG
139ifeq "$(OPTIMIZATION)" "0"
140DEFINES += -DDEBUG
141endif
142
4cff3ded 143# Libraries to be linked into final binary
8fcce42e
AG
144MBED_LIBS = $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libmbed.a $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libcapi.a
145SYS_LIBS = -lstdc++ -lsupc++ -lm -lgcc -lc -lgcc -lc -lnosys
58baeec1 146LIBS = $(LIBS_PREFIX)
8fcce42e
AG
147
148ifeq "$(MRI_ENABLE)" "1"
149LIBS += $(GCC4MBED_DIR)/mri/mri.ar
150endif
151
152LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/startup_LPC17xx.o
153LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/cmsis_nvic.o
154LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/core_cm3.o
155LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/system_LPC17xx.o
156LIBS += $(MBED_LIBS)
157LIBS += $(SYS_LIBS)
158LIBS += $(LIBS_SUFFIX)
4cff3ded 159
8fcce42e
AG
160# Compiler flags used to enable creation of header dependencies.
161DEPFLAGS = -MMD -MP
162
163# Compiler Options
58baeec1
MM
164
165# C/C++ flags
a3a2398f 166GCFLAGS = -O$(OPTIMIZATION) -g3 -mcpu=cortex-m3 -mthumb -mthumb-interwork
58baeec1
MM
167GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions
168GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
169GCFLAGS += $(DEFINES)
170GCFLAGS += $(DEPFLAGS)
171GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
172
173# C++ only flags
174GPFLAGS = $(GCFLAGS)
58baeec1
MM
175GPFLAGS += -std=gnu++0x
176# GPFLAGS += ...
177
178# Setup wraps for newlib read/writes to redirect to MRI debugger.
8fcce42e
AG
179ifeq "$(MRI_ENABLE)" "1"
180MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
181else
182MRI_WRAP=
183endif
4cff3ded 184
8fcce42e
AG
185# Linker Options.
186LDFLAGS = -mcpu=cortex-m3 -mthumb -O$(OPTIMIZATION) -specs=$(GCC4MBED_DIR)/build/startfile.spec -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty$(MRI_WRAPS) -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
4cff3ded
AW
187
188ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
189ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
190
191# Compiler/Assembler/Linker Paths
8fcce42e 192GCC = arm-none-eabi-gcc
4cff3ded
AW
193GPP = arm-none-eabi-g++
194AS = arm-none-eabi-gcc
195LD = arm-none-eabi-g++
196OBJCOPY = arm-none-eabi-objcopy
197OBJDUMP = arm-none-eabi-objdump
4cff3ded 198SIZE = arm-none-eabi-size
068f6a12 199
8fcce42e
AG
200# Some tools are different on Windows in comparison to Unix.
201ifeq "$(OS)" "Windows_NT"
202REMOVE = del
13e4a3f9 203SHELL=cmd.exe
8fcce42e
AG
204REMOVE_DIR = rd /s /q
205MKDIR = mkdir
206QUIET=>nul 2>nul & exit 0
207else
208REMOVE = rm
209REMOVE_DIR = rm -r -f
210MKDIR = mkdir -p
211QUIET=> /dev/null 2>&1 ; exit 0
212endif
213
214# Create macro which will convert / to \ on Windows.
215ifeq "$(OS)" "Windows_NT"
216define convert-slash
217$(subst /,\,$1)
218endef
219else
220define convert-slash
221$1
222endef
068f6a12 223endif
4cff3ded
AW
224
225#########################################################################
58baeec1 226.PHONY: all clean deploy size
4cff3ded 227
58baeec1 228all:: $(PROJECT).hex $(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm size
4cff3ded
AW
229
230$(PROJECT).bin: $(PROJECT).elf
58baeec1
MM
231 @echo " OBJCOPY " $@
232 @$(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
4cff3ded
AW
233
234$(PROJECT).hex: $(PROJECT).elf
58baeec1
MM
235 @echo " OBJCOPY " $@
236 @$(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
237
8fcce42e 238$(OUTDIR)/$(PROJECT).disasm: $(PROJECT).elf
58baeec1 239 @echo " DISASM " $@
00e0a1b3 240 @$(OBJDUMP) -d -f -M reg-names-std --demangle $(PROJECT).elf >$(OUTDIR)/$(PROJECT).disasm
58baeec1 241
8fcce42e 242$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
58baeec1
MM
243 @echo " LD " $@
244 @$(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
245 @$(SIZE) $(PROJECT).elf
4cff3ded 246
4cff3ded 247clean:
58baeec1
MM
248 @echo " RM " "*.o"
249 @$(REMOVE) -f $(call convert-slash,$(OBJECTS)) $(QUIET)
250 @echo " RM " "*.dep"
251 @$(REMOVE) -f $(call convert-slash,$(DEPFILES)) $(QUIET)
252 @echo " RM " $(OUTDIR)/
253 @$(REMOVE_DIR) $(OUTDIR) $(QUIET)
254 @echo " RM " "$(PROJECT).map"
255 @$(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).map) $(QUIET)
256 @echo " RM " "$(PROJECT).disasm"
257 @$(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).disasm) $(QUIET)
258 @echo " RM " "$(PROJECT).bin"
259 @$(REMOVE) -f $(PROJECT).bin $(QUIET)
260 @echo " RM " "$(PROJECT).hex"
261 @$(REMOVE) -f $(PROJECT).hex $(QUIET)
262 @echo " RM " "$(PROJECT).elf"
263 @$(REMOVE) -f $(PROJECT).elf $(QUIET)
264
265size: $(PROJECT).elf
450b1b4b 266 @$(SIZE) $(PROJECT).elf
8fcce42e
AG
267
268-include $(DEPFILES)
068f6a12
AW
269
270ifdef LPC_DEPLOY
271DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY))
272deploy:
273 $(DEPLOY_COMMAND)
274endif
4cff3ded
AW
275
276#########################################################################
277# Default rules to compile .c and .cpp file to .o
278# and assemble .s files to .o
279
64c668c2 280$(OUTDIR)/gcc4mbed.o : $(GCC4MBED_DIR)/src/gcc4mbed.c makefile
58baeec1
MM
281 @echo " CC " $<
282 @$(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
283 @$(GPP) $(GPFLAGS) -c $< -o $@
8fcce42e 284
64c668c2 285$(OUTDIR)/%.o : %.cpp makefile
58baeec1
MM
286 @echo " CC " $<
287 @$(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
288# if you want to see the whole compile command, remove the @ preceding the line below
289 @$(GPP) $(GPFLAGS) -c $< -o $@
4cff3ded 290
64c668c2 291$(OUTDIR)/%.o : %.c makefile
58baeec1
MM
292 @echo " CC " $<
293 @$(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
294 @$(GCC) $(GCFLAGS) -c $< -o $@
4cff3ded 295
64c668c2 296$(OUTDIR)/%.o : %.S makefile
58baeec1
MM
297 @echo " AS " $<
298 @$(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
299 @$(AS) $(ASFLAGS) -c $< -o $@
4cff3ded 300
672298b2 301#########################################################################