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