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