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
24 # compile for OpenPilot BootLoader support
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
33 # Insert the debugging hardfault debugger
34 # releases should not be built with this flag as it does not disable pwm output
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.
44 ###############################################################################
45 # Things that need to be maintained as the source changes
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 $(ROOT
)/src
/main
/startup
59 LINKER_DIR
:= $(ROOT
)/src
/main
/target
/link
61 ## V : Set verbosity level based on the V= parameter
64 include $(ROOT
)/make
/build_verbosity.mk
66 # Build tools, so we all share the same versions
67 # import macros common to all supported build systems
68 include $(ROOT
)/make
/system-id.mk
70 # developer preferences, edit these at will, they'll be gitignored
71 -include $(ROOT
)/make
/local.mk
73 # configure some directories that are relative to wherever ROOT_DIR is located
75 TOOLS_DIR
:= $(ROOT
)/tools
77 BUILD_DIR
:= $(ROOT
)/build
78 DL_DIR
:= $(ROOT
)/downloads
82 # import macros that are OS specific
83 include $(ROOT
)/make
/$(OSFAMILY
).mk
85 # include the tools makefile
86 include $(ROOT
)/make
/tools.mk
88 # default xtal value for F4 targets
91 # used for turning on features like VCP and SDCARD
94 # used to disable features based on flash space shortage (larger number => more features disabled)
95 FEATURE_CUT_LEVEL_SUPPLIED
:= $(FEATURE_CUT_LEVEL
)
98 include $(ROOT
)/make
/targets.mk
100 REVISION
:= $(shell git log
-1 --format
="%h")
102 FC_VER_MAJOR
:= $(shell grep
" FC_VERSION_MAJOR" src
/main
/build
/version.h | awk
'{print $$3}' )
103 FC_VER_MINOR
:= $(shell grep
" FC_VERSION_MINOR" src
/main
/build
/version.h | awk
'{print $$3}' )
104 FC_VER_PATCH
:= $(shell grep
" FC_VERSION_PATCH" src
/main
/build
/version.h | awk
'{print $$3}' )
106 FC_VER
:= $(FC_VER_MAJOR
).
$(FC_VER_MINOR
).
$(FC_VER_PATCH
)
108 # Search path for sources
109 VPATH
:= $(SRC_DIR
):$(SRC_DIR
)/startup
110 USBFS_DIR
= $(ROOT
)/lib
/main
/STM32_USB-FS-Device_Driver
111 USBPERIPH_SRC
= $(notdir $(wildcard $(USBFS_DIR
)/src
/*.c
))
112 FATFS_DIR
= $(ROOT
)/lib
/main
/FatFS
113 FATFS_SRC
= $(notdir $(wildcard $(FATFS_DIR
)/*.c
))
115 CSOURCES
:= $(shell find
$(SRC_DIR
) -name
'*.c')
120 # Default Tool options - can be overridden in {mcu}.mk files.
123 OPTIMISE_DEFAULT
:= -Og
125 LTO_FLAGS
:= $(OPTIMISE_DEFAULT
)
126 DEBUG_FLAGS
= -ggdb3
-DDEBUG
131 OPTIMISATION_BASE
:= -flto
-fuse-linker-plugin
-ffast-math
132 OPTIMISE_DEFAULT
:= -O2
133 OPTIMISE_SPEED
:= -Ofast
136 LTO_FLAGS
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
139 VPATH
:= $(VPATH
):$(ROOT
)/make
/mcu
140 VPATH
:= $(VPATH
):$(ROOT
)/make
142 # start specific includes
143 include $(ROOT
)/make
/mcu
/$(TARGET_MCU
).mk
145 # openocd specific includes
146 include $(ROOT
)/make
/openocd.mk
148 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
149 ifeq ($(FLASH_SIZE
),)
150 ifneq ($(TARGET_FLASH
),)
151 FLASH_SIZE
:= $(TARGET_FLASH
)
153 $(error FLASH_SIZE not configured for target
$(TARGET
))
157 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
159 ifneq ($(HSE_VALUE
),)
160 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
163 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED
),)
164 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFEATURE_CUT_LEVEL
=$(FEATURE_CUT_LEVEL_SUPPLIED
)
165 else ifneq ($(FEATURE_CUT_LEVEL
),)
166 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFEATURE_CUT_LEVEL
=$(FEATURE_CUT_LEVEL
)
169 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
170 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
173 TARGET_FLAGS
:= -DOPBL
$(TARGET_FLAGS
)
174 .DEFAULT_GOAL
:= binary
179 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
180 $(ROOT
)/lib
/main
/MAVLink
182 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
185 VPATH
:= $(VPATH
):$(TARGET_DIR
)
187 include $(ROOT
)/make
/source.mk
189 ###############################################################################
190 # Things that might need changing to use different tools
193 # Find out if ccache is installed on the system
195 RESULT
= $(shell (which
$(CCACHE
) > /dev
/null
2>&1; echo
$$?
) )
201 CROSS_CC
:= $(CCACHE
) $(ARM_SDK_PREFIX
)gcc
202 CROSS_CXX
:= $(CCACHE
) $(ARM_SDK_PREFIX
)g
++
203 CROSS_GDB
:= $(ARM_SDK_PREFIX
)gdb
204 OBJCOPY
:= $(ARM_SDK_PREFIX
)objcopy
205 OBJDUMP
:= $(ARM_SDK_PREFIX
)objdump
206 SIZE
:= $(ARM_SDK_PREFIX
)size
211 CC_DEBUG_OPTIMISATION
:= $(OPTIMISE_DEFAULT
)
212 CC_DEFAULT_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_DEFAULT
)
213 CC_SPEED_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
214 CC_SIZE_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SIZE
)
216 CFLAGS
+= $(ARCH_FLAGS
) \
217 $(addprefix -D
,$(OPTIONS
)) \
218 $(addprefix -I
,$(INCLUDE_DIRS
)) \
221 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
222 -ffunction-sections \
228 -DUSE_STDPERIPH_DRIVER \
231 -D
'__FORKNAME__="$(FORKNAME)"' \
232 -D
'__TARGET__="$(TARGET)"' \
233 -D
'__REVISION__="$(REVISION)"' \
238 ASFLAGS
= $(ARCH_FLAGS
) \
240 -x assembler-with-cpp \
241 $(addprefix -I
,$(INCLUDE_DIRS
)) \
254 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
255 -Wl
,-L
$(LINKER_DIR
) \
257 -Wl
,--no-wchar-size-warning \
258 -Wl
,--print-memory-usage \
262 ###############################################################################
263 # No user-serviceable parts below
264 ###############################################################################
266 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
267 --std
=c99
--inline-suppr
--quiet
--force \
268 $(addprefix -I
,$(INCLUDE_DIRS
)) \
269 -I
/usr
/include -I
/usr
/include/linux
272 # Things we will build
274 TARGET_BIN
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).bin
275 TARGET_HEX
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).hex
276 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
277 TARGET_LST
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).lst
278 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
279 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
280 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
283 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
284 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
285 CLEAN_ARTIFACTS
+= $(TARGET_ELF
) $(TARGET_OBJS
) $(TARGET_MAP
)
286 CLEAN_ARTIFACTS
+= $(TARGET_LST
)
288 # Make sure build date and revision is updated on every incremental build
289 $(OBJECT_DIR
)/$(TARGET
)/build
/version.o
: $(SRC
)
291 # List of buildable ELF files and their object dependencies.
292 # It would be nice to compute these lists, but that seems to be just beyond make.
294 $(TARGET_LST
): $(TARGET_ELF
)
295 $(V0
) $(OBJDUMP
) -S
--disassemble
$< > $@
297 $(TARGET_HEX
): $(TARGET_ELF
)
298 @echo
"Creating HEX $(TARGET_HEX)" "$(STDOUT)"
299 $(V1
) $(OBJCOPY
) -O ihex
--set-start
0x8000000 $< $@
301 $(TARGET_BIN
): $(TARGET_ELF
)
302 @echo
"Creating BIN $(TARGET_BIN)" "$(STDOUT)"
303 $(V1
) $(OBJCOPY
) -O binary
$< $@
305 $(TARGET_ELF
): $(TARGET_OBJS
)
306 @echo
"Linking $(TARGET)" "$(STDOUT)"
307 $(V1
) $(CROSS_CC
) -o
$@
$^
$(LD_FLAGS
)
308 $(V1
) $(SIZE
) $(TARGET_ELF
)
312 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
313 $(V1
) mkdir
-p
$(dir $@
)
314 $(V1
) echo
"%% (debug) $(notdir $<)" "$(STDOUT)" && \
315 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEBUG_OPTIMISATION
) $<
317 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
318 $(V1
) mkdir
-p
$(dir $@
)
319 $(V1
) $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SPEED_OPTIMISED_SRC
)), \
320 echo
"%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
321 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SPEED_OPTIMISATION
) $<, \
322 $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SIZE_OPTIMISED_SRC
)), \
323 echo
"%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
324 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SIZE_OPTIMISATION
) $<, \
325 echo
"%% $(notdir $<)" "$(STDOUT)" && \
326 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEFAULT_OPTIMISATION
) $<))
330 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.s
331 $(V1
) mkdir
-p
$(dir $@
)
332 @echo
"%% $(notdir $<)" "$(STDOUT)"
333 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
335 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.S
336 $(V1
) mkdir
-p
$(dir $@
)
337 @echo
"%% $(notdir $<)" "$(STDOUT)"
338 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
341 ## all : Build all targets (excluding unsupported)
342 all: $(SUPPORTED_TARGETS
)
344 ## all_with_unsupported : Build all targets (including unsupported)
345 all_with_unsupported
: $(VALID_TARGETS
)
347 ## unsupported : Build unsupported targets
348 unsupported
: $(UNSUPPORTED_TARGETS
)
350 ## official : Build all official (travis) targets
351 official
: $(OFFICIAL_TARGETS
)
353 ## targets-group-1 : build some targets
354 targets-group-1
: $(GROUP_1_TARGETS
)
356 ## targets-group-2 : build some targets
357 targets-group-2
: $(GROUP_2_TARGETS
)
359 ## targets-group-3 : build some targets
360 targets-group-3
: $(GROUP_3_TARGETS
)
362 ## targets-group-3 : build some targets
363 targets-group-4
: $(GROUP_4_TARGETS
)
365 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
366 targets-group-rest
: $(GROUP_OTHER_TARGETS
)
369 $(V0
) @echo
"Building $@" && \
370 $(MAKE
) binary hex TARGET
=$@
&& \
371 echo
"Building $@ succeeded."
376 CLEAN_TARGETS
= $(addprefix clean_
,$(VALID_TARGETS
) )
377 TARGETS_CLEAN
= $(addsuffix _clean
,$(VALID_TARGETS
) )
379 ## clean : clean up temporary / machine-generated files
381 @echo
"Cleaning $(TARGET)"
382 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
383 $(V0
) rm -rf
$(OBJECT_DIR
)/$(TARGET
)
384 @echo
"Cleaning $(TARGET) succeeded."
386 ## clean_test : clean up temporary / machine-generated files (tests)
388 $(V0
) cd src
/test && $(MAKE
) clean || true
390 ## clean_<TARGET> : clean up one specific target
392 $(V0
) $(MAKE
) -j TARGET
=$(subst clean_
,,$@
) clean
394 ## <TARGET>_clean : clean up one specific target (alias for above)
396 $(V0
) $(MAKE
) -j TARGET
=$(subst _clean
,,$@
) clean
398 ## clean_all : clean all valid targets
399 clean_all
: $(CLEAN_TARGETS
)
401 ## all_clean : clean all valid targets (alias for above)
402 all_clean
: $(TARGETS_CLEAN
)
405 flash_
$(TARGET
): $(TARGET_HEX
)
406 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
407 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
408 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
410 ## flash : flash firmware (.hex) onto flight controller
411 flash
: flash_
$(TARGET
)
413 st-flash_
$(TARGET
): $(TARGET_BIN
)
414 $(V0
) st-flash
--reset write
$< 0x08000000
416 ## st-flash : flash firmware (.bin) onto flight controller
417 st-flash
: st-flash_
$(TARGET
)
419 ifneq ($(OPENOCD_COMMAND
),)
420 openocd-gdb
: $(TARGET_ELF
)
421 $(V0
) $(OPENOCD_COMMAND
) & $(CROSS_GDB
) $(TARGET_ELF
) -ex
"target remote localhost:3333" -ex
"load"
425 $(V0
) $(MAKE
) -j
$(TARGET_BIN
)
428 $(V0
) $(MAKE
) -j
$(TARGET_HEX
)
430 unbrick_
$(TARGET
): $(TARGET_HEX
)
431 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
432 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
434 ## unbrick : unbrick flight controller
435 unbrick
: unbrick_
$(TARGET
)
437 ## cppcheck : run static analysis on C source code
438 cppcheck
: $(CSOURCES
)
441 cppcheck-result.xml
: $(CSOURCES
)
442 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
454 ## version : print firmware version
458 ## help : print this help message and exit
459 help
: Makefile make
/tools.mk
461 @echo
"Makefile for the $(FORKNAME) firmware"
464 @echo
" make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
466 @echo
" make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
468 @echo
"Valid TARGET values are: $(VALID_TARGETS)"
470 @sed
-n
's/^## //p' $?
472 ## targets : print a list of all valid target platforms (for consumption by scripts)
474 @echo
"Valid targets: $(VALID_TARGETS)"
475 @echo
"Supported targets: $(SUPPORTED_TARGETS)"
476 @echo
"Unsupported targets: $(UNSUPPORTED_TARGETS)"
477 @echo
"Target: $(TARGET)"
478 @echo
"Base target: $(BASE_TARGET)"
479 @echo
"targets-group-1: $(GROUP_1_TARGETS)"
480 @echo
"targets-group-2: $(GROUP_2_TARGETS)"
481 @echo
"targets-group-3: $(GROUP_3_TARGETS)"
482 @echo
"targets-group-4: $(GROUP_4_TARGETS)"
483 @echo
"targets-group-rest: $(GROUP_OTHER_TARGETS)"
485 @echo
"targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
486 @echo
"targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
487 @echo
"targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
488 @echo
"targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
489 @echo
"targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
490 @echo
"total in all groups $(words $(SUPPORTED_TARGETS)) targets"
492 ## target-mcu : print the MCU type of the target
496 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
498 $(V1
) for target in
$(VALID_TARGETS
); do \
499 TARGET_MCU_TYPE
=$$($(MAKE
) -s TARGET
=$${target} target-mcu
); \
500 if
[ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
501 if
[ "$${DO_BUILD}" = 1 ]; then \
502 echo
"Building target $${target}..."; \
503 $(MAKE
) TARGET
=$${target}; \
504 if
[ $$?
-ne
0 ]; then \
505 echo
"Building target $${target} failed, aborting."; \
509 echo
-n
"$${target} "; \
515 ## targets-f3 : make all F3 targets
517 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F3 DO_BUILD
=1
520 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F3
522 ## targets-f4 : make all F4 targets
524 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F4 DO_BUILD
=1
527 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F4
529 ## targets-f7 : make all F7 targets
531 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F7 DO_BUILD
=1
534 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F7
536 ## test : run the Betaflight test suite
537 ## junittest : run the Betaflight test suite, producing Junit XML result files.
538 ## 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)
539 ## test-all: run the Betaflight test suite including all per-target expanded tests
540 test junittest test-all test-representative
:
541 $(V0
) cd src
/test && $(MAKE
) $@
543 ## test_help : print the help message for the test suite (including a list of the available tests)
545 $(V0
) cd src
/test && $(MAKE
) help
547 ## test_% : run test 'test_%' from the test suite
549 $(V0
) cd src
/test && $(MAKE
) $@
552 check-target-independence
:
553 $(V1
) for test_target in
$(VALID_TARGETS
); do \
554 FOUND
=$$(grep
-rE
"\W$${test_target}\W?" src
/main | grep
-vE
"(//)|(/\*).*\W$${test_target}\W?" | grep
-vE
"^src/main/target"); \
555 if
[ "$${FOUND}" != "" ]; then \
556 echo
"Target dependencies found:"; \
562 check-fastram-usage-correctness
:
563 $(V1
) NON_TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM_ZERO_INIT\W.*=.*" src
/main
/ | grep
-Ev
"=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
564 if
[ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
565 echo
"Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
566 echo
"$${NON_TRIVIALLY_INITIALIZED}"; \
569 TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM\W.*;" src
/main
/ | grep
-v
"="); \
570 EXPLICITLY_TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM\W.*;" src
/main
/ | grep
-E
"=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
571 if
[ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
572 echo
"Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
573 echo
"$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
577 # rebuild everything when makefile changes
578 $(TARGET_OBJS
): Makefile
$(TARGET_DIR
)/target.mk
581 # include auto-generated dependencies
582 -include $(TARGET_DEPS
)