Add debug modes to MSP (#8681)
[betaflight.git] / Makefile
blob20c540bebf8b2979f59af7ebecc058d2685a9dca
1 ###############################################################################
2 # "THE BEER-WARE LICENSE" (Revision 42):
3 # <msmith@FreeBSD.ORG> wrote this file. As long as you retain this notice you
4 # can do whatever you want with this stuff. If we meet some day, and you think
5 # this stuff is worth it, you can buy me a beer in return
6 ###############################################################################
8 # Makefile for building the betaflight firmware.
10 # Invoke this with 'make help' to see the list of supported targets.
12 ###############################################################################
15 # Things that the user might override on the commandline
18 # The target to build, see VALID_TARGETS below
19 TARGET ?= OMNIBUSF4
21 # Compile-time options
22 OPTIONS ?=
24 # compile for OpenPilot BootLoader support
25 OPBL ?= no
27 # compile for External Storage Bootloader support
28 EXST ?= no
30 # compile for target loaded into RAM
31 RAM_BASED ?= no
33 # Debugger optons:
34 # empty - ordinary build with all optimizations enabled
35 # RELWITHDEBINFO - ordinary build with debug symbols and all optimizations enabled
36 # GDB - debug build with minimum number of optimizations
37 DEBUG ?=
39 # Insert the debugging hardfault debugger
40 # releases should not be built with this flag as it does not disable pwm output
41 DEBUG_HARDFAULTS ?=
43 # Serial port/Device for flashing
44 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
46 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
47 FLASH_SIZE ?=
50 ###############################################################################
51 # Things that need to be maintained as the source changes
54 FORKNAME = betaflight
56 # Working directories
57 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
58 SRC_DIR := $(ROOT)/src/main
59 OBJECT_DIR := $(ROOT)/obj/main
60 BIN_DIR := $(ROOT)/obj
61 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
62 INCLUDE_DIRS := $(SRC_DIR) \
63 $(ROOT)/src/main/target \
64 $(ROOT)/src/main/startup
65 LINKER_DIR := $(ROOT)/src/link
67 ## V : Set verbosity level based on the V= parameter
68 ## V=0 Low
69 ## V=1 High
70 include $(ROOT)/make/build_verbosity.mk
72 # Build tools, so we all share the same versions
73 # import macros common to all supported build systems
74 include $(ROOT)/make/system-id.mk
76 # developer preferences, edit these at will, they'll be gitignored
77 -include $(ROOT)/make/local.mk
79 # configure some directories that are relative to wherever ROOT_DIR is located
80 ifndef TOOLS_DIR
81 TOOLS_DIR := $(ROOT)/tools
82 endif
83 BUILD_DIR := $(ROOT)/build
84 DL_DIR := $(ROOT)/downloads
86 export RM := rm
88 # import macros that are OS specific
89 include $(ROOT)/make/$(OSFAMILY).mk
91 # include the tools makefile
92 include $(ROOT)/make/tools.mk
94 # default xtal value for F4 targets
95 HSE_VALUE ?= 8000000
97 # used for turning on features like VCP and SDCARD
98 FEATURES =
100 # used to disable features based on flash space shortage (larger number => more features disabled)
101 FEATURE_CUT_LEVEL_SUPPLIED := $(FEATURE_CUT_LEVEL)
102 FEATURE_CUT_LEVEL =
104 # The list of targets to build for 'pre-push'
105 PRE_PUSH_TARGET_LIST ?= OMNIBUSF4 STM32F405 SPRACINGF7DUAL STM32F7X2 SITL test-representative
107 include $(ROOT)/make/targets.mk
109 REVISION := $(shell git log -1 --format="%h")
111 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
112 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
113 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
115 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
117 # Search path for sources
118 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
119 USBFS_DIR = $(ROOT)/lib/main/STM32_USB-FS-Device_Driver
120 USBPERIPH_SRC = $(notdir $(wildcard $(USBFS_DIR)/src/*.c))
121 FATFS_DIR = $(ROOT)/lib/main/FatFS
122 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
124 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
126 LD_FLAGS :=
129 # Default Tool options - can be overridden in {mcu}.mk files.
131 ifeq ($(DEBUG),GDB)
132 OPTIMISE_DEFAULT := -Og
134 LTO_FLAGS := $(OPTIMISE_DEFAULT)
135 DEBUG_FLAGS = -ggdb3 -DDEBUG
136 else
137 ifeq ($(DEBUG),INFO)
138 DEBUG_FLAGS = -ggdb3
139 endif
140 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math
141 OPTIMISE_DEFAULT := -O2
142 OPTIMISE_SPEED := -Ofast
143 OPTIMISE_SIZE := -Os
145 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
146 endif
148 VPATH := $(VPATH):$(ROOT)/make/mcu
149 VPATH := $(VPATH):$(ROOT)/make
151 # start specific includes
152 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
154 # openocd specific includes
155 include $(ROOT)/make/openocd.mk
157 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
158 ifeq ($(FLASH_SIZE),)
159 ifneq ($(TARGET_FLASH),)
160 FLASH_SIZE := $(TARGET_FLASH)
161 else
162 $(error FLASH_SIZE not configured for target $(TARGET))
163 endif
164 endif
166 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
168 ifneq ($(HSE_VALUE),)
169 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
170 endif
172 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED),)
173 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL_SUPPLIED)
174 else ifneq ($(FEATURE_CUT_LEVEL),)
175 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL)
176 endif
178 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
179 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
181 ifeq ($(OPBL),yes)
182 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
183 .DEFAULT_GOAL := binary
184 else
185 .DEFAULT_GOAL := hex
186 endif
188 INCLUDE_DIRS := $(INCLUDE_DIRS) \
189 $(ROOT)/lib/main/MAVLink
191 INCLUDE_DIRS := $(INCLUDE_DIRS) \
192 $(TARGET_DIR)
194 VPATH := $(VPATH):$(TARGET_DIR)
196 include $(ROOT)/make/source.mk
198 ###############################################################################
199 # Things that might need changing to use different tools
202 # Find out if ccache is installed on the system
203 CCACHE := ccache
204 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
205 ifneq ($(RESULT),0)
206 CCACHE :=
207 endif
209 # Tool names
210 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
211 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
212 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
213 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
214 OBJDUMP := $(ARM_SDK_PREFIX)objdump
215 SIZE := $(ARM_SDK_PREFIX)size
218 # Tool options.
220 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
221 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
222 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
223 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
224 CC_NO_OPTIMISATION :=
226 CFLAGS += $(ARCH_FLAGS) \
227 $(addprefix -D,$(OPTIONS)) \
228 $(addprefix -I,$(INCLUDE_DIRS)) \
229 $(DEBUG_FLAGS) \
230 -std=gnu11 \
231 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
232 -ffunction-sections \
233 -fdata-sections \
234 -fno-common \
235 -pedantic \
236 $(DEVICE_FLAGS) \
237 -D_GNU_SOURCE \
238 -DUSE_STDPERIPH_DRIVER \
239 -D$(TARGET) \
240 $(TARGET_FLAGS) \
241 -D'__FORKNAME__="$(FORKNAME)"' \
242 -D'__TARGET__="$(TARGET)"' \
243 -D'__REVISION__="$(REVISION)"' \
244 -save-temps=obj \
245 -MMD -MP \
246 $(EXTRA_FLAGS)
248 ASFLAGS = $(ARCH_FLAGS) \
249 $(DEBUG_FLAGS) \
250 -x assembler-with-cpp \
251 $(addprefix -I,$(INCLUDE_DIRS)) \
252 -MMD -MP
254 ifeq ($(LD_FLAGS),)
255 LD_FLAGS = -lm \
256 -nostartfiles \
257 --specs=nano.specs \
258 -lc \
259 -lnosys \
260 $(ARCH_FLAGS) \
261 $(LTO_FLAGS) \
262 $(DEBUG_FLAGS) \
263 -static \
264 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
265 -Wl,-L$(LINKER_DIR) \
266 -Wl,--cref \
267 -Wl,--no-wchar-size-warning \
268 -Wl,--print-memory-usage \
269 -T$(LD_SCRIPT)
270 endif
272 ###############################################################################
273 # No user-serviceable parts below
274 ###############################################################################
276 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
277 --std=c99 --inline-suppr --quiet --force \
278 $(addprefix -I,$(INCLUDE_DIRS)) \
279 -I/usr/include -I/usr/include/linux
282 # Things we will build
284 TARGET_S19 = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).s19
285 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
286 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
287 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
288 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_EXST.elf
289 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_UNPATCHED.bin
290 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
291 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
292 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
293 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
295 TARGET_EXST_HASH_SECTION_FILE = $(OBJECT_DIR)/$(TARGET)/exst_hash_section.bin
297 CLEAN_ARTIFACTS := $(TARGET_BIN)
298 CLEAN_ARTIFACTS += $(TARGET_HEX)
299 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
300 CLEAN_ARTIFACTS += $(TARGET_LST)
302 # Make sure build date and revision is updated on every incremental build
303 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
305 # List of buildable ELF files and their object dependencies.
306 # It would be nice to compute these lists, but that seems to be just beyond make.
308 $(TARGET_LST): $(TARGET_ELF)
309 $(V0) $(OBJDUMP) -S --disassemble $< > $@
312 $(TARGET_S19): $(TARGET_ELF)
313 @echo "Creating srec/S19 $(TARGET_S19)" "$(STDOUT)"
314 $(V1) $(OBJCOPY) --output-target=srec $(TARGET_S19)
316 ifeq ($(EXST),no)
317 $(TARGET_BIN): $(TARGET_ELF)
318 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
319 $(V1) $(OBJCOPY) -O binary $< $@
321 $(TARGET_HEX): $(TARGET_ELF)
322 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
323 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
325 else
326 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
328 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
329 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
330 $(V1) $(OBJCOPY) -O binary $< $@
332 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
333 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
334 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
335 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
336 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
337 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
339 @echo "Generating MD5 hash of binary" "$(STDOUT)"
340 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
342 @echo "Patching MD5 hash into binary" "$(STDOUT)"
343 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
344 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
346 # Note: From the objcopy manual "If you do not specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile"
347 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
348 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
349 # a) regeneration of $(TARGET_BIN), and
350 # b) the results of $(TARGET_BIN) will not be as expected.
351 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
352 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
353 rm $(TARGET_EXST_ELF).tmp
355 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
356 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
358 @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
359 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
361 $(TARGET_HEX): $(TARGET_BIN)
362 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
364 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
365 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
367 endif
369 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT)
370 @echo "Linking $(TARGET)" "$(STDOUT)"
371 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
372 $(V1) $(SIZE) $(TARGET_ELF)
374 # Compile
376 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
377 define compile_file
378 echo "%% ($(1)) $<" "$(STDOUT)" && \
379 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
380 endef
382 ifeq ($(DEBUG),GDB)
383 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
384 $(V1) mkdir -p $(dir $@)
385 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
386 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
388 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
390 else
391 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
392 $(V1) mkdir -p $(dir $@)
393 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
394 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
396 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
397 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
399 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
400 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
402 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
406 endif
408 # Assemble
409 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
410 $(V1) mkdir -p $(dir $@)
411 @echo "%% $(notdir $<)" "$(STDOUT)"
412 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
414 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
415 $(V1) mkdir -p $(dir $@)
416 @echo "%% $(notdir $<)" "$(STDOUT)"
417 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
420 ## all : Build all targets (excluding unsupported)
421 all: $(SUPPORTED_TARGETS)
423 ## all_with_unsupported : Build all targets (including unsupported)
424 all_with_unsupported: $(VALID_TARGETS)
426 ## unsupported : Build unsupported targets
427 unsupported: $(UNSUPPORTED_TARGETS)
429 ## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
430 pre-push:
431 $(MAKE) $(addsuffix _clean,$(PRE_PUSH_TARGET_LIST)) $(PRE_PUSH_TARGET_LIST) EXTRA_FLAGS=-Werror
433 ## official : Build all official (travis) targets
434 official: $(OFFICIAL_TARGETS)
436 ## targets-group-1 : build some targets
437 targets-group-1: $(GROUP_1_TARGETS)
439 ## targets-group-2 : build some targets
440 targets-group-2: $(GROUP_2_TARGETS)
442 ## targets-group-3 : build some targets
443 targets-group-3: $(GROUP_3_TARGETS)
445 ## targets-group-3 : build some targets
446 targets-group-4: $(GROUP_4_TARGETS)
448 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
449 targets-group-rest: $(GROUP_OTHER_TARGETS)
451 $(VALID_TARGETS):
452 $(V0) @echo "Building $@" && \
453 $(MAKE) binary hex TARGET=$@ && \
454 echo "Building $@ succeeded."
456 $(NOBUILD_TARGETS):
457 $(MAKE) TARGET=$@
459 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS) )
461 ## clean : clean up temporary / machine-generated files
462 clean:
463 @echo "Cleaning $(TARGET)"
464 $(V0) rm -f $(CLEAN_ARTIFACTS)
465 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
466 @echo "Cleaning $(TARGET) succeeded."
468 ## test_clean : clean up temporary / machine-generated files (tests)
469 test-%_clean:
470 $(MAKE) test_clean
472 test_clean:
473 $(V0) cd src/test && $(MAKE) clean || true
475 ## <TARGET>_clean : clean up one specific target (alias for above)
476 $(TARGETS_CLEAN):
477 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
479 ## clean_all : clean all valid targets
480 clean_all: $(TARGETS_CLEAN) test_clean
482 TARGETS_FLASH = $(addsuffix _flash,$(VALID_TARGETS) )
484 ## <TARGET>_flash : build and flash a target
485 $(TARGETS_FLASH):
486 $(V0) $(MAKE) binary hex TARGET=$(subst _flash,,$@)
487 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
488 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
489 else
490 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
491 endif
493 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
494 tty_flash:
495 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
496 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
497 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
499 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
500 dfu_flash:
501 ifneq (no-port-found,$(SERIAL_DEVICE))
502 # potentially this is because the MCU already is in DFU mode, try anyway
503 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
504 $(V0) sleep 1
505 endif
506 $(V0) dfu-util -a 0 -D $(TARGET_BIN) -s 0x08000000:leave
508 st-flash_$(TARGET): $(TARGET_BIN)
509 $(V0) st-flash --reset write $< 0x08000000
511 ## st-flash : flash firmware (.bin) onto flight controller
512 st-flash: st-flash_$(TARGET)
514 ifneq ($(OPENOCD_COMMAND),)
515 openocd-gdb: $(TARGET_ELF)
516 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
517 endif
519 binary:
520 $(V0) $(MAKE) -j $(TARGET_BIN)
522 srec:
523 $(V0) $(MAKE) -j $(TARGET_S19)
525 hex:
526 $(V0) $(MAKE) -j $(TARGET_HEX)
528 unbrick_$(TARGET): $(TARGET_HEX)
529 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
530 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
532 ## unbrick : unbrick flight controller
533 unbrick: unbrick_$(TARGET)
535 ## cppcheck : run static analysis on C source code
536 cppcheck: $(CSOURCES)
537 $(V0) $(CPPCHECK)
539 cppcheck-result.xml: $(CSOURCES)
540 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
542 # mkdirs
543 $(DL_DIR):
544 mkdir -p $@
546 $(TOOLS_DIR):
547 mkdir -p $@
549 $(BUILD_DIR):
550 mkdir -p $@
552 ## version : print firmware version
553 version:
554 @echo $(FC_VER)
556 ## help : print this help message and exit
557 help: Makefile make/tools.mk
558 @echo ""
559 @echo "Makefile for the $(FORKNAME) firmware"
560 @echo ""
561 @echo "Usage:"
562 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
563 @echo "Or:"
564 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
565 @echo ""
566 @echo "Valid TARGET values are: $(VALID_TARGETS)"
567 @echo ""
568 @sed -n 's/^## //p' $?
570 ## targets : print a list of all valid target platforms (for consumption by scripts)
571 targets:
572 @echo "Valid targets: $(VALID_TARGETS)"
573 @echo "Supported targets: $(SUPPORTED_TARGETS)"
574 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
575 @echo "Target: $(TARGET)"
576 @echo "Base target: $(BASE_TARGET)"
577 @echo "targets-group-1: $(GROUP_1_TARGETS)"
578 @echo "targets-group-2: $(GROUP_2_TARGETS)"
579 @echo "targets-group-3: $(GROUP_3_TARGETS)"
580 @echo "targets-group-4: $(GROUP_4_TARGETS)"
581 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
583 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
584 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
585 @echo "targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
586 @echo "targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
587 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
588 @echo "total in all groups $(words $(SUPPORTED_TARGETS)) targets"
590 ## target-mcu : print the MCU type of the target
591 target-mcu:
592 @echo $(TARGET_MCU)
594 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
595 targets-by-mcu:
596 $(V1) for target in $(VALID_TARGETS); do \
597 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
598 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
599 if [ "$${DO_BUILD}" = 1 ]; then \
600 echo "Building target $${target}..."; \
601 $(MAKE) TARGET=$${target}; \
602 if [ $$? -ne 0 ]; then \
603 echo "Building target $${target} failed, aborting."; \
604 exit 1; \
605 fi; \
606 else \
607 echo -n "$${target} "; \
608 fi; \
609 fi; \
610 done
611 @echo
613 ## targets-f3 : make all F3 targets
614 targets-f3:
615 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3 DO_BUILD=1
617 targets-f3-print:
618 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3
620 ## targets-f4 : make all F4 targets
621 targets-f4:
622 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 DO_BUILD=1
624 targets-f4-print:
625 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4
627 ## targets-f7 : make all F7 targets
628 targets-f7:
629 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 DO_BUILD=1
631 targets-f7-print:
632 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7
634 ## test : run the Betaflight test suite
635 ## junittest : run the Betaflight test suite, producing Junit XML result files.
636 ## test-representative: run a representative subset of the Betaflight test suite (i.e. run all tests, but run each expanded test only for one target)
637 ## test-all: run the Betaflight test suite including all per-target expanded tests
638 test junittest test-all test-representative:
639 $(V0) cd src/test && $(MAKE) $@
641 ## test_help : print the help message for the test suite (including a list of the available tests)
642 test_help:
643 $(V0) cd src/test && $(MAKE) help
645 ## test_% : run test 'test_%' from the test suite
646 test_%:
647 $(V0) cd src/test && $(MAKE) $@
650 check-target-independence:
651 $(V1) for test_target in $(VALID_TARGETS); do \
652 FOUND=$$(grep -rE "\W$${test_target}(\W.*)?$$" src/main | grep -vE "(//)|(/\*).*\W$${test_target}(\W.*)?$$" | grep -vE "^src/main/target"); \
653 if [ "$${FOUND}" != "" ]; then \
654 echo "Target dependencies for target '$${test_target}' found:"; \
655 echo "$${FOUND}"; \
656 exit 1; \
657 fi; \
658 done
660 check-fastram-usage-correctness:
661 $(V1) NON_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM_ZERO_INIT\W.*=.*" src/main/ | grep -Ev "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
662 if [ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
663 echo "Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
664 echo "$${NON_TRIVIALLY_INITIALIZED}"; \
665 exit 1; \
666 fi; \
667 TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -v "="); \
668 EXPLICITLY_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -E "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
669 if [ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
670 echo "Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
671 echo "$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
672 exit 1; \
675 check-platform-included:
676 $(V1) PLATFORM_NOT_INCLUDED=$$(find src/main -type d -name target -prune -o -type f -name \*.c -exec grep -L "^#include \"platform.h\"" {} \;); \
677 if [ "$$(echo $${PLATFORM_NOT_INCLUDED} | grep -v -e '^$$' | wc -l)" -ne 0 ]; then \
678 echo "The following compilation units do not include the required target specific configuration provided by 'platform.h':"; \
679 echo "$${PLATFORM_NOT_INCLUDED}"; \
680 exit 1; \
683 # rebuild everything when makefile changes
684 $(TARGET_OBJS): Makefile $(TARGET_DIR)/target.mk $(wildcard make/*)
686 # include auto-generated dependencies
687 -include $(TARGET_DEPS)