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