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 LINKER_DIR
:= $(ROOT
)/src
/main
/target
/link
60 ## V : Set verbosity level based on the V= parameter
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
74 TOOLS_DIR
:= $(ROOT
)/tools
76 BUILD_DIR
:= $(ROOT
)/build
77 DL_DIR
:= $(ROOT
)/downloads
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
90 # used for turning on features like VCP and SDCARD
93 # used to disable features based on flash space shortage (larger number => more features disabled)
94 FEATURE_CUT_LEVEL_SUPPLIED
:= $(FEATURE_CUT_LEVEL
)
97 include $(ROOT
)/make
/targets.mk
99 REVISION
:= $(shell git log
-1 --format
="%h")
101 FC_VER_MAJOR
:= $(shell grep
" FC_VERSION_MAJOR" src
/main
/build
/version.h | awk
'{print $$3}' )
102 FC_VER_MINOR
:= $(shell grep
" FC_VERSION_MINOR" src
/main
/build
/version.h | awk
'{print $$3}' )
103 FC_VER_PATCH
:= $(shell grep
" FC_VERSION_PATCH" src
/main
/build
/version.h | awk
'{print $$3}' )
105 FC_VER
:= $(FC_VER_MAJOR
).
$(FC_VER_MINOR
).
$(FC_VER_PATCH
)
107 # Search path for sources
108 VPATH
:= $(SRC_DIR
):$(SRC_DIR
)/startup
109 USBFS_DIR
= $(ROOT
)/lib
/main
/STM32_USB-FS-Device_Driver
110 USBPERIPH_SRC
= $(notdir $(wildcard $(USBFS_DIR
)/src
/*.c
))
111 FATFS_DIR
= $(ROOT
)/lib
/main
/FatFS
112 FATFS_SRC
= $(notdir $(wildcard $(FATFS_DIR
)/*.c
))
114 CSOURCES
:= $(shell find
$(SRC_DIR
) -name
'*.c')
119 # Default Tool options - can be overridden in {mcu}.mk files.
122 OPTIMISE_DEFAULT
:= -Og
124 LTO_FLAGS
:= $(OPTIMISE_DEFAULT
)
125 DEBUG_FLAGS
= -ggdb3
-DDEBUG
130 OPTIMISATION_BASE
:= -flto
-fuse-linker-plugin
-ffast-math
131 OPTIMISE_DEFAULT
:= -O2
132 OPTIMISE_SPEED
:= -Ofast
135 LTO_FLAGS
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
138 VPATH
:= $(VPATH
):$(ROOT
)/make
/mcu
139 VPATH
:= $(VPATH
):$(ROOT
)/make
141 # start specific includes
142 include $(ROOT
)/make
/mcu
/$(TARGET_MCU
).mk
144 # openocd specific includes
145 include $(ROOT
)/make
/openocd.mk
147 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
148 ifeq ($(FLASH_SIZE
),)
149 ifneq ($(TARGET_FLASH
),)
150 FLASH_SIZE
:= $(TARGET_FLASH
)
152 $(error FLASH_SIZE not configured for target
$(TARGET
))
156 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
158 ifneq ($(HSE_VALUE
),)
159 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
162 ifneq ($(FEATURE_CUT_LEVEL_SUPPLIED
),)
163 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFEATURE_CUT_LEVEL
=$(FEATURE_CUT_LEVEL_SUPPLIED
)
164 else ifneq ($(FEATURE_CUT_LEVEL
),)
165 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFEATURE_CUT_LEVEL
=$(FEATURE_CUT_LEVEL
)
168 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
169 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
172 TARGET_FLAGS
:= -DOPBL
$(TARGET_FLAGS
)
173 .DEFAULT_GOAL
:= binary
178 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
179 $(ROOT
)/lib
/main
/MAVLink
181 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
184 VPATH
:= $(VPATH
):$(TARGET_DIR
)
186 include $(ROOT
)/make
/source.mk
188 ###############################################################################
189 # Things that might need changing to use different tools
192 # Find out if ccache is installed on the system
194 RESULT
= $(shell (which
$(CCACHE
) > /dev
/null
2>&1; echo
$$?
) )
200 CROSS_CC
:= $(CCACHE
) $(ARM_SDK_PREFIX
)gcc
201 CROSS_CXX
:= $(CCACHE
) $(ARM_SDK_PREFIX
)g
++
202 CROSS_GDB
:= $(ARM_SDK_PREFIX
)gdb
203 OBJCOPY
:= $(ARM_SDK_PREFIX
)objcopy
204 OBJDUMP
:= $(ARM_SDK_PREFIX
)objdump
205 SIZE
:= $(ARM_SDK_PREFIX
)size
210 CC_DEBUG_OPTIMISATION
:= $(OPTIMISE_DEFAULT
)
211 CC_DEFAULT_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_DEFAULT
)
212 CC_SPEED_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
213 CC_SIZE_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SIZE
)
215 CFLAGS
+= $(ARCH_FLAGS
) \
216 $(addprefix -D
,$(OPTIONS
)) \
217 $(addprefix -I
,$(INCLUDE_DIRS
)) \
220 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
221 -ffunction-sections \
227 -DUSE_STDPERIPH_DRIVER \
230 -D
'__FORKNAME__="$(FORKNAME)"' \
231 -D
'__TARGET__="$(TARGET)"' \
232 -D
'__REVISION__="$(REVISION)"' \
237 ASFLAGS
= $(ARCH_FLAGS
) \
239 -x assembler-with-cpp \
240 $(addprefix -I
,$(INCLUDE_DIRS
)) \
253 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
254 -Wl
,-L
$(LINKER_DIR
) \
256 -Wl
,--no-wchar-size-warning \
257 -Wl
,--print-memory-usage \
261 ###############################################################################
262 # No user-serviceable parts below
263 ###############################################################################
265 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
266 --std
=c99
--inline-suppr
--quiet
--force \
267 $(addprefix -I
,$(INCLUDE_DIRS
)) \
268 -I
/usr
/include -I
/usr
/include/linux
271 # Things we will build
273 TARGET_BIN
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).bin
274 TARGET_HEX
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).hex
275 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
276 TARGET_LST
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).lst
277 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
278 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
279 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
282 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
283 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
284 CLEAN_ARTIFACTS
+= $(TARGET_ELF
) $(TARGET_OBJS
) $(TARGET_MAP
)
285 CLEAN_ARTIFACTS
+= $(TARGET_LST
)
287 # Make sure build date and revision is updated on every incremental build
288 $(OBJECT_DIR
)/$(TARGET
)/build
/version.o
: $(SRC
)
290 # List of buildable ELF files and their object dependencies.
291 # It would be nice to compute these lists, but that seems to be just beyond make.
293 $(TARGET_LST
): $(TARGET_ELF
)
294 $(V0
) $(OBJDUMP
) -S
--disassemble
$< > $@
296 $(TARGET_HEX
): $(TARGET_ELF
)
297 @echo
"Creating HEX $(TARGET_HEX)" "$(STDOUT)"
298 $(V1
) $(OBJCOPY
) -O ihex
--set-start
0x8000000 $< $@
300 $(TARGET_BIN
): $(TARGET_ELF
)
301 @echo
"Creating BIN $(TARGET_BIN)" "$(STDOUT)"
302 $(V1
) $(OBJCOPY
) -O binary
$< $@
304 $(TARGET_ELF
): $(TARGET_OBJS
)
305 @echo
"Linking $(TARGET)" "$(STDOUT)"
306 $(V1
) $(CROSS_CC
) -o
$@
$^
$(LD_FLAGS
)
307 $(V1
) $(SIZE
) $(TARGET_ELF
)
311 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
312 $(V1
) mkdir
-p
$(dir $@
)
313 $(V1
) echo
"%% (debug) $(notdir $<)" "$(STDOUT)" && \
314 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEBUG_OPTIMISATION
) $<
316 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
317 $(V1
) mkdir
-p
$(dir $@
)
318 $(V1
) $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SPEED_OPTIMISED_SRC
)), \
319 echo
"%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
320 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SPEED_OPTIMISATION
) $<, \
321 $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SIZE_OPTIMISED_SRC
)), \
322 echo
"%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
323 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SIZE_OPTIMISATION
) $<, \
324 echo
"%% $(notdir $<)" "$(STDOUT)" && \
325 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEFAULT_OPTIMISATION
) $<))
329 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.s
330 $(V1
) mkdir
-p
$(dir $@
)
331 @echo
"%% $(notdir $<)" "$(STDOUT)"
332 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
334 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.S
335 $(V1
) mkdir
-p
$(dir $@
)
336 @echo
"%% $(notdir $<)" "$(STDOUT)"
337 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
340 ## all : Build all targets (excluding unsupported)
341 all: $(SUPPORTED_TARGETS
)
343 ## all_with_unsupported : Build all targets (including unsupported)
344 all_with_unsupported
: $(VALID_TARGETS
)
346 ## unsupported : Build unsupported targets
347 unsupported
: $(UNSUPPORTED_TARGETS
)
349 ## official : Build all official (travis) targets
350 official
: $(OFFICIAL_TARGETS
)
352 ## targets-group-1 : build some targets
353 targets-group-1
: $(GROUP_1_TARGETS
)
355 ## targets-group-2 : build some targets
356 targets-group-2
: $(GROUP_2_TARGETS
)
358 ## targets-group-3 : build some targets
359 targets-group-3
: $(GROUP_3_TARGETS
)
361 ## targets-group-3 : build some targets
362 targets-group-4
: $(GROUP_4_TARGETS
)
364 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
365 targets-group-rest
: $(GROUP_OTHER_TARGETS
)
368 $(V0
) @echo
"Building $@" && \
369 $(MAKE
) binary hex TARGET
=$@
&& \
370 echo
"Building $@ succeeded."
375 CLEAN_TARGETS
= $(addprefix clean_
,$(VALID_TARGETS
) )
376 TARGETS_CLEAN
= $(addsuffix _clean
,$(VALID_TARGETS
) )
378 ## clean : clean up temporary / machine-generated files
380 @echo
"Cleaning $(TARGET)"
381 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
382 $(V0
) rm -rf
$(OBJECT_DIR
)/$(TARGET
)
383 @echo
"Cleaning $(TARGET) succeeded."
385 ## clean_test : clean up temporary / machine-generated files (tests)
387 $(V0
) cd src
/test && $(MAKE
) clean || true
389 ## clean_<TARGET> : clean up one specific target
391 $(V0
) $(MAKE
) -j TARGET
=$(subst clean_
,,$@
) clean
393 ## <TARGET>_clean : clean up one specific target (alias for above)
395 $(V0
) $(MAKE
) -j TARGET
=$(subst _clean
,,$@
) clean
397 ## clean_all : clean all valid targets
398 clean_all
: $(CLEAN_TARGETS
)
400 ## all_clean : clean all valid targets (alias for above)
401 all_clean
: $(TARGETS_CLEAN
)
404 flash_
$(TARGET
): $(TARGET_HEX
)
405 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
406 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
407 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
409 ## flash : flash firmware (.hex) onto flight controller
410 flash
: flash_
$(TARGET
)
412 st-flash_
$(TARGET
): $(TARGET_BIN
)
413 $(V0
) st-flash
--reset write
$< 0x08000000
415 ## st-flash : flash firmware (.bin) onto flight controller
416 st-flash
: st-flash_
$(TARGET
)
418 ifneq ($(OPENOCD_COMMAND
),)
419 openocd-gdb
: $(TARGET_ELF
)
420 $(V0
) $(OPENOCD_COMMAND
) & $(CROSS_GDB
) $(TARGET_ELF
) -ex
"target remote localhost:3333" -ex
"load"
424 $(V0
) $(MAKE
) -j
$(TARGET_BIN
)
427 $(V0
) $(MAKE
) -j
$(TARGET_HEX
)
429 unbrick_
$(TARGET
): $(TARGET_HEX
)
430 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
431 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
433 ## unbrick : unbrick flight controller
434 unbrick
: unbrick_
$(TARGET
)
436 ## cppcheck : run static analysis on C source code
437 cppcheck
: $(CSOURCES
)
440 cppcheck-result.xml
: $(CSOURCES
)
441 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
453 ## version : print firmware version
457 ## help : print this help message and exit
458 help
: Makefile make
/tools.mk
460 @echo
"Makefile for the $(FORKNAME) firmware"
463 @echo
" make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
465 @echo
" make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
467 @echo
"Valid TARGET values are: $(VALID_TARGETS)"
469 @sed
-n
's/^## //p' $?
471 ## targets : print a list of all valid target platforms (for consumption by scripts)
473 @echo
"Valid targets: $(VALID_TARGETS)"
474 @echo
"Supported targets: $(SUPPORTED_TARGETS)"
475 @echo
"Unsupported targets: $(UNSUPPORTED_TARGETS)"
476 @echo
"Target: $(TARGET)"
477 @echo
"Base target: $(BASE_TARGET)"
478 @echo
"targets-group-1: $(GROUP_1_TARGETS)"
479 @echo
"targets-group-2: $(GROUP_2_TARGETS)"
480 @echo
"targets-group-3: $(GROUP_3_TARGETS)"
481 @echo
"targets-group-4: $(GROUP_4_TARGETS)"
482 @echo
"targets-group-rest: $(GROUP_OTHER_TARGETS)"
484 @echo
"targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
485 @echo
"targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
486 @echo
"targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
487 @echo
"targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
488 @echo
"targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
489 @echo
"total in all groups $(words $(SUPPORTED_TARGETS)) targets"
491 ## target-mcu : print the MCU type of the target
495 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
497 $(V1
) for target in
$(VALID_TARGETS
); do \
498 TARGET_MCU_TYPE
=$$($(MAKE
) -s TARGET
=$${target} target-mcu
); \
499 if
[ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
500 if
[ "$${DO_BUILD}" = 1 ]; then \
501 echo
"Building target $${target}..."; \
502 $(MAKE
) TARGET
=$${target}; \
503 if
[ $$?
-ne
0 ]; then \
504 echo
"Building target $${target} failed, aborting."; \
508 echo
-n
"$${target} "; \
514 ## targets-f3 : make all F3 targets
516 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F3 DO_BUILD
=1
519 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F3
521 ## targets-f4 : make all F4 targets
523 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F4 DO_BUILD
=1
526 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F4
528 ## targets-f7 : make all F7 targets
530 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F7 DO_BUILD
=1
533 $(V1
) $(MAKE
) -s targets-by-mcu MCU_TYPE
=STM32F7
535 ## test : run the cleanflight test suite
536 ## junittest : run the cleanflight test suite, producing Junit XML result files.
538 $(V0
) cd src
/test && $(MAKE
) $@
541 check-target-independence
:
542 $(V1
) for test_target in
$(VALID_TARGETS
); do \
543 FOUND
=$$(grep
-rE
"\W$${test_target}\W?" src
/main | grep
-vE
"(//)|(/\*).*\W$${test_target}\W?" | grep
-vE
"^src/main/target"); \
544 if
[ "$${FOUND}" != "" ]; then \
545 echo
"Target dependencies found:"; \
551 check-fastram-usage-correctness
:
552 $(V1
) NON_TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM_ZERO_INIT\W.*=.*" src
/main
/ | grep
-Ev
"=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
553 if
[ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
554 echo
"Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
555 echo
"$${NON_TRIVIALLY_INITIALIZED}"; \
558 TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM\W.*;" src
/main
/ | grep
-v
"="); \
559 EXPLICITLY_TRIVIALLY_INITIALIZED
=$$(grep
-Ern
"\W?FAST_RAM\W.*;" src
/main
/ | grep
-E
"=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
560 if
[ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
561 echo
"Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
562 echo
"$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
566 # rebuild everything when makefile changes
567 $(TARGET_OBJS
): Makefile
$(TARGET_DIR
)/target.mk
570 # include auto-generated dependencies
571 -include $(TARGET_DEPS
)