Merge pull request #1322 from wolfmanjm/upstreamedge
[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 CSRCS1 = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c $(SRC)/*/*/*/*/*/*.c)
80 # Totally exclude network if NONETWORK is defined
81 ifeq "$(NONETWORK)" "1"
82 CSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CSRCS1))
83 DEFINES += -DNONETWORK
84 else
85 CSRCS2 = $(CSRCS1)
86 endif
87
88 # do not compile the src/testframework as that can only be done with rake
89 CSRCS = $(filter-out $(SRC)/testframework/%,$(CSRCS2))
90
91 ifeq "$(DISABLEMSD)" "1"
92 DEFINES += -DDISABLEMSD
93 endif
94
95 ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
96 ifneq "$(OS)" "Windows_NT"
97 ASRCS += $(wildcard $(SRC)/*.s $(SRC)/*/*.s $(SRC)/*/*/*.s $(SRC)/*/*/*/*.s $(SRC)/*/*/*/*/*.s)
98 endif
99
100 CPPSRCS1 = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp $(SRC)/*/*/*/*/*/*.cpp)
101 ifeq "$(NONETWORK)" "1"
102 CPPSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CPPSRCS1))
103 else
104 ifneq "$(PLAN9)" "1"
105 DEFINES += -DNOPLAN9
106 CPPSRCS2 = $(filter-out $(SRC)/libs/Network/uip/plan9/%,$(CPPSRCS1))
107 else
108 CPPSRCS2 = $(CPPSRCS1)
109 endif
110 endif
111
112 # CNC build
113 ifeq "$(CNC)" "1"
114 CPPSRCS21 = $(filter-out $(SRC)/modules/utils/panel/screens/3dprinter/%,$(CPPSRCS2))
115 DEFINES += -DCNC
116 else
117 CPPSRCS21 = $(filter-out $(SRC)/modules/utils/panel/screens/cnc/%,$(CPPSRCS2))
118 endif
119
120 # Totally exclude any modules listed in EXCLUDE_MODULES
121 # uppercase function
122 uc = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))
123 EXL = $(patsubst %,$(SRC)/modules/%/%,$(EXCLUDED_MODULES))
124 CPPSRCS3 = $(filter-out $(EXL),$(CPPSRCS21))
125 DEFINES += $(call uc, $(subst /,_,$(patsubst %,-DNO_%,$(EXCLUDED_MODULES))))
126
127 # do not compile the src/testframework as that can only be done with rake
128 CPPSRCS = $(filter-out $(SRC)/testframework/%,$(CPPSRCS3))
129
130 # List of the objects files to be compiled/assembled
131 OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.s,$(OUTDIR)/%.o,$(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS))) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
132
133 # Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
134 OBJECTS += $(OUTDIR)/mbed_custom.o
135
136 OBJECTS += $(OUTDIR)/configdefault.o
137
138 # List of the header dependency files, one per object file.
139 DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
140
141 # Linker script to be used. Indicates what code should be placed where in memory.
142 LSCRIPT=$(MBED_DIR)/$(DEVICE)/GCC_ARM/$(DEVICE).ld
143
144 # Location of external library and header dependencies.
145 MBED_DIR = $(BUILD_DIR)/../mbed/drop
146 MRI_DIR = $(BUILD_DIR)/../mri
147
148 # Include path which points to external library headers and to subdirectories of this project which contain headers.
149 SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/* $(SRC)/*/*/*/*/*/*)
150 PROJINCS = $(sort $(dir $(SUBDIRS)))
151 INCDIRS += $(SRC) $(PROJINCS) $(MRI_DIR) $(MBED_DIR) $(MBED_DIR)/$(DEVICE)
152
153 # DEFINEs to be used when building C/C++ code
154 DEFINES += -DTARGET_$(DEVICE)
155 DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
156 DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
157 DEFINES += -DWRITE_BUFFER_DISABLE=$(WRITE_BUFFER_DISABLE) -DSTACK_SIZE=$(STACK_SIZE)
158
159 ifeq "$(OPTIMIZATION)" "0"
160 DEFINES += -DDEBUG
161 endif
162
163 # Libraries to be linked into final binary
164 MBED_LIBS = $(MBED_DIR)/$(DEVICE)/GCC_ARM/libmbed.a
165 #SYS_LIBS = -lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys
166 SYS_LIBS = -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys
167 LIBS = $(LIBS_PREFIX)
168
169 ifeq "$(MRI_ENABLE)" "1"
170 LIBS += $(MRI_DIR)/mri.ar
171 endif
172
173 LIBS += $(MBED_LIBS)
174 LIBS += $(SYS_LIBS)
175 LIBS += $(LIBS_SUFFIX)
176
177 # Compiler flags used to enable creation of header dependencies.
178 DEPFLAGS = -MMD -MP
179
180 # Setup wraps for newlib read/writes to redirect to MRI debugger.
181 ifeq "$(MRI_ENABLE)" "1"
182 MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
183 else
184 MRI_WRAPS=
185 endif
186
187 # Setup wraps to memory allocations routines if we want to tag heap allocations.
188 ifeq "$(HEAP_TAGS)" "1"
189 DEFINES += -DHEAP_TAGS
190 endif
191
192 # Compiler Options
193 GCFLAGS += -O$(OPTIMIZATION) -g3 $(DEVICE_CFLAGS)
194 GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
195 GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
196 GCFLAGS += $(DEFINES)
197 GCFLAGS += $(DEPFLAGS)
198 GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
199
200 GPFLAGS += $(GCFLAGS) -fno-rtti -std=gnu++11
201
202 AS_GCFLAGS += -g3 $(DEVICE_FLAGS) -x assembler-with-cpp
203 AS_GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
204 AS_FLAGS += -g3 $(DEVICE_FLAGS)
205
206
207 # Linker Options.
208 LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec
209 LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free$(MRI_WRAPS)
210 LDFLAGS += -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
211 ifneq "$(NO_FLOAT_SCANF)" "1"
212 LDFLAGS += -u _scanf_float
213 endif
214 ifneq "$(NO_FLOAT_PRINTF)" "1"
215 LDFLAGS += -u _printf_float
216 endif
217
218
219 # Compiler/Assembler/Linker Paths
220 GCC = arm-none-eabi-gcc
221 GPP = arm-none-eabi-g++
222 AS = arm-none-eabi-as
223 LD = arm-none-eabi-g++
224 OBJCOPY = arm-none-eabi-objcopy
225 OBJDUMP = arm-none-eabi-objdump
226 SIZE = arm-none-eabi-size
227
228 # Some tools are different on Windows in comparison to Unix.
229 ifeq "$(OS)" "Windows_NT"
230 REMOVE = del
231 SHELL=cmd.exe
232 REMOVE_DIR = rd /s /q
233 MKDIR = mkdir
234 QUIET=>nul 2>nul & exit 0
235 BLANK_LINE=echo -
236 else
237 REMOVE = rm
238 REMOVE_DIR = rm -r -f
239 MKDIR = mkdir -p
240 QUIET=> /dev/null 2>&1 ; exit 0
241 BLANK_LINE=echo
242 endif
243
244 # Create macro which will convert / to \ on Windows.
245 ifeq "$(OS)" "Windows_NT"
246 define convert-slash
247 $(subst /,\,$1)
248 endef
249 else
250 define convert-slash
251 $1
252 endef
253 endif
254
255 #########################################################################
256 .PHONY: all clean size
257
258 all:: $(OUTDIR)/$(PROJECT).hex $(OUTDIR)/$(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm size
259
260 $(OUTDIR)/$(PROJECT).bin: $(OUTDIR)/$(PROJECT).elf
261 @echo Extracting $@
262 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
263 $(Q) $(OBJCOPY) -O binary $< $@
264
265 $(OUTDIR)/$(PROJECT).hex: $(OUTDIR)/$(PROJECT).elf
266 @echo Extracting $@
267 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
268 $(Q) $(OBJCOPY) -R .stack -O ihex $< $@
269
270 $(OUTDIR)/$(PROJECT).disasm: $(OUTDIR)/$(PROJECT).elf
271 @echo Extracting disassembly to $@
272 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
273 $(Q) $(OBJDUMP) -d -f -M reg-names-std --demangle $< >$@
274
275 $(OUTDIR)/$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
276 @echo Linking $@
277 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
278 $(Q) $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
279
280 size: $(OUTDIR)/$(PROJECT).elf
281 $(Q) $(SIZE) $<
282 @$(BLANK_LINE)
283
284 clean:
285 @echo Cleaning up all build generated files
286 $(Q) $(REMOVE_DIR) $(call convert-slash,$(OUTDIR)) $(QUIET)
287
288 -include $(DEPFILES)
289
290 #########################################################################
291 # Default rules to compile .c and .cpp file to .o
292 # and assemble .s files to .o
293
294 $(OUTDIR)/mbed_custom.o : $(BUILD_DIR)/mbed_custom.cpp makefile
295 @echo Compiling $<
296 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
297 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
298
299 $(OUTDIR)/%.o : %.cpp makefile
300 @echo Compiling $<
301 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
302 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
303
304 $(OUTDIR)/%.o : %.c makefile
305 @echo Compiling $<
306 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
307 $(Q) $(GCC) $(GCFLAGS) -c $< -o $@
308
309 $(OUTDIR)/%.o : %.S makefile
310 @echo Assembling $<
311 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
312 $(Q) $(GCC) $(AS_GCFLAGS) -c $< -o $@
313
314 $(OUTDIR)/%.o : %.s makefile
315 @echo Assembling $<
316 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
317 $(Q) $(AS) $(AS_FLAGS) -o $@ $<
318
319 $(OUTDIR)/configdefault.o : config.default
320 $(Q) $(OBJCOPY) -I binary -O elf32-littlearm -B arm --readonly-text --rename-section .data=.rodata.configdefault $< $@
321
322 #########################################################################