update serTcpOpen declaration to fix compile errors (#14113)
[betaflight.git] / Makefile
blobb8cfc77eabf285bed8a93e36aeb1c219e43457fb
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
127 # default xtal value
128 HSE_VALUE ?= 8000000
130 CI_EXCLUDED_TARGETS := $(sort $(notdir $(patsubst %/,%,$(dir $(wildcard $(PLATFORM_DIR)/*/target/*/.exclude)))))
131 CI_TARGETS := $(filter-out $(CI_EXCLUDED_TARGETS), $(BASE_TARGETS)) $(filter STM32F4DISCOVERY CRAZYBEEF4SX1280 CRAZYBEEF4FR IFLIGHT_BLITZ_F722 NUCLEOF446 SPRACINGH7EXTREME SPRACINGH7RF, $(BASE_CONFIGS))
133 TARGET_PLATFORM := $(notdir $(patsubst %/,%,$(subst target/$(TARGET)/,, $(dir $(wildcard $(PLATFORM_DIR)/*/target/$(TARGET)/target.mk)))))
134 TARGET_PLATFORM_DIR := $(PLATFORM_DIR)/$(TARGET_PLATFORM)
135 LINKER_DIR := $(TARGET_PLATFORM_DIR)/link
137 ifneq ($(TARGET),)
138 include $(TARGET_PLATFORM_DIR)/target/$(TARGET)/target.mk
139 endif
141 REVISION := norevision
142 ifeq ($(shell git diff --shortstat),)
143 REVISION := $(shell git log -1 --format="%h")
144 endif
146 LD_FLAGS :=
147 EXTRA_LD_FLAGS :=
150 # Default Tool options - can be overridden in {mcu}.mk files.
152 DEBUG_MIXED = no
154 ifeq ($(DEBUG),INFO)
155 DEBUG_MIXED = yes
156 endif
157 ifeq ($(DEBUG),GDB)
158 DEBUG_MIXED = yes
159 endif
161 ifeq ($(DEBUG),GDB)
162 OPTIMISE_DEFAULT := -Og
164 LTO_FLAGS := $(OPTIMISE_DEFAULT)
165 DEBUG_FLAGS = -ggdb2 -gdwarf-5 -DDEBUG
166 else
167 ifeq ($(DEBUG),INFO)
168 DEBUG_FLAGS = -ggdb2
169 endif
170 OPTIMISATION_BASE := -flto=auto -fuse-linker-plugin -ffast-math -fmerge-all-constants
171 OPTIMISE_DEFAULT := -O2
172 OPTIMISE_SPEED := -Ofast
173 OPTIMISE_SIZE := -Os
175 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
176 endif
178 VPATH := $(VPATH):$(MAKE_SCRIPT_DIR)
180 ifneq ($(TARGET),)
182 # start specific includes
183 ifeq ($(TARGET_MCU),)
184 $(error No TARGET_MCU specified. Is the target.mk valid for $(TARGET)?)
185 endif
187 ifeq ($(TARGET_MCU_FAMILY),)
188 $(error No TARGET_MCU_FAMILY specified. Is the target.mk valid for $(TARGET)?)
189 endif
191 TARGET_FLAGS := -D$(TARGET) -D$(TARGET_PLATFORM) -D$(TARGET_MCU_FAMILY) $(TARGET_FLAGS)
193 ifneq ($(CONFIG),)
194 TARGET_FLAGS := $(TARGET_FLAGS) -DUSE_CONFIG
195 endif
197 SPEED_OPTIMISED_SRC :=
198 SIZE_OPTIMISED_SRC :=
200 include $(TARGET_PLATFORM_DIR)/mk/$(TARGET_MCU_FAMILY).mk
202 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
203 ifeq ($(TARGET_FLASH_SIZE),)
204 ifneq ($(MCU_FLASH_SIZE),)
205 TARGET_FLASH_SIZE := $(MCU_FLASH_SIZE)
206 else
207 $(error MCU_FLASH_SIZE not configured for target $(TARGET))
208 endif
209 endif
211 DEVICE_FLAGS := $(DEVICE_FLAGS) -DTARGET_FLASH_SIZE=$(TARGET_FLASH_SIZE)
213 ifneq ($(HSE_VALUE),)
214 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
215 endif
217 TARGET_DIR = $(TARGET_PLATFORM_DIR)/target/$(TARGET)
218 endif # TARGET specified
220 # openocd specific includes
221 include $(MAKE_SCRIPT_DIR)/openocd.mk
223 .DEFAULT_GOAL := all
225 INCLUDE_DIRS := $(INCLUDE_DIRS) \
226 $(ROOT)/lib/main/MAVLink
228 INCLUDE_DIRS := $(INCLUDE_DIRS) \
229 $(TARGET_DIR)
231 VPATH := $(VPATH):$(TARGET_DIR)
233 include $(MAKE_SCRIPT_DIR)/source.mk
235 ifneq ($(TARGET),)
236 ifneq ($(filter-out $(SRC),$(SPEED_OPTIMISED_SRC)),)
237 $(error Speed optimised sources not valid: $(strip $(filter-out $(SRC),$(SPEED_OPTIMISED_SRC))))
238 endif
240 ifneq ($(filter-out $(SRC),$(SIZE_OPTIMISED_SRC)),)
241 $(error Size optimised sources not valid: $(strip $(filter-out $(SRC),$(SIZE_OPTIMISED_SRC))))
242 endif
243 endif
245 ###############################################################################
246 # Things that might need changing to use different tools
249 # Find out if ccache is installed on the system
250 CCACHE := ccache
251 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
252 ifneq ($(RESULT),0)
253 CCACHE :=
254 endif
256 # Tool names
257 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
258 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
259 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
260 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
261 OBJDUMP := $(ARM_SDK_PREFIX)objdump
262 READELF := $(ARM_SDK_PREFIX)readelf
263 SIZE := $(ARM_SDK_PREFIX)size
264 DFUSE-PACK := src/utils/dfuse-pack.py
267 # Tool options.
269 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
270 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
271 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
272 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
273 CC_NO_OPTIMISATION :=
276 # Added after GCC version update, remove once the warnings have been fixed
278 TEMPORARY_FLAGS :=
280 EXTRA_WARNING_FLAGS := -Wold-style-definition
282 CFLAGS += $(ARCH_FLAGS) \
283 $(addprefix -D,$(OPTIONS)) \
284 $(addprefix -I,$(INCLUDE_DIRS)) \
285 $(DEBUG_FLAGS) \
286 -std=gnu17 \
287 -Wall -Wextra -Werror -Wunsafe-loop-optimizations -Wdouble-promotion \
288 $(EXTRA_WARNING_FLAGS) \
289 -ffunction-sections \
290 -fdata-sections \
291 -fno-common \
292 $(TEMPORARY_FLAGS) \
293 $(DEVICE_FLAGS) \
294 -D_GNU_SOURCE \
295 -DUSE_STDPERIPH_DRIVER \
296 -D$(TARGET) \
297 $(TARGET_FLAGS) \
298 -D'__FORKNAME__="$(FORKNAME)"' \
299 -D'__TARGET__="$(TARGET)"' \
300 -D'__REVISION__="$(REVISION)"' \
301 $(CONFIG_REVISION_DEFINE) \
302 -pipe \
303 -MMD -MP \
304 $(EXTRA_FLAGS)
306 ASFLAGS = $(ARCH_FLAGS) \
307 $(DEBUG_FLAGS) \
308 -x assembler-with-cpp \
309 $(addprefix -I,$(INCLUDE_DIRS)) \
310 -MMD -MP
312 ifeq ($(LD_FLAGS),)
313 LD_FLAGS = -lm \
314 -nostartfiles \
315 --specs=nano.specs \
316 -lc \
317 -lnosys \
318 $(ARCH_FLAGS) \
319 $(LTO_FLAGS) \
320 $(DEBUG_FLAGS) \
321 -static \
322 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
323 -Wl,-L$(LINKER_DIR) \
324 -Wl,--cref \
325 -Wl,--no-wchar-size-warning \
326 -Wl,--print-memory-usage \
327 -T$(LD_SCRIPT) \
328 $(EXTRA_LD_FLAGS)
329 endif
331 ###############################################################################
332 # No user-serviceable parts below
333 ###############################################################################
335 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
336 --std=c99 --inline-suppr --quiet --force \
337 $(addprefix -I,$(INCLUDE_DIRS)) \
338 -I/usr/include -I/usr/include/linux
340 TARGET_NAME := $(TARGET)
342 ifneq ($(CONFIG),)
343 TARGET_NAME := $(TARGET_NAME)_$(CONFIG)
344 endif
346 ifeq ($(REV),yes)
347 TARGET_NAME := $(TARGET_NAME)_$(REVISION)
348 endif
350 TARGET_FULLNAME = $(FORKNAME)_$(FC_VER)_$(TARGET_NAME)
352 # Things we will build
354 TARGET_BIN = $(BIN_DIR)/$(TARGET_FULLNAME).bin
355 TARGET_HEX = $(BIN_DIR)/$(TARGET_FULLNAME).hex
356 TARGET_DFU = $(BIN_DIR)/$(TARGET_FULLNAME).dfu
357 TARGET_ZIP = $(BIN_DIR)/$(TARGET_FULLNAME).zip
358 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET_NAME)
359 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).elf
360 TARGET_EXST_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_EXST.elf
361 TARGET_UNPATCHED_BIN = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME)_UNPATCHED.bin
362 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).lst
363 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
364 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(SRC))))
365 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET_NAME).map
367 TARGET_EXST_HASH_SECTION_FILE = $(TARGET_OBJ_DIR)/exst_hash_section.bin
369 ifeq ($(DEBUG_MIXED),yes)
370 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
371 else
372 TARGET_EF_HASH := $(shell echo -n -- "$(EXTRA_FLAGS)" "$(OPTIONS)" "$(DEBUG_FLAGS)" "$(DEVICE_FLAGS)" "$(TARGET_FLAGS)" | openssl dgst -md5 -r | awk '{print $$1;}')
373 endif
375 TARGET_EF_HASH_FILE := $(TARGET_OBJ_DIR)/.efhash_$(TARGET_EF_HASH)
377 CLEAN_ARTIFACTS := $(TARGET_BIN)
378 CLEAN_ARTIFACTS += $(TARGET_HEX_REV) $(TARGET_HEX)
379 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
380 CLEAN_ARTIFACTS += $(TARGET_LST)
381 CLEAN_ARTIFACTS += $(TARGET_DFU)
383 # Make sure build date and revision is updated on every incremental build
384 $(TARGET_OBJ_DIR)/build/version.o : $(SRC)
386 # List of buildable ELF files and their object dependencies.
387 # It would be nice to compute these lists, but that seems to be just beyond make.
389 $(TARGET_LST): $(TARGET_ELF)
390 $(V0) $(OBJDUMP) -S --disassemble $< > $@
392 ifeq ($(EXST),no)
393 $(TARGET_BIN): $(TARGET_ELF)
394 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
395 $(V1) $(OBJCOPY) -O binary $< $@
397 $(TARGET_HEX): $(TARGET_ELF)
398 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
399 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
401 $(TARGET_DFU): $(TARGET_HEX)
402 @echo "Creating DFU $(TARGET_DFU)" "$(STDOUT)"
403 $(V1) $(PYTHON) $(DFUSE-PACK) -i $< $@
405 else
406 CLEAN_ARTIFACTS += $(TARGET_UNPATCHED_BIN) $(TARGET_EXST_HASH_SECTION_FILE) $(TARGET_EXST_ELF)
408 $(TARGET_UNPATCHED_BIN): $(TARGET_ELF)
409 @echo "Creating BIN (without checksum) $(TARGET_UNPATCHED_BIN)" "$(STDOUT)"
410 $(V1) $(OBJCOPY) -O binary $< $@
412 $(TARGET_BIN): $(TARGET_UNPATCHED_BIN)
413 @echo "Creating EXST $(TARGET_BIN)" "$(STDOUT)"
414 # Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
415 # These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
416 $(V1) dd if=/dev/zero ibs=1k count=$(FIRMWARE_SIZE) of=$(TARGET_BIN)
417 $(V1) dd if=$(TARGET_UNPATCHED_BIN) of=$(TARGET_BIN) conv=notrunc
419 @echo "Generating MD5 hash of binary" "$(STDOUT)"
420 $(V1) openssl dgst -md5 $(TARGET_BIN) > $(TARGET_UNPATCHED_BIN).md5
422 @echo "Patching MD5 hash into binary" "$(STDOUT)"
423 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $(TARGET_BIN)
424 $(V1) echo $(FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
426 # 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"
427 # Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
428 # If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
429 # a) regeneration of $(TARGET_BIN), and
430 # b) the results of $(TARGET_BIN) will not be as expected.
431 @echo "Extracting HASH section from unpatched EXST elf $(TARGET_ELF)" "$(STDOUT)"
432 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF).tmp --dump-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE) -j .exst_hash
433 rm $(TARGET_EXST_ELF).tmp
435 @echo "Patching MD5 hash into HASH section" "$(STDOUT)"
436 $(V1) cat $(TARGET_UNPATCHED_BIN).md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $(TARGET_EXST_HASH_SECTION_FILE)
438 $(V1) @echo "Patching updated HASH section into $(TARGET_EXST_ELF)" "$(STDOUT)"
439 $(OBJCOPY) $(TARGET_ELF) $(TARGET_EXST_ELF) --update-section .exst_hash=$(TARGET_EXST_HASH_SECTION_FILE)
441 $(V1) $(READELF) -S $(TARGET_EXST_ELF)
442 $(V1) $(READELF) -l $(TARGET_EXST_ELF)
444 $(TARGET_HEX): $(TARGET_BIN)
445 $(if $(EXST_ADJUST_VMA),,$(error "EXST_ADJUST_VMA not specified"))
447 @echo "Creating EXST HEX from patched EXST BIN $(TARGET_BIN), VMA Adjust $(EXST_ADJUST_VMA)" "$(STDOUT)"
448 $(V1) $(OBJCOPY) -I binary -O ihex --adjust-vma=$(EXST_ADJUST_VMA) $(TARGET_BIN) $@
450 endif
452 $(TARGET_ELF): $(TARGET_OBJS) $(LD_SCRIPT) $(LD_SCRIPTS)
453 @echo "Linking $(TARGET_NAME)" "$(STDOUT)"
454 $(V1) $(CROSS_CC) -o $@ $(filter-out %.ld,$^) $(LD_FLAGS)
455 $(V1) $(SIZE) $(TARGET_ELF)
457 # Compile
459 ## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
460 define compile_file
461 echo "%% ($(1)) $<" "$(STDOUT)" && \
462 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
463 endef
465 ## `paths` is a list of paths that will be replaced for checking of speed, and size optimised sources
466 paths := $(SRC_DIR)/ $(LIB_MAIN_DIR)/ $(PLATFORM_DIR)/
467 subst_paths_for = $(foreach path,$(paths),$(filter-out $(1),$(subst $(path),,$(1))))
468 subst_paths = $(strip $(if $(call subst_paths_for,$(1)), $(call subst_paths_for,$(1)), $(1)))
470 ifeq ($(DEBUG),GDB)
471 $(TARGET_OBJ_DIR)/%.o: %.c
472 $(V1) mkdir -p $(dir $@)
473 $(V1) $(if $(findstring $<,$(NOT_OPTIMISED_SRC)), \
474 $(call compile_file,not optimised, $(CC_NO_OPTIMISATION)) \
476 $(call compile_file,debug,$(CC_DEBUG_OPTIMISATION)) \
478 else
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 $(if $(findstring $(call subst_paths,$<),$(SPEED_OPTIMISED_SRC)), \
485 $(call compile_file,speed optimised,$(CC_SPEED_OPTIMISATION)) \
487 $(if $(findstring $(call subst_paths,$<),$(SIZE_OPTIMISED_SRC)), \
488 $(call compile_file,size optimised,$(CC_SIZE_OPTIMISATION)) \
490 $(call compile_file,optimised,$(CC_DEFAULT_OPTIMISATION)) \
494 endif
496 # Assemble
497 $(TARGET_OBJ_DIR)/%.o: %.s
498 $(V1) mkdir -p $(dir $@)
499 @echo "%% $(notdir $<)" "$(STDOUT)"
500 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
502 $(TARGET_OBJ_DIR)/%.o: %.S
503 $(V1) mkdir -p $(dir $@)
504 @echo "%% $(notdir $<)" "$(STDOUT)"
505 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
508 ## all : Build all currently built targets
509 all: $(CI_TARGETS)
511 $(BASE_TARGETS):
512 $(V0) @echo "Building target $@" && \
513 $(MAKE) hex TARGET=$@ && \
514 echo "Building $@ succeeded."
516 TARGETS_CLEAN = $(addsuffix _clean,$(BASE_TARGETS))
518 CONFIGS_CLEAN = $(addsuffix _clean,$(BASE_CONFIGS))
520 ## clean : clean up temporary / machine-generated files
521 clean:
522 @echo "Cleaning $(TARGET_NAME)"
523 $(V0) rm -f $(CLEAN_ARTIFACTS)
524 $(V0) rm -rf $(TARGET_OBJ_DIR)
525 @echo "Cleaning $(TARGET_NAME) succeeded."
527 ## test_clean : clean up temporary / machine-generated files (tests)
528 test-%_clean:
529 $(MAKE) test_clean
531 test_clean:
532 $(V0) cd src/test && $(MAKE) clean || true
534 ## <TARGET>_clean : clean up one specific target (alias for above)
535 $(TARGETS_CLEAN):
536 $(V0) $(MAKE) $(MAKE_PARALLEL) TARGET=$(subst _clean,,$@) clean
538 ## <CONFIG>_clean : clean up one specific target (alias for above)
539 $(CONFIGS_CLEAN):
540 $(V0) $(MAKE) $(MAKE_PARALLEL) CONFIG=$(subst _clean,,$@) clean
542 ## clean_all : clean all targets
543 clean_all: $(TARGETS_CLEAN) test_clean
545 ## all_configs : Build all configs
546 all_configs: $(BASE_CONFIGS)
548 TARGETS_FLASH = $(addsuffix _flash,$(BASE_TARGETS))
550 ## <TARGET>_flash : build and flash a target
551 $(TARGETS_FLASH):
552 $(V0) $(MAKE) hex TARGET=$(subst _flash,,$@)
553 ifneq (,$(findstring /dev/ttyUSB,$(SERIAL_DEVICE)))
554 $(V0) $(MAKE) tty_flash TARGET=$(subst _flash,,$@)
555 else
556 $(V0) $(MAKE) dfu_flash TARGET=$(subst _flash,,$@)
557 endif
559 ## tty_flash : flash firmware (.hex) onto flight controller via a serial port
560 tty_flash:
561 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
562 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
563 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
565 ## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
566 dfu_flash:
567 ifneq (no-port-found,$(SERIAL_DEVICE))
568 # potentially this is because the MCU already is in DFU mode, try anyway
569 $(V0) echo -n 'R' > $(SERIAL_DEVICE)
570 $(V0) sleep 1
571 endif
572 $(V0) $(MAKE) $(TARGET_DFU)
573 $(V0) dfu-util -a 0 -D $(TARGET_DFU) -s :leave
575 st-flash_$(TARGET): $(TARGET_BIN)
576 $(V0) st-flash --reset write $< 0x08000000
578 ## st-flash : flash firmware (.bin) onto flight controller
579 st-flash: st-flash_$(TARGET)
581 ifneq ($(OPENOCD_COMMAND),)
582 openocd-gdb: $(TARGET_ELF)
583 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
584 endif
586 TARGETS_ZIP = $(addsuffix _zip,$(BASE_TARGETS))
588 ## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
589 $(TARGETS_ZIP):
590 $(V0) $(MAKE) hex TARGET=$(subst _zip,,$@)
591 $(V0) $(MAKE) zip TARGET=$(subst _zip,,$@)
593 zip:
594 $(V0) zip $(TARGET_ZIP) $(TARGET_HEX)
596 binary:
597 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_BIN)
599 hex:
600 $(V0) $(MAKE) $(MAKE_PARALLEL) $(TARGET_HEX)
602 TARGETS_REVISION = $(addsuffix _rev,$(BASE_TARGETS))
603 ## <TARGET>_rev : build target and add revision to filename
604 $(TARGETS_REVISION):
605 $(V0) $(MAKE) hex REV=yes TARGET=$(subst _rev,,$@)
607 all_rev: $(addsuffix _rev,$(CI_TARGETS))
609 unbrick_$(TARGET): $(TARGET_HEX)
610 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
611 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
613 ## unbrick : unbrick flight controller
614 unbrick: unbrick_$(TARGET)
616 ## cppcheck : run static analysis on C source code
617 cppcheck: $(CSOURCES)
618 $(V0) $(CPPCHECK)
620 cppcheck-result.xml: $(CSOURCES)
621 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
623 # mkdirs
624 $(DIRECTORIES):
625 mkdir -p $@
627 ## version : print firmware version
628 version:
629 @echo $(FC_VER)
631 ## help : print this help message and exit
632 help: Makefile mk/tools.mk
633 @echo ""
634 @echo "Makefile for the $(FORKNAME) firmware"
635 @echo ""
636 @echo "Usage:"
637 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
638 @echo "Or:"
639 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
640 @echo "Or:"
641 @echo " make <config-target> [V=<verbosity>] [OPTIONS=\"<options>\"] [EXTRA_FLAGS=\"<extra_flags>\"]"
642 @echo ""
643 @echo "To populate configuration targets:"
644 @echo " make configs"
645 @echo ""
646 @echo "Valid TARGET values are: $(BASE_TARGETS)"
647 @echo ""
648 @sed -n 's/^## //p' $?
650 ## targets : print a list of all valid target platforms (for consumption by scripts)
651 targets:
652 @echo "Valid targets: $(BASE_TARGETS)"
653 @echo "Built targets: $(CI_TARGETS)"
654 @echo "Default target: $(TARGET)"
656 targets-ci-print:
657 @echo $(CI_TARGETS)
659 ## target-mcu : print the MCU type of the target
660 target-mcu:
661 @echo "$(TARGET_MCU_FAMILY) : $(TARGET_MCU)"
663 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
664 targets-by-mcu:
665 $(V1) for target in $${TARGETS}; do \
666 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
667 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
668 if [ "$${DO_BUILD}" = 1 ]; then \
669 echo "Building target $${target}..."; \
670 $(MAKE) TARGET=$${target}; \
671 if [ $$? -ne 0 ]; then \
672 echo "Building target $${target} failed, aborting."; \
673 exit 1; \
674 fi; \
675 else \
676 echo -n "$${target} "; \
677 fi; \
678 fi; \
679 done
680 @echo
682 ## test : run the Betaflight test suite
683 ## junittest : run the Betaflight test suite, producing Junit XML result files.
684 ## 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)
685 ## test-all: run the Betaflight test suite including all per-target expanded tests
686 test junittest test-all test-representative:
687 $(V0) cd src/test && $(MAKE) $@
689 ## test_help : print the help message for the test suite (including a list of the available tests)
690 test_help:
691 $(V0) cd src/test && $(MAKE) help
693 ## test_versions : print the compiler versions used for the test suite
694 test_versions:
695 $(V0) cd src/test && $(MAKE) versions
697 ## test_% : run test 'test_%' from the test suite
698 test_%:
699 $(V0) cd src/test && $(MAKE) $@
701 $(TARGET_EF_HASH_FILE):
702 $(V1) mkdir -p $(dir $@)
703 $(V1) rm -f $(TARGET_OBJ_DIR)/.efhash_*
704 @echo "EF HASH -> $(TARGET_EF_HASH_FILE)"
705 $(V1) touch $(TARGET_EF_HASH_FILE)
707 # rebuild everything when makefile changes or the extra flags have changed
708 $(TARGET_OBJS): $(TARGET_EF_HASH_FILE) Makefile $(TARGET_DIR)/target.mk $(wildcard make/*) $(CONFIG_FILE)
710 # include auto-generated dependencies
711 -include $(TARGET_DEPS)