Refactoring of PR 13050 - support for custom OSD messages from external device (...
[betaflight.git] / Makefile
blob827e74632f0c61bc1935c9a81af308d141923fc9
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 BASE_TARGETS below
19 DEFAULT_TARGET ?= STM32F405
20 TARGET ?=
21 CONFIG ?=
23 # Compile-time options
24 OPTIONS ?=
26 # compile for External Storage Bootloader support
27 EXST ?= no
29 # compile for target loaded into RAM
30 RAM_BASED ?= no
32 # reserve space for custom defaults
33 CUSTOM_DEFAULTS_EXTENDED ?= no
35 # Debugger optons:
36 # empty - ordinary build with all optimizations enabled
37 # INFO - ordinary build with debug symbols and all optimizations enabled. Only builds touched files.
38 # GDB - debug build with minimum number of optimizations
39 DEBUG ?=
41 # Insert the debugging hardfault debugger
42 # releases should not be built with this flag as it does not disable pwm output
43 DEBUG_HARDFAULTS ?=
45 # Serial port/Device for flashing
46 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
48 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
49 FLASH_SIZE ?=
51 ###############################################################################
52 # Things that need to be maintained as the source changes
55 FORKNAME = betaflight
57 # Working directories
58 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
59 PLATFORM_DIR := $(ROOT)/src/platform
60 SRC_DIR := $(ROOT)/src/main
61 LIB_MAIN_DIR := $(ROOT)/lib/main
62 OBJECT_DIR := $(ROOT)/obj/main
63 BIN_DIR := $(ROOT)/obj
64 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
65 INCLUDE_DIRS := $(SRC_DIR)
67 MAKE_SCRIPT_DIR := $(ROOT)/mk
69 ## V : Set verbosity level based on the V= parameter
70 ## V=0 Low
71 ## V=1 High
72 include $(MAKE_SCRIPT_DIR)/build_verbosity.mk
74 # Build tools, so we all share the same versions
75 # import macros common to all supported build systems
76 include $(MAKE_SCRIPT_DIR)/system-id.mk
78 # developer preferences, edit these at will, they'll be gitignored
79 ifneq ($(wildcard $(MAKE_SCRIPT_DIR)/local.mk),)
80 include $(MAKE_SCRIPT_DIR)/local.mk
81 endif
83 # some targets use parallel build by default
84 # MAKEFLAGS is valid only inside target, do not use this at parse phase
85 DEFAULT_PARALLEL_JOBS := # all jobs in parallel (for backward compatibility)
86 MAKE_PARALLEL = $(if $(filter -j%, $(MAKEFLAGS)),$(EMPTY),-j$(DEFAULT_PARALLEL_JOBS))
88 # pre-build sanity checks
89 include $(MAKE_SCRIPT_DIR)/checks.mk
91 # basic target list
92 PLATFORMS := $(sort $(notdir $(patsubst /%,%, $(wildcard $(PLATFORM_DIR)/*))))
93 BASE_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(PLATFORM_DIR)/*/target/*/target.mk)))))
95 # configure some directories that are relative to wherever ROOT_DIR is located
96 TOOLS_DIR ?= $(ROOT)/tools
97 DL_DIR := $(ROOT)/downloads
98 CONFIG_DIR ?= $(BETAFLIGHT_CONFIG)
99 ifeq ($(CONFIG_DIR),)
100 CONFIG_DIR := $(ROOT)/src/config
101 endif
102 DIRECTORIES := $(DL_DIR) $(TOOLS_DIR)
104 export RM := rm
106 # import macros that are OS specific
107 include $(MAKE_SCRIPT_DIR)/$(OSFAMILY).mk
109 # include the tools makefile
110 include $(MAKE_SCRIPT_DIR)/tools.mk
112 # Search path for sources
113 VPATH := $(SRC_DIR):$(LIB_MAIN_DIR):$(PLATFORM_DIR)
114 FATFS_DIR = $(ROOT)/lib/main/FatFS
115 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
116 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
118 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
119 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
120 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
122 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
124 # import config handling
125 include $(MAKE_SCRIPT_DIR)/config.mk
127 ifeq ($(CONFIG),)
128 ifeq ($(TARGET),)
129 TARGET := $(DEFAULT_TARGET)
130 SKIPCHECKS := yes
131 endif
132 endif
134 # default xtal value
135 HSE_VALUE ?= 8000000
137 CI_EXCLUDED_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(PLATFORM_DIR)/*/target/*/.exclude)))))
138 CI_TARGETS := $(filter-out $(CI_EXCLUDED_TARGETS), $(BASE_TARGETS)) $(filter STM32F4DISCOVERY CRAZYBEEF4SX1280 CRAZYBEEF4FR IFLIGHT_BLITZ_F722 NUCLEOF446 SPRACINGH7EXTREME SPRACINGH7RF, $(BASE_CONFIGS))
140 TARGET_PLATFORM := $(notdir $(patsubst %/,%,$(subst target/$(TARGET)/,, $(dir $(wildcard $(PLATFORM_DIR)/*/target/$(TARGET)/target.mk)))))
141 TARGET_PLATFORM_DIR := $(PLATFORM_DIR)/$(TARGET_PLATFORM)
142 LINKER_DIR := $(TARGET_PLATFORM_DIR)/link
144 include $(TARGET_PLATFORM_DIR)/target/$(TARGET)/target.mk
146 REVISION := norevision
147 ifeq ($(shell git diff --shortstat),)
148 REVISION := $(shell git log -1 --format="%h")
149 endif
151 LD_FLAGS :=
152 EXTRA_LD_FLAGS :=
155 # Default Tool options - can be overridden in {mcu}.mk files.
157 DEBUG_MIXED = no
159 ifeq ($(DEBUG),INFO)
160 DEBUG_MIXED = yes
161 endif
162 ifeq ($(DEBUG),GDB)
163 DEBUG_MIXED = yes
164 endif
166 ifeq ($(DEBUG),GDB)
167 OPTIMISE_DEFAULT := -Og
169 LTO_FLAGS := $(OPTIMISE_DEFAULT)
170 DEBUG_FLAGS = -ggdb2 -gdwarf-5 -DDEBUG
171 else
172 ifeq ($(DEBUG),INFO)
173 DEBUG_FLAGS = -ggdb2
174 endif
175 OPTIMISATION_BASE := -flto=auto -fuse-linker-plugin -ffast-math -fmerge-all-constants
176 OPTIMISE_DEFAULT := -O2
177 OPTIMISE_SPEED := -Ofast
178 OPTIMISE_SIZE := -Os
180 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
181 endif
183 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)
185 # start specific includes
186 ifeq ($(TARGET_MCU),)
187 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
188 endif
190 ifeq ($(TARGET_MCU_FAMILY),)
191 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
192 endif
194 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_PLATFORM) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
196 ifneq ($(CONFIG),)
197 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
198 endif
200 SPEED_OPTIMISED_SRC :=
201 SIZE_OPTIMISED_SRC :=
203 include $(TARGET_PLATFORM_DIR)/mk/$(TARGET_MCU_FAMILY).mk
205 # openocd specific includes
206 include $(MAKE_SCRIPT_DIR)/openocd.mk
208 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
209 ifeq ($(TARGET_FLASH_SIZE),)
210 ifneq ($(MCU_FLASH_SIZE),)
211 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
212 else
213 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
214 endif
215 endif
217 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
219 ifneq ($(HSE_VALUE),)
220 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
221 endif
223 TARGET_DIR = $(TARGET_PLATFORM_DIR)/target/$(TARGET)
225 .DEFAULT_GOAL := hex
227 INCLUDE_DIRS := $(INCLUDE_DIRS) \
228 $(ROOT)/lib/main/MAVLink
230 INCLUDE_DIRS := $(INCLUDE_DIRS) \
231 $(TARGET_DIR)
233 VPATH := $(VPATH):$(TARGET_DIR)
235 include $(MAKE_SCRIPT_DIR)/source.mk
237 ifneq ($(SKIPCHECKS),yes)
238 ifneq ($(filter-out $(SRC),$(SPEED_OPTIMISED_SRC)),)
239 $(error Speed optimised sources not valid: $(strip $(filter-out $(SRC),$(SPEED_OPTIMISED_SRC))))
240 endif
242 ifneq ($(filter-out $(SRC),$(SIZE_OPTIMISED_SRC)),)
243 $(error Size optimised sources not valid: $(strip $(filter-out $(SRC),$(SIZE_OPTIMISED_SRC))))
244 endif
245 endif
247 ###############################################################################
248 # Things that might need changing to use different tools
251 # Find out if ccache is installed on the system
252 CCACHE := ccache
253 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
254 ifneq ($(RESULT),0)
255 CCACHE :=
256 endif
258 # Tool names
259 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
260 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
261 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
262 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
263 OBJDUMP := $(ARM_SDK_PREFIX)objdump
264 READELF := $(ARM_SDK_PREFIX)readelf
265 SIZE := $(ARM_SDK_PREFIX)size
266 DFUSE-PACK := src/utils/dfuse-pack.py
269 # Tool options.
271 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
272 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
273 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
274 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
275 CC_NO_OPTIMISATION :=
278 # Added after GCC version update, remove once the warnings have been fixed
280 TEMPORARY_FLAGS :=
282 EXTRA_WARNING_FLAGS := -Wold-style-definition
284 CFLAGS += $(ARCH_FLAGS) \
285 $(addprefix -D,$(OPTIONS)) \
286 $(addprefix -I,$(INCLUDE_DIRS)) \
287 $(DEBUG_FLAGS) \
288 -std=gnu17 \
289 -Wall -Wextra -Werror -Wunsafe-loop-optimizations -Wdouble-promotion \
290 $(EXTRA_WARNING_FLAGS) \
291 -ffunction-sections \
292 -fdata-sections \
293 -fno-common \
294 $(TEMPORARY_FLAGS) \
295 $(DEVICE_FLAGS) \
296 -D_GNU_SOURCE \
297 -DUSE_STDPERIPH_DRIVER \
298 -D$(TARGET) \
299 $(TARGET_FLAGS) \
300 -D'__FORKNAME__="$(FORKNAME)"' \
301 -D'__TARGET__="$(TARGET)"' \
302 -D'__REVISION__="$(REVISION)"' \
303 $(CONFIG_REVISION_DEFINE) \
304 -pipe \
305 -MMD -MP \
306 $(EXTRA_FLAGS)
308 ASFLAGS = $(ARCH_FLAGS) \
309 $(DEBUG_FLAGS) \
310 -x assembler-with-cpp \
311 $(addprefix -I,$(INCLUDE_DIRS)) \
312 -MMD -MP
314 ifeq ($(LD_FLAGS),)
315 LD_FLAGS = -lm \
316 -nostartfiles \
317 --specs=nano.specs \
318 -lc \
319 -lnosys \
320 $(ARCH_FLAGS) \
321 $(LTO_FLAGS) \
322 $(DEBUG_FLAGS) \
323 -static \
324 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
325 -Wl,-L$(LINKER_DIR) \
326 -Wl,--cref \
327 -Wl,--no-wchar-size-warning \
328 -Wl,--print-memory-usage \
329 -T$(LD_SCRIPT) \
330 $(EXTRA_LD_FLAGS)
331 endif
333 ###############################################################################
334 # No user-serviceable parts below
335 ###############################################################################
337 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
338 --std=c99 --inline-suppr --quiet --force \
339 $(addprefix -I,$(INCLUDE_DIRS)) \
340 -I/usr/include -I/usr/include/linux
342 TARGET_NAME := $(TARGET)
344 ifneq ($(CONFIG),)
345 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
346 endif
348 ifeq ($(REV),yes)
349 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
350 endif
352 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
354 # Things we will build
356 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
357 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
358 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
359 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
360 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
361 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
362 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
363 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
364 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
365 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
366 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
367 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
369 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
371 ifeq ($(DEBUG_MIXED),yes)
372 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
373 else
374 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEBUG_FLAGS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
375 endif
377 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
379 CLEAN_ARTIFACTS := $(TARGET_BIN)
380 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
381 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
382 CLEAN_ARTIFACTS += $(TARGET_LST)
383 CLEAN_ARTIFACTS += $(TARGET_DFU)
385 # Make sure build date and revision is updated on every incremental build
386 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
388 # List of buildable ELF files and their object dependencies.
389 # It would be nice to compute these lists, but that seems to be just beyond make.
391 $(TARGET_LST): $(TARGET_ELF)
392 $(V0) $(OBJDUMP) -S --disassemble $< > $@
394 ifeq ($(EXST),no)
395 $(TARGET_BIN): $(TARGET_ELF)
396 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
397 $(V1) $(OBJCOPY) -O binary $< $@
399 $(TARGET_HEX): $(TARGET_ELF)
400 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
401 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
403 $(TARGET_DFU): $(TARGET_HEX)
404 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
405 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
407 else
408 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
410 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
411 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
412 $(V1) $(OBJCOPY) -O binary $< $@
414 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
415 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
416 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
417 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
418 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
419 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
421 @echo "Generating MD5 hash of binary" "$(STDOUT)"
422 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
424 @echo "Patching MD5 hash into binary" "$(STDOUT)"
425 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
426 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
428 # 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"
429 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
430 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
431 # a) regeneration of $(TARGET_BIN), and
432 # b) the results of $(TARGET_BIN) will not be as expected.
433 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
434 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
435 rm $(TARGET_EXST_ELF).tmp
437 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
438 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
440 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
441 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
443 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
444 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
446 $(TARGET_HEX): $(TARGET_BIN)
447 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
449 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
450 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
452 endif
454 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
455 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
456 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
457 $(V1) $(SIZE) $(TARGET_ELF)
459 # Compile
461 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
462 define compile_file
463 echo "%% ($(1)) $<" "$(STDOUT)" && \
464 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
465 endef
467 ## `paths` is a list of paths that will be replaced for checking of speed, and size optimised sources
468 paths := $(SRC_DIR)/ $(LIB_MAIN_DIR)/ $(PLATFORM_DIR)/
469 subst_paths_for = $(foreach path,$(paths),$(filter-out $(1),$(subst $(path),,$(1))))
470 subst_paths = $(strip $(if $(call subst_paths_for,$(1)), $(call subst_paths_for,$(1)), $(1)))
472 ifeq ($(DEBUG),GDB)
473 $(TARGET_OBJ_DIR)/%.o: %.c
474 $(V1) mkdir -p $(dir $@)
475 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
476 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
478 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
480 else
481 $(TARGET_OBJ_DIR)/%.o: %.c
482 $(V1) mkdir -p $(dir $@)
483 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
484 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
486 $(if $(findstring $(call subst_paths,$<),$(SPEED_OPTIMISED_SRC)), \
487 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
489 $(if $(findstring $(call subst_paths,$<),$(SIZE_OPTIMISED_SRC)), \
490 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
492 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
496 endif
498 # Assemble
499 $(TARGET_OBJ_DIR)/%.o: %.s
500 $(V1) mkdir -p $(dir $@)
501 @echo "%% $(notdir $<)" "$(STDOUT)"
502 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
504 $(TARGET_OBJ_DIR)/%.o: %.S
505 $(V1) mkdir -p $(dir $@)
506 @echo "%% $(notdir $<)" "$(STDOUT)"
507 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
510 ## all : Build all currently built targets
511 all: $(CI_TARGETS)
513 $(BASE_TARGETS):
514 $(V0) @echo "Building target $@" && \
515 $(MAKE) hex TARGET=$@ && \
516 echo "Building $@ succeeded."
518 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
520 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
522 ## clean : clean up temporary / machine-generated files
523 clean:
524 @echo "Cleaning $(TARGET_NAME)"
525 $(V0) rm -f $(CLEAN_ARTIFACTS)
526 $(V0) rm -rf $(TARGET_OBJ_DIR)
527 @echo "Cleaning $(TARGET_NAME) succeeded."
529 ## test_clean : clean up temporary / machine-generated files (tests)
530 test-%_clean:
531 $(MAKE) test_clean
533 test_clean:
534 $(V0) cd src/test && $(MAKE) clean || true
536 ## <TARGET>_clean : clean up one specific target (alias for above)
537 $(TARGETS_CLEAN):
538 $(V0) $(MAKE) $(MAKE_PARALLEL) TARGET=$(subst _clean,,$@) clean
540 ## <CONFIG>_clean : clean up one specific target (alias for above)
541 $(CONFIGS_CLEAN):
542 $(V0) $(MAKE) $(MAKE_PARALLEL) CONFIG=$(subst _clean,,$@) clean
544 ## clean_all : clean all targets
545 clean_all: $(TARGETS_CLEAN) test_clean
547 ## all_configs : Build all configs
548 all_configs: $(BASE_CONFIGS)
550 TARGETS_FLASH = $(addsuffix _flash,$(BASE_TARGETS))
552 ## <TARGET>_flash : build and flash a target
553 $(TARGETS_FLASH):
554 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
555 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
556 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
557 else
558 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
559 endif
561 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
562 tty_flash:
563 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
564 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
565 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
567 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
568 dfu_flash:
569 ifneq (no-port-found,$(SERIAL_DEVICE))
570 # potentially this is because the MCU already is in DFU mode, try anyway
571 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
572 $(V0) sleep 1
573 endif
574 $(V0) $(MAKE) $(TARGET_DFU)
575 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
577 st-flash_$(TARGET): $(TARGET_BIN)
578 $(V0) st-flash --reset write $< 0x08000000
580 ## st-flash : flash firmware (.bin) onto flight controller
581 st-flash: st-flash_$(TARGET)
583 ifneq ($(OPENOCD_COMMAND),)
584 openocd-gdb: $(TARGET_ELF)
585 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
586 endif
588 TARGETS_ZIP = $(addsuffix _zip,$(BASE_TARGETS))
590 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
591 $(TARGETS_ZIP):
592 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
593 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
595 zip:
596 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
598 binary:
599 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_BIN)
601 hex:
602 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_HEX)
604 TARGETS_REVISION = $(addsuffix _rev,$(BASE_TARGETS))
605 ## <TARGET>_rev : build target and add revision to filename
606 $(TARGETS_REVISION):
607 $(V0) $(MAKE) hex REV=yes TARGET=$(subst _rev,,$@)
609 all_rev: $(addsuffix _rev,$(CI_TARGETS))
611 unbrick_$(TARGET): $(TARGET_HEX)
612 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
613 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
615 ## unbrick : unbrick flight controller
616 unbrick: unbrick_$(TARGET)
618 ## cppcheck : run static analysis on C source code
619 cppcheck: $(CSOURCES)
620 $(V0) $(CPPCHECK)
622 cppcheck-result.xml: $(CSOURCES)
623 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
625 # mkdirs
626 $(DIRECTORIES):
627 mkdir -p $@
629 ## version : print firmware version
630 version:
631 @echo $(FC_VER)
633 ## help : print this help message and exit
634 help: Makefile mk/tools.mk
635 @echo ""
636 @echo "Makefile for the $(FORKNAME) firmware"
637 @echo ""
638 @echo "Usage:"
639 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
640 @echo "Or:"
641 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
642 @echo "Or:"
643 @echo " make <config-target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
644 @echo ""
645 @echo "To pupulate configuration targets:"
646 @echo " make configs"
647 @echo ""
648 @echo "Valid TARGET values are: $(BASE_TARGETS)"
649 @echo ""
650 @sed -n 's/^## //p' $?
652 ## targets : print a list of all valid target platforms (for consumption by scripts)
653 targets:
654 @echo "Valid targets: $(BASE_TARGETS)"
655 @echo "Built targets: $(CI_TARGETS)"
656 @echo "Default target: $(TARGET)"
658 targets-ci-print:
659 @echo $(CI_TARGETS)
661 ## target-mcu : print the MCU type of the target
662 target-mcu:
663 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
665 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
666 targets-by-mcu:
667 $(V1) for target in $${TARGETS}; do \
668 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
669 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
670 if [ "$${DO_BUILD}" = 1 ]; then \
671 echo "Building target $${target}..."; \
672 $(MAKE) TARGET=$${target}; \
673 if [ $$? -ne 0 ]; then \
674 echo "Building target $${target} failed, aborting."; \
675 exit 1; \
676 fi; \
677 else \
678 echo -n "$${target} "; \
679 fi; \
680 fi; \
681 done
682 @echo
684 ## test : run the Betaflight test suite
685 ## junittest : run the Betaflight test suite, producing Junit XML result files.
686 ## 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)
687 ## test-all: run the Betaflight test suite including all per-target expanded tests
688 test junittest test-all test-representative:
689 $(V0) cd src/test && $(MAKE) $@
691 ## test_help : print the help message for the test suite (including a list of the available tests)
692 test_help:
693 $(V0) cd src/test && $(MAKE) help
695 ## test_versions : print the compiler versions used for the test suite
696 test_versions:
697 $(V0) cd src/test && $(MAKE) versions
699 ## test_% : run test 'test_%' from the test suite
700 test_%:
701 $(V0) cd src/test && $(MAKE) $@
703 $(TARGET_EF_HASH_FILE):
704 $(V1) mkdir -p $(dir $@)
705 $(V1) rm -f $(TARGET_OBJ_DIR)/.efhash_*
706 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
707 $(V1) touch $(TARGET_EF_HASH_FILE)
709 # rebuild everything when makefile changes or the extra flags have changed
710 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
712 # include auto-generated dependencies
713 -include $(TARGET_DEPS)