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
21 # Compile-time options
24 # compile for OpenPilot BootLoader support
27 # Debugger optons, must be empty or GDB
30 # Insert the debugging hardfault debugger
31 # releases should not be built with this flag as it does not disable pwm output
34 # Serial port/Device for flashing
35 SERIAL_DEVICE ?
= $(firstword $(wildcard /dev
/ttyUSB
*) no-port-found
)
37 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
41 ###############################################################################
42 # Things that need to be maintained as the source changes
48 ROOT
:= $(patsubst %/,%,$(dir $(lastword
$(MAKEFILE_LIST
))))
49 SRC_DIR
= $(ROOT
)/src
/main
50 OBJECT_DIR
= $(ROOT
)/obj
/main
52 CMSIS_DIR
= $(ROOT
)/lib
/main
/CMSIS
53 INCLUDE_DIRS
= $(SRC_DIR
) \
54 $(ROOT
)/src
/main
/target
55 LINKER_DIR
= $(ROOT
)/src
/main
/target
/link
57 ## V : Set verbosity level based on the V= parameter
60 include $(ROOT
)/make
/build_verbosity.mk
62 # Build tools, so we all share the same versions
63 # import macros common to all supported build systems
64 include $(ROOT
)/make
/system-id.mk
66 # developer preferences, edit these at will, they'll be gitignored
67 -include $(ROOT
)/make
/local.mk
69 # configure some directories that are relative to wherever ROOT_DIR is located
71 TOOLS_DIR
:= $(ROOT
)/tools
73 BUILD_DIR
:= $(ROOT
)/build
74 DL_DIR
:= $(ROOT
)/downloads
78 # import macros that are OS specific
79 include $(ROOT
)/make
/$(OSFAMILY
).mk
81 # include the tools makefile
82 include $(ROOT
)/make
/tools.mk
84 # default xtal value for F4 targets
87 # used for turning on features like VCP and SDCARD
90 include $(ROOT
)/make
/targets.mk
92 REVISION
:= $(shell git log
-1 --format
="%h")
94 FC_VER_MAJOR
:= $(shell grep
" FC_VERSION_MAJOR" src
/main
/build
/version.h | awk
'{print $$3}' )
95 FC_VER_MINOR
:= $(shell grep
" FC_VERSION_MINOR" src
/main
/build
/version.h | awk
'{print $$3}' )
96 FC_VER_PATCH
:= $(shell grep
" FC_VERSION_PATCH" src
/main
/build
/version.h | awk
'{print $$3}' )
98 FC_VER
:= $(FC_VER_MAJOR
).
$(FC_VER_MINOR
).
$(FC_VER_PATCH
)
100 # Search path for sources
101 VPATH
:= $(SRC_DIR
):$(SRC_DIR
)/startup
102 USBFS_DIR
= $(ROOT
)/lib
/main
/STM32_USB-FS-Device_Driver
103 USBPERIPH_SRC
= $(notdir $(wildcard $(USBFS_DIR
)/src
/*.c
))
104 FATFS_DIR
= $(ROOT
)/lib
/main
/FatFS
105 FATFS_SRC
= $(notdir $(wildcard $(FATFS_DIR
)/*.c
))
107 CSOURCES
:= $(shell find
$(SRC_DIR
) -name
'*.c')
112 # Default Tool options - can be overridden in {mcu}.mk files.
115 OPTIMISE_DEFAULT
:= -Og
117 LTO_FLAGS
:= $(OPTIMISE_DEFAULT
)
118 DEBUG_FLAGS
= -ggdb3
-DDEBUG
120 OPTIMISATION_BASE
:= -flto
-fuse-linker-plugin
-ffast-math
121 OPTIMISE_DEFAULT
:= -O2
122 OPTIMISE_SPEED
:= -Ofast
125 LTO_FLAGS
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
128 VPATH
:= $(VPATH
):$(ROOT
)/make
/mcu
129 VPATH
:= $(VPATH
):$(ROOT
)/make
131 # start specific includes
132 include $(ROOT
)/make
/mcu
/$(TARGET_MCU
).mk
134 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
135 ifeq ($(FLASH_SIZE
),)
136 ifneq ($(TARGET_FLASH
),)
137 FLASH_SIZE
:= $(TARGET_FLASH
)
139 $(error FLASH_SIZE not configured for target
$(TARGET
))
143 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
145 ifneq ($(HSE_VALUE
),)
146 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
149 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
150 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
153 TARGET_FLAGS
:= -DOPBL
$(TARGET_FLAGS
)
154 .DEFAULT_GOAL
:= binary
159 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
160 $(ROOT
)/lib
/main
/MAVLink
162 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
165 VPATH
:= $(VPATH
):$(TARGET_DIR
)
167 include $(ROOT
)/make
/source.mk
169 ###############################################################################
170 # Things that might need changing to use different tools
173 # Find out if ccache is installed on the system
175 RESULT
= $(shell (which
$(CCACHE
) > /dev
/null
2>&1; echo
$$?
) )
181 CROSS_CC
:= $(CCACHE
) $(ARM_SDK_PREFIX
)gcc
182 CROSS_CXX
:= $(CCACHE
) $(ARM_SDK_PREFIX
)g
++
183 OBJCOPY
:= $(ARM_SDK_PREFIX
)objcopy
184 OBJDUMP
:= $(ARM_SDK_PREFIX
)objdump
185 SIZE
:= $(ARM_SDK_PREFIX
)size
190 CC_DEBUG_OPTIMISATION
:= $(OPTIMISE_DEFAULT
)
191 CC_DEFAULT_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_DEFAULT
)
192 CC_SPEED_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SPEED
)
193 CC_SIZE_OPTIMISATION
:= $(OPTIMISATION_BASE
) $(OPTIMISE_SIZE
)
195 CFLAGS
+= $(ARCH_FLAGS
) \
196 $(addprefix -D
,$(OPTIONS
)) \
197 $(addprefix -I
,$(INCLUDE_DIRS
)) \
200 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
201 -ffunction-sections \
205 -DUSE_STDPERIPH_DRIVER \
208 -D
'__FORKNAME__="$(FORKNAME)"' \
209 -D
'__TARGET__="$(TARGET)"' \
210 -D
'__REVISION__="$(REVISION)"' \
215 ASFLAGS
= $(ARCH_FLAGS
) \
216 -x assembler-with-cpp \
217 $(addprefix -I
,$(INCLUDE_DIRS
)) \
230 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
231 -Wl
,-L
$(LINKER_DIR
) \
233 -Wl
,--no-wchar-size-warning \
237 ###############################################################################
238 # No user-serviceable parts below
239 ###############################################################################
241 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
242 --std
=c99
--inline-suppr
--quiet
--force \
243 $(addprefix -I
,$(INCLUDE_DIRS
)) \
244 -I
/usr
/include -I
/usr
/include/linux
247 # Things we will build
249 TARGET_BIN
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).bin
250 TARGET_HEX
= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)_
$(TARGET
).hex
251 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
252 TARGET_LST
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).lst
253 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
254 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(OBJECT_DIR
)/$(TARGET
)/,$(basename $(SRC
))))
255 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
258 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
259 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
260 CLEAN_ARTIFACTS
+= $(TARGET_ELF
) $(TARGET_OBJS
) $(TARGET_MAP
)
261 CLEAN_ARTIFACTS
+= $(TARGET_LST
)
263 # Make sure build date and revision is updated on every incremental build
264 $(OBJECT_DIR
)/$(TARGET
)/build
/version.o
: $(SRC
)
266 # List of buildable ELF files and their object dependencies.
267 # It would be nice to compute these lists, but that seems to be just beyond make.
269 $(TARGET_LST
): $(TARGET_ELF
)
270 $(V0
) $(OBJDUMP
) -S
--disassemble
$< > $@
272 $(TARGET_HEX
): $(TARGET_ELF
)
273 $(V0
) $(OBJCOPY
) -O ihex
--set-start
0x8000000 $< $@
275 $(TARGET_BIN
): $(TARGET_ELF
)
276 $(V0
) $(OBJCOPY
) -O binary
$< $@
278 $(TARGET_ELF
): $(TARGET_OBJS
)
279 $(V1
) echo Linking
$(TARGET
)
280 $(V1
) $(CROSS_CC
) -o
$@
$^
$(LD_FLAGS
)
281 $(V0
) $(SIZE
) $(TARGET_ELF
)
285 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
286 $(V1
) mkdir
-p
$(dir $@
)
287 $(V1
) echo
"%% (debug) $(notdir $<)" "$(STDOUT)" && \
288 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEBUG_OPTIMISATION
) $<
290 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.c
291 $(V1
) mkdir
-p
$(dir $@
)
292 $(V1
) $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SPEED_OPTIMISED_SRC
)), \
293 echo
"%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
294 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SPEED_OPTIMISATION
) $<, \
295 $(if
$(findstring $(subst .
/src
/main
/,,$<),$(SIZE_OPTIMISED_SRC
)), \
296 echo
"%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
297 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_SIZE_OPTIMISATION
) $<, \
298 echo
"%% $(notdir $<)" "$(STDOUT)" && \
299 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(CC_DEFAULT_OPTIMISATION
) $<))
303 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.s
304 $(V1
) mkdir
-p
$(dir $@
)
305 $(V1
) echo
"%% $(notdir $<)" "$(STDOUT)"
306 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
308 $(OBJECT_DIR
)/$(TARGET
)/%.o
: %.S
309 $(V1
) mkdir
-p
$(dir $@
)
310 $(V1
) echo
"%% $(notdir $<)" "$(STDOUT)"
311 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
314 ## all : Build all valid targets
315 all: $(VALID_TARGETS
)
317 ## official : Build all official (travis) targets
318 official
: $(OFFICIAL_TARGETS
)
320 ## targets-group-1 : build some targets
321 targets-group-1
: $(GROUP_1_TARGETS
)
323 ## targets-group-2 : build some targets
324 targets-group-2
: $(GROUP_2_TARGETS
)
326 ## targets-group-3 : build some targets
327 targets-group-3
: $(GROUP_3_TARGETS
)
329 ## targets-group-3 : build some targets
330 targets-group-4
: $(GROUP_4_TARGETS
)
332 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
333 targets-group-rest
: $(GROUP_OTHER_TARGETS
)
336 $(V0
) @echo
"Building $@" && \
337 time
$(MAKE
) binary hex TARGET
=$@
&& \
338 echo
"Building $@ succeeded."
343 CLEAN_TARGETS
= $(addprefix clean_
,$(VALID_TARGETS
) $(SKIP_TARGETS
) )
344 TARGETS_CLEAN
= $(addsuffix _clean
,$(VALID_TARGETS
) $(SKIP_TARGETS
) )
346 ## clean : clean up temporary / machine-generated files
348 $(V0
) @echo
"Cleaning $(TARGET)"
349 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
350 $(V0
) rm -rf
$(OBJECT_DIR
)/$(TARGET
)
351 $(V0
) @echo
"Cleaning $(TARGET) succeeded."
353 ## clean_test : clean up temporary / machine-generated files (tests)
355 $(V0
) cd src
/test && $(MAKE
) clean || true
357 ## clean_<TARGET> : clean up one specific target
359 $(V0
) $(MAKE
) -j TARGET
=$(subst clean_
,,$@
) clean
361 ## <TARGET>_clean : clean up one specific target (alias for above)
363 $(V0
) $(MAKE
) -j TARGET
=$(subst _clean
,,$@
) clean
365 ## clean_all : clean all valid targets
366 clean_all
: $(CLEAN_TARGETS
)
368 ## all_clean : clean all valid targets (alias for above)
369 all_clean
: $(TARGETS_CLEAN
)
372 flash_
$(TARGET
): $(TARGET_HEX
)
373 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
374 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
375 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
377 ## flash : flash firmware (.hex) onto flight controller
378 flash
: flash_
$(TARGET
)
380 st-flash_
$(TARGET
): $(TARGET_BIN
)
381 $(V0
) st-flash
--reset write
$< 0x08000000
383 ## st-flash : flash firmware (.bin) onto flight controller
384 st-flash
: st-flash_
$(TARGET
)
387 $(V0
) $(MAKE
) -j
$(TARGET_BIN
)
390 $(V0
) $(MAKE
) -j
$(TARGET_HEX
)
392 unbrick_
$(TARGET
): $(TARGET_HEX
)
393 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
394 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
396 ## unbrick : unbrick flight controller
397 unbrick
: unbrick_
$(TARGET
)
399 ## cppcheck : run static analysis on C source code
400 cppcheck
: $(CSOURCES
)
403 cppcheck-result.xml
: $(CSOURCES
)
404 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
416 ## version : print firmware version
420 ## help : print this help message and exit
421 help
: Makefile make
/tools.mk
423 $(V0
) @echo
"Makefile for the $(FORKNAME) firmware"
426 $(V0
) @echo
" make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
428 $(V0
) @echo
" make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
430 $(V0
) @echo
"Valid TARGET values are: $(VALID_TARGETS)"
432 $(V0
) @sed
-n
's/^## //p' $?
434 ## targets : print a list of all valid target platforms (for consumption by scripts)
436 $(V0
) @echo
"Valid targets: $(VALID_TARGETS)"
437 $(V0
) @echo
"Target: $(TARGET)"
438 $(V0
) @echo
"Base target: $(BASE_TARGET)"
439 $(V0
) @echo
"targets-group-1: $(GROUP_1_TARGETS)"
440 $(V0
) @echo
"targets-group-2: $(GROUP_2_TARGETS)"
441 $(V0
) @echo
"targets-group-3: $(GROUP_3_TARGETS)"
442 $(V0
) @echo
"targets-group-4: $(GROUP_4_TARGETS)"
443 $(V0
) @echo
"targets-group-rest: $(GROUP_OTHER_TARGETS)"
445 ## test : run the cleanflight test suite
446 ## junittest : run the cleanflight test suite, producing Junit XML result files.
448 $(V0
) cd src
/test && $(MAKE
) $@
450 # rebuild everything when makefile changes
451 $(TARGET_OBJS
) : Makefile
453 # include auto-generated dependencies
454 -include $(TARGET_DEPS
)