reprap discount panel working in character mode.
[clinton/Smoothieware.git] / build / common.mk
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 - 2012 - Updated to work with mbed.
23 ###############################################################################
24 # Check for undefined variables.
25 ifndef PROJECT
26 $(error makefile must set PROJECT variable.)
27 endif
28
29 ifndef BUILD_DIR
30 $(error makefile must set BUILD_DIR.)
31 endif
32
33 # Set VERBOSE make variable to 1 to output all tool commands.
34 VERBOSE?=0
35 ifeq "$(VERBOSE)" "0"
36 Q=@
37 else
38 Q=
39 endif
40
41
42 # Default variables.
43 SRC ?= .
44 BUILD_TYPE ?= Release
45 MRI_BREAK_ON_INIT ?= 1
46 MRI_UART ?= MRI_UART_MBED_USB
47 HEAP_TAGS ?= 0
48 WRITE_BUFFER_DISABLE ?= 0
49 STACK_SIZE ?= 0
50
51
52 # Configure MRI variables based on BUILD_TYPE build type variable.
53 ifeq "$(BUILD_TYPE)" "Release"
54 OPTIMIZATION ?= 2
55 MRI_ENABLE = 0
56 MRI_SEMIHOST_STDIO ?= 0
57 endif
58
59
60 ifeq "$(BUILD_TYPE)" "Debug"
61 OPTIMIZATION = 0
62 MRI_ENABLE ?= 1
63 MRI_SEMIHOST_STDIO ?= 1
64 endif
65
66
67 ifeq "$(BUILD_TYPE)" "Checked"
68 OPTIMIZATION ?= 2
69 MRI_ENABLE = 1
70 MRI_SEMIHOST_STDIO ?= 1
71 endif
72
73 MRI_INIT_PARAMETERS=$(MRI_UART)
74
75 # Output Object Directory
76 OUTDIR=../$(DEVICE)
77
78 # List of sources to be compiled/assembled
79 CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c $(SRC)/*/*/*/*/*/*.c)
80 ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
81 ifneq "$(OS)" "Windows_NT"
82 ASRCS += $(wildcard $(SRC)/*.s $(SRC)/*/*.s $(SRC)/*/*/*.s $(SRC)/*/*/*/*.s $(SRC)/*/*/*/*/*.s)
83 endif
84 CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp $(SRC)/*/*/*/*/*/*.cpp)
85
86 # List of the objects files to be compiled/assembled
87 OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.s,$(OUTDIR)/%.o,$(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS))) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
88
89 # Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
90 OBJECTS += $(OUTDIR)/mbed_custom.o
91
92 OBJECTS += $(OUTDIR)/configdefault.o
93
94 # List of the header dependency files, one per object file.
95 DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
96
97 # Linker script to be used. Indicates what code should be placed where in memory.
98 LSCRIPT=$(MBED_DIR)/$(DEVICE)/GCC_ARM/$(DEVICE).ld
99
100 # Location of external library and header dependencies.
101 MBED_DIR = $(BUILD_DIR)/../mbed/drop
102 MRI_DIR = $(BUILD_DIR)/../mri
103
104 # Include path which points to external library headers and to subdirectories of this project which contain headers.
105 SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
106 PROJINCS = $(sort $(dir $(SUBDIRS)))
107 INCDIRS += $(SRC) $(PROJINCS) $(MRI_DIR) $(MBED_DIR) $(MBED_DIR)/$(DEVICE)
108
109 # DEFINEs to be used when building C/C++ code
110 DEFINES += -DTARGET_$(DEVICE)
111 DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
112 DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
113 DEFINES += -DWRITE_BUFFER_DISABLE=$(WRITE_BUFFER_DISABLE) -DSTACK_SIZE=$(STACK_SIZE)
114
115 ifeq "$(OPTIMIZATION)" "0"
116 DEFINES += -DDEBUG
117 endif
118
119 # Libraries to be linked into final binary
120 MBED_LIBS = $(MBED_DIR)/$(DEVICE)/GCC_ARM/libmbed.a
121 SYS_LIBS = -lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys
122 LIBS = $(LIBS_PREFIX)
123
124 ifeq "$(MRI_ENABLE)" "1"
125 LIBS += $(MRI_DIR)/mri.ar
126 endif
127
128 LIBS += $(MBED_LIBS)
129 LIBS += $(SYS_LIBS)
130 LIBS += $(LIBS_SUFFIX)
131
132 # Compiler flags used to enable creation of header dependencies.
133 DEPFLAGS = -MMD -MP
134
135 # Setup wraps for newlib read/writes to redirect to MRI debugger.
136 ifeq "$(MRI_ENABLE)" "1"
137 MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
138 else
139 MRI_WRAPS=
140 endif
141
142 # Setup wraps to memory allocations routines if we want to tag heap allocations.
143 ifeq "$(HEAP_TAGS)" "1"
144 DEFINES += -DHEAP_TAGS
145 endif
146
147 # Compiler Options
148 GCFLAGS += -O$(OPTIMIZATION) -g3 $(DEVICE_CFLAGS)
149 GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
150 GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
151 GCFLAGS += $(DEFINES)
152 GCFLAGS += $(DEPFLAGS)
153 GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
154
155 GPFLAGS += $(GCFLAGS) -fno-rtti -std=gnu++11
156
157 AS_GCFLAGS += -g3 $(DEVICE_FLAGS) -x assembler-with-cpp
158 AS_GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
159 AS_FLAGS += -g3 $(DEVICE_FLAGS)
160
161
162 # Linker Options.
163 LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec
164 LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free$(MRI_WRAPS)
165 LDFLAGS += -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
166 ifneq "$(NO_FLOAT_SCANF)" "1"
167 LDFLAGS += -u _scanf_float
168 endif
169 ifneq "$(NO_FLOAT_PRINTF)" "1"
170 LDFLAGS += -u _printf_float
171 endif
172
173
174 # Compiler/Assembler/Linker Paths
175 GCC = arm-none-eabi-gcc
176 GPP = arm-none-eabi-g++
177 AS = arm-none-eabi-as
178 LD = arm-none-eabi-g++
179 OBJCOPY = arm-none-eabi-objcopy
180 OBJDUMP = arm-none-eabi-objdump
181 SIZE = arm-none-eabi-size
182
183 # Some tools are different on Windows in comparison to Unix.
184 ifeq "$(OS)" "Windows_NT"
185 REMOVE = del
186 SHELL=cmd.exe
187 REMOVE_DIR = rd /s /q
188 MKDIR = mkdir
189 QUIET=>nul 2>nul & exit 0
190 BLANK_LINE=echo -
191 else
192 REMOVE = rm
193 REMOVE_DIR = rm -r -f
194 MKDIR = mkdir -p
195 QUIET=> /dev/null 2>&1 ; exit 0
196 BLANK_LINE=echo
197 endif
198
199 # Create macro which will convert / to \ on Windows.
200 ifeq "$(OS)" "Windows_NT"
201 define convert-slash
202 $(subst /,\,$1)
203 endef
204 else
205 define convert-slash
206 $1
207 endef
208 endif
209
210 #########################################################################
211 .PHONY: all clean size
212
213 all:: $(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
235 size: $(OUTDIR)/$(PROJECT).elf
236 $(Q) $(SIZE) $<
237 @$(BLANK_LINE)
238
239 clean:
240 @echo Cleaning up all build generated files
241 $(Q) $(REMOVE_DIR) $(OUTDIR) $(QUIET)
242
243 -include $(DEPFILES)
244
245 #########################################################################
246 # Default rules to compile .c and .cpp file to .o
247 # and assemble .s files to .o
248
249 $(OUTDIR)/mbed_custom.o : $(BUILD_DIR)/mbed_custom.cpp makefile
250 @echo Compiling $<
251 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
252 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
253
254 $(OUTDIR)/%.o : %.cpp makefile
255 @echo Compiling $<
256 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
257 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
258
259 $(OUTDIR)/%.o : %.c makefile
260 @echo Compiling $<
261 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
262 $(Q) $(GCC) $(GCFLAGS) -c $< -o $@
263
264 $(OUTDIR)/%.o : %.S makefile
265 @echo Assembling $<
266 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
267 $(Q) $(GCC) $(AS_GCFLAGS) -c $< -o $@
268
269 $(OUTDIR)/%.o : %.s makefile
270 @echo Assembling $<
271 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
272 $(Q) $(AS) $(AS_FLAGS) -o $@ $<
273
274 $(OUTDIR)/configdefault.o : config.default
275 $(Q) $(OBJCOPY) -I binary -O elf32-littlearm -B arm --readonly-text --rename-section .data=.rodata.configdefault $< $@
276
277 #########################################################################