Merge pull request #6459 from mikeller/fix_kissfcv2f7_leds
[betaflight.git] / Makefile
blob692c3afcc7528e8dae1c880b83a9bdbd76e0f929
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=gnu99 \
210 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
211 -ffunction-sections \
212 -fdata-sections \
213 -pedantic \
214 $(DEVICE_FLAGS) \
215 -D_GNU_SOURCE \
216 -DUSE_STDPERIPH_DRIVER \
217 -D$(TARGET) \
218 $(TARGET_FLAGS) \
219 -D'__FORKNAME__="$(FORKNAME)"' \
220 -D'__TARGET__="$(TARGET)"' \
221 -D'__REVISION__="$(REVISION)"' \
222 -save-temps=obj \
223 -MMD -MP \
224 $(EXTRA_FLAGS)
226 ASFLAGS = $(ARCH_FLAGS) \
227 -x assembler-with-cpp \
228 $(addprefix -I,$(INCLUDE_DIRS)) \
229 -MMD -MP
231 ifeq ($(LD_FLAGS),)
232 LD_FLAGS = -lm \
233 -nostartfiles \
234 --specs=nano.specs \
235 -lc \
236 -lnosys \
237 $(ARCH_FLAGS) \
238 $(LTO_FLAGS) \
239 $(DEBUG_FLAGS) \
240 -static \
241 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
242 -Wl,-L$(LINKER_DIR) \
243 -Wl,--cref \
244 -Wl,--no-wchar-size-warning \
245 -Wl,--print-memory-usage \
246 -T$(LD_SCRIPT)
247 endif
249 ###############################################################################
250 # No user-serviceable parts below
251 ###############################################################################
253 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
254 --std=c99 --inline-suppr --quiet --force \
255 $(addprefix -I,$(INCLUDE_DIRS)) \
256 -I/usr/include -I/usr/include/linux
259 # Things we will build
261 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
262 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
263 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
264 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
265 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
266 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
267 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
270 CLEAN_ARTIFACTS := $(TARGET_BIN)
271 CLEAN_ARTIFACTS += $(TARGET_HEX)
272 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
273 CLEAN_ARTIFACTS += $(TARGET_LST)
275 # Make sure build date and revision is updated on every incremental build
276 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
278 # List of buildable ELF files and their object dependencies.
279 # It would be nice to compute these lists, but that seems to be just beyond make.
281 $(TARGET_LST): $(TARGET_ELF)
282 $(V0) $(OBJDUMP) -S --disassemble $< > $@
284 $(TARGET_HEX): $(TARGET_ELF)
285 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
286 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
288 $(TARGET_BIN): $(TARGET_ELF)
289 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
290 $(V1) $(OBJCOPY) -O binary $< $@
292 $(TARGET_ELF): $(TARGET_OBJS)
293 @echo "Linking $(TARGET)" "$(STDOUT)"
294 $(V1) $(CROSS_CC) -o $@ $^ $(LD_FLAGS)
295 $(V1) $(SIZE) $(TARGET_ELF)
297 # Compile
298 ifeq ($(DEBUG),GDB)
299 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
300 $(V1) mkdir -p $(dir $@)
301 $(V1) echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
302 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<
303 else
304 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
305 $(V1) mkdir -p $(dir $@)
306 $(V1) $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
307 echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
308 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
309 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
310 echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
311 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
312 echo "%% $(notdir $<)" "$(STDOUT)" && \
313 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))
314 endif
316 # Assemble
317 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
318 $(V1) mkdir -p $(dir $@)
319 @echo "%% $(notdir $<)" "$(STDOUT)"
320 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
322 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
323 $(V1) mkdir -p $(dir $@)
324 @echo "%% $(notdir $<)" "$(STDOUT)"
325 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
328 ## all : Build all targets (excluding unsupported)
329 all: $(SUPPORTED_TARGETS)
331 ## all_with_unsupported : Build all targets (including unsupported)
332 all_with_unsupported: $(VALID_TARGETS)
334 ## unsupported : Build unsupported targets
335 unsupported: $(UNSUPPORTED_TARGETS)
337 ## official : Build all official (travis) targets
338 official: $(OFFICIAL_TARGETS)
340 ## targets-group-1 : build some targets
341 targets-group-1: $(GROUP_1_TARGETS)
343 ## targets-group-2 : build some targets
344 targets-group-2: $(GROUP_2_TARGETS)
346 ## targets-group-3 : build some targets
347 targets-group-3: $(GROUP_3_TARGETS)
349 ## targets-group-3 : build some targets
350 targets-group-4: $(GROUP_4_TARGETS)
352 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
353 targets-group-rest: $(GROUP_OTHER_TARGETS)
355 $(VALID_TARGETS):
356 $(V0) @echo "Building $@" && \
357 $(MAKE) binary hex TARGET=$@ && \
358 echo "Building $@ succeeded."
360 $(NOBUILD_TARGETS):
361 $(MAKE) TARGET=$@
363 CLEAN_TARGETS = $(addprefix clean_,$(VALID_TARGETS) )
364 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS) )
366 ## clean : clean up temporary / machine-generated files
367 clean:
368 @echo "Cleaning $(TARGET)"
369 $(V0) rm -f $(CLEAN_ARTIFACTS)
370 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
371 @echo "Cleaning $(TARGET) succeeded."
373 ## clean_test : clean up temporary / machine-generated files (tests)
374 clean_test:
375 $(V0) cd src/test && $(MAKE) clean || true
377 ## clean_<TARGET> : clean up one specific target
378 $(CLEAN_TARGETS):
379 $(V0) $(MAKE) -j TARGET=$(subst clean_,,$@) clean
381 ## <TARGET>_clean : clean up one specific target (alias for above)
382 $(TARGETS_CLEAN):
383 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
385 ## clean_all : clean all valid targets
386 clean_all: $(CLEAN_TARGETS)
388 ## all_clean : clean all valid targets (alias for above)
389 all_clean: $(TARGETS_CLEAN)
392 flash_$(TARGET): $(TARGET_HEX)
393 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
394 $(V0) echo -n 'R' >$(SERIAL_DEVICE)
395 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
397 ## flash : flash firmware (.hex) onto flight controller
398 flash: flash_$(TARGET)
400 st-flash_$(TARGET): $(TARGET_BIN)
401 $(V0) st-flash --reset write $< 0x08000000
403 ## st-flash : flash firmware (.bin) onto flight controller
404 st-flash: st-flash_$(TARGET)
406 ifneq ($(OPENOCD_COMMAND),)
407 openocd-gdb: $(TARGET_ELF)
408 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
409 endif
411 binary:
412 $(V0) $(MAKE) -j $(TARGET_BIN)
414 hex:
415 $(V0) $(MAKE) -j $(TARGET_HEX)
417 unbrick_$(TARGET): $(TARGET_HEX)
418 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
419 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
421 ## unbrick : unbrick flight controller
422 unbrick: unbrick_$(TARGET)
424 ## cppcheck : run static analysis on C source code
425 cppcheck: $(CSOURCES)
426 $(V0) $(CPPCHECK)
428 cppcheck-result.xml: $(CSOURCES)
429 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
431 # mkdirs
432 $(DL_DIR):
433 mkdir -p $@
435 $(TOOLS_DIR):
436 mkdir -p $@
438 $(BUILD_DIR):
439 mkdir -p $@
441 ## version : print firmware version
442 version:
443 @echo $(FC_VER)
445 ## help : print this help message and exit
446 help: Makefile make/tools.mk
447 @echo ""
448 @echo "Makefile for the $(FORKNAME) firmware"
449 @echo ""
450 @echo "Usage:"
451 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
452 @echo "Or:"
453 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
454 @echo ""
455 @echo "Valid TARGET values are: $(VALID_TARGETS)"
456 @echo ""
457 @sed -n 's/^## //p' $?
459 ## targets : print a list of all valid target platforms (for consumption by scripts)
460 targets:
461 @echo "Valid targets: $(VALID_TARGETS)"
462 @echo "Supported targets: $(SUPPORTED_TARGETS)"
463 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
464 @echo "Target: $(TARGET)"
465 @echo "Base target: $(BASE_TARGET)"
466 @echo "targets-group-1: $(GROUP_1_TARGETS)"
467 @echo "targets-group-2: $(GROUP_2_TARGETS)"
468 @echo "targets-group-3: $(GROUP_3_TARGETS)"
469 @echo "targets-group-4: $(GROUP_4_TARGETS)"
470 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
472 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
473 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
474 @echo "targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
475 @echo "targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
476 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
477 @echo "total in all groups $(words $(SUPPORTED_TARGETS)) targets"
479 ## target-mcu : print the MCU type of the target
480 target-mcu:
481 @echo $(TARGET_MCU)
483 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
484 targets-by-mcu:
485 @echo "Building all $(MCU_TYPE) targets..."
486 $(V1) for target in $(VALID_TARGETS); do \
487 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
488 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
489 echo "Building target $${target}..."; \
490 $(MAKE) TARGET=$${target}; \
491 if [ $$? -ne 0 ]; then \
492 echo "Building target $${target} failed, aborting."; \
493 exit 1; \
494 fi; \
495 fi; \
496 done
498 ## targets-f3 : make all F3 targets
499 targets-f3:
500 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3
502 ## targets-f4 : make all F4 targets
503 targets-f4:
504 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4
506 ## targets-f7 : make all F7 targets
507 targets-f7:
508 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7
510 ## test : run the cleanflight test suite
511 ## junittest : run the cleanflight test suite, producing Junit XML result files.
512 test junittest:
513 $(V0) cd src/test && $(MAKE) $@
516 check-target-independence:
517 $(V1) for test_target in $(VALID_TARGETS); do \
518 FOUND=$$(grep -rE "\W$${test_target}\W?" src/main | grep -vE "(//)|(/\*).*\W$${test_target}\W?" | grep -vE "^src/main/target"); \
519 if [ "$${FOUND}" != "" ]; then \
520 echo "Target dependencies found:"; \
521 echo "$${FOUND}"; \
522 exit 1; \
523 fi; \
524 done
526 check-fastram-usage-correctness:
527 $(V1) NON_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM_ZERO_INIT\W.*=.*" src/main/ | grep -Ev "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
528 if [ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
529 echo "Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
530 echo "$${NON_TRIVIALLY_INITIALIZED}"; \
531 exit 1; \
532 fi; \
533 TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -v "="); \
534 EXPLICITLY_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -E "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
535 if [ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
536 echo "Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
537 echo "$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
538 exit 1; \
541 # rebuild everything when makefile changes
542 $(TARGET_OBJS) : Makefile
544 # include auto-generated dependencies
545 -include $(TARGET_DEPS)