Merge pull request #1342 from wolfmanjm/query-for-http
[clinton/Smoothieware.git] / build / common.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#
dc735ba6 21# Updates:
172d42d9 22# Arthur Wolf & Adam Green in 2011 - 2012 - Updated to work with mbed.
4cff3ded 23###############################################################################
6c79da43
AG
24# Check for undefined variables.
25ifndef PROJECT
26$(error makefile must set PROJECT variable.)
13e4a3f9
AW
27endif
28
172d42d9
AG
29ifndef BUILD_DIR
30$(error makefile must set BUILD_DIR.)
31endif
32
33# Set VERBOSE make variable to 1 to output all tool commands.
34VERBOSE?=0
35ifeq "$(VERBOSE)" "0"
36Q=@
37else
38Q=
4cff3ded
AW
39endif
40
6c79da43
AG
41
42# Default variables.
43SRC ?= .
172d42d9 44BUILD_TYPE ?= Release
6c79da43
AG
45MRI_BREAK_ON_INIT ?= 1
46MRI_UART ?= MRI_UART_MBED_USB
383036c1
AG
47HEAP_TAGS ?= 0
48WRITE_BUFFER_DISABLE ?= 0
49STACK_SIZE ?= 0
6c79da43
AG
50
51
172d42d9
AG
52# Configure MRI variables based on BUILD_TYPE build type variable.
53ifeq "$(BUILD_TYPE)" "Release"
8fcce42e 54OPTIMIZATION ?= 2
6c79da43
AG
55MRI_ENABLE = 0
56MRI_SEMIHOST_STDIO ?= 0
57endif
58
59
172d42d9 60ifeq "$(BUILD_TYPE)" "Debug"
6c79da43 61OPTIMIZATION = 0
172d42d9 62MRI_ENABLE ?= 1
6c79da43
AG
63MRI_SEMIHOST_STDIO ?= 1
64endif
65
66
172d42d9 67ifeq "$(BUILD_TYPE)" "Checked"
8fcce42e 68OPTIMIZATION ?= 2
6c79da43
AG
69MRI_ENABLE = 1
70MRI_SEMIHOST_STDIO ?= 1
71endif
72
73MRI_INIT_PARAMETERS=$(MRI_UART)
74
8fcce42e 75# Output Object Directory
3d11c05c 76OUTDIR=../$(DEVICE)
8fcce42e 77
4cff3ded 78# List of sources to be compiled/assembled
d4ee6ee2 79CSRCS1 = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c $(SRC)/*/*/*/*/*/*.c)
df930a5f 80# Totally exclude network if NONETWORK is defined
d4ee6ee2 81ifeq "$(NONETWORK)" "1"
93ea6adb 82CSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CSRCS1))
d4ee6ee2
JM
83DEFINES += -DNONETWORK
84else
93ea6adb 85CSRCS2 = $(CSRCS1)
d4ee6ee2
JM
86endif
87
93ea6adb
JM
88# do not compile the src/testframework as that can only be done with rake
89CSRCS = $(filter-out $(SRC)/testframework/%,$(CSRCS2))
90
718eaf93
JM
91ifeq "$(DISABLEMSD)" "1"
92DEFINES += -DDISABLEMSD
93endif
94
068f6a12 95ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
172d42d9
AG
96ifneq "$(OS)" "Windows_NT"
97ASRCS += $(wildcard $(SRC)/*.s $(SRC)/*/*.s $(SRC)/*/*/*.s $(SRC)/*/*/*/*.s $(SRC)/*/*/*/*/*.s)
98endif
54be3bab 99
d4ee6ee2
JM
100CPPSRCS1 = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp $(SRC)/*/*/*/*/*/*.cpp)
101ifeq "$(NONETWORK)" "1"
c150a0d3 102 CPPSRCS2 = $(filter-out $(SRC)/libs/Network/%,$(CPPSRCS1))
d4ee6ee2 103else
54be3bab
JM
104 ifneq "$(PLAN9)" "1"
105 DEFINES += -DNOPLAN9
106 CPPSRCS2 = $(filter-out $(SRC)/libs/Network/uip/plan9/%,$(CPPSRCS1))
107 else
108 CPPSRCS2 = $(CPPSRCS1)
109 endif
d4ee6ee2 110endif
4cff3ded 111
c150a0d3
JM
112# CNC build
113ifeq "$(CNC)" "1"
114 CPPSRCS21 = $(filter-out $(SRC)/modules/utils/panel/screens/3dprinter/%,$(CPPSRCS2))
f0cb675c 115 DEFINES += -DCNC
c150a0d3
JM
116else
117 CPPSRCS21 = $(filter-out $(SRC)/modules/utils/panel/screens/cnc/%,$(CPPSRCS2))
118endif
119
df930a5f
JM
120# Totally exclude any modules listed in EXCLUDE_MODULES
121# uppercase function
122uc = $(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))))))))))))))))))))))))))
123EXL = $(patsubst %,$(SRC)/modules/%/%,$(EXCLUDED_MODULES))
c150a0d3 124CPPSRCS3 = $(filter-out $(EXL),$(CPPSRCS21))
df930a5f
JM
125DEFINES += $(call uc, $(subst /,_,$(patsubst %,-DNO_%,$(EXCLUDED_MODULES))))
126
93ea6adb
JM
127# do not compile the src/testframework as that can only be done with rake
128CPPSRCS = $(filter-out $(SRC)/testframework/%,$(CPPSRCS3))
129
4cff3ded 130# List of the objects files to be compiled/assembled
172d42d9 131OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.s,$(OUTDIR)/%.o,$(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS))) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
8fcce42e 132
172d42d9
AG
133# Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
134OBJECTS += $(OUTDIR)/mbed_custom.o
8fcce42e 135
33110301
L
136OBJECTS += $(OUTDIR)/configdefault.o
137
8fcce42e
AG
138# List of the header dependency files, one per object file.
139DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
140
141# Linker script to be used. Indicates what code should be placed where in memory.
172d42d9 142LSCRIPT=$(MBED_DIR)/$(DEVICE)/GCC_ARM/$(DEVICE).ld
4cff3ded
AW
143
144# Location of external library and header dependencies.
172d42d9
AG
145MBED_DIR = $(BUILD_DIR)/../mbed/drop
146MRI_DIR = $(BUILD_DIR)/../mri
4cff3ded 147
068f6a12 148# Include path which points to external library headers and to subdirectories of this project which contain headers.
c150a0d3 149SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/* $(SRC)/*/*/*/*/*/*)
068f6a12 150PROJINCS = $(sort $(dir $(SUBDIRS)))
172d42d9 151INCDIRS += $(SRC) $(PROJINCS) $(MRI_DIR) $(MBED_DIR) $(MBED_DIR)/$(DEVICE)
4cff3ded
AW
152
153# DEFINEs to be used when building C/C++ code
172d42d9 154DEFINES += -DTARGET_$(DEVICE)
dc735ba6 155DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"'
8fcce42e 156DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
383036c1 157DEFINES += -DWRITE_BUFFER_DISABLE=$(WRITE_BUFFER_DISABLE) -DSTACK_SIZE=$(STACK_SIZE)
4cff3ded 158
d9664db4
AG
159ifeq "$(OPTIMIZATION)" "0"
160DEFINES += -DDEBUG
161endif
162
4cff3ded 163# Libraries to be linked into final binary
172d42d9 164MBED_LIBS = $(MBED_DIR)/$(DEVICE)/GCC_ARM/libmbed.a
3b02cd71
JM
165#SYS_LIBS = -lstdc++_s -lsupc++_s -lm -lgcc -lc_s -lgcc -lc_s -lnosys
166SYS_LIBS = -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys
dc735ba6 167LIBS = $(LIBS_PREFIX)
8fcce42e
AG
168
169ifeq "$(MRI_ENABLE)" "1"
172d42d9 170LIBS += $(MRI_DIR)/mri.ar
8fcce42e
AG
171endif
172
8fcce42e
AG
173LIBS += $(MBED_LIBS)
174LIBS += $(SYS_LIBS)
175LIBS += $(LIBS_SUFFIX)
4cff3ded 176
8fcce42e
AG
177# Compiler flags used to enable creation of header dependencies.
178DEPFLAGS = -MMD -MP
179
dc735ba6 180# Setup wraps for newlib read/writes to redirect to MRI debugger.
383036c1
AG
181ifeq "$(MRI_ENABLE)" "1"
182MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
183else
184MRI_WRAPS=
185endif
186
187# Setup wraps to memory allocations routines if we want to tag heap allocations.
383036c1 188ifeq "$(HEAP_TAGS)" "1"
383036c1
AG
189DEFINES += -DHEAP_TAGS
190endif
191
8fcce42e 192# Compiler Options
172d42d9
AG
193GCFLAGS += -O$(OPTIMIZATION) -g3 $(DEVICE_CFLAGS)
194GCFLAGS += -ffunction-sections -fdata-sections -fno-exceptions -fno-delete-null-pointer-checks
58baeec1
MM
195GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
196GCFLAGS += $(DEFINES)
197GCFLAGS += $(DEPFLAGS)
198GCFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
199
172d42d9
AG
200GPFLAGS += $(GCFLAGS) -fno-rtti -std=gnu++11
201
202AS_GCFLAGS += -g3 $(DEVICE_FLAGS) -x assembler-with-cpp
203AS_GCFLAGS += $(patsubst %,-I%,$(INCDIRS))
204AS_FLAGS += -g3 $(DEVICE_FLAGS)
58baeec1 205
172d42d9 206
8fcce42e 207# Linker Options.
dc735ba6 208LDFLAGS = $(DEVICE_FLAGS) -specs=$(BUILD_DIR)/startfile.spec
b4e5c830 209LDFLAGS += -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty,--wrap=malloc,--wrap=realloc,--wrap=free$(MRI_WRAPS)
383036c1 210LDFLAGS += -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
172d42d9
AG
211ifneq "$(NO_FLOAT_SCANF)" "1"
212LDFLAGS += -u _scanf_float
213endif
214ifneq "$(NO_FLOAT_PRINTF)" "1"
215LDFLAGS += -u _printf_float
216endif
4cff3ded 217
4cff3ded
AW
218
219# Compiler/Assembler/Linker Paths
8fcce42e 220GCC = arm-none-eabi-gcc
4cff3ded 221GPP = arm-none-eabi-g++
172d42d9 222AS = arm-none-eabi-as
4cff3ded
AW
223LD = arm-none-eabi-g++
224OBJCOPY = arm-none-eabi-objcopy
225OBJDUMP = arm-none-eabi-objdump
4cff3ded 226SIZE = arm-none-eabi-size
068f6a12 227
8fcce42e
AG
228# Some tools are different on Windows in comparison to Unix.
229ifeq "$(OS)" "Windows_NT"
230REMOVE = del
13e4a3f9 231SHELL=cmd.exe
8fcce42e
AG
232REMOVE_DIR = rd /s /q
233MKDIR = mkdir
234QUIET=>nul 2>nul & exit 0
172d42d9 235BLANK_LINE=echo -
8fcce42e
AG
236else
237REMOVE = rm
238REMOVE_DIR = rm -r -f
239MKDIR = mkdir -p
240QUIET=> /dev/null 2>&1 ; exit 0
172d42d9 241BLANK_LINE=echo
8fcce42e
AG
242endif
243
244# Create macro which will convert / to \ on Windows.
245ifeq "$(OS)" "Windows_NT"
246define convert-slash
247$(subst /,\,$1)
248endef
249else
250define convert-slash
251$1
252endef
068f6a12 253endif
4cff3ded
AW
254
255#########################################################################
172d42d9
AG
256.PHONY: all clean size
257
258all:: $(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 $< $@
dc735ba6 269
172d42d9
AG
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 $< >$@
dc735ba6 274
172d42d9
AG
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
280size: $(OUTDIR)/$(PROJECT).elf
281 $(Q) $(SIZE) $<
282 @$(BLANK_LINE)
4cff3ded 283
4cff3ded 284clean:
172d42d9 285 @echo Cleaning up all build generated files
1a06e28a 286 $(Q) $(REMOVE_DIR) $(call convert-slash,$(OUTDIR)) $(QUIET)
8fcce42e
AG
287
288-include $(DEPFILES)
068f6a12 289
4cff3ded
AW
290#########################################################################
291# Default rules to compile .c and .cpp file to .o
292# and assemble .s files to .o
293
383036c1 294$(OUTDIR)/mbed_custom.o : $(BUILD_DIR)/mbed_custom.cpp makefile
172d42d9
AG
295 @echo Compiling $<
296 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
383036c1 297 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
8fcce42e 298
64c668c2 299$(OUTDIR)/%.o : %.cpp makefile
172d42d9
AG
300 @echo Compiling $<
301 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
302 $(Q) $(GPP) $(GPFLAGS) -c $< -o $@
4cff3ded 303
64c668c2 304$(OUTDIR)/%.o : %.c makefile
172d42d9
AG
305 @echo Compiling $<
306 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
307 $(Q) $(GCC) $(GCFLAGS) -c $< -o $@
4cff3ded 308
64c668c2 309$(OUTDIR)/%.o : %.S makefile
172d42d9
AG
310 @echo Assembling $<
311 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
312 $(Q) $(GCC) $(AS_GCFLAGS) -c $< -o $@
4cff3ded 313
172d42d9
AG
314$(OUTDIR)/%.o : %.s makefile
315 @echo Assembling $<
316 $(Q) $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
317 $(Q) $(AS) $(AS_FLAGS) -o $@ $<
318
33110301 319$(OUTDIR)/configdefault.o : config.default
dc735ba6 320 $(Q) $(OBJCOPY) -I binary -O elf32-littlearm -B arm --readonly-text --rename-section .data=.rodata.configdefault $< $@
33110301
L
321
322#########################################################################