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 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')
115 # Default Tool options - can be overridden in {mcu}.mk files.
118 OPTIMISE_DEFAULT
:= -Og
120 LTO_FLAGS
:= $(OPTIMISE_DEFAULT
)
121 DEBUG_FLAGS
= -ggdb3
-DDEBUG
126 OPTIMISATION_BASE
:= -flto
-fuse-linker-plugin
-ffast-math
127 OPTIMISE_DEFAULT
:= -O2
128 OPTIMISE_SPEED
:= -Ofast
131 LTO_FLAGS
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
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
)
148 $(error FLASH_SIZE not configured for target
$(TARGET
))
152 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
154 ifneq ($(HSE_VALUE
),)
155 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
158 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
159 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
162 TARGET_FLAGS
:= -DOPBL
$(TARGET_FLAGS
)
163 .DEFAULT_GOAL
:= binary
168 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
169 $(ROOT
)/lib
/main
/MAVLink
171 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
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
184 RESULT
= $(shell (which
$(CCACHE
) > /dev
/null
2>&1; echo
$$?
) )
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
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
)) \
210 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
211 -ffunction-sections \
216 -DUSE_STDPERIPH_DRIVER \
219 -D
'__FORKNAME__="$(FORKNAME)"' \
220 -D
'__TARGET__="$(TARGET)"' \
221 -D
'__REVISION__="$(REVISION)"' \
226 ASFLAGS
= $(ARCH_FLAGS
) \
227 -x assembler-with-cpp \
228 $(addprefix -I
,$(INCLUDE_DIRS
)) \
241 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
242 -Wl
,-L
$(LINKER_DIR
) \
244 -Wl
,--no-wchar-size-warning \
248 ###############################################################################
249 # No user-serviceable parts below
250 ###############################################################################
252 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
253 --std
=c99
--inline-suppr
--quiet
--force \
254 $(addprefix -I
,$(INCLUDE_DIRS
)) \
255 -I
/usr
/include -I
/usr
/include/linux
258 # Things we will build
260 TARGET_BIN
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).bin
261 TARGET_HEX
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).hex
262 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
263 TARGET_LST
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).lst
264 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
265 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
266 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
269 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
270 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
271 CLEAN_ARTIFACTS
+= $(TARGET_ELF
) $(TARGET_OBJS
) $(TARGET_MAP
)
272 CLEAN_ARTIFACTS
+= $(TARGET_LST
)
274 # Make sure build date and revision is updated on every incremental build
275 $(OBJECT_DIR
)/$(TARGET
)/build
/version.o
: $(SRC
)
277 # List of buildable ELF files and their object dependencies.
278 # It would be nice to compute these lists, but that seems to be just beyond make.
280 $(TARGET_LST
): $(TARGET_ELF
)
281 $(V0
) $(OBJDUMP
) -S
--disassemble
$< > $@
283 $(TARGET_HEX
): $(TARGET_ELF
)
284 @echo
"Creating HEX $(TARGET_HEX)" "$(STDOUT)"
285 $(V1
) $(OBJCOPY
) -O ihex
--set-start
0x8000000 $< $@
287 $(TARGET_BIN
): $(TARGET_ELF
)
288 @echo
"Creating BIN $(TARGET_BIN)" "$(STDOUT)"
289 $(V1
) $(OBJCOPY
) -O binary
$< $@
291 $(TARGET_ELF
): $(TARGET_OBJS
)
292 @echo
"Linking $(TARGET)" "$(STDOUT)"
293 $(V1
) $(CROSS_CC
) -o
$@
$^
$(LD_FLAGS
)
294 $(V1
) $(SIZE
) $(TARGET_ELF
)
298 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
299 $(V1
) mkdir
-p
$(dir $@
)
300 $(V1
) echo
"%% (debug) $(notdir $<)" "$(STDOUT)" && \
301 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEBUG_OPTIMISATION
) $<
303 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
304 $(V1
) mkdir
-p
$(dir $@
)
305 $(V1
) $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SPEED_OPTIMISED_SRC
)), \
306 echo
"%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
307 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SPEED_OPTIMISATION
) $<, \
308 $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SIZE_OPTIMISED_SRC
)), \
309 echo
"%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
310 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SIZE_OPTIMISATION
) $<, \
311 echo
"%% $(notdir $<)" "$(STDOUT)" && \
312 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEFAULT_OPTIMISATION
) $<))
316 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.s
317 $(V1
) mkdir
-p
$(dir $@
)
318 @echo
"%% $(notdir $<)" "$(STDOUT)"
319 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
321 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.S
322 $(V1
) mkdir
-p
$(dir $@
)
323 @echo
"%% $(notdir $<)" "$(STDOUT)"
324 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
327 ## all : Build all targets (excluding unsupported)
328 all: $(SUPPORTED_TARGETS
)
330 ## all_with_unsupported : Build all targets (excluding unsupported)
331 all_with_unsupported
: $(VALID_TARGETS
)
333 ## unsupported : Build unsupported targets
334 unsupported
: $(UNSUPPORTED_TARGETS
)
336 ## official : Build all official (travis) targets
337 official
: $(OFFICIAL_TARGETS
)
339 ## targets-group-1 : build some targets
340 targets-group-1
: $(GROUP_1_TARGETS
)
342 ## targets-group-2 : build some targets
343 targets-group-2
: $(GROUP_2_TARGETS
)
345 ## targets-group-3 : build some targets
346 targets-group-3
: $(GROUP_3_TARGETS
)
348 ## targets-group-3 : build some targets
349 targets-group-4
: $(GROUP_4_TARGETS
)
351 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
352 targets-group-rest
: $(GROUP_OTHER_TARGETS
)
355 $(V0
) @echo
"Building $@" && \
356 $(MAKE
) binary hex TARGET
=$@
&& \
357 echo
"Building $@ succeeded."
362 CLEAN_TARGETS
= $(addprefix clean_
,$(VALID_TARGETS
) )
363 TARGETS_CLEAN
= $(addsuffix _clean
,$(VALID_TARGETS
) )
365 ## clean : clean up temporary / machine-generated files
367 @echo
"Cleaning $(TARGET)"
368 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
369 $(V0
) rm -rf
$(OBJECT_DIR
)/$(TARGET
)
370 @echo
"Cleaning $(TARGET) succeeded."
372 ## clean_test : clean up temporary / machine-generated files (tests)
374 $(V0
) cd src
/test && $(MAKE
) clean || true
376 ## clean_<TARGET> : clean up one specific target
378 $(V0
) $(MAKE
) -j TARGET
=$(subst clean_
,,$@
) clean
380 ## <TARGET>_clean : clean up one specific target (alias for above)
382 $(V0
) $(MAKE
) -j TARGET
=$(subst _clean
,,$@
) clean
384 ## clean_all : clean all valid targets
385 clean_all
: $(CLEAN_TARGETS
)
387 ## all_clean : clean all valid targets (alias for above)
388 all_clean
: $(TARGETS_CLEAN
)
391 flash_
$(TARGET
): $(TARGET_HEX
)
392 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
393 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
394 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
396 ## flash : flash firmware (.hex) onto flight controller
397 flash
: flash_
$(TARGET
)
399 st-flash_
$(TARGET
): $(TARGET_BIN
)
400 $(V0
) st-flash
--reset write
$< 0x08000000
402 ## st-flash : flash firmware (.bin) onto flight controller
403 st-flash
: st-flash_
$(TARGET
)
405 ifneq ($(OPENOCD_COMMAND
),)
406 openocd-gdb
: $(TARGET_ELF
)
407 $(V0
) $(OPENOCD_COMMAND
) & $(CROSS_GDB
) $(TARGET_ELF
) -ex
"target remote localhost:3333" -ex
"load"
411 $(V0
) $(MAKE
) -j
$(TARGET_BIN
)
414 $(V0
) $(MAKE
) -j
$(TARGET_HEX
)
416 unbrick_
$(TARGET
): $(TARGET_HEX
)
417 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
418 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
420 ## unbrick : unbrick flight controller
421 unbrick
: unbrick_
$(TARGET
)
423 ## cppcheck : run static analysis on C source code
424 cppcheck
: $(CSOURCES
)
427 cppcheck-result.xml
: $(CSOURCES
)
428 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
440 ## version : print firmware version
444 ## help : print this help message and exit
445 help
: Makefile make
/tools.mk
447 @echo
"Makefile for the $(FORKNAME) firmware"
450 @echo
" make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
452 @echo
" make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
454 @echo
"Valid TARGET values are: $(VALID_TARGETS)"
456 @sed
-n
's/^## //p' $?
458 ## targets : print a list of all valid target platforms (for consumption by scripts)
460 @echo
"Valid targets: $(VALID_TARGETS)"
461 @echo
"Supported targets: $(SUPPORTED_TARGETS)"
462 @echo
"Unsupported targets: $(UNSUPPORTED_TARGETS)"
463 @echo
"Target: $(TARGET)"
464 @echo
"Base target: $(BASE_TARGET)"
465 @echo
"targets-group-1: $(GROUP_1_TARGETS)"
466 @echo
"targets-group-2: $(GROUP_2_TARGETS)"
467 @echo
"targets-group-3: $(GROUP_3_TARGETS)"
468 @echo
"targets-group-4: $(GROUP_4_TARGETS)"
469 @echo
"targets-group-rest: $(GROUP_OTHER_TARGETS)"
471 ## test : run the cleanflight test suite
472 ## junittest : run the cleanflight test suite, producing Junit XML result files.
474 $(V0
) cd src
/test && $(MAKE
) $@
476 # rebuild everything when makefile changes
477 $(TARGET_OBJS
) : Makefile
479 # include auto-generated dependencies
480 -include $(TARGET_DEPS
)