Refactor missing prototypes 2 (#14170)
[betaflight.git] / Makefile
blobd7e2136d4ece9af7ed04ba1320cb02737d5a42be
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 TARGET ?=
20 CONFIG ?=
22 # Compile-time options
23 OPTIONS ?=
25 # compile for External Storage Bootloader support
26 EXST ?= no
28 # compile for target loaded into RAM
29 RAM_BASED ?= no
31 # reserve space for custom defaults
32 CUSTOM_DEFAULTS_EXTENDED ?= no
34 # Debugger optons:
35 # empty - ordinary build with all optimizations enabled
36 # INFO - ordinary build with debug symbols and all optimizations enabled. Only builds touched files.
37 # GDB - debug build with minimum number of optimizations
38 DEBUG ?=
40 # Insert the debugging hardfault debugger
41 # releases should not be built with this flag as it does not disable pwm output
42 DEBUG_HARDFAULTS ?=
44 # Serial port/Device for flashing
45 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyACM*) $(firstword $(wildcard /dev/ttyUSB*) no-port-found))
47 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
48 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 PLATFORM_DIR := $(ROOT)/src/platform
59 SRC_DIR := $(ROOT)/src/main
60 LIB_MAIN_DIR := $(ROOT)/lib/main
61 OBJECT_DIR := $(ROOT)/obj/main
62 BIN_DIR := $(ROOT)/obj
63 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
64 INCLUDE_DIRS := $(SRC_DIR)
66 MAKE_SCRIPT_DIR := $(ROOT)/mk
68 ## V : Set verbosity level based on the V= parameter
69 ## V=0 Low
70 ## V=1 High
71 include $(MAKE_SCRIPT_DIR)/build_verbosity.mk
73 # Build tools, so we all share the same versions
74 # import macros common to all supported build systems
75 include $(MAKE_SCRIPT_DIR)/system-id.mk
77 # developer preferences, edit these at will, they'll be gitignored
78 ifneq ($(wildcard $(MAKE_SCRIPT_DIR)/local.mk),)
79 include $(MAKE_SCRIPT_DIR)/local.mk
80 endif
82 # some targets use parallel build by default
83 # MAKEFLAGS is valid only inside target, do not use this at parse phase
84 DEFAULT_PARALLEL_JOBS := # all jobs in parallel (for backward compatibility)
85 MAKE_PARALLEL = $(if $(filter -j%, $(MAKEFLAGS)),$(EMPTY),-j$(DEFAULT_PARALLEL_JOBS))
87 # pre-build sanity checks
88 include $(MAKE_SCRIPT_DIR)/checks.mk
90 # basic target list
91 PLATFORMS := $(sort $(notdir $(patsubst /%,%, $(wildcard $(PLATFORM_DIR)/*))))
92 BASE_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(PLATFORM_DIR)/*/target/*/target.mk)))))
94 # configure some directories that are relative to wherever ROOT_DIR is located
95 TOOLS_DIR ?= $(ROOT)/tools
96 DL_DIR := $(ROOT)/downloads
97 CONFIG_DIR ?= $(BETAFLIGHT_CONFIG)
98 ifeq ($(CONFIG_DIR),)
99 CONFIG_DIR := $(ROOT)/src/config
100 endif
101 DIRECTORIES := $(DL_DIR) $(TOOLS_DIR)
103 export RM := rm
105 # import macros that are OS specific
106 include $(MAKE_SCRIPT_DIR)/$(OSFAMILY).mk
108 # include the tools makefile
109 include $(MAKE_SCRIPT_DIR)/tools.mk
111 # Search path for sources
112 VPATH := $(SRC_DIR):$(LIB_MAIN_DIR):$(PLATFORM_DIR)
113 FATFS_DIR = $(ROOT)/lib/main/FatFS
114 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
115 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
117 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
118 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
119 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
121 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
123 # import config handling
124 include $(MAKE_SCRIPT_DIR)/config.mk
126 # default xtal value
127 HSE_VALUE ?= 8000000
129 CI_EXCLUDED_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(PLATFORM_DIR)/*/target/*/.exclude)))))
130 CI_COMMON_TARGETS := STM32F4DISCOVERY CRAZYBEEF4SX1280 CRAZYBEEF4FR MATEKF405TE AIRBOTG4AIO TBS_LUCID_FC IFLIGHT_BLITZ_F722 NUCLEOF446 SPRACINGH7EXTREME SPRACINGH7RF
131 CI_TARGETS := $(filter-out $(CI_EXCLUDED_TARGETS), $(BASE_TARGETS)) $(filter $(CI_COMMON_TARGETS), $(BASE_CONFIGS))
132 PREVIEW_TARGETS := MATEKF411 AIKONF4V2 AIRBOTG4AIO ZEEZF7V3 FOXEERF745V4_AIO KAKUTEH7 TBS_LUCID_FC SITL SPRACINGH7EXTREME SPRACINGH7RF
134 TARGET_PLATFORM := $(notdir $(patsubst %/,%,$(subst target/$(TARGET)/,, $(dir $(wildcard $(PLATFORM_DIR)/*/target/$(TARGET)/target.mk)))))
135 TARGET_PLATFORM_DIR := $(PLATFORM_DIR)/$(TARGET_PLATFORM)
136 LINKER_DIR := $(TARGET_PLATFORM_DIR)/link
138 ifneq ($(TARGET),)
139 include $(TARGET_PLATFORM_DIR)/target/$(TARGET)/target.mk
140 endif
142 REVISION := norevision
143 ifeq ($(shell git diff --shortstat),)
144 REVISION := $(shell git rev-parse --short=9 HEAD)
145 endif
147 LD_FLAGS :=
148 EXTRA_LD_FLAGS :=
151 # Default Tool options - can be overridden in {mcu}.mk files.
153 DEBUG_MIXED = no
155 ifeq ($(DEBUG),INFO)
156 DEBUG_MIXED = yes
157 endif
158 ifeq ($(DEBUG),GDB)
159 DEBUG_MIXED = yes
160 endif
162 ifeq ($(DEBUG),GDB)
163 OPTIMISE_DEFAULT := -Og
165 LTO_FLAGS := $(OPTIMISE_DEFAULT)
166 DEBUG_FLAGS = -ggdb2 -gdwarf-5 -DDEBUG
167 else
168 ifeq ($(DEBUG),INFO)
169 DEBUG_FLAGS = -ggdb2
170 endif
171 OPTIMISATION_BASE := -flto=auto -fuse-linker-plugin -ffast-math -fmerge-all-constants
172 OPTIMISE_DEFAULT := -O2
173 OPTIMISE_SPEED := -Ofast
174 OPTIMISE_SIZE := -Os
176 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
177 endif
179 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)
181 ifneq ($(TARGET),)
183 # start specific includes
184 ifeq ($(TARGET_MCU),)
185 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
186 endif
188 ifeq ($(TARGET_MCU_FAMILY),)
189 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
190 endif
192 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_PLATFORM) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
194 ifneq ($(CONFIG),)
195 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
196 endif
198 SPEED_OPTIMISED_SRC :=
199 SIZE_OPTIMISED_SRC :=
201 include $(TARGET_PLATFORM_DIR)/mk/$(TARGET_MCU_FAMILY).mk
203 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
204 ifeq ($(TARGET_FLASH_SIZE),)
205 ifneq ($(MCU_FLASH_SIZE),)
206 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
207 else
208 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
209 endif
210 endif
212 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
214 ifneq ($(HSE_VALUE),)
215 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
216 endif
218 TARGET_DIR = $(TARGET_PLATFORM_DIR)/target/$(TARGET)
219 endif # TARGET specified
221 # openocd specific includes
222 include $(MAKE_SCRIPT_DIR)/openocd.mk
224 ifeq ($(CONFIG),)
225 ifeq ($(TARGET),)
226 .DEFAULT_GOAL := all
227 else
228 .DEFAULT_GOAL := hex
229 endif
230 else
231 .DEFAULT_GOAL := hex
232 endif
234 INCLUDE_DIRS := $(INCLUDE_DIRS) \
235 $(ROOT)/lib/main/MAVLink
237 INCLUDE_DIRS := $(INCLUDE_DIRS) \
238 $(TARGET_DIR)
240 VPATH := $(VPATH):$(TARGET_DIR)
242 include $(MAKE_SCRIPT_DIR)/source.mk
244 ifneq ($(TARGET),)
245 ifneq ($(filter-out $(SRC),$(SPEED_OPTIMISED_SRC)),)
246 $(error Speed optimised sources not valid: $(strip $(filter-out $(SRC),$(SPEED_OPTIMISED_SRC))))
247 endif
249 ifneq ($(filter-out $(SRC),$(SIZE_OPTIMISED_SRC)),)
250 $(error Size optimised sources not valid: $(strip $(filter-out $(SRC),$(SIZE_OPTIMISED_SRC))))
251 endif
252 endif
254 ###############################################################################
255 # Things that might need changing to use different tools
258 # Find out if ccache is installed on the system
259 CCACHE := ccache
260 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
261 ifneq ($(RESULT),0)
262 CCACHE :=
263 endif
265 # Tool names
266 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
267 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
268 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
269 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
270 OBJDUMP := $(ARM_SDK_PREFIX)objdump
271 READELF := $(ARM_SDK_PREFIX)readelf
272 SIZE := $(ARM_SDK_PREFIX)size
273 DFUSE-PACK := src/utils/dfuse-pack.py
276 # Tool options.
278 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
279 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
280 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
281 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
282 CC_NO_OPTIMISATION :=
285 # Added after GCC version update, remove once the warnings have been fixed
287 TEMPORARY_FLAGS :=
289 EXTRA_WARNING_FLAGS := -Wold-style-definition
291 CFLAGS += $(ARCH_FLAGS) \
292 $(addprefix -D,$(OPTIONS)) \
293 $(addprefix -I,$(INCLUDE_DIRS)) \
294 $(DEBUG_FLAGS) \
295 -std=gnu17 \
296 -Wall -Wextra -Werror -Wunsafe-loop-optimizations -Wdouble-promotion \
297 $(EXTRA_WARNING_FLAGS) \
298 -ffunction-sections \
299 -fdata-sections \
300 -fno-common \
301 $(TEMPORARY_FLAGS) \
302 $(DEVICE_FLAGS) \
303 -D_GNU_SOURCE \
304 -D$(TARGET) \
305 $(TARGET_FLAGS) \
306 -D'__FORKNAME__="$(FORKNAME)"' \
307 -D'__TARGET__="$(TARGET)"' \
308 -D'__REVISION__="$(REVISION)"' \
309 $(CONFIG_REVISION_DEFINE) \
310 -pipe \
311 -MMD -MP \
312 $(EXTRA_FLAGS)
314 ASFLAGS = $(ARCH_FLAGS) \
315 $(DEBUG_FLAGS) \
316 -x assembler-with-cpp \
317 $(addprefix -I,$(INCLUDE_DIRS)) \
318 -MMD -MP
320 ifeq ($(LD_FLAGS),)
321 LD_FLAGS = -lm \
322 -nostartfiles \
323 --specs=nano.specs \
324 -lc \
325 -lnosys \
326 $(ARCH_FLAGS) \
327 $(LTO_FLAGS) \
328 $(DEBUG_FLAGS) \
329 -static \
330 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
331 -Wl,-L$(LINKER_DIR) \
332 -Wl,--cref \
333 -Wl,--no-wchar-size-warning \
334 -Wl,--print-memory-usage \
335 -T$(LD_SCRIPT) \
336 $(EXTRA_LD_FLAGS)
337 endif
339 ###############################################################################
340 # No user-serviceable parts below
341 ###############################################################################
343 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
344 --std=c99 --inline-suppr --quiet --force \
345 $(addprefix -I,$(INCLUDE_DIRS)) \
346 -I/usr/include -I/usr/include/linux
348 TARGET_NAME := $(TARGET)
350 ifneq ($(CONFIG),)
351 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
352 endif
354 ifeq ($(REV),yes)
355 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
356 endif
358 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
360 # Things we will build
362 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
363 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
364 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
365 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
366 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
367 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
368 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
369 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
370 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
371 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
372 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
373 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
375 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
377 ifeq ($(DEBUG_MIXED),yes)
378 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
379 else
380 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEBUG_FLAGS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
381 endif
383 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
385 CLEAN_ARTIFACTS := $(TARGET_BIN)
386 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
387 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
388 CLEAN_ARTIFACTS += $(TARGET_LST)
389 CLEAN_ARTIFACTS += $(TARGET_DFU)
391 # Make sure build date and revision is updated on every incremental build
392 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
394 # List of buildable ELF files and their object dependencies.
395 # It would be nice to compute these lists, but that seems to be just beyond make.
397 $(TARGET_LST): $(TARGET_ELF)
398 $(V0) $(OBJDUMP) -S --disassemble $< > $@
400 ifeq ($(EXST),no)
401 $(TARGET_BIN): $(TARGET_ELF)
402 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
403 $(V1) $(OBJCOPY) -O binary $< $@
405 $(TARGET_HEX): $(TARGET_ELF)
406 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
407 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
409 $(TARGET_DFU): $(TARGET_HEX)
410 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
411 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
413 else
414 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
416 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
417 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
418 $(V1) $(OBJCOPY) -O binary $< $@
420 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
421 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
422 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
423 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
424 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
425 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
427 @echo "Generating MD5 hash of binary" "$(STDOUT)"
428 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
430 @echo "Patching MD5 hash into binary" "$(STDOUT)"
431 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
432 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
434 # 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"
435 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
436 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
437 # a) regeneration of $(TARGET_BIN), and
438 # b) the results of $(TARGET_BIN) will not be as expected.
439 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
440 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
441 rm $(TARGET_EXST_ELF).tmp
443 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
444 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
446 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
447 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
449 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
450 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
452 $(TARGET_HEX): $(TARGET_BIN)
453 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
455 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
456 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
458 endif
460 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
461 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
462 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
463 $(V1) $(SIZE) $(TARGET_ELF)
465 # Compile
467 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
468 define compile_file
469 echo "%% ($(1)) $<" "$(STDOUT)" && \
470 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
471 endef
473 ## `paths` is a list of paths that will be replaced for checking of speed, and size optimised sources
474 paths := $(SRC_DIR)/ $(LIB_MAIN_DIR)/ $(PLATFORM_DIR)/
475 subst_paths_for = $(foreach path,$(paths),$(filter-out $(1),$(subst $(path),,$(1))))
476 subst_paths = $(strip $(if $(call subst_paths_for,$(1)), $(call subst_paths_for,$(1)), $(1)))
478 ifeq ($(DEBUG),GDB)
479 $(TARGET_OBJ_DIR)/%.o: %.c
480 $(V1) mkdir -p $(dir $@)
481 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
482 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
484 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
486 else
487 $(TARGET_OBJ_DIR)/%.o: %.c
488 $(V1) mkdir -p $(dir $@)
489 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
490 $(call compile_file,not optimised,$(CC_NO_OPTIMISATION)) \
492 $(if $(findstring $(call subst_paths,$<),$(SPEED_OPTIMISED_SRC)), \
493 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
495 $(if $(findstring $(call subst_paths,$<),$(SIZE_OPTIMISED_SRC)), \
496 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
498 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
502 endif
504 # Assemble
505 $(TARGET_OBJ_DIR)/%.o: %.s
506 $(V1) mkdir -p $(dir $@)
507 @echo "%% $(notdir $<)" "$(STDOUT)"
508 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
510 $(TARGET_OBJ_DIR)/%.o: %.S
511 $(V1) mkdir -p $(dir $@)
512 @echo "%% $(notdir $<)" "$(STDOUT)"
513 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
516 ## all : Build all currently built targets
517 all: $(CI_TARGETS)
519 $(BASE_TARGETS):
520 $(V0) @echo "Building target $@" && \
521 $(MAKE) hex TARGET=$@ && \
522 echo "Building $@ succeeded."
524 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
526 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
528 ## clean : clean up temporary / machine-generated files
529 clean:
530 @echo "Cleaning $(TARGET_NAME)"
531 $(V0) rm -f $(CLEAN_ARTIFACTS)
532 $(V0) rm -rf $(TARGET_OBJ_DIR)
533 @echo "Cleaning $(TARGET_NAME) succeeded."
535 ## test_clean : clean up temporary / machine-generated files (tests)
536 test-%_clean:
537 $(MAKE) test_clean
539 test_clean:
540 $(V0) cd src/test && $(MAKE) clean || true
542 ## <TARGET>_clean : clean up one specific target (alias for above)
543 $(TARGETS_CLEAN):
544 $(V0) $(MAKE) $(MAKE_PARALLEL) TARGET=$(subst _clean,,$@) clean
546 ## <CONFIG>_clean : clean up one specific target (alias for above)
547 $(CONFIGS_CLEAN):
548 $(V0) $(MAKE) $(MAKE_PARALLEL) CONFIG=$(subst _clean,,$@) clean
550 ## clean_all : clean all targets
551 clean_all: $(TARGETS_CLEAN) test_clean
553 ## preview : build one target for each platform and execute make test
554 preview: $(PREVIEW_TARGETS) test
556 ## all_configs : Build all configs
557 all_configs: $(BASE_CONFIGS)
559 TARGETS_FLASH = $(addsuffix _flash,$(BASE_TARGETS))
561 ## <TARGET>_flash : build and flash a target
562 $(TARGETS_FLASH):
563 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
564 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
565 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
566 else
567 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
568 endif
570 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
571 tty_flash:
572 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
573 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
574 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
576 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
577 dfu_flash:
578 ifneq (no-port-found,$(SERIAL_DEVICE))
579 # potentially this is because the MCU already is in DFU mode, try anyway
580 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
581 $(V0) sleep 1
582 endif
583 $(V0) $(MAKE) $(TARGET_DFU)
584 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
586 st-flash_$(TARGET): $(TARGET_BIN)
587 $(V0) st-flash --reset write $< 0x08000000
589 ## st-flash : flash firmware (.bin) onto flight controller
590 st-flash: st-flash_$(TARGET)
592 ifneq ($(OPENOCD_COMMAND),)
593 openocd-gdb: $(TARGET_ELF)
594 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
595 endif
597 TARGETS_ZIP = $(addsuffix _zip,$(BASE_TARGETS))
599 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
600 $(TARGETS_ZIP):
601 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
602 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
604 zip:
605 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
607 binary:
608 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_BIN)
610 hex:
611 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_HEX)
613 TARGETS_REVISION = $(addsuffix _rev,$(BASE_TARGETS))
614 ## <TARGET>_rev : build target and add revision to filename
615 $(TARGETS_REVISION):
616 $(V0) $(MAKE) hex REV=yes TARGET=$(subst _rev,,$@)
618 all_rev: $(addsuffix _rev,$(CI_TARGETS))
620 unbrick_$(TARGET): $(TARGET_HEX)
621 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
622 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
624 ## unbrick : unbrick flight controller
625 unbrick: unbrick_$(TARGET)
627 ## cppcheck : run static analysis on C source code
628 cppcheck: $(CSOURCES)
629 $(V0) $(CPPCHECK)
631 cppcheck-result.xml: $(CSOURCES)
632 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
634 # mkdirs
635 $(DIRECTORIES):
636 mkdir -p $@
638 ## version : print firmware version
639 version:
640 @echo $(FC_VER)
642 ## help : print this help message and exit
643 help: Makefile mk/tools.mk
644 @echo ""
645 @echo "Makefile for the $(FORKNAME) firmware"
646 @echo ""
647 @echo "Usage:"
648 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
649 @echo "Or:"
650 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
651 @echo "Or:"
652 @echo " make <config-target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
653 @echo ""
654 @echo "To populate configuration targets:"
655 @echo " make configs"
656 @echo ""
657 @echo "Valid TARGET values are: $(BASE_TARGETS)"
658 @echo ""
659 @sed -n 's/^## //p' $?
661 ## targets : print a list of all valid target platforms (for consumption by scripts)
662 targets:
663 @echo "Platforms: $(PLATFORMS)"
664 @echo "Valid targets: $(BASE_TARGETS)"
665 @echo "Built targets: $(CI_TARGETS)"
666 @echo "Default target: $(TARGET)"
667 @echo "CI common targets: $(CI_COMMON_TARGETS)"
668 @echo "CI excluded targets: $(CI_EXCLUDED_TARGETS)"
669 @echo "Preview targets: $(PREVIEW_TARGETS)"
671 targets-ci-print:
672 @echo $(CI_TARGETS)
674 ## target-mcu : print the MCU type of the target
675 target-mcu:
676 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
678 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
679 targets-by-mcu:
680 $(V1) for target in $${TARGETS}; do \
681 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
682 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
683 if [ "$${DO_BUILD}" = 1 ]; then \
684 echo "Building target $${target}..."; \
685 $(MAKE) TARGET=$${target}; \
686 if [ $$? -ne 0 ]; then \
687 echo "Building target $${target} failed, aborting."; \
688 exit 1; \
689 fi; \
690 else \
691 echo -n "$${target} "; \
692 fi; \
693 fi; \
694 done
695 @echo
697 ## test : run the Betaflight test suite
698 ## junittest : run the Betaflight test suite, producing Junit XML result files.
699 ## 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)
700 ## test-all: run the Betaflight test suite including all per-target expanded tests
701 test junittest test-all test-representative:
702 $(V0) cd src/test && $(MAKE) $@
704 ## test_help : print the help message for the test suite (including a list of the available tests)
705 test_help:
706 $(V0) cd src/test && $(MAKE) help
708 ## test_versions : print the compiler versions used for the test suite
709 test_versions:
710 $(V0) cd src/test && $(MAKE) versions
712 ## test_% : run test 'test_%' from the test suite
713 test_%:
714 $(V0) cd src/test && $(MAKE) $@
716 $(TARGET_EF_HASH_FILE):
717 $(V1) mkdir -p $(dir $@)
718 $(V1) rm -f $(TARGET_OBJ_DIR)/.efhash_*
719 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
720 $(V1) touch $(TARGET_EF_HASH_FILE)
722 # rebuild everything when makefile changes or the extra flags have changed
723 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
725 # include auto-generated dependencies
726 -include $(TARGET_DEPS)