added homing options
[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#
21# Updates:
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
35# links in debug MRI runtime. Best debugging
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
62#
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
8fcce42e
AG
135DEFINES = -DTARGET_LPC1768
136DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
137DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
4cff3ded
AW
138
139# Libraries to be linked into final binary
8fcce42e
AG
140MBED_LIBS = $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libmbed.a $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libcapi.a
141SYS_LIBS = -lstdc++ -lsupc++ -lm -lgcc -lc -lgcc -lc -lnosys
142LIBS = $(LIBS_PREFIX)
143
144ifeq "$(MRI_ENABLE)" "1"
145LIBS += $(GCC4MBED_DIR)/mri/mri.ar
146endif
147
148LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/startup_LPC17xx.o
149LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/cmsis_nvic.o
150LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/core_cm3.o
151LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/system_LPC17xx.o
152LIBS += $(MBED_LIBS)
153LIBS += $(SYS_LIBS)
154LIBS += $(LIBS_SUFFIX)
4cff3ded 155
8fcce42e
AG
156# Compiler flags used to enable creation of header dependencies.
157DEPFLAGS = -MMD -MP
158
159# Compiler Options
bd0f7508 160GPFLAGS = -O$(OPTIMIZATION) -g3 -mcpu=cortex-m3 -mthumb -mthumb-interwork
8fcce42e 161GPFLAGS += -ffunction-sections -fdata-sections -fno-exceptions
4cff3ded
AW
162GPFLAGS += $(patsubst %,-I%,$(INCDIRS))
163GPFLAGS += $(DEFINES)
8fcce42e
AG
164GPFLAGS += $(DEPFLAGS)
165GPFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
166GCFLAGS = $(GPFLAGS)
167
168# Setup wraps for newlib read/writes to redirect to MRI debugger.
169ifeq "$(MRI_ENABLE)" "1"
170MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
171else
172MRI_WRAP=
173endif
4cff3ded 174
8fcce42e
AG
175# Linker Options.
176LDFLAGS = -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
177
178ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
179ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
180
181# Compiler/Assembler/Linker Paths
8fcce42e 182GCC = arm-none-eabi-gcc
4cff3ded
AW
183GPP = arm-none-eabi-g++
184AS = arm-none-eabi-gcc
185LD = arm-none-eabi-g++
186OBJCOPY = arm-none-eabi-objcopy
187OBJDUMP = arm-none-eabi-objdump
4cff3ded 188SIZE = arm-none-eabi-size
068f6a12 189
8fcce42e
AG
190# Some tools are different on Windows in comparison to Unix.
191ifeq "$(OS)" "Windows_NT"
192REMOVE = del
13e4a3f9 193SHELL=cmd.exe
8fcce42e
AG
194REMOVE_DIR = rd /s /q
195MKDIR = mkdir
196QUIET=>nul 2>nul & exit 0
197else
198REMOVE = rm
199REMOVE_DIR = rm -r -f
200MKDIR = mkdir -p
201QUIET=> /dev/null 2>&1 ; exit 0
202endif
203
204# Create macro which will convert / to \ on Windows.
205ifeq "$(OS)" "Windows_NT"
206define convert-slash
207$(subst /,\,$1)
208endef
209else
210define convert-slash
211$1
212endef
068f6a12 213endif
4cff3ded
AW
214
215#########################################################################
3c132bd0 216.PHONY: all clean deploy
4cff3ded 217
8fcce42e 218all:: $(PROJECT).hex $(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm
4cff3ded
AW
219
220$(PROJECT).bin: $(PROJECT).elf
221 $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
222
223$(PROJECT).hex: $(PROJECT).elf
224 $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
225
8fcce42e
AG
226$(OUTDIR)/$(PROJECT).disasm: $(PROJECT).elf
227 $(OBJDUMP) -d -f -M reg-names-std $(PROJECT).elf >$(OUTDIR)/$(PROJECT).disasm
4cff3ded 228
8fcce42e 229$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
4cff3ded
AW
230 $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
231 $(SIZE) $(PROJECT).elf
232
4cff3ded 233clean:
8fcce42e
AG
234 $(REMOVE) -f $(call convert-slash,$(OBJECTS)) $(QUIET)
235 $(REMOVE) -f $(call convert-slash,$(DEPFILES)) $(QUIET)
236 $(REMOVE_DIR) $(OUTDIR) $(QUIET)
237 $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).map) $(QUIET)
238 $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).disasm) $(QUIET)
239 $(REMOVE) -f $(PROJECT).bin $(QUIET)
240 $(REMOVE) -f $(PROJECT).hex $(QUIET)
241 $(REMOVE) -f $(PROJECT).elf $(QUIET)
242
243-include $(DEPFILES)
068f6a12
AW
244
245ifdef LPC_DEPLOY
246DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY))
247deploy:
248 $(DEPLOY_COMMAND)
249endif
4cff3ded
AW
250
251#########################################################################
252# Default rules to compile .c and .cpp file to .o
253# and assemble .s files to .o
254
8fcce42e
AG
255$(OUTDIR)/gcc4mbed.o : $(GCC4MBED_DIR)/src/gcc4mbed.c
256 $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
257 $(GPP) $(GPFLAGS) -c $< -o $@
258
259$(OUTDIR)/%.o : %.cpp
260 $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
261 $(GPP) $(GPFLAGS) -c $< -o $@
4cff3ded 262
8fcce42e
AG
263$(OUTDIR)/%.o : %.c
264 $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
265 $(GCC) $(GCFLAGS) -c $< -o $@
4cff3ded 266
8fcce42e
AG
267$(OUTDIR)/%.o : %.S
268 $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
269 $(AS) $(ASFLAGS) -c $< -o $@
4cff3ded 270
672298b2 271#########################################################################