Merge pull request #11302 from xuhao1/sitl-seg-fix
[betaflight.git] / Makefile
blob7e9d9d63242967ed2a3b35621de70305431ed15b
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 FATFS_DIR = $(ROOT)/lib/main/FatFS
130 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
132 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
134 LD_FLAGS :=
135 EXTRA_LD_FLAGS :=
138 # Default Tool options - can be overridden in {mcu}.mk files.
140 ifeq ($(DEBUG),GDB)
141 OPTIMISE_DEFAULT := -Og
143 LTO_FLAGS := $(OPTIMISE_DEFAULT)
144 DEBUG_FLAGS = -ggdb3 -gdwarf-5 -DDEBUG
145 else
146 ifeq ($(DEBUG),INFO)
147 DEBUG_FLAGS = -ggdb3
148 endif
149 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math -fmerge-all-constants
150 OPTIMISE_DEFAULT := -O2
151 OPTIMISE_SPEED := -Ofast
152 OPTIMISE_SIZE := -Os
154 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
155 endif
157 VPATH := $(VPATH):$(ROOT)/make/mcu
158 VPATH := $(VPATH):$(ROOT)/make
160 # start specific includes
161 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
163 # openocd specific includes
164 include $(ROOT)/make/openocd.mk
166 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
167 ifeq ($(TARGET_FLASH_SIZE),)
168 ifneq ($(MCU_FLASH_SIZE),)
169 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
170 else
171 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
172 endif
173 endif
175 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
177 ifneq ($(HSE_VALUE),)
178 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
179 endif
181 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED),)
182 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL_SUPPLIED)
183 else ifneq ($(FEATURE_CUT_LEVEL),)
184 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFEATURE_CUT_LEVEL=$(FEATURE_CUT_LEVEL)
185 endif
187 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
188 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
190 ifeq ($(OPBL),yes)
191 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
192 .DEFAULT_GOAL := binary
193 else
194 .DEFAULT_GOAL := hex
195 endif
197 ifeq ($(CUSTOM_DEFAULTS_EXTENDED),yes)
198 TARGET_FLAGS += -DUSE_CUSTOM_DEFAULTS=
199 EXTRA_LD_FLAGS += -Wl,--defsym=USE_CUSTOM_DEFAULTS_EXTENDED=1
200 endif
202 INCLUDE_DIRS := $(INCLUDE_DIRS) \
203 $(ROOT)/lib/main/MAVLink
205 INCLUDE_DIRS := $(INCLUDE_DIRS) \
206 $(TARGET_DIR)
208 VPATH := $(VPATH):$(TARGET_DIR)
210 include $(ROOT)/make/source.mk
212 ###############################################################################
213 # Things that might need changing to use different tools
216 # Find out if ccache is installed on the system
217 CCACHE := ccache
218 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
219 ifneq ($(RESULT),0)
220 CCACHE :=
221 endif
223 # Tool names
224 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
225 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
226 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
227 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
228 OBJDUMP := $(ARM_SDK_PREFIX)objdump
229 READELF := $(ARM_SDK_PREFIX)readelf
230 SIZE := $(ARM_SDK_PREFIX)size
231 DFUSE-PACK := src/utils/dfuse-pack.py
234 # Tool options.
236 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
237 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
238 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
239 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
240 CC_NO_OPTIMISATION :=
243 # Added after GCC version update, remove once the warnings have been fixed
245 TEMPORARY_FLAGS :=
247 CFLAGS += $(ARCH_FLAGS) \
248 $(addprefix -D,$(OPTIONS)) \
249 $(addprefix -I,$(INCLUDE_DIRS)) \
250 $(DEBUG_FLAGS) \
251 -std=gnu11 \
252 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
253 -ffunction-sections \
254 -fdata-sections \
255 -fno-common \
256 -pedantic \
257 $(TEMPORARY_FLAGS) \
258 $(DEVICE_FLAGS) \
259 -D_GNU_SOURCE \
260 -DUSE_STDPERIPH_DRIVER \
261 -D$(TARGET) \
262 $(TARGET_FLAGS) \
263 -D'__FORKNAME__="$(FORKNAME)"' \
264 -D'__TARGET__="$(TARGET)"' \
265 -D'__REVISION__="$(REVISION)"' \
266 -save-temps=obj \
267 -MMD -MP \
268 $(EXTRA_FLAGS)
270 ASFLAGS = $(ARCH_FLAGS) \
271 $(DEBUG_FLAGS) \
272 -x assembler-with-cpp \
273 $(addprefix -I,$(INCLUDE_DIRS)) \
274 -MMD -MP
276 ifeq ($(LD_FLAGS),)
277 LD_FLAGS = -lm \
278 -nostartfiles \
279 --specs=nano.specs \
280 -lc \
281 -lnosys \
282 $(ARCH_FLAGS) \
283 $(LTO_FLAGS) \
284 $(DEBUG_FLAGS) \
285 -static \
286 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
287 -Wl,-L$(LINKER_DIR) \
288 -Wl,--cref \
289 -Wl,--no-wchar-size-warning \
290 -Wl,--print-memory-usage \
291 -T$(LD_SCRIPT) \
292 $(EXTRA_LD_FLAGS)
293 endif
295 ###############################################################################
296 # No user-serviceable parts below
297 ###############################################################################
299 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
300 --std=c99 --inline-suppr --quiet --force \
301 $(addprefix -I,$(INCLUDE_DIRS)) \
302 -I/usr/include -I/usr/include/linux
304 TARGET_BASENAME = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)
307 # Things we will build
309 TARGET_BIN = $(TARGET_BASENAME).bin
310 TARGET_HEX = $(TARGET_BASENAME).hex
311 TARGET_HEX_REV = $(TARGET_BASENAME)_$(REVISION).hex
312 TARGET_DFU = $(TARGET_BASENAME).dfu
313 TARGET_ZIP = $(TARGET_BASENAME).zip
314 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
315 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_EXST.elf
316 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_UNPATCHED.bin
317 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
318 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
319 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
320 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
322 TARGET_EXST_HASH_SECTION_FILE = $(OBJECT_DIR)/$(TARGET)/exst_hash_section.bin
324 CLEAN_ARTIFACTS := $(TARGET_BIN)
325 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
326 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
327 CLEAN_ARTIFACTS += $(TARGET_LST)
328 CLEAN_ARTIFACTS += $(TARGET_DFU)
330 # Make sure build date and revision is updated on every incremental build
331 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
333 # List of buildable ELF files and their object dependencies.
334 # It would be nice to compute these lists, but that seems to be just beyond make.
336 $(TARGET_LST): $(TARGET_ELF)
337 $(V0) $(OBJDUMP) -S --disassemble $< > $@
339 ifeq ($(EXST),no)
340 $(TARGET_BIN): $(TARGET_ELF)
341 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
342 $(V1) $(OBJCOPY) -O binary $< $@
344 $(TARGET_HEX): $(TARGET_ELF)
345 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
346 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
348 $(TARGET_DFU): $(TARGET_HEX)
349 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
350 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
352 else
353 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
355 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
356 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
357 $(V1) $(OBJCOPY) -O binary $< $@
359 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
360 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
361 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
362 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
363 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
364 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
366 @echo "Generating MD5 hash of binary" "$(STDOUT)"
367 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
369 @echo "Patching MD5 hash into binary" "$(STDOUT)"
370 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
371 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
373 # 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"
374 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
375 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
376 # a) regeneration of $(TARGET_BIN), and
377 # b) the results of $(TARGET_BIN) will not be as expected.
378 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
379 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
380 rm $(TARGET_EXST_ELF).tmp
382 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
383 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
385 # 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
386 # 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.
387 # The issue can be seen with `readelf -S $(TARGET_EXST_ELF)' vs `readelf -S $(TARGET_ELF)`
388 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
389 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --remove-section ._user_heap_stack --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
391 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
392 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
394 $(TARGET_HEX): $(TARGET_BIN)
395 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
397 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
398 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
400 endif
402 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
403 @echo "Linking $(TARGET)" "$(STDOUT)"
404 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
405 $(V1) $(SIZE) $(TARGET_ELF)
407 # Compile
409 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
410 define compile_file
411 echo "%% ($(1)) $<" "$(STDOUT)" && \
412 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
413 endef
415 ifeq ($(DEBUG),GDB)
416 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
417 $(V1) mkdir -p $(dir $@)
418 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
419 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
421 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
423 else
424 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
425 $(V1) mkdir -p $(dir $@)
426 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
427 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
429 $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
430 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
432 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
433 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
435 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
439 endif
441 # Assemble
442 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
443 $(V1) mkdir -p $(dir $@)
444 @echo "%% $(notdir $<)" "$(STDOUT)"
445 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
447 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
448 $(V1) mkdir -p $(dir $@)
449 @echo "%% $(notdir $<)" "$(STDOUT)"
450 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
453 ## all : Build all currently built targets
454 all: $(CI_TARGETS)
456 ## all_all : Build all targets (including legacy / unsupported)
457 all_all: $(VALID_TARGETS)
459 ## unified : build all Unified Targets
460 unified: $(UNIFIED_TARGETS)
462 ## unified_zip : build all Unified Targets as zip files (for posting on GitHub)
463 unified_zip: $(addsuffix _clean,$(UNIFIED_TARGETS)) $(addsuffix _zip,$(UNIFIED_TARGETS))
465 ## legacy : Build legacy targets
466 legacy: $(LEGACY_TARGETS)
468 ## unsupported : Build unsupported targets
469 unsupported: $(UNSUPPORTED_TARGETS)
471 ## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
472 pre-push:
473 $(MAKE) $(addsuffix _clean,$(PRE_PUSH_TARGET_LIST)) $(PRE_PUSH_TARGET_LIST) EXTRA_FLAGS=-Werror
475 ## targets-group-1 : build some targets
476 targets-group-1: $(GROUP_1_TARGETS)
478 ## targets-group-2 : build some targets
479 targets-group-2: $(GROUP_2_TARGETS)
481 ## targets-group-rest: build the rest of the targets (not listed in the other groups)
482 targets-group-rest: $(GROUP_OTHER_TARGETS)
484 $(VALID_TARGETS):
485 $(V0) @echo "Building $@" && \
486 $(MAKE) hex TARGET=$@ && \
487 echo "Building $@ succeeded."
489 $(NOBUILD_TARGETS):
490 $(MAKE) TARGET=$@
492 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS))
494 ## clean : clean up temporary / machine-generated files
495 clean:
496 @echo "Cleaning $(TARGET)"
497 $(V0) rm -f $(CLEAN_ARTIFACTS)
498 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
499 @echo "Cleaning $(TARGET) succeeded."
501 ## test_clean : clean up temporary / machine-generated files (tests)
502 test-%_clean:
503 $(MAKE) test_clean
505 test_clean:
506 $(V0) cd src/test && $(MAKE) clean || true
508 ## <TARGET>_clean : clean up one specific target (alias for above)
509 $(TARGETS_CLEAN):
510 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
512 ## clean_all : clean all valid targets
513 clean_all: $(TARGETS_CLEAN) test_clean
515 TARGETS_FLASH = $(addsuffix _flash,$(VALID_TARGETS))
517 ## <TARGET>_flash : build and flash a target
518 $(TARGETS_FLASH):
519 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
520 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
521 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
522 else
523 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
524 endif
526 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
527 tty_flash:
528 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
529 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
530 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
532 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
533 dfu_flash:
534 ifneq (no-port-found,$(SERIAL_DEVICE))
535 # potentially this is because the MCU already is in DFU mode, try anyway
536 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
537 $(V0) sleep 1
538 endif
539 $(V0) $(MAKE) $(TARGET_DFU)
540 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
542 st-flash_$(TARGET): $(TARGET_BIN)
543 $(V0) st-flash --reset write $< 0x08000000
545 ## st-flash : flash firmware (.bin) onto flight controller
546 st-flash: st-flash_$(TARGET)
548 ifneq ($(OPENOCD_COMMAND),)
549 openocd-gdb: $(TARGET_ELF)
550 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
551 endif
553 TARGETS_ZIP = $(addsuffix _zip,$(VALID_TARGETS))
555 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
556 $(TARGETS_ZIP):
557 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
558 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
560 zip:
561 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
563 binary:
564 $(V0) $(MAKE) -j $(TARGET_BIN)
566 hex:
567 $(V0) $(MAKE) -j $(TARGET_HEX)
569 TARGETS_REVISION = $(addsuffix _rev,$(VALID_TARGETS))
570 ## <TARGET>_rev : build target and add revision to filename
571 $(TARGETS_REVISION):
572 $(V0) $(MAKE) hex_rev TARGET=$(subst _rev,,$@)
574 hex_rev: hex
575 $(V0) mv -f $(TARGET_HEX) $(TARGET_HEX_REV)
577 all_rev: $(addsuffix _rev,$(CI_TARGETS))
579 unbrick_$(TARGET): $(TARGET_HEX)
580 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
581 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
583 ## unbrick : unbrick flight controller
584 unbrick: unbrick_$(TARGET)
586 ## cppcheck : run static analysis on C source code
587 cppcheck: $(CSOURCES)
588 $(V0) $(CPPCHECK)
590 cppcheck-result.xml: $(CSOURCES)
591 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
593 # mkdirs
594 $(DL_DIR):
595 mkdir -p $@
597 $(TOOLS_DIR):
598 mkdir -p $@
600 ## version : print firmware version
601 version:
602 @echo $(FC_VER)
604 ## help : print this help message and exit
605 help: Makefile make/tools.mk
606 @echo ""
607 @echo "Makefile for the $(FORKNAME) firmware"
608 @echo ""
609 @echo "Usage:"
610 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
611 @echo "Or:"
612 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
613 @echo ""
614 @echo "Valid TARGET values are: $(VALID_TARGETS)"
615 @echo ""
616 @sed -n 's/^## //p' $?
618 ## targets : print a list of all valid target platforms (for consumption by scripts)
619 targets:
620 @echo "Valid targets: $(VALID_TARGETS)"
621 @echo "Built targets: $(CI_TARGETS)"
622 @echo "Unified targets: $(UNIFIED_TARGETS)"
623 @echo "Legacy targets: $(LEGACY_TARGETS)"
624 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
625 @echo "Default target: $(TARGET)"
626 @echo "targets-group-1: $(GROUP_1_TARGETS)"
627 @echo "targets-group-2: $(GROUP_2_TARGETS)"
628 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
630 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
631 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
632 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
633 @echo "total in all groups $(words $(CI_TARGETS)) targets"
635 ## target-mcu : print the MCU type of the target
636 target-mcu:
637 @echo $(TARGET_MCU)
639 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
640 targets-by-mcu:
641 $(V1) for target in $${TARGETS}; do \
642 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
643 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
644 if [ "$${DO_BUILD}" = 1 ]; then \
645 echo "Building target $${target}..."; \
646 $(MAKE) TARGET=$${target}; \
647 if [ $$? -ne 0 ]; then \
648 echo "Building target $${target} failed, aborting."; \
649 exit 1; \
650 fi; \
651 else \
652 echo -n "$${target} "; \
653 fi; \
654 fi; \
655 done
656 @echo
658 ## targets-f4 : make all F4 targets
659 targets-f4:
660 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
662 targets-f4-print:
663 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(VALID_TARGETS)"
665 targets-ci-f4-print:
666 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4 TARGETS="$(CI_TARGETS)"
668 ## targets-f7 : make all F7 targets
669 targets-f7:
670 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)" DO_BUILD=1
672 targets-f7-print:
673 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(VALID_TARGETS)"
675 targets-ci-f7-print:
676 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7 TARGETS="$(CI_TARGETS)"
678 ## test : run the Betaflight test suite
679 ## junittest : run the Betaflight test suite, producing Junit XML result files.
680 ## 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)
681 ## test-all: run the Betaflight test suite including all per-target expanded tests
682 test junittest test-all test-representative:
683 $(V0) cd src/test && $(MAKE) $@
685 ## test_help : print the help message for the test suite (including a list of the available tests)
686 test_help:
687 $(V0) cd src/test && $(MAKE) help
689 ## test_versions : print the compiler versions used for the test suite
690 test_versions:
691 $(V0) cd src/test && $(MAKE) versions
693 ## test_% : run test 'test_%' from the test suite
694 test_%:
695 $(V0) cd src/test && $(MAKE) $@
698 # rebuild everything when makefile changes
699 $(TARGET_OBJS): Makefile $(TARGET_DIR)/target.mk $(wildcard make/*)
701 # include auto-generated dependencies
702 -include $(TARGET_DEPS)