1 #Copyright (C) 2011 by Sagar G V
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:
10 #The above copyright notice and this permission notice shall be included in
11 #all copies or substantial portions of the Software.
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
22 # Arthur Wolf & Adam Green in 2011 - 2012 - Updated to work with mbed.
23 ###############################################################################
24 # Check for undefined variables.
26 $(error makefile must set PROJECT variable.
)
30 $(error makefile must set BUILD_DIR.
)
33 # Set VERBOSE make variable to 1 to output all tool commands.
45 MRI_BREAK_ON_INIT ?
= 1
46 MRI_UART ?
= MRI_UART_MBED_USB
48 WRITE_BUFFER_DISABLE ?
= 0
52 # Configure MRI variables based on BUILD_TYPE build type variable.
53 ifeq "$(BUILD_TYPE)" "Release"
56 MRI_SEMIHOST_STDIO ?
= 0
60 ifeq "$(BUILD_TYPE)" "Debug"
63 MRI_SEMIHOST_STDIO ?
= 1
67 ifeq "$(BUILD_TYPE)" "Checked"
70 MRI_SEMIHOST_STDIO ?
= 1
73 MRI_INIT_PARAMETERS
=$(MRI_UART
)
75 # Output Object Directory
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
88 # do not compile the src/testframework as that can only be done with rake
89 CSRCS
= $(filter-out $(SRC
)/testframework
/%,$(CSRCS2
))
91 ifeq "$(DISABLEMSD)" "1"
92 DEFINES
+= -DDISABLEMSD
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
)
99 CPPSRCS1
= $(wildcard $(SRC
)/*.
cpp $(SRC
)/*/*.
cpp $(SRC
)/*/*/*.
cpp $(SRC
)/*/*/*/*.
cpp $(SRC
)/*/*/*/*/*.
cpp $(SRC
)/*/*/*/*/*/*.
cpp)
100 ifeq "$(NONETWORK)" "1"
101 CPPSRCS2
= $(filter-out $(SRC
)/libs
/Network
/%,$(CPPSRCS1
))
103 CPPSRCS2
= $(CPPSRCS1
)
106 # Totally exclude any modules listed in EXCLUDE_MODULES
108 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))))))))))))))))))))))))))
109 EXL
= $(patsubst %,$(SRC
)/modules
/%/%,$(EXCLUDED_MODULES
))
110 CPPSRCS3
= $(filter-out $(EXL
),$(CPPSRCS2
))
111 DEFINES
+= $(call uc
, $(subst /,_
,$(patsubst %,-DNO_
%,$(EXCLUDED_MODULES
))))
113 # do not compile the src/testframework as that can only be done with rake
114 CPPSRCS
= $(filter-out $(SRC
)/testframework
/%,$(CPPSRCS3
))
116 # List of the objects files to be compiled/assembled
117 OBJECTS
= $(patsubst %.c
,$(OUTDIR
)/%.o
,$(CSRCS
)) $(patsubst %.s
,$(OUTDIR
)/%.o
,$(patsubst %.S
,$(OUTDIR
)/%.o
,$(ASRCS
))) $(patsubst %.
cpp,$(OUTDIR
)/%.o
,$(CPPSRCS
))
119 # Add in the MBED customization stubs which allow hooking in the MRI debug monitor.
120 OBJECTS
+= $(OUTDIR
)/mbed_custom.o
122 OBJECTS
+= $(OUTDIR
)/configdefault.o
124 # List of the header dependency files, one per object file.
125 DEPFILES
= $(patsubst %.o
,%.d
,$(OBJECTS
))
127 # Linker script to be used. Indicates what code should be placed where in memory.
128 LSCRIPT
=$(MBED_DIR
)/$(DEVICE
)/GCC_ARM
/$(DEVICE
).
ld
130 # Location of external library and header dependencies.
131 MBED_DIR
= $(BUILD_DIR
)/..
/mbed
/drop
132 MRI_DIR
= $(BUILD_DIR
)/..
/mri
134 # Include path which points to external library headers and to subdirectories of this project which contain headers.
135 SUBDIRS
= $(wildcard $(SRC
)/* $(SRC
)/*/* $(SRC
)/*/*/* $(SRC
)/*/*/*/* $(SRC
)/*/*/*/*/*)
136 PROJINCS
= $(sort $(dir $(SUBDIRS
)))
137 INCDIRS
+= $(SRC
) $(PROJINCS
) $(MRI_DIR
) $(MBED_DIR
) $(MBED_DIR
)/$(DEVICE
)
139 # DEFINEs to be used when building C/C++ code
140 DEFINES
+= -DTARGET_
$(DEVICE
)
141 DEFINES
+= -DMRI_ENABLE
=$(MRI_ENABLE
) -DMRI_INIT_PARAMETERS
='"$(MRI_INIT_PARAMETERS)"'
142 DEFINES
+= -DMRI_BREAK_ON_INIT
=$(MRI_BREAK_ON_INIT
) -DMRI_SEMIHOST_STDIO
=$(MRI_SEMIHOST_STDIO
)
143 DEFINES
+= -DWRITE_BUFFER_DISABLE
=$(WRITE_BUFFER_DISABLE
) -DSTACK_SIZE
=$(STACK_SIZE
)
145 ifeq "$(OPTIMIZATION)" "0"
149 # Libraries to be linked into final binary
150 MBED_LIBS
= $(MBED_DIR
)/$(DEVICE
)/GCC_ARM
/libmbed.a
151 SYS_LIBS
= -lstdc
++_s
-lsupc
++_s
-lm
-lgcc
-lc_s
-lgcc
-lc_s
-lnosys
152 LIBS
= $(LIBS_PREFIX
)
154 ifeq "$(MRI_ENABLE)" "1"
155 LIBS
+= $(MRI_DIR
)/mri.
ar
160 LIBS
+= $(LIBS_SUFFIX
)
162 # Compiler flags used to enable creation of header dependencies.
165 # Setup wraps for newlib read/writes to redirect to MRI debugger.
166 ifeq "$(MRI_ENABLE)" "1"
167 MRI_WRAPS
=,--wrap
=_read
,--wrap
=_write
,--wrap
=semihost_connected
172 # Setup wraps to memory allocations routines if we want to tag heap allocations.
173 ifeq "$(HEAP_TAGS)" "1"
174 DEFINES
+= -DHEAP_TAGS
178 GCFLAGS
+= -O
$(OPTIMIZATION
) -g3
$(DEVICE_CFLAGS
)
179 GCFLAGS
+= -ffunction-sections
-fdata-sections
-fno-exceptions
-fno-delete-null-pointer-checks
180 GCFLAGS
+= $(patsubst %,-I
%,$(INCDIRS
))
181 GCFLAGS
+= $(DEFINES
)
182 GCFLAGS
+= $(DEPFLAGS
)
183 GCFLAGS
+= -Wall
-Wextra
-Wno-unused-parameter
-Wcast-align
-Wpointer-arith
-Wredundant-decls
-Wcast-qual
-Wcast-align
185 GPFLAGS
+= $(GCFLAGS
) -fno-rtti
-std
=gnu
++11
187 AS_GCFLAGS
+= -g3
$(DEVICE_FLAGS
) -x assembler-with-cpp
188 AS_GCFLAGS
+= $(patsubst %,-I
%,$(INCDIRS
))
189 AS_FLAGS
+= -g3
$(DEVICE_FLAGS
)
193 LDFLAGS
= $(DEVICE_FLAGS
) -specs
=$(BUILD_DIR
)/startfile.spec
194 LDFLAGS
+= -Wl
,-Map
=$(OUTDIR
)/$(PROJECT
).map
,--cref
,--gc-sections
,--wrap
=_isatty
,--wrap
=malloc
,--wrap
=realloc
,--wrap
=free
$(MRI_WRAPS
)
195 LDFLAGS
+= -T
$(LSCRIPT
) -L
$(EXTERNAL_DIR
)/gcc
/LPC1768
196 ifneq "$(NO_FLOAT_SCANF)" "1"
197 LDFLAGS
+= -u _scanf_float
199 ifneq "$(NO_FLOAT_PRINTF)" "1"
200 LDFLAGS
+= -u _printf_float
204 # Compiler/Assembler/Linker Paths
205 GCC
= arm-none-eabi-gcc
206 GPP
= arm-none-eabi-g
++
207 AS
= arm-none-eabi-as
208 LD
= arm-none-eabi-g
++
209 OBJCOPY
= arm-none-eabi-objcopy
210 OBJDUMP
= arm-none-eabi-objdump
211 SIZE
= arm-none-eabi-size
213 # Some tools are different on Windows in comparison to Unix.
214 ifeq "$(OS)" "Windows_NT"
217 REMOVE_DIR
= rd
/s
/q
219 QUIET
=>nul
2>nul
& exit
0
223 REMOVE_DIR
= rm -r
-f
225 QUIET
=> /dev
/null
2>&1 ; exit
0
229 # Create macro which will convert / to \ on Windows.
230 ifeq "$(OS)" "Windows_NT"
240 #########################################################################
241 .PHONY
: all clean size
243 all:: $(OUTDIR
)/$(PROJECT
).hex
$(OUTDIR
)/$(PROJECT
).bin
$(OUTDIR
)/$(PROJECT
).disasm size
245 $(OUTDIR
)/$(PROJECT
).bin
: $(OUTDIR
)/$(PROJECT
).elf
247 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
248 $(Q
) $(OBJCOPY
) -O binary
$< $@
250 $(OUTDIR
)/$(PROJECT
).hex
: $(OUTDIR
)/$(PROJECT
).elf
252 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
253 $(Q
) $(OBJCOPY
) -R .stack
-O ihex
$< $@
255 $(OUTDIR
)/$(PROJECT
).disasm
: $(OUTDIR
)/$(PROJECT
).elf
256 @echo Extracting disassembly to
$@
257 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
258 $(Q
) $(OBJDUMP
) -d
-f
-M reg-names-std
--demangle
$< >$@
260 $(OUTDIR
)/$(PROJECT
).elf
: $(LSCRIPT
) $(OBJECTS
)
262 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
263 $(Q
) $(LD
) $(LDFLAGS
) $(OBJECTS
) $(LIBS
) -o
$@
265 size
: $(OUTDIR
)/$(PROJECT
).elf
270 @echo Cleaning up
all build generated files
271 $(Q
) $(REMOVE_DIR
) $(OUTDIR
) $(QUIET
)
275 #########################################################################
276 # Default rules to compile .c and .cpp file to .o
277 # and assemble .s files to .o
279 $(OUTDIR
)/mbed_custom.o
: $(BUILD_DIR
)/mbed_custom.
cpp makefile
281 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
282 $(Q
) $(GPP
) $(GPFLAGS
) -c
$< -o
$@
284 $(OUTDIR
)/%.o
: %.
cpp makefile
286 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
287 $(Q
) $(GPP
) $(GPFLAGS
) -c
$< -o
$@
289 $(OUTDIR
)/%.o
: %.c makefile
291 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
292 $(Q
) $(GCC
) $(GCFLAGS
) -c
$< -o
$@
294 $(OUTDIR
)/%.o
: %.S makefile
296 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
297 $(Q
) $(GCC
) $(AS_GCFLAGS
) -c
$< -o
$@
299 $(OUTDIR
)/%.o
: %.s makefile
301 $(Q
) $(MKDIR
) $(call convert-slash
,$(dir $@
)) $(QUIET
)
302 $(Q
) $(AS
) $(AS_FLAGS
) -o
$@
$<
304 $(OUTDIR
)/configdefault.o
: config.default
305 $(Q
) $(OBJCOPY
) -I binary
-O elf32-littlearm
-B arm
--readonly-text
--rename-section .data
=.rodata.configdefault
$< $@
307 #########################################################################