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 | # | |
21 | # Updates: | |
22 | # Arthur Wolf & Adam Green in 2011 - Updated to work with mbed. | |
23 | ############################################################################### | |
24 | # USAGE: | |
25 | # Variables that must be defined in including makefile. | |
26 | # PROJECT: Name to be given to the output binary for this project. | |
27 | # SRC: The root directory for the sources of your project. | |
28 | # GCC4MED_DIR: The root directory for where the gcc4mbed sources are located | |
29 | # in your project. This should point to the parent directory | |
30 | # of the build directory which contains this gcc4mbed.mk file. | |
31 | # LIBS_PREFIX: List of library/object files to prepend to mbed.ar capi.ar libs. | |
32 | # LIBS_SUFFIX: List of library/object files to append to mbed.ar capi.ar libs. | |
6c79da43 AG |
33 | # GCC4MBED_TYPE: Type of build to produce. Allowed values are: |
34 | # Debug - Build for debugging. Disables optimizations and | |
35 | # links in debug MRI runtime. Best debugging | |
36 | # experience. | |
37 | # Release - Build for release with no debug support. | |
38 | # Checked - Release build with debug support. Due to | |
39 | # optimizations, debug experience won't be as good | |
40 | # as Debug but might be needed when bugs don't | |
41 | # reproduce in Debug builds. | |
42 | # default: Release | |
43 | # MRI_BREAK_ON_INIT: Should the program halt before calling into main(), | |
44 | # allowing the developer time to set breakpoints in main() | |
45 | # or in code run from within global constructors. | |
46 | # default: 1 - break on init. | |
47 | # MRI_SEMIHOST_STDIO: Set to non-zero value to allow debug monitor to use | |
48 | # semi-host calls to redirect stdin/stdout/stderr to the | |
49 | # gdb console. | |
50 | # default: 1 for Debug/Checked builds and 0 for Release. | |
51 | # MRI_UART: Select the UART to be used by the debugger. See mri.h for | |
52 | # allowed values. | |
53 | # default: MRI_UART_MBED_USB - Use USB based UART on the mbed. | |
4cff3ded AW |
54 | # Example makefile: |
55 | # PROJECT=HelloWorld | |
56 | # SRC=. | |
57 | # GCC4MBED_DIR=../.. | |
58 | # LIBS_PREFIX=../agutil/agutil.ar | |
59 | # LIBS_SUFFIX= | |
60 | # | |
61 | # include ../../build/gcc4mbed.mk | |
62 | # | |
63 | ############################################################################### | |
64 | ||
6c79da43 AG |
65 | # Check for undefined variables. |
66 | ifndef PROJECT | |
67 | $(error makefile must set PROJECT variable.) | |
13e4a3f9 AW |
68 | endif |
69 | ||
6c79da43 AG |
70 | ifndef GCC4MBED_DIR |
71 | $(error makefile must set GCC4MBED_DIR.) | |
4cff3ded AW |
72 | endif |
73 | ||
6c79da43 AG |
74 | |
75 | # Default variables. | |
76 | SRC ?= . | |
6c79da43 AG |
77 | GCC4MBED_TYPE ?= Release |
78 | MRI_BREAK_ON_INIT ?= 1 | |
79 | MRI_UART ?= MRI_UART_MBED_USB | |
80 | ||
81 | ||
82 | # Configure MRI variables based on GCC4MBED_TYPE build type variable. | |
83 | ifeq "$(GCC4MBED_TYPE)" "Release" | |
8fcce42e | 84 | OPTIMIZATION ?= 2 |
6c79da43 AG |
85 | MRI_ENABLE = 0 |
86 | MRI_SEMIHOST_STDIO ?= 0 | |
87 | endif | |
88 | ||
89 | ||
90 | ifeq "$(GCC4MBED_TYPE)" "Debug" | |
91 | OPTIMIZATION = 0 | |
92 | MRI_ENABLE = 1 | |
93 | MRI_SEMIHOST_STDIO ?= 1 | |
94 | endif | |
95 | ||
96 | ||
97 | ifeq "$(GCC4MBED_TYPE)" "Checked" | |
8fcce42e | 98 | OPTIMIZATION ?= 2 |
6c79da43 AG |
99 | MRI_ENABLE = 1 |
100 | MRI_SEMIHOST_STDIO ?= 1 | |
101 | endif | |
102 | ||
103 | MRI_INIT_PARAMETERS=$(MRI_UART) | |
104 | ||
105 | ||
8fcce42e AG |
106 | # Output Object Directory |
107 | OUTDIR=LPC176x | |
108 | ||
4cff3ded | 109 | # List of sources to be compiled/assembled |
068f6a12 AW |
110 | CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c) |
111 | ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S) | |
112 | CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp) | |
4cff3ded | 113 | |
4cff3ded | 114 | # List of the objects files to be compiled/assembled |
8fcce42e AG |
115 | OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS)) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS)) |
116 | ||
117 | # Add in the GCC4MBED stubs which allow hooking in the MRI debug monitor. | |
118 | OBJECTS += $(OUTDIR)/gcc4mbed.o | |
119 | ||
120 | # List of the header dependency files, one per object file. | |
121 | DEPFILES = $(patsubst %.o,%.d,$(OBJECTS)) | |
122 | ||
123 | # Linker script to be used. Indicates what code should be placed where in memory. | |
4cff3ded AW |
124 | LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld |
125 | ||
126 | # Location of external library and header dependencies. | |
127 | EXTERNAL_DIR = $(GCC4MBED_DIR)/external | |
128 | ||
068f6a12 AW |
129 | # Include path which points to external library headers and to subdirectories of this project which contain headers. |
130 | SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*) | |
131 | PROJINCS = $(sort $(dir $(SUBDIRS))) | |
8fcce42e | 132 | INCDIRS += $(PROJINCS) $(GCC4MBED_DIR)/mri $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768 |
4cff3ded AW |
133 | |
134 | # DEFINEs to be used when building C/C++ code | |
8fcce42e AG |
135 | DEFINES = -DTARGET_LPC1768 |
136 | DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"' | |
137 | DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO) | |
4cff3ded AW |
138 | |
139 | # Libraries to be linked into final binary | |
8fcce42e AG |
140 | MBED_LIBS = $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libmbed.a $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libcapi.a |
141 | SYS_LIBS = -lstdc++ -lsupc++ -lm -lgcc -lc -lgcc -lc -lnosys | |
142 | LIBS = $(LIBS_PREFIX) | |
143 | ||
144 | ifeq "$(MRI_ENABLE)" "1" | |
145 | LIBS += $(GCC4MBED_DIR)/mri/mri.ar | |
146 | endif | |
147 | ||
148 | LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/startup_LPC17xx.o | |
149 | LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/cmsis_nvic.o | |
150 | LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/core_cm3.o | |
151 | LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/system_LPC17xx.o | |
152 | LIBS += $(MBED_LIBS) | |
153 | LIBS += $(SYS_LIBS) | |
154 | LIBS += $(LIBS_SUFFIX) | |
4cff3ded | 155 | |
8fcce42e AG |
156 | # Compiler flags used to enable creation of header dependencies. |
157 | DEPFLAGS = -MMD -MP | |
158 | ||
159 | # Compiler Options | |
bd0f7508 | 160 | GPFLAGS = -O$(OPTIMIZATION) -g3 -mcpu=cortex-m3 -mthumb -mthumb-interwork |
8fcce42e | 161 | GPFLAGS += -ffunction-sections -fdata-sections -fno-exceptions |
4cff3ded AW |
162 | GPFLAGS += $(patsubst %,-I%,$(INCDIRS)) |
163 | GPFLAGS += $(DEFINES) | |
8fcce42e AG |
164 | GPFLAGS += $(DEPFLAGS) |
165 | GPFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align | |
166 | GCFLAGS = $(GPFLAGS) | |
167 | ||
168 | # Setup wraps for newlib read/writes to redirect to MRI debugger. | |
169 | ifeq "$(MRI_ENABLE)" "1" | |
170 | MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected | |
171 | else | |
172 | MRI_WRAP= | |
173 | endif | |
4cff3ded | 174 | |
8fcce42e AG |
175 | # Linker Options. |
176 | LDFLAGS = -mcpu=cortex-m3 -mthumb -O$(OPTIMIZATION) -specs=$(GCC4MBED_DIR)/build/startfile.spec -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty$(MRI_WRAPS) -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768 | |
4cff3ded AW |
177 | |
178 | ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp | |
179 | ASFLAGS += $(patsubst %,-I%,$(INCDIRS)) | |
180 | ||
181 | # Compiler/Assembler/Linker Paths | |
8fcce42e | 182 | GCC = arm-none-eabi-gcc |
4cff3ded AW |
183 | GPP = arm-none-eabi-g++ |
184 | AS = arm-none-eabi-gcc | |
185 | LD = arm-none-eabi-g++ | |
186 | OBJCOPY = arm-none-eabi-objcopy | |
187 | OBJDUMP = arm-none-eabi-objdump | |
4cff3ded | 188 | SIZE = arm-none-eabi-size |
068f6a12 | 189 | |
8fcce42e AG |
190 | # Some tools are different on Windows in comparison to Unix. |
191 | ifeq "$(OS)" "Windows_NT" | |
192 | REMOVE = del | |
13e4a3f9 | 193 | SHELL=cmd.exe |
8fcce42e AG |
194 | REMOVE_DIR = rd /s /q |
195 | MKDIR = mkdir | |
196 | QUIET=>nul 2>nul & exit 0 | |
197 | else | |
198 | REMOVE = rm | |
199 | REMOVE_DIR = rm -r -f | |
200 | MKDIR = mkdir -p | |
201 | QUIET=> /dev/null 2>&1 ; exit 0 | |
202 | endif | |
203 | ||
204 | # Create macro which will convert / to \ on Windows. | |
205 | ifeq "$(OS)" "Windows_NT" | |
206 | define convert-slash | |
207 | $(subst /,\,$1) | |
208 | endef | |
209 | else | |
210 | define convert-slash | |
211 | $1 | |
212 | endef | |
068f6a12 | 213 | endif |
4cff3ded AW |
214 | |
215 | ######################################################################### | |
3c132bd0 | 216 | .PHONY: all clean deploy |
4cff3ded | 217 | |
8fcce42e | 218 | all:: $(PROJECT).hex $(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm |
4cff3ded AW |
219 | |
220 | $(PROJECT).bin: $(PROJECT).elf | |
221 | $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin | |
222 | ||
223 | $(PROJECT).hex: $(PROJECT).elf | |
224 | $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex | |
225 | ||
8fcce42e AG |
226 | $(OUTDIR)/$(PROJECT).disasm: $(PROJECT).elf |
227 | $(OBJDUMP) -d -f -M reg-names-std $(PROJECT).elf >$(OUTDIR)/$(PROJECT).disasm | |
4cff3ded | 228 | |
8fcce42e | 229 | $(PROJECT).elf: $(LSCRIPT) $(OBJECTS) |
4cff3ded AW |
230 | $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf |
231 | $(SIZE) $(PROJECT).elf | |
232 | ||
4cff3ded | 233 | clean: |
8fcce42e AG |
234 | $(REMOVE) -f $(call convert-slash,$(OBJECTS)) $(QUIET) |
235 | $(REMOVE) -f $(call convert-slash,$(DEPFILES)) $(QUIET) | |
236 | $(REMOVE_DIR) $(OUTDIR) $(QUIET) | |
237 | $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).map) $(QUIET) | |
238 | $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).disasm) $(QUIET) | |
239 | $(REMOVE) -f $(PROJECT).bin $(QUIET) | |
240 | $(REMOVE) -f $(PROJECT).hex $(QUIET) | |
241 | $(REMOVE) -f $(PROJECT).elf $(QUIET) | |
242 | ||
243 | -include $(DEPFILES) | |
068f6a12 AW |
244 | |
245 | ifdef LPC_DEPLOY | |
246 | DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY)) | |
247 | deploy: | |
248 | $(DEPLOY_COMMAND) | |
249 | endif | |
4cff3ded AW |
250 | |
251 | ######################################################################### | |
252 | # Default rules to compile .c and .cpp file to .o | |
253 | # and assemble .s files to .o | |
254 | ||
8fcce42e AG |
255 | $(OUTDIR)/gcc4mbed.o : $(GCC4MBED_DIR)/src/gcc4mbed.c |
256 | $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET) | |
257 | $(GPP) $(GPFLAGS) -c $< -o $@ | |
258 | ||
259 | $(OUTDIR)/%.o : %.cpp | |
260 | $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET) | |
261 | $(GPP) $(GPFLAGS) -c $< -o $@ | |
4cff3ded | 262 | |
8fcce42e AG |
263 | $(OUTDIR)/%.o : %.c |
264 | $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET) | |
265 | $(GCC) $(GCFLAGS) -c $< -o $@ | |
4cff3ded | 266 | |
8fcce42e AG |
267 | $(OUTDIR)/%.o : %.S |
268 | $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET) | |
269 | $(AS) $(ASFLAGS) -c $< -o $@ | |
4cff3ded | 270 | |
672298b2 | 271 | ######################################################################### |