Adding `make all_rev` goal
[betaflight.git] / Makefile
blob741fd38fcab2aa948c188a8258dc2173eca65822
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 ?= STM32F405
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 # reserve space for custom defaults
34 CUSTOM_DEFAULTS_EXTENDED ?= no
36 # Debugger optons:
37 # empty - ordinary build with all optimizations enabled
38 # INFO - ordinary build with debug symbols and all optimizations enabled
39 # GDB - debug build with minimum number of optimizations
40 DEBUG ?=
42 # Insert the debugging hardfault debugger
43 # releases should not be built with this flag as it does not disable pwm output
44 DEBUG_HARDFAULTS ?=
46 # Serial port/Device for flashing
47 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
49 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
50 FLASH_SIZE ?=
52 ###############################################################################
53 # Things that need to be maintained as the source changes
56 FORKNAME = betaflight
58 # Working directories
59 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
60 SRC_DIR := $(ROOT)/src/main
61 OBJECT_DIR := $(ROOT)/obj/main
62 BIN_DIR := $(ROOT)/obj
63 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
64 INCLUDE_DIRS := $(SRC_DIR) \
65 $(ROOT)/src/main/target \
66 $(ROOT)/src/main/startup
67 LINKER_DIR := $(ROOT)/src/link
69 ## V : Set verbosity level based on the V= parameter
70 ## V=0 Low
71 ## V=1 High
72 include $(ROOT)/make/build_verbosity.mk
74 # Build tools, so we all share the same versions
75 # import macros common to all supported build systems
76 include $(ROOT)/make/system-id.mk
78 # developer preferences, edit these at will, they'll be gitignored
79 -include $(ROOT)/make/local.mk
81 # pre-build sanity checks
82 include $(ROOT)/make/checks.mk
84 # configure some directories that are relative to wherever ROOT_DIR is located
85 TOOLS_DIR ?= $(ROOT)/tools
86 DL_DIR := $(ROOT)/downloads
88 export RM := rm
90 # import macros that are OS specific
91 include $(ROOT)/make/$(OSFAMILY).mk
93 # include the tools makefile
94 include $(ROOT)/make/tools.mk
96 # default xtal value for F4 targets
97 HSE_VALUE ?= 8000000
99 # used for turning on features like VCP and SDCARD
100 FEATURES =
102 # used to disable features based on flash space shortage (larger number => more features disabled)
103 FEATURE_CUT_LEVEL_SUPPLIED := $(FEATURE_CUT_LEVEL)
104 FEATURE_CUT_LEVEL =
106 # The list of targets to build for 'pre-push'
107 ifeq ($(OSFAMILY), macosx)
108 # SITL is not buildable on MacOS
109 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) STM32F4DISCOVERY_DEBUG test-representative
110 else
111 PRE_PUSH_TARGET_LIST ?= $(UNIFIED_TARGETS) SITL STM32F4DISCOVERY_DEBUG test-representative
112 endif
114 include $(ROOT)/make/targets.mk
116 REVISION := norevision
117 ifeq ($(shell git diff --shortstat),)
118 REVISION := $(shell git log -1 --format="%h")
119 endif
121 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
122 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
123 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
125 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
127 # Search path for sources
128 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
129 USBFS_DIR = $(ROOT)/lib/main/STM32_USB-FS-Device_Driver
130 USBPERIPH_SRC = $(notdir $(wildcard $(USBFS_DIR)/src/*.c))
131 FATFS_DIR = $(ROOT)/lib/main/FatFS
132 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
134 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
136 LD_FLAGS :=
137 EXTRA_LD_FLAGS :=
140 # Default Tool options - can be overridden in {mcu}.mk files.
142 ifeq ($(DEBUG),GDB)
143 OPTIMISE_DEFAULT := -Og
145 LTO_FLAGS := $(OPTIMISE_DEFAULT)
146 DEBUG_FLAGS = -ggdb3 -gdwarf-5 -DDEBUG
147 else
148 ifeq ($(DEBUG),INFO)
149 DEBUG_FLAGS = -ggdb3
150 endif
151 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
152 OPTIMISE_DEFAULT := -O2
153 OPTIMISE_SPEED := -Ofast
154 OPTIMISE_SIZE := -Os
156 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
157 endif
159 VPATH := $(VPATH):$(ROOT)/make/mcu
160 VPATH := $(VPATH):$(ROOT)/make
162 # start specific includes
163 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
165 # openocd specific includes
166 include $(ROOT)/make/openocd.mk
168 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
169 ifeq ($(TARGET_FLASH_SIZE),)
170 ifneq ($(MCU_FLASH_SIZE),)
171 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
172 else
173 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
174 endif
175 endif
177 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
179 ifneq ($(HSE_VALUE),)
180 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
181 endif
183 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED),)
184 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL_SUPPLIED)
185 else ifneq ($(FEATURE_CUT_LEVEL),)
186 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL)
187 endif
189 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
190 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
192 ifeq ($(OPBL),yes)
193 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
194 .DEFAULT_GOAL := binary
195 else
196 .DEFAULT_GOAL := hex
197 endif
199 ifeq ($(CUSTOM_DEFAULTS_EXTENDED),yes)
200 TARGET_FLAGS += -DUSE_CUSTOM_DEFAULTS=
201 EXTRA_LD_FLAGS += -Wl,--defsym=USE_CUSTOM_DEFAULTS_EXTENDED=1
202 endif
204 INCLUDE_DIRS := $(INCLUDE_DIRS) \
205 $(ROOT)/lib/main/MAVLink
207 INCLUDE_DIRS := $(INCLUDE_DIRS) \
208 $(TARGET_DIR)
210 VPATH := $(VPATH):$(TARGET_DIR)
212 include $(ROOT)/make/source.mk
214 ###############################################################################
215 # Things that might need changing to use different tools
218 # Find out if ccache is installed on the system
219 CCACHE := ccache
220 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
221 ifneq ($(RESULT),0)
222 CCACHE :=
223 endif
225 # Tool names
226 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
227 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
228 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
229 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
230 OBJDUMP := $(ARM_SDK_PREFIX)objdump
231 READELF := $(ARM_SDK_PREFIX)readelf
232 SIZE := $(ARM_SDK_PREFIX)size
233 DFUSE-PACK := src/utils/dfuse-pack.py
236 # Tool options.
238 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
239 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
240 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
241 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
242 CC_NO_OPTIMISATION :=
245 # Added after GCC version update, remove once the warnings have been fixed
247 TEMPORARY_FLAGS :=
249 CFLAGS += $(ARCH_FLAGS) \
250 $(addprefix -D,$(OPTIONS)) \
251 $(addprefix -I,$(INCLUDE_DIRS)) \
252 $(DEBUG_FLAGS) \
253 -std=gnu11 \
254 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
255 -ffunction-sections \
256 -fdata-sections \
257 -fno-common \
258 -pedantic \
259 $(TEMPORARY_FLAGS) \
260 $(DEVICE_FLAGS) \
261 -D_GNU_SOURCE \
262 -DUSE_STDPERIPH_DRIVER \
263 -D$(TARGET) \
264 $(TARGET_FLAGS) \
265 -D'__FORKNAME__="$(FORKNAME)"' \
266 -D'__TARGET__="$(TARGET)"' \
267 -D'__REVISION__="$(REVISION)"' \
268 -save-temps=obj \
269 -MMD -MP \
270 $(EXTRA_FLAGS)
272 ASFLAGS = $(ARCH_FLAGS) \
273 $(DEBUG_FLAGS) \
274 -x assembler-with-cpp \
275 $(addprefix -I,$(INCLUDE_DIRS)) \
276 -MMD -MP
278 ifeq ($(LD_FLAGS),)
279 LD_FLAGS = -lm \
280 -nostartfiles \
281 --specs=nano.specs \
282 -lc \
283 -lnosys \
284 $(ARCH_FLAGS) \
285 $(LTO_FLAGS) \
286 $(DEBUG_FLAGS) \
287 -static \
288 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
289 -Wl,-L$(LINKER_DIR) \
290 -Wl,--cref \
291 -Wl,--no-wchar-size-warning \
292 -Wl,--print-memory-usage \
293 -T$(LD_SCRIPT) \
294 $(EXTRA_LD_FLAGS)
295 endif
297 ###############################################################################
298 # No user-serviceable parts below
299 ###############################################################################
301 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
302 --std=c99 --inline-suppr --quiet --force \
303 $(addprefix -I,$(INCLUDE_DIRS)) \
304 -I/usr/include -I/usr/include/linux
306 TARGET_BASENAME = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)
309 # Things we will build
311 TARGET_BIN = $(TARGET_BASENAME).bin
312 TARGET_HEX = $(TARGET_BASENAME).hex
313 TARGET_HEX_REV = $(TARGET_BASENAME)_$(REVISION).hex
314 TARGET_DFU = $(TARGET_BASENAME).dfu
315 TARGET_ZIP = $(TARGET_BASENAME).zip
316 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
317 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_EXST.elf
318 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_UNPATCHED.bin
319 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
320 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
321 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
322 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
324 TARGET_EXST_HASH_SECTION_FILE = $(OBJECT_DIR)/$(TARGET)/exst_hash_section.bin
326 CLEAN_ARTIFACTS := $(TARGET_BIN)
327 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
328 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
329 CLEAN_ARTIFACTS += $(TARGET_LST)
330 CLEAN_ARTIFACTS += $(TARGET_DFU)
332 # Make sure build date and revision is updated on every incremental build
333 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
335 # List of buildable ELF files and their object dependencies.
336 # It would be nice to compute these lists, but that seems to be just beyond make.
338 $(TARGET_LST): $(TARGET_ELF)
339 $(V0) $(OBJDUMP) -S --disassemble $< > $@
341 ifeq ($(EXST),no)
342 $(TARGET_BIN): $(TARGET_ELF)
343 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
344 $(V1) $(OBJCOPY) -O binary $< $@
346 $(TARGET_HEX): $(TARGET_ELF)
347 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
348 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
350 $(TARGET_DFU): $(TARGET_HEX)
351 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
352 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
354 else
355 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
357 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
358 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
359 $(V1) $(OBJCOPY) -O binary $< $@
361 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
362 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
363 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
364 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
365 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
366 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
368 @echo "Generating MD5 hash of binary" "$(STDOUT)"
369 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
371 @echo "Patching MD5 hash into binary" "$(STDOUT)"
372 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
373 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
375 # 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"
376 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
377 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
378 # a) regeneration of $(TARGET_BIN), and
379 # b) the results of $(TARGET_BIN) will not be as expected.
380 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
381 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
382 rm $(TARGET_EXST_ELF).tmp
384 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
385 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
387 # For some currently unknown reason, OBJCOPY, with only input/output files, will generate a file around 2GB for the H730 unless we remove an unused-section
388 # As a workaround drop the ._user_heap_stack section, which is only used during build to show errors if there's not enough space for the heap/stack.
389 # The issue can be seen with `readelf -S $(TARGET_EXST_ELF)' vs `readelf -S $(TARGET_ELF)`
390 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
391 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --remove-section ._user_heap_stack --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
393 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
394 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
396 $(TARGET_HEX): $(TARGET_BIN)
397 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
399 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
400 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
402 endif
404 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
405 @echo "Linking $(TARGET)" "$(STDOUT)"
406 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
407 $(V1) $(SIZE) $(TARGET_ELF)
409 # Compile
411 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
412 define compile_file
413 echo "%% ($(1)) $<" "$(STDOUT)" && \
414 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
415 endef
417 ifeq ($(DEBUG),GDB)
418 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
419 $(V1) mkdir -p $(dir $@)
420 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
421 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
423 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
425 else
426 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
427 $(V1) mkdir -p $(dir $@)
428 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
429 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
431 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
432 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
434 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
435 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
437 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
441 endif
443 # Assemble
444 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
445 $(V1) mkdir -p $(dir $@)
446 @echo "%% $(notdir $<)" "$(STDOUT)"
447 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
449 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
450 $(V1) mkdir -p $(dir $@)
451 @echo "%% $(notdir $<)" "$(STDOUT)"
452 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
455 ## all : Build all currently built targets
456 all: $(CI_TARGETS)
458 ## all_all : Build all targets (including legacy / unsupported)
459 all_all: $(VALID_TARGETS)
461 ## unified : build all Unified Targets
462 unified: $(UNIFIED_TARGETS)
464 ## unified_zip : build all Unified Targets as zip files (for posting on GitHub)
465 unified_zip: $(addsuffix _clean,$(UNIFIED_TARGETS)) $(addsuffix _zip,$(UNIFIED_TARGETS))
467 ## legacy : Build legacy targets
468 legacy: $(LEGACY_TARGETS)
470 ## unsupported : Build unsupported targets
471 unsupported: $(UNSUPPORTED_TARGETS)
473 ## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
474 pre-push:
475 $(MAKE) $(addsuffix _clean,$(PRE_PUSH_TARGET_LIST)) $(PRE_PUSH_TARGET_LIST) EXTRA_FLAGS=-Werror
477 ## targets-group-1 : build some targets
478 targets-group-1: $(GROUP_1_TARGETS)
480 ## targets-group-2 : build some targets
481 targets-group-2: $(GROUP_2_TARGETS)
483 ## targets-group-rest: build the rest of the targets (not listed in the other groups)
484 targets-group-rest: $(GROUP_OTHER_TARGETS)
486 $(VALID_TARGETS):
487 $(V0) @echo "Building $@" && \
488 $(MAKE) hex TARGET=$@ && \
489 echo "Building $@ succeeded."
491 $(NOBUILD_TARGETS):
492 $(MAKE) TARGET=$@
494 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS))
496 ## clean : clean up temporary / machine-generated files
497 clean:
498 @echo "Cleaning $(TARGET)"
499 $(V0) rm -f $(CLEAN_ARTIFACTS)
500 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
501 @echo "Cleaning $(TARGET) succeeded."
503 ## test_clean : clean up temporary / machine-generated files (tests)
504 test-%_clean:
505 $(MAKE) test_clean
507 test_clean:
508 $(V0) cd src/test && $(MAKE) clean || true
510 ## <TARGET>_clean : clean up one specific target (alias for above)
511 $(TARGETS_CLEAN):
512 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
514 ## clean_all : clean all valid targets
515 clean_all: $(TARGETS_CLEAN) test_clean
517 TARGETS_FLASH = $(addsuffix _flash,$(VALID_TARGETS))
519 ## <TARGET>_flash : build and flash a target
520 $(TARGETS_FLASH):
521 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
522 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
523 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
524 else
525 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
526 endif
528 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
529 tty_flash:
530 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
531 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
532 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
534 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
535 dfu_flash:
536 ifneq (no-port-found,$(SERIAL_DEVICE))
537 # potentially this is because the MCU already is in DFU mode, try anyway
538 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
539 $(V0) sleep 1
540 endif
541 $(V0) $(MAKE) $(TARGET_DFU)
542 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
544 st-flash_$(TARGET): $(TARGET_BIN)
545 $(V0) st-flash --reset write $< 0x08000000
547 ## st-flash : flash firmware (.bin) onto flight controller
548 st-flash: st-flash_$(TARGET)
550 ifneq ($(OPENOCD_COMMAND),)
551 openocd-gdb: $(TARGET_ELF)
552 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
553 endif
555 TARGETS_ZIP = $(addsuffix _zip,$(VALID_TARGETS))
557 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
558 $(TARGETS_ZIP):
559 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
560 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
562 zip:
563 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
565 binary:
566 $(V0) $(MAKE) -j $(TARGET_BIN)
568 hex:
569 $(V0) $(MAKE) -j $(TARGET_HEX)
571 TARGETS_REVISION = $(addsuffix _rev,$(VALID_TARGETS))
572 ## <TARGET>_rev : build target and add revision to filename
573 $(TARGETS_REVISION):
574 $(V0) $(MAKE) hex_rev TARGET=$(subst _rev,,$@)
576 hex_rev: hex
577 $(V0) mv -f $(TARGET_HEX) $(TARGET_HEX_REV)
579 all_rev: $(addsuffix _rev,$(CI_TARGETS))
581 unbrick_$(TARGET): $(TARGET_HEX)
582 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
583 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
585 ## unbrick : unbrick flight controller
586 unbrick: unbrick_$(TARGET)
588 ## cppcheck : run static analysis on C source code
589 cppcheck: $(CSOURCES)
590 $(V0) $(CPPCHECK)
592 cppcheck-result.xml: $(CSOURCES)
593 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
595 # mkdirs
596 $(DL_DIR):
597 mkdir -p $@
599 $(TOOLS_DIR):
600 mkdir -p $@
602 ## version : print firmware version
603 version:
604 @echo $(FC_VER)
606 ## help : print this help message and exit
607 help: Makefile make/tools.mk
608 @echo ""
609 @echo "Makefile for the $(FORKNAME) firmware"
610 @echo ""
611 @echo "Usage:"
612 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
613 @echo "Or:"
614 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
615 @echo ""
616 @echo "Valid TARGET values are: $(VALID_TARGETS)"
617 @echo ""
618 @sed -n 's/^## //p' $?
620 ## targets : print a list of all valid target platforms (for consumption by scripts)
621 targets:
622 @echo "Valid targets: $(VALID_TARGETS)"
623 @echo "Built targets: $(CI_TARGETS)"
624 @echo "Unified targets: $(UNIFIED_TARGETS)"
625 @echo "Legacy targets: $(LEGACY_TARGETS)"
626 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
627 @echo "Default target: $(TARGET)"
628 @echo "targets-group-1: $(GROUP_1_TARGETS)"
629 @echo "targets-group-2: $(GROUP_2_TARGETS)"
630 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
632 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
633 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
634 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
635 @echo "total in all groups $(words $(CI_TARGETS)) targets"
637 ## target-mcu : print the MCU type of the target
638 target-mcu:
639 @echo $(TARGET_MCU)
641 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
642 targets-by-mcu:
643 $(V1) for target in $${TARGETS}; do \
644 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
645 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
646 if [ "$${DO_BUILD}" = 1 ]; then \
647 echo "Building target $${target}..."; \
648 $(MAKE) TARGET=$${target}; \
649 if [ $$? -ne 0 ]; then \
650 echo "Building target $${target} failed, aborting."; \
651 exit 1; \
652 fi; \
653 else \
654 echo -n "$${target} "; \
655 fi; \
656 fi; \
657 done
658 @echo
660 ## targets-f4 : make all F4 targets
661 targets-f4:
662 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
664 targets-f4-print:
665 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)"
667 targets-ci-f4-print:
668 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(CI_TARGETS)"
670 ## targets-f7 : make all F7 targets
671 targets-f7:
672 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
674 targets-f7-print:
675 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)"
677 targets-ci-f7-print:
678 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(CI_TARGETS)"
680 ## test : run the Betaflight test suite
681 ## junittest : run the Betaflight test suite, producing Junit XML result files.
682 ## 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)
683 ## test-all: run the Betaflight test suite including all per-target expanded tests
684 test junittest test-all test-representative:
685 $(V0) cd src/test && $(MAKE) $@
687 ## test_help : print the help message for the test suite (including a list of the available tests)
688 test_help:
689 $(V0) cd src/test && $(MAKE) help
691 ## test_versions : print the compiler versions used for the test suite
692 test_versions:
693 $(V0) cd src/test && $(MAKE) versions
695 ## test_% : run test 'test_%' from the test suite
696 test_%:
697 $(V0) cd src/test && $(MAKE) $@
700 # rebuild everything when makefile changes
701 $(TARGET_OBJS): Makefile $(TARGET_DIR)/target.mk $(wildcard make/*)
703 # include auto-generated dependencies
704 -include $(TARGET_DEPS)