re-enabling serial
[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.
13e4a3f9
AW
33# GCC4MBED_DELAYED_STDIO_INIT: Set to non-zero value to have intialization of
34# stdin/stdout/stderr delayed which will
35# shrink the size of the resulting binary if
36# APIs like printf(), scanf(), etc. aren't used.
6c79da43
AG
37# GCC4MBED_TYPE: Type of build to produce. Allowed values are:
38# Debug - Build for debugging. Disables optimizations and
39# links in debug MRI runtime. Best debugging
40# experience.
41# Release - Build for release with no debug support.
42# Checked - Release build with debug support. Due to
43# optimizations, debug experience won't be as good
44# as Debug but might be needed when bugs don't
45# reproduce in Debug builds.
46# default: Release
47# MRI_BREAK_ON_INIT: Should the program halt before calling into main(),
48# allowing the developer time to set breakpoints in main()
49# or in code run from within global constructors.
50# default: 1 - break on init.
51# MRI_SEMIHOST_STDIO: Set to non-zero value to allow debug monitor to use
52# semi-host calls to redirect stdin/stdout/stderr to the
53# gdb console.
54# default: 1 for Debug/Checked builds and 0 for Release.
55# MRI_UART: Select the UART to be used by the debugger. See mri.h for
56# allowed values.
57# default: MRI_UART_MBED_USB - Use USB based UART on the mbed.
4cff3ded
AW
58# Example makefile:
59# PROJECT=HelloWorld
60# SRC=.
61# GCC4MBED_DIR=../..
62# LIBS_PREFIX=../agutil/agutil.ar
63# LIBS_SUFFIX=
64#
65# include ../../build/gcc4mbed.mk
66#
67###############################################################################
68
6c79da43
AG
69# Check for undefined variables.
70ifndef PROJECT
71$(error makefile must set PROJECT variable.)
13e4a3f9
AW
72endif
73
6c79da43
AG
74ifndef GCC4MBED_DIR
75$(error makefile must set GCC4MBED_DIR.)
4cff3ded
AW
76endif
77
6c79da43
AG
78
79# Default variables.
80SRC ?= .
81GCC4MBED_DELAYED_STDIO_INIT ?= 0
82GCC4MBED_TYPE ?= Release
83MRI_BREAK_ON_INIT ?= 1
84MRI_UART ?= MRI_UART_MBED_USB
85
86
87# Configure MRI variables based on GCC4MBED_TYPE build type variable.
88ifeq "$(GCC4MBED_TYPE)" "Release"
89OPTIMIZATION = 2
90MRI_ENABLE = 0
91MRI_SEMIHOST_STDIO ?= 0
92endif
93
94
95ifeq "$(GCC4MBED_TYPE)" "Debug"
96OPTIMIZATION = 0
97MRI_ENABLE = 1
98MRI_SEMIHOST_STDIO ?= 1
99endif
100
101
102ifeq "$(GCC4MBED_TYPE)" "Checked"
103OPTIMIZATION = 2
104MRI_ENABLE = 1
105MRI_SEMIHOST_STDIO ?= 1
106endif
107
108MRI_INIT_PARAMETERS=$(MRI_UART)
109
110
4cff3ded 111# List of sources to be compiled/assembled
068f6a12
AW
112CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c)
113ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
114CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp)
4cff3ded
AW
115
116# Add in the gcc4mbed shim sources that allow mbed code build under GCC
117CSRCS += $(GCC4MBED_DIR)/src/gcc4mbed.c $(GCC4MBED_DIR)/src/syscalls.c
118
119# List of the objects files to be compiled/assembled
120OBJECTS= $(CSRCS:.c=.o) $(ASRCS:.S=.o) $(CPPSRCS:.cpp=.o)
121LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld
122
123# Location of external library and header dependencies.
124EXTERNAL_DIR = $(GCC4MBED_DIR)/external
125
068f6a12
AW
126# Include path which points to external library headers and to subdirectories of this project which contain headers.
127SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
128PROJINCS = $(sort $(dir $(SUBDIRS)))
3c132bd0 129INCDIRS += $(PROJINCS) $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768 $(EXTERNAL_DIR)/FATFileSystem $(GCC4MBED_DIR)/mri
4cff3ded
AW
130
131# DEFINEs to be used when building C/C++ code
6c79da43
AG
132DEFINES = -DTARGET_LPC1768 -DGCC4MBED_DELAYED_STDIO_INIT=$(GCC4MBED_DELAYED_STDIO_INIT)
133DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"' -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT)
134DEFINES += -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
4cff3ded
AW
135
136# Libraries to be linked into final binary
3c132bd0 137LIBS = $(LIBS_PREFIX) $(GCC4MBED_DIR)/mri/mri.ar $(EXTERNAL_DIR)/mbed/LPC1768/mbed.ar $(EXTERNAL_DIR)/mbed/LPC1768/capi.ar $(EXTERNAL_DIR)/FATFileSystem/LPC1768/FATFileSystem.ar $(LIBS_SUFFIX)
4cff3ded 138
4cff3ded 139# Compiler Options
3c132bd0 140GPFLAGS = -O$(OPTIMIZATION) -gstabs+3 -mcpu=cortex-m3 -mthumb -mthumb-interwork -fshort-wchar -ffunction-sections -fdata-sections -fpromote-loop-indices -Wall -Wextra -Wimplicit -Wcast-align -Wpointer-arith -Wredundant-decls -Wshadow -Wcast-qual -Wcast-align -fno-exceptions
4cff3ded
AW
141GPFLAGS += $(patsubst %,-I%,$(INCDIRS))
142GPFLAGS += $(DEFINES)
143
144LDFLAGS = -mcpu=cortex-m3 -mthumb -O$(OPTIMIZATION) -Wl,-Map=$(PROJECT).map,--cref,--gc-sections,--no-wchar-size-warning -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
145
146ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
147ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
148
149# Compiler/Assembler/Linker Paths
150GPP = arm-none-eabi-g++
151AS = arm-none-eabi-gcc
152LD = arm-none-eabi-g++
153OBJCOPY = arm-none-eabi-objcopy
154OBJDUMP = arm-none-eabi-objdump
4cff3ded 155SIZE = arm-none-eabi-size
068f6a12
AW
156REMOVE = rm
157
13e4a3f9 158# Switch to cs-rm on Windows and make sure that cmd.exe is used as shell.
068f6a12
AW
159ifeq "$(MAKE)" "cs-make"
160REMOVE = cs-rm
13e4a3f9 161SHELL=cmd.exe
068f6a12 162endif
4cff3ded
AW
163
164#########################################################################
3c132bd0 165.PHONY: all clean deploy
4cff3ded
AW
166
167all:: $(PROJECT).hex $(PROJECT).bin $(PROJECT).disasm
168
169$(PROJECT).bin: $(PROJECT).elf
170 $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
171
172$(PROJECT).hex: $(PROJECT).elf
173 $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
174
175$(PROJECT).disasm: $(PROJECT).elf
176 $(OBJDUMP) -d $(PROJECT).elf >$(PROJECT).disasm
177
3c132bd0 178$(PROJECT).elf: $(LSCRIPT) $(OBJECTS) $(LIBS)
4cff3ded
AW
179 $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
180 $(SIZE) $(PROJECT).elf
181
4cff3ded 182clean:
068f6a12
AW
183 $(REMOVE) -f $(OBJECTS)
184 $(REMOVE) -f $(PROJECT).hex
185 $(REMOVE) -f $(PROJECT).elf
186 $(REMOVE) -f $(PROJECT).map
187 $(REMOVE) -f $(PROJECT).bin
188 $(REMOVE) -f $(PROJECT).disasm
189
190ifdef LPC_DEPLOY
191DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY))
192deploy:
193 $(DEPLOY_COMMAND)
194endif
4cff3ded
AW
195
196#########################################################################
197# Default rules to compile .c and .cpp file to .o
198# and assemble .s files to .o
199
200.c.o :
201 $(GPP) $(GPFLAGS) -c $< -o $(<:.c=.o)
202
203.cpp.o :
204 $(GPP) $(GPFLAGS) -c $< -o $(<:.cpp=.o)
205
206.S.o :
207 $(AS) $(ASFLAGS) -c $< -o $(<:.S=.o)
208
6c79da43 209#########################################################################