Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / make / firmware-defs.mk
bloba77932ede38eab8107a094c923ed49375115d161
2 # Copyright (C) 2015, The LibrePilot Project, http://www.librepilot.org
3 # Copyright (c) 2010-2013, The OpenPilot Team, http://www.openpilot.org
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ifndef FLIGHT_MAKEFILE
21 $(error Top level Makefile must be used to build this target)
22 endif
24 # Define toolchain component names.
25 CC = $(CCACHE) $(ARM_SDK_PREFIX)gcc
26 CXX = $(CCACHE) $(ARM_SDK_PREFIX)g++
27 AR = $(ARM_SDK_PREFIX)ar
28 OBJCOPY = $(ARM_SDK_PREFIX)objcopy
29 OBJDUMP = $(ARM_SDK_PREFIX)objdump
30 SIZE = $(ARM_SDK_PREFIX)size
31 NM = $(ARM_SDK_PREFIX)nm
32 STRIP = $(ARM_SDK_PREFIX)strip
34 THUMB = -mthumb
36 # Add a board designator to the terse message text
37 ifeq ($(ENABLE_MSG_EXTRA),yes)
38 MSG_EXTRA := [$(BUILD_TYPE)|$(BOARD_SHORT_NAME)]
39 else
40 MSG_EXTRA :=
41 endif
43 # Define Messages
44 MSG_FORMATERROR = $(QUOTE) Can not handle output-format$(QUOTE)
45 MSG_MODINIT = $(QUOTE) MODINIT $(MSG_EXTRA) $(QUOTE)
46 MSG_SIZE = $(QUOTE) SIZE $(MSG_EXTRA) $(QUOTE)
47 MSG_LOAD_FILE = $(QUOTE) BIN/HEX $(MSG_EXTRA) $(QUOTE)
48 MSG_BIN_OBJ = $(QUOTE) BINO $(MSG_EXTRA) $(QUOTE)
49 MSG_STRIP_FILE = $(QUOTE) STRIP $(MSG_EXTRA) $(QUOTE)
50 MSG_EXTENDED_LISTING = $(QUOTE) LIS $(MSG_EXTRA) $(QUOTE)
51 MSG_SYMBOL_TABLE = $(QUOTE) NM $(MSG_EXTRA) $(QUOTE)
52 MSG_ARCHIVING = $(QUOTE) AR $(MSG_EXTRA) $(QUOTE)
53 MSG_LINKING = $(QUOTE) LD $(MSG_EXTRA) $(QUOTE)
54 MSG_COMPILING = $(QUOTE) CC $(MSG_EXTRA) $(QUOTE)
55 MSG_COMPILING_ARM = $(QUOTE) CC-ARM $(MSG_EXTRA) $(QUOTE)
56 MSG_COMPILINGCXX = $(QUOTE) CXX $(MSG_EXTRA) $(QUOTE)
57 MSG_COMPILINGCXX_ARM = $(QUOTE) CXX-ARM $(MSG_EXTRA) $(QUOTE)
58 MSG_ASSEMBLING = $(QUOTE) AS $(MSG_EXTRA) $(QUOTE)
59 MSG_ASSEMBLING_ARM = $(QUOTE) AS-ARM $(MSG_EXTRA) $(QUOTE)
60 MSG_CLEANING = $(QUOTE) CLEAN $(MSG_EXTRA) $(QUOTE)
61 MSG_ASMFROMC = $(QUOTE) AS(C) $(MSG_EXTRA) $(QUOTE)
62 MSG_ASMFROMC_ARM = $(QUOTE) AS(C)-ARM $(MSG_EXTRA) $(QUOTE)
63 MSG_PYMITEINIT = $(QUOTE) PY $(MSG_EXTRA) $(QUOTE)
64 MSG_OPFIRMWARE = $(QUOTE) OPFW $(MSG_EXTRA) $(QUOTE)
65 MSG_FWINFO = $(QUOTE) FWINFO $(MSG_EXTRA) $(QUOTE)
66 MSG_JTAG_PROGRAM = $(QUOTE) JTAG-PGM $(MSG_EXTRA) $(QUOTE)
67 MSG_JTAG_WIPE = $(QUOTE) JTAG-WIPE $(MSG_EXTRA) $(QUOTE)
68 MSG_PADDING = $(QUOTE) PADDING $(MSG_EXTRA) $(QUOTE)
69 MSG_FLASH_IMG = $(QUOTE) FLASH_IMG $(MSG_EXTRA) $(QUOTE)
70 MSG_PREPROCESSING_LDS = $(QUOTE) PP $(MSG_EXTRA) $(QUOTE)
72 # Function for converting an absolute path to one relative
73 # to the top of the source tree.
74 toprel = $(subst $(realpath $(FLIGHT_ROOT_DIR))/,,$(abspath $(1)))
76 # Function to replace special characters like is done for the symbols.
77 replace_special_chars = $(subst +,_,$(subst ~,_,$(subst @,_,$(subst :,_,$(subst -,_,$(subst .,_,$(subst /,_,$1)))))))
79 # Display compiler version information.
80 .PHONY: gccversion
81 gccversion:
82 @$(CC) --version
84 # Create final output file (.hex) from ELF output file.
85 %.hex: %.elf
86 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
87 $(V1) $(OBJCOPY) -O ihex $< $@
89 # Create stripped output file (.elf.stripped) from ELF output file.
90 %.elf.stripped: %.elf
91 @echo $(MSG_STRIP_FILE) $(call toprel, $@)
92 $(V1) $(STRIP) --strip-unneeded $< -o $@
94 # Create final output file (.bin) from ELF output file.
95 %.bin: %.elf
96 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
97 $(V1) $(OBJCOPY) -O binary $< $@
99 %.bin: %.o
100 @echo $(MSG_LOAD_FILE) $(call toprel, $@)
101 $(V1) $(OBJCOPY) -O binary $< $@
103 %.bin.o: %.bin
104 @echo $(MSG_BIN_OBJ) $(call toprel, $@)
105 $(V1) $(OBJCOPY) -I binary -O elf32-littlearm --binary-architecture arm \
106 --rename-section .data=.rodata,alloc,load,readonly,data,contents \
107 --wildcard \
108 --redefine-sym _binary_$(call replace_special_chars,$<)_start=_binary_start \
109 --redefine-sym _binary_$(call replace_special_chars,$<)_end=_binary_end \
110 --redefine-sym _binary_$(call replace_special_chars,$<)_size=_binary_size \
111 $< $@
113 # Create extended listing file/disassambly from ELF output file.
114 # using objdump testing: option -C
115 %.lss: %.elf
116 @echo $(MSG_EXTENDED_LISTING) $(call toprel, $@)
117 $(V1) $(OBJDUMP) -h -S -C -r $< > $@
119 # Create a symbol table from ELF output file.
120 %.sym: %.elf
121 @echo $(MSG_SYMBOL_TABLE) $(call toprel, $@)
122 $(V1) $(NM) -n $< > $@
124 define SIZE_TEMPLATE
125 .PHONY: size
126 size: $(1)_size
128 .PHONY: $(1)_size
129 $(1)_size: $(1)
130 @echo $(MSG_SIZE) $$(call toprel, $$<)
131 $(V1) $(SIZE) -A $$<
132 endef
134 # OpenPilot firmware image template
135 # $(1) = path to bin file
136 # $(2) = boardtype in hex
137 # $(3) = board revision in hex
138 define OPFW_TEMPLATE
139 FORCE:
141 $(1).firmware_info.c: $(1) $(FLIGHT_ROOT_DIR)/templates/firmware_info.c.template FORCE
142 @echo $(MSG_FWINFO) $$(call toprel, $$@)
143 $(V1) $(VERSION_INFO) \
144 --template=$(FLIGHT_ROOT_DIR)/templates/firmware_info.c.template \
145 --outfile=$$@ \
146 --image=$(1) \
147 --type=$(2) \
148 --revision=$(3) \
149 --uavodir=$(FLIGHT_ROOT_DIR)/../shared/uavobjectdefinition
151 $(eval $(call COMPILE_C_TEMPLATE, $(1).firmware_info.c))
153 $(OUTDIR)/$(notdir $(basename $(1))).opfw : $(1) $(1).firmware_info.bin
154 @echo $(MSG_OPFIRMWARE) $$(call toprel, $$@)
155 $(V1) cat $(1) $(1).firmware_info.bin > $$@
156 endef
158 # Assemble: create object files from assembler source files.
159 define ASSEMBLE_TEMPLATE
160 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
161 @echo $(MSG_ASSEMBLING) $$(call toprel, $$<)
162 $(V1) $(CC) -c $(THUMB) $$(ASFLAGS) $$< -o $$@
163 endef
165 # Assemble: create object files from assembler source files. ARM-only
166 define ASSEMBLE_ARM_TEMPLATE
167 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
168 @echo $(MSG_ASSEMBLING_ARM) $$(call toprel, $$<)
169 $(V1) $(CC) -c $$(ASFLAGS) $$< -o $$@
170 endef
172 # Compile: create object files from C source files.
173 define COMPILE_C_TEMPLATE
174 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
175 @echo $(MSG_COMPILING) $$(call toprel, $$<)
176 $(V1) $(CC) -c $(THUMB) $$(CFLAGS) $$(CONLYFLAGS) $$(CPPFLAGS) $$< -o $$@
177 endef
179 # Preprocess: create linker scripts from .lds files.
180 define PREPROCESS_LDS_TEMPLATE
181 $(OUTDIR)/$(notdir $(basename $(1))).ld : $(1)
182 @echo $(MSG_PREPROCESSING_LDS) $$(call toprel, $$<)
183 $(V1) $(CXX) -E -P -x c $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$< -o $$@
184 endef
186 # Compile: create object files from C source files. ARM-only
187 define COMPILE_C_ARM_TEMPLATE
188 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
189 @echo $(MSG_COMPILING_ARM) $$(call toprel, $$<)
190 $(V1) $(CC) -c $$(CFLAGS) $$(CONLYFLAGS) $$(CPPFLAGS) $$< -o $$@
191 endef
193 # Compile: create object files from C++ source files.
194 define COMPILE_CXX_TEMPLATE
195 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
196 @echo $(MSG_COMPILINGCXX) $$(call toprel, $$<)
197 $(V1) $(CXX) -c $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $$< -o $$@
198 endef
200 # Compile: create object files from C++ source files. ARM-only
201 define COMPILE_CXX_ARM_TEMPLATE
202 $(OUTDIR)/$(notdir $(basename $(1))).o : $(1)
203 @echo $(MSG_COMPILINGCXX_ARM) $$(call toprel, $$<)
204 $(V1) $(CPP) -c $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) $$< -o $$@
205 endef
207 # Archive: create ar library file from object files.
208 # $1 = library file to produce
209 # $2 = list of object files that make up the library file
210 # $3 = optional object files directory
211 define ARCHIVE_TEMPLATE
212 .SECONDARY : $(1)
213 .PRECIOUS : $(2)
214 $(1): $(2)
215 @echo $(MSG_ARCHIVING) $$(call toprel, $$@)
216 ifeq ($(3),)
217 $(V1) $(AR) rcs $$@ $(2)
218 else
219 # This is a workaround for Windows CreateProcess() line length
220 # limitation. It is assumed that if the object files directory
221 # is given, all object files are in that directory.
222 $(V1) ( \
223 pwd=`pwd` && \
224 cd $(3) && \
225 $(AR) rcs $$@ $(notdir $(2)) && \
226 cd $$$${pwd} >/dev/null \
228 endif
229 endef
231 # Link: create ELF output file from object files.
232 # $1 = elf file to produce
233 # $2 = list of object files that make up the elf file
234 # $3 = optional list of libraries to build and link
235 # $4 = optional list of linker scripts to build and link
236 define LINK_TEMPLATE
237 .SECONDARY : $(1)
238 .PRECIOUS : $(2) $(3) $(4)
239 $(1).input_files: $(2) $(3) $(4)
240 $(V1) rm -rf $(1).input_files
241 $(foreach file,$(2) $(3),
242 $(V1) echo $(file) >> $$@)
243 $(foreach file,$(4),
244 $(V1) echo -T$(file) >> $$@)
246 $(1): $(1).input_files
247 @echo $(MSG_LINKING) $$(call toprel, $$@)
248 $(V1) $(CC) $(THUMB) $$(CFLAGS) $$(CPPFLAGS) @$(1).input_files --output $$@ $$(LDFLAGS)
249 endef
251 # Link: create ELF output file from object files.
252 # $1 = elf file to produce
253 # $2 = list of object files that make up the elf file
254 # $3 = optional list of libraries to build and link
255 # $4 = optional list of linker scripts to build and link
256 define LINK_CXX_TEMPLATE
257 .SECONDARY : $(1)
258 .PRECIOUS : $(2) $(3) $(4)
259 $(1).input_files: $(2) $(3) $(4)
260 $(V1) rm -rf $(1).input_files
261 $(foreach file,$(2) $(3),
262 $(V1) echo $(file) >> $$@)
263 $(foreach file,$(4),
264 $(V1) echo -T$(file) >> $$@)
266 $(1): $(1).input_files
267 @echo $(MSG_LINKING) $$(call toprel, $$@)
268 $(V1) $(CXX) $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$(CXXFLAGS) @$(1).input_files --output $$@ $$(LDFLAGS)
269 endef
271 # Compile: create assembler files from C source files. ARM/Thumb
272 define PARTIAL_COMPILE_TEMPLATE
273 $($(1):.c=.s) : %.s : %.c
274 @echo $(MSG_ASMFROMC) $$(call toprel, $$<)
275 $(V1) $(CC) $(THUMB) -S $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@
276 endef
278 # Compile: create assembler files from C source files. ARM only
279 define PARTIAL_COMPILE_ARM_TEMPLATE
280 $($(1):.c=.s) : %.s : %.c
281 @echo $(MSG_ASMFROMC_ARM) $$(call toprel, $$<)
282 $(V1) $(CC) -S $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@
283 endef
285 # $(1) = Name of binary image to write
286 # $(2) = Base of flash region to write/wipe
287 # $(3) = Size of flash region to write/wipe
288 # $(4) = OpenOCD JTAG interface configuration file to use
289 # $(5) = OpenOCD configuration file to use
290 define JTAG_TEMPLATE
291 # ---------------------------------------------------------------------------
292 # Options for OpenOCD flash-programming
293 # see openocd.pdf/openocd.texi for further information
295 # debug level
296 OOCD_JTAG_SETUP = -d0
297 # interface and board/target settings (using the OOCD target-library here)
298 OOCD_JTAG_SETUP += -s $(FLIGHT_ROOT_DIR)/Project/OpenOCD
299 OOCD_JTAG_SETUP += -f $(4) -f $(5)
301 # initialize
302 OOCD_BOARD_RESET = -c init
303 # show the targets
304 #OOCD_BOARD_RESET += -c targets
305 # commands to prepare flash-write
306 OOCD_BOARD_RESET += -c "reset halt"
308 .PHONY: program
309 program: $(1)
310 @echo $(MSG_JTAG_PROGRAM) $$(call toprel, $$<)
311 $(V1) $(OPENOCD) \
312 $$(OOCD_JTAG_SETUP) \
313 $$(OOCD_BOARD_RESET) \
314 -c "flash write_image erase $$< $(2) bin" \
315 -c "verify_image $$< $(2) bin" \
316 -c "reset run" \
317 -c "shutdown"
319 .PHONY: wipe
320 wipe:
321 @echo $(MSG_JTAG_WIPE) wiping $(3) bytes starting from $(2)
322 $(V1) $(OPENOCD) \
323 $$(OOCD_JTAG_SETUP) \
324 $$(OOCD_BOARD_RESET) \
325 -c "flash erase_address pad $(2) $(3)" \
326 -c "reset run" \
327 -c "shutdown"
328 endef