2 ## This file is part of the libopencm3 project.
4 ## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
5 ## Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
6 ## Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
8 ## This library is free software: you can redistribute it and/or modify
9 ## it under the terms of the GNU Lesser General Public License as published by
10 ## the Free Software Foundation, either version 3 of the License, or
11 ## (at your option) any later version.
13 ## This library is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU Lesser General Public License for more details.
18 ## You should have received a copy of the GNU Lesser General Public License
19 ## along with this library. If not, see <http://www.gnu.org/licenses/>.
22 # Be silent per default, but 'make V=1' will show all compiler calls.
28 ###############################################################################
31 PREFIX ?
= arm-none-eabi
38 OBJCOPY
:= $(PREFIX
)-objcopy
39 OBJDUMP
:= $(PREFIX
)-objdump
40 SIZE
:= $(PREFIX
)-size
42 STFLASH
= $(shell which st-flash
)
43 STYLECHECK
:= /checkpatch.pl
44 STYLECHECKFLAGS
:= --no-tree
-f
--terse
--mailback
45 STYLECHECKFILES
:= $(shell find .
-name
'*.[ch]')
50 ###############################################################################
56 ifeq ($(strip $(OPENCM3_DIR
)),)
57 # user has not specified the library path, so we try to detect it
59 # where we search for the library
60 LIBPATHS
:= .
/libopencm3 ..
/..
/..
/..
/libopencm3 ..
/..
/..
/..
/..
/libopencm3
62 OPENCM3_DIR
:= $(wildcard $(LIBPATHS
:=/locm3.sublime-project
))
63 OPENCM3_DIR
:= $(firstword $(dir $(OPENCM3_DIR
)))
65 ifeq ($(strip $(OPENCM3_DIR
)),)
66 $(warning Cannot find libopencm3 library in the standard search paths.
)
67 $(error Please specify it through OPENCM3_DIR variable
!)
72 $(info Using
$(OPENCM3_DIR
) path to library
)
75 define ERR_DEVICE_LDSCRIPT_CONFLICT
76 You can either specify DEVICE
=blah
, and have the LDSCRIPT generated
,
77 or you can provide LDSCRIPT
, and ensure CPPFLAGS
, LDFLAGS and LDLIBS
78 all contain the correct values for the target you wish to use.
79 You cannot provide both
!
82 ifeq ($(strip $(DEVICE
)),)
83 # Old style, assume LDSCRIPT exists
84 DEFS
+= -I
$(OPENCM3_DIR
)/include
85 LDFLAGS
+= -L
$(OPENCM3_DIR
)/lib
86 LDLIBS
+= -l
$(LIBNAME
)
87 LDSCRIPT ?
= $(BINARY
).
ld
89 # New style, assume device is provided, and we're generating the rest.
90 ifneq ($(strip $(LDSCRIPT
)),)
91 $(error
$(ERR_DEVICE_LDSCRIPT_CONFLICT
))
93 include $(OPENCM3_DIR
)/mk
/genlink-config.mk
96 SCRIPT_DIR
= $(OPENCM3_DIR
)/scripts
98 ###############################################################################
101 TGT_CFLAGS
+= $(OPT
) $(CSTD
) -g
102 TGT_CFLAGS
+= $(ARCH_FLAGS
)
103 TGT_CFLAGS
+= -Wextra
-Wshadow
-Wimplicit-function-declaration
104 TGT_CFLAGS
+= -Wredundant-decls
-Wstrict-prototypes
105 TGT_CFLAGS
+= -fno-common
-ffunction-sections
-fdata-sections
107 ###############################################################################
110 TGT_CXXFLAGS
+= $(OPT
) $(CXXSTD
) -g
111 TGT_CXXFLAGS
+= $(ARCH_FLAGS
)
112 TGT_CXXFLAGS
+= -Wextra
-Wshadow
-Wredundant-decls
-Weffc
++
113 TGT_CXXFLAGS
+= -fno-common
-ffunction-sections
-fdata-sections
115 ###############################################################################
116 # C & C++ preprocessor common flags
119 TGT_CPPFLAGS
+= -Wall
-Wundef
120 TGT_CPPFLAGS
+= $(DEFS
)
122 ###############################################################################
125 TGT_LDFLAGS
+= --static
-nostartfiles
--specs
=nano.specs
126 TGT_LDFLAGS
+= -T
$(LDSCRIPT
)
127 TGT_LDFLAGS
+= $(ARCH_FLAGS
)
128 TGT_LDFLAGS
+= -Wl
,-Map
=$(*).map
129 TGT_LDFLAGS
+= -Wl
,--gc-sections
131 TGT_LDFLAGS
+= -Wl
,--print-gc-sections
134 ###############################################################################
137 LDLIBS
+= -Wl
,--start-group
-lc
-lgcc
-lnosys
-Wl
,--end-group
139 ###############################################################################
140 ###############################################################################
141 ###############################################################################
143 .SUFFIXES
: .elf .bin .hex .srec .list .map .images
156 images
: $(BINARY
).images
157 flash
: $(BINARY
).stlink-flash
159 # Either verify the user provided LDSCRIPT exists, or generate it.
160 ifeq ($(strip $(DEVICE
)),)
162 ifeq (,$(wildcard $(LDSCRIPT
)))
163 $(error Unable to find specified linker script
: $(LDSCRIPT
))
166 include $(OPENCM3_DIR
)/mk
/genlink-rules.mk
169 # Define a helper macro for debugging make errors online
170 # you can type "make print-OPENCM3_DIR" and it will show you
171 # how that ended up being resolved by all of the included
176 %.images
: %.bin
%.hex
%.srec
%.list
%.map
177 @
#printf "*** $* images generated ***\n"
180 @
#printf " OBJCOPY $(*).bin\n"
181 $(Q
)$(OBJCOPY
) -Obinary
$(*).elf
$(*).bin
184 @
#printf " OBJCOPY $(*).hex\n"
185 $(Q
)$(OBJCOPY
) -Oihex
$(*).elf
$(*).hex
188 @
#printf " OBJCOPY $(*).srec\n"
189 $(Q
)$(OBJCOPY
) -Osrec
$(*).elf
$(*).srec
192 @
#printf " OBJDUMP $(*).list\n"
193 $(Q
)$(OBJDUMP
) -S
$(*).elf
> $(*).list
195 %.elf
%.map
: $(OBJS
) $(LDSCRIPT
)
196 @
#printf " LD $(*).elf\n"
197 $(Q
)$(LD
) $(TGT_LDFLAGS
) $(LDFLAGS
) $(OBJS
) $(LDLIBS
) -o
$(*).elf
200 @
#printf " CC $(*).c\n"
201 $(Q
)$(CC
) $(TGT_CFLAGS
) $(CFLAGS
) $(TGT_CPPFLAGS
) $(CPPFLAGS
) -o
$(*).o
-c
$(*).c
204 @
#printf " CXX $(*).cxx\n"
205 $(Q
)$(CXX
) $(TGT_CXXFLAGS
) $(CXXFLAGS
) $(TGT_CPPFLAGS
) $(CPPFLAGS
) -o
$(*).o
-c
$(*).
cxx
208 @
#printf " CXX $(*).cpp\n"
209 $(Q
)$(CXX
) $(TGT_CXXFLAGS
) $(CXXFLAGS
) $(TGT_CPPFLAGS
) $(CPPFLAGS
) -o
$(*).o
-c
$(*).
cpp
213 $(Q
)$(RM
) *.o
*.d
*.elf
*.bin
*.hex
*.srec
*.list
*.map generated.
* ${OBJS} ${OBJS
:%.o
=%.d
}
216 @
#printf " SIZE $(*).elf\n"
219 stylecheck
: $(STYLECHECKFILES
:=.stylecheck
)
220 styleclean
: $(STYLECHECKFILES
:=.styleclean
)
222 # the cat is due to multithreaded nature - we like to have consistent chunks of text on the output
224 $(Q
)$(SCRIPT_DIR
)$(STYLECHECK
) $(STYLECHECKFLAGS
) $* > $*.stylecheck
; \
225 if
[ -s
$*.stylecheck
]; then \
228 rm -f
$*.stylecheck
; \
232 $(Q
)rm -f
$*.stylecheck
;
235 %.stlink-flash
: %.bin
236 @printf
" FLASH $<\n"
237 $(STFLASH
) write
$(*).bin
0x8000000
239 ifeq ($(STLINK_PORT
),)
243 @printf
" FLASH $<\n"
244 (echo
"halt; program $(realpath $(*).elf) verify reset" | nc
-4 localhost
4444 2>/dev
/null
) || \
245 $(OOCD
) -f interface
/$(OOCD_INTERFACE
).cfg \
246 -f target
/$(OOCD_TARGET
).cfg \
247 -c
"program $(*).elf verify reset exit" \
251 @printf
" FLASH $<\n"
252 (echo
"halt; program $(realpath $(*).elf) verify reset" | nc
-4 localhost
4444 2>/dev
/null
) || \
253 $(OOCD
) -f
$(OOCD_FILE
) \
254 -c
"program $(*).elf verify reset exit" \
259 @printf
" GDB $(*).elf (flash)\n"
261 -ex
'target extended-remote $(BMP_PORT)' \
262 -x
$(SCRIPT_DIR
)/black_magic_probe_flash.scr \
267 @printf
" GDB $(*).elf (flash)\n"
269 -ex
'target extended-remote $(STLINK_PORT)' \
270 -x
$(SCRIPT_DIR
)/stlink_flash.scr \
274 .PHONY
: images
clean stylecheck styleclean elf bin hex srec list size
276 -include $(OBJS
:.o
=.d
)