]> cloudbase.mooo.com Git - irmp-demo.git/blob - libopencm3.rules.mk
Add .gitignore
[irmp-demo.git] / libopencm3.rules.mk
1 ##
2 ## This file is part of the libopencm3 project.
3 ##
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>
7 ##
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.
12 ##
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.
17 ##
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/>.
20 ##
21
22 # Be silent per default, but 'make V=1' will show all compiler calls.
23 ifneq ($(V),1)
24 Q := @
25 NULL := 2>/dev/null
26 endif
27
28 ###############################################################################
29 # Executables
30
31 PREFIX ?= arm-none-eabi
32
33 CC := $(PREFIX)-gcc
34 CXX := $(PREFIX)-g++
35 LD := $(PREFIX)-gcc
36 AR := $(PREFIX)-ar
37 AS := $(PREFIX)-as
38 OBJCOPY := $(PREFIX)-objcopy
39 OBJDUMP := $(PREFIX)-objdump
40 SIZE := $(PREFIX)-size
41 GDB := $(PREFIX)-gdb
42 STFLASH = $(shell which st-flash)
43 STYLECHECK := /checkpatch.pl
44 STYLECHECKFLAGS := --no-tree -f --terse --mailback
45 STYLECHECKFILES := $(shell find . -name '*.[ch]')
46 OPT := -Os
47 CSTD ?= -std=c99
48
49
50 ###############################################################################
51 # Source files
52
53 OBJS += $(BINARY).o
54
55
56 ifeq ($(strip $(OPENCM3_DIR)),)
57 # user has not specified the library path, so we try to detect it
58
59 # where we search for the library
60 LIBPATHS := ./libopencm3 ../../../../libopencm3 ../../../../../libopencm3
61
62 OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project))
63 OPENCM3_DIR := $(firstword $(dir $(OPENCM3_DIR)))
64
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!)
68 endif
69 endif
70
71 ifeq ($(V),1)
72 $(info Using $(OPENCM3_DIR) path to library)
73 endif
74
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!
80 endef
81
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
88 else
89 # New style, assume device is provided, and we're generating the rest.
90 ifneq ($(strip $(LDSCRIPT)),)
91 $(error $(ERR_DEVICE_LDSCRIPT_CONFLICT))
92 endif
93 include $(OPENCM3_DIR)/mk/genlink-config.mk
94 endif
95
96 SCRIPT_DIR = $(OPENCM3_DIR)/scripts
97
98 ###############################################################################
99 # C flags
100
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
106
107 ###############################################################################
108 # C++ flags
109
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
114
115 ###############################################################################
116 # C & C++ preprocessor common flags
117
118 TGT_CPPFLAGS += -MD
119 TGT_CPPFLAGS += -Wall -Wundef
120 TGT_CPPFLAGS += $(DEFS)
121
122 ###############################################################################
123 # Linker flags
124
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
130 ifeq ($(V),99)
131 TGT_LDFLAGS += -Wl,--print-gc-sections
132 endif
133
134 ###############################################################################
135 # Used libraries
136
137 LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
138
139 ###############################################################################
140 ###############################################################################
141 ###############################################################################
142
143 .SUFFIXES: .elf .bin .hex .srec .list .map .images
144 .SECONDEXPANSION:
145 .SECONDARY:
146
147 all: elf
148
149 elf: $(BINARY).elf
150 bin: $(BINARY).bin
151 hex: $(BINARY).hex
152 srec: $(BINARY).srec
153 list: $(BINARY).list
154 size: $(BINARY).size
155
156 images: $(BINARY).images
157 flash: $(BINARY).stlink-flash
158
159 # Either verify the user provided LDSCRIPT exists, or generate it.
160 ifeq ($(strip $(DEVICE)),)
161 $(LDSCRIPT):
162 ifeq (,$(wildcard $(LDSCRIPT)))
163 $(error Unable to find specified linker script: $(LDSCRIPT))
164 endif
165 else
166 include $(OPENCM3_DIR)/mk/genlink-rules.mk
167 endif
168
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
172 # makefiles.
173 print-%:
174 @echo $*=$($*)
175
176 %.images: %.bin %.hex %.srec %.list %.map
177 @#printf "*** $* images generated ***\n"
178
179 %.bin: %.elf
180 @#printf " OBJCOPY $(*).bin\n"
181 $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
182
183 %.hex: %.elf
184 @#printf " OBJCOPY $(*).hex\n"
185 $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex
186
187 %.srec: %.elf
188 @#printf " OBJCOPY $(*).srec\n"
189 $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec
190
191 %.list: %.elf
192 @#printf " OBJDUMP $(*).list\n"
193 $(Q)$(OBJDUMP) -S $(*).elf > $(*).list
194
195 %.elf %.map: $(OBJS) $(LDSCRIPT)
196 @#printf " LD $(*).elf\n"
197 $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
198
199 %.o: %.c
200 @#printf " CC $(*).c\n"
201 $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).c
202
203 %.o: %.cxx
204 @#printf " CXX $(*).cxx\n"
205 $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cxx
206
207 %.o: %.cpp
208 @#printf " CXX $(*).cpp\n"
209 $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp
210
211 clean:
212 @#printf " CLEAN\n"
213 $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map generated.* ${OBJS} ${OBJS:%.o=%.d}
214
215 %.size: %.elf
216 @#printf " SIZE $(*).elf\n"
217 $(Q)$(SIZE) $(*).elf
218
219 stylecheck: $(STYLECHECKFILES:=.stylecheck)
220 styleclean: $(STYLECHECKFILES:=.styleclean)
221
222 # the cat is due to multithreaded nature - we like to have consistent chunks of text on the output
223 %.stylecheck: %
224 $(Q)$(SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \
225 if [ -s $*.stylecheck ]; then \
226 cat $*.stylecheck; \
227 else \
228 rm -f $*.stylecheck; \
229 fi;
230
231 %.styleclean:
232 $(Q)rm -f $*.stylecheck;
233
234
235 %.stlink-flash: %.bin
236 @printf " FLASH $<\n"
237 $(STFLASH) write $(*).bin 0x8000000
238
239 ifeq ($(STLINK_PORT),)
240 ifeq ($(BMP_PORT),)
241 ifeq ($(OOCD_FILE),)
242 %.flash: %.elf
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" \
248 $(NULL)
249 else
250 %.flash: %.elf
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" \
255 $(NULL)
256 endif
257 else
258 %.flash: %.elf
259 @printf " GDB $(*).elf (flash)\n"
260 $(GDB) --batch \
261 -ex 'target extended-remote $(BMP_PORT)' \
262 -x $(SCRIPT_DIR)/black_magic_probe_flash.scr \
263 $(*).elf
264 endif
265 else
266 %.flash: %.elf
267 @printf " GDB $(*).elf (flash)\n"
268 $(GDB) --batch \
269 -ex 'target extended-remote $(STLINK_PORT)' \
270 -x $(SCRIPT_DIR)/stlink_flash.scr \
271 $(*).elf
272 endif
273
274 .PHONY: images clean stylecheck styleclean elf bin hex srec list size
275
276 -include $(OBJS:.o=.d)