Optimize throttle-based dynamic filter cutoff updates
[betaflight.git] / Makefile
blob2a9d30194595c5e30e850ae09009e0046786bf12
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 ?= BETAFLIGHTF3
21 # Compile-time options
22 OPTIONS ?=
24 # compile for OpenPilot BootLoader support
25 OPBL ?= no
27 # Debugger optons:
28 # empty - ordinary build with all optimizations enabled
29 # RELWITHDEBINFO - ordinary build with debug symbols and all optimizations enabled
30 # GDB - debug build with minimum number of optimizations
31 DEBUG ?=
33 # Insert the debugging hardfault debugger
34 # releases should not be built with this flag as it does not disable pwm output
35 DEBUG_HARDFAULTS ?=
37 # Serial port/Device for flashing
38 SERIAL_DEVICE ?= $(firstword $(wildcard /dev/ttyUSB*) no-port-found)
40 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
41 FLASH_SIZE ?=
44 ###############################################################################
45 # Things that need to be maintained as the source changes
48 FORKNAME = betaflight
50 # Working directories
51 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
52 SRC_DIR := $(ROOT)/src/main
53 OBJECT_DIR := $(ROOT)/obj/main
54 BIN_DIR := $(ROOT)/obj
55 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
56 INCLUDE_DIRS := $(SRC_DIR) \
57 $(ROOT)/src/main/target
58 LINKER_DIR := $(ROOT)/src/main/target/link
60 ## V : Set verbosity level based on the V= parameter
61 ## V=0 Low
62 ## V=1 High
63 include $(ROOT)/make/build_verbosity.mk
65 # Build tools, so we all share the same versions
66 # import macros common to all supported build systems
67 include $(ROOT)/make/system-id.mk
69 # developer preferences, edit these at will, they'll be gitignored
70 -include $(ROOT)/make/local.mk
72 # configure some directories that are relative to wherever ROOT_DIR is located
73 ifndef TOOLS_DIR
74 TOOLS_DIR := $(ROOT)/tools
75 endif
76 BUILD_DIR := $(ROOT)/build
77 DL_DIR := $(ROOT)/downloads
79 export RM := rm
81 # import macros that are OS specific
82 include $(ROOT)/make/$(OSFAMILY).mk
84 # include the tools makefile
85 include $(ROOT)/make/tools.mk
87 # default xtal value for F4 targets
88 HSE_VALUE ?= 8000000
90 # used for turning on features like VCP and SDCARD
91 FEATURES =
93 include $(ROOT)/make/targets.mk
95 REVISION := $(shell git log -1 --format="%h")
97 FC_VER_MAJOR := $(shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
98 FC_VER_MINOR := $(shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
99 FC_VER_PATCH := $(shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
101 FC_VER := $(FC_VER_MAJOR).$(FC_VER_MINOR).$(FC_VER_PATCH)
103 # Search path for sources
104 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
105 USBFS_DIR = $(ROOT)/lib/main/STM32_USB-FS-Device_Driver
106 USBPERIPH_SRC = $(notdir $(wildcard $(USBFS_DIR)/src/*.c))
107 FATFS_DIR = $(ROOT)/lib/main/FatFS
108 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
110 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
112 LD_FLAGS :=
115 # Default Tool options - can be overridden in {mcu}.mk files.
117 ifeq ($(DEBUG),GDB)
118 OPTIMISE_DEFAULT := -Og
120 LTO_FLAGS := $(OPTIMISE_DEFAULT)
121 DEBUG_FLAGS = -ggdb3 -DDEBUG
122 else
123 ifeq ($(DEBUG),INFO)
124 DEBUG_FLAGS = -ggdb3
125 endif
126 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math
127 OPTIMISE_DEFAULT := -O2
128 OPTIMISE_SPEED := -Ofast
129 OPTIMISE_SIZE := -Os
131 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
132 endif
134 VPATH := $(VPATH):$(ROOT)/make/mcu
135 VPATH := $(VPATH):$(ROOT)/make
137 # start specific includes
138 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
140 # openocd specific includes
141 include $(ROOT)/make/openocd.mk
143 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
144 ifeq ($(FLASH_SIZE),)
145 ifneq ($(TARGET_FLASH),)
146 FLASH_SIZE := $(TARGET_FLASH)
147 else
148 $(error FLASH_SIZE not configured for target $(TARGET))
149 endif
150 endif
152 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
154 ifneq ($(HSE_VALUE),)
155 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
156 endif
158 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
159 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
161 ifeq ($(OPBL),yes)
162 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
163 .DEFAULT_GOAL := binary
164 else
165 .DEFAULT_GOAL := hex
166 endif
168 INCLUDE_DIRS := $(INCLUDE_DIRS) \
169 $(ROOT)/lib/main/MAVLink
171 INCLUDE_DIRS := $(INCLUDE_DIRS) \
172 $(TARGET_DIR)
174 VPATH := $(VPATH):$(TARGET_DIR)
176 include $(ROOT)/make/source.mk
178 ###############################################################################
179 # Things that might need changing to use different tools
182 # Find out if ccache is installed on the system
183 CCACHE := ccache
184 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
185 ifneq ($(RESULT),0)
186 CCACHE :=
187 endif
189 # Tool names
190 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
191 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
192 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
193 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
194 OBJDUMP := $(ARM_SDK_PREFIX)objdump
195 SIZE := $(ARM_SDK_PREFIX)size
198 # Tool options.
200 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
201 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
202 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
203 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
205 CFLAGS += $(ARCH_FLAGS) \
206 $(addprefix -D,$(OPTIONS)) \
207 $(addprefix -I,$(INCLUDE_DIRS)) \
208 $(DEBUG_FLAGS) \
209 -std=gnu11 \
210 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
211 -ffunction-sections \
212 -fdata-sections \
213 -fno-common \
214 -pedantic \
215 $(DEVICE_FLAGS) \
216 -D_GNU_SOURCE \
217 -DUSE_STDPERIPH_DRIVER \
218 -D$(TARGET) \
219 $(TARGET_FLAGS) \
220 -D'__FORKNAME__="$(FORKNAME)"' \
221 -D'__TARGET__="$(TARGET)"' \
222 -D'__REVISION__="$(REVISION)"' \
223 -save-temps=obj \
224 -MMD -MP \
225 $(EXTRA_FLAGS)
227 ASFLAGS = $(ARCH_FLAGS) \
228 -x assembler-with-cpp \
229 $(addprefix -I,$(INCLUDE_DIRS)) \
230 -MMD -MP
232 ifeq ($(LD_FLAGS),)
233 LD_FLAGS = -lm \
234 -nostartfiles \
235 --specs=nano.specs \
236 -lc \
237 -lnosys \
238 $(ARCH_FLAGS) \
239 $(LTO_FLAGS) \
240 $(DEBUG_FLAGS) \
241 -static \
242 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
243 -Wl,-L$(LINKER_DIR) \
244 -Wl,--cref \
245 -Wl,--no-wchar-size-warning \
246 -Wl,--print-memory-usage \
247 -T$(LD_SCRIPT)
248 endif
250 ###############################################################################
251 # No user-serviceable parts below
252 ###############################################################################
254 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
255 --std=c99 --inline-suppr --quiet --force \
256 $(addprefix -I,$(INCLUDE_DIRS)) \
257 -I/usr/include -I/usr/include/linux
260 # Things we will build
262 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
263 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
264 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
265 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
266 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
267 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
268 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
271 CLEAN_ARTIFACTS := $(TARGET_BIN)
272 CLEAN_ARTIFACTS += $(TARGET_HEX)
273 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
274 CLEAN_ARTIFACTS += $(TARGET_LST)
276 # Make sure build date and revision is updated on every incremental build
277 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
279 # List of buildable ELF files and their object dependencies.
280 # It would be nice to compute these lists, but that seems to be just beyond make.
282 $(TARGET_LST): $(TARGET_ELF)
283 $(V0) $(OBJDUMP) -S --disassemble $< > $@
285 $(TARGET_HEX): $(TARGET_ELF)
286 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
287 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
289 $(TARGET_BIN): $(TARGET_ELF)
290 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
291 $(V1) $(OBJCOPY) -O binary $< $@
293 $(TARGET_ELF): $(TARGET_OBJS)
294 @echo "Linking $(TARGET)" "$(STDOUT)"
295 $(V1) $(CROSS_CC) -o $@ $^ $(LD_FLAGS)
296 $(V1) $(SIZE) $(TARGET_ELF)
298 # Compile
299 ifeq ($(DEBUG),GDB)
300 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
301 $(V1) mkdir -p $(dir $@)
302 $(V1) echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
303 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<
304 else
305 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
306 $(V1) mkdir -p $(dir $@)
307 $(V1) $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
308 echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
309 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
310 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
311 echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
312 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
313 echo "%% $(notdir $<)" "$(STDOUT)" && \
314 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))
315 endif
317 # Assemble
318 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
319 $(V1) mkdir -p $(dir $@)
320 @echo "%% $(notdir $<)" "$(STDOUT)"
321 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
323 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
324 $(V1) mkdir -p $(dir $@)
325 @echo "%% $(notdir $<)" "$(STDOUT)"
326 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
329 ## all : Build all targets (excluding unsupported)
330 all: $(SUPPORTED_TARGETS)
332 ## all_with_unsupported : Build all targets (including unsupported)
333 all_with_unsupported: $(VALID_TARGETS)
335 ## unsupported : Build unsupported targets
336 unsupported: $(UNSUPPORTED_TARGETS)
338 ## official : Build all official (travis) targets
339 official: $(OFFICIAL_TARGETS)
341 ## targets-group-1 : build some targets
342 targets-group-1: $(GROUP_1_TARGETS)
344 ## targets-group-2 : build some targets
345 targets-group-2: $(GROUP_2_TARGETS)
347 ## targets-group-3 : build some targets
348 targets-group-3: $(GROUP_3_TARGETS)
350 ## targets-group-3 : build some targets
351 targets-group-4: $(GROUP_4_TARGETS)
353 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
354 targets-group-rest: $(GROUP_OTHER_TARGETS)
356 $(VALID_TARGETS):
357 $(V0) @echo "Building $@" && \
358 $(MAKE) binary hex TARGET=$@ && \
359 echo "Building $@ succeeded."
361 $(NOBUILD_TARGETS):
362 $(MAKE) TARGET=$@
364 CLEAN_TARGETS = $(addprefix clean_,$(VALID_TARGETS) )
365 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS) )
367 ## clean : clean up temporary / machine-generated files
368 clean:
369 @echo "Cleaning $(TARGET)"
370 $(V0) rm -f $(CLEAN_ARTIFACTS)
371 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
372 @echo "Cleaning $(TARGET) succeeded."
374 ## clean_test : clean up temporary / machine-generated files (tests)
375 clean_test:
376 $(V0) cd src/test && $(MAKE) clean || true
378 ## clean_<TARGET> : clean up one specific target
379 $(CLEAN_TARGETS):
380 $(V0) $(MAKE) -j TARGET=$(subst clean_,,$@) clean
382 ## <TARGET>_clean : clean up one specific target (alias for above)
383 $(TARGETS_CLEAN):
384 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
386 ## clean_all : clean all valid targets
387 clean_all: $(CLEAN_TARGETS)
389 ## all_clean : clean all valid targets (alias for above)
390 all_clean: $(TARGETS_CLEAN)
393 flash_$(TARGET): $(TARGET_HEX)
394 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
395 $(V0) echo -n 'R' >$(SERIAL_DEVICE)
396 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
398 ## flash : flash firmware (.hex) onto flight controller
399 flash: flash_$(TARGET)
401 st-flash_$(TARGET): $(TARGET_BIN)
402 $(V0) st-flash --reset write $< 0x08000000
404 ## st-flash : flash firmware (.bin) onto flight controller
405 st-flash: st-flash_$(TARGET)
407 ifneq ($(OPENOCD_COMMAND),)
408 openocd-gdb: $(TARGET_ELF)
409 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
410 endif
412 binary:
413 $(V0) $(MAKE) -j $(TARGET_BIN)
415 hex:
416 $(V0) $(MAKE) -j $(TARGET_HEX)
418 unbrick_$(TARGET): $(TARGET_HEX)
419 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
420 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
422 ## unbrick : unbrick flight controller
423 unbrick: unbrick_$(TARGET)
425 ## cppcheck : run static analysis on C source code
426 cppcheck: $(CSOURCES)
427 $(V0) $(CPPCHECK)
429 cppcheck-result.xml: $(CSOURCES)
430 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
432 # mkdirs
433 $(DL_DIR):
434 mkdir -p $@
436 $(TOOLS_DIR):
437 mkdir -p $@
439 $(BUILD_DIR):
440 mkdir -p $@
442 ## version : print firmware version
443 version:
444 @echo $(FC_VER)
446 ## help : print this help message and exit
447 help: Makefile make/tools.mk
448 @echo ""
449 @echo "Makefile for the $(FORKNAME) firmware"
450 @echo ""
451 @echo "Usage:"
452 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
453 @echo "Or:"
454 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
455 @echo ""
456 @echo "Valid TARGET values are: $(VALID_TARGETS)"
457 @echo ""
458 @sed -n 's/^## //p' $?
460 ## targets : print a list of all valid target platforms (for consumption by scripts)
461 targets:
462 @echo "Valid targets: $(VALID_TARGETS)"
463 @echo "Supported targets: $(SUPPORTED_TARGETS)"
464 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
465 @echo "Target: $(TARGET)"
466 @echo "Base target: $(BASE_TARGET)"
467 @echo "targets-group-1: $(GROUP_1_TARGETS)"
468 @echo "targets-group-2: $(GROUP_2_TARGETS)"
469 @echo "targets-group-3: $(GROUP_3_TARGETS)"
470 @echo "targets-group-4: $(GROUP_4_TARGETS)"
471 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
473 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
474 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
475 @echo "targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
476 @echo "targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
477 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
478 @echo "total in all groups $(words $(SUPPORTED_TARGETS)) targets"
480 ## target-mcu : print the MCU type of the target
481 target-mcu:
482 @echo $(TARGET_MCU)
484 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
485 targets-by-mcu:
486 @echo "Building all $(MCU_TYPE) targets..."
487 $(V1) for target in $(VALID_TARGETS); do \
488 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
489 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
490 echo "Building target $${target}..."; \
491 $(MAKE) TARGET=$${target}; \
492 if [ $$? -ne 0 ]; then \
493 echo "Building target $${target} failed, aborting."; \
494 exit 1; \
495 fi; \
496 fi; \
497 done
499 ## targets-f3 : make all F3 targets
500 targets-f3:
501 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3
503 ## targets-f4 : make all F4 targets
504 targets-f4:
505 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4
507 ## targets-f7 : make all F7 targets
508 targets-f7:
509 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7
511 ## test : run the cleanflight test suite
512 ## junittest : run the cleanflight test suite, producing Junit XML result files.
513 test junittest:
514 $(V0) cd src/test && $(MAKE) $@
517 check-target-independence:
518 $(V1) for test_target in $(VALID_TARGETS); do \
519 FOUND=$$(grep -rE "\W$${test_target}\W?" src/main | grep -vE "(//)|(/\*).*\W$${test_target}\W?" | grep -vE "^src/main/target"); \
520 if [ "$${FOUND}" != "" ]; then \
521 echo "Target dependencies found:"; \
522 echo "$${FOUND}"; \
523 exit 1; \
524 fi; \
525 done
527 check-fastram-usage-correctness:
528 $(V1) NON_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM_ZERO_INIT\W.*=.*" src/main/ | grep -Ev "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
529 if [ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
530 echo "Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
531 echo "$${NON_TRIVIALLY_INITIALIZED}"; \
532 exit 1; \
533 fi; \
534 TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -v "="); \
535 EXPLICITLY_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -E "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
536 if [ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
537 echo "Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
538 echo "$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
539 exit 1; \
542 # rebuild everything when makefile changes
543 $(TARGET_OBJS) : Makefile
545 # include auto-generated dependencies
546 -include $(TARGET_DEPS)