2014-08-01 06:12:39 -07:00
###############################################################################
# "THE BEER-WARE LICENSE" (Revision 42):
# <msmith@FreeBSD.ORG> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return
###############################################################################
#
2016-06-07 17:20:26 -07:00
# Makefile for building the betaflight firmware.
2014-08-01 06:12:39 -07:00
#
# Invoke this with 'make help' to see the list of supported targets.
2014-11-07 06:26:29 -08:00
#
2014-08-01 06:12:39 -07:00
###############################################################################
2015-11-24 16:18:10 -08:00
2014-08-01 06:12:39 -07:00
# Things that the user might override on the commandline
#
# The target to build, see VALID_TARGETS below
2019-11-27 19:59:32 -08:00
TARGET ?= STM32F405
2014-08-01 06:12:39 -07:00
# Compile-time options
2016-06-12 00:26:52 -07:00
OPTIONS ?=
2014-08-01 06:12:39 -07:00
2015-10-15 00:33:24 -07:00
# compile for OpenPilot BootLoader support
2016-05-31 04:19:00 -07:00
OPBL ?= no
2015-10-15 00:33:24 -07:00
2019-05-07 23:38:31 -07:00
# compile for External Storage Bootloader support
EXST ?= no
2019-06-26 06:51:10 -07:00
# compile for target loaded into RAM
RAM_BASED ?= no
2019-06-24 07:13:09 -07:00
# reserve space for custom defaults
2019-07-21 01:55:18 -07:00
CUSTOM_DEFAULTS_EXTENDED ?= no
2019-06-24 07:13:09 -07:00
2018-04-12 00:03:50 -07:00
# Debugger optons:
# empty - ordinary build with all optimizations enabled
# RELWITHDEBINFO - ordinary build with debug symbols and all optimizations enabled
# GDB - debug build with minimum number of optimizations
2016-05-31 04:19:00 -07:00
DEBUG ?=
2014-08-01 06:12:39 -07:00
2016-04-30 12:16:02 -07:00
# Insert the debugging hardfault debugger
# releases should not be built with this flag as it does not disable pwm output
DEBUG_HARDFAULTS ?=
2014-08-01 06:12:39 -07:00
# Serial port/Device for flashing
2019-05-12 01:22:22 -07:00
SERIAL_DEVICE ?= $( firstword $( wildcard /dev/ttyACM*) $( firstword $( wildcard /dev/ttyUSB*) no-port-found) )
2014-08-01 06:12:39 -07:00
2015-03-25 14:00:58 -07:00
# Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
FLASH_SIZE ?=
2016-08-10 15:00:26 -07:00
2014-08-01 06:12:39 -07:00
###############################################################################
# Things that need to be maintained as the source changes
#
2016-05-31 04:19:00 -07:00
FORKNAME = betaflight
2014-08-01 06:12:39 -07:00
2016-06-12 22:20:33 -07:00
# Working directories
ROOT := $( patsubst %/,%,$( dir $( lastword $( MAKEFILE_LIST) ) ) )
2017-07-29 06:02:39 -07:00
SRC_DIR := $( ROOT) /src/main
OBJECT_DIR := $( ROOT) /obj/main
BIN_DIR := $( ROOT) /obj
CMSIS_DIR := $( ROOT) /lib/main/CMSIS
INCLUDE_DIRS := $( SRC_DIR) \
2019-02-19 23:24:16 -08:00
$( ROOT) /src/main/target \
$( ROOT) /src/main/startup
2019-02-19 22:08:26 -08:00
LINKER_DIR := $( ROOT) /src/link
2016-06-12 22:20:33 -07:00
2017-04-03 14:40:25 -07:00
## V : Set verbosity level based on the V= parameter
## V=0 Low
## V=1 High
i n c l u d e $( ROOT ) / m a k e / b u i l d _ v e r b o s i t y . m k
2016-10-22 05:47:26 -07:00
# Build tools, so we all share the same versions
2016-09-02 19:56:15 -07:00
# import macros common to all supported build systems
i n c l u d e $( ROOT ) / m a k e / s y s t e m - i d . m k
2016-12-28 15:55:33 -08:00
2016-09-13 20:14:55 -07:00
# developer preferences, edit these at will, they'll be gitignored
2016-12-28 15:55:33 -08:00
- i n c l u d e $( ROOT ) / m a k e / l o c a l . m k
2016-09-02 19:56:15 -07:00
2019-08-13 02:42:08 -07:00
# pre-build sanity checks
i n c l u d e $( ROOT ) / m a k e / c h e c k s . m k
2016-09-02 19:56:15 -07:00
# configure some directories that are relative to wherever ROOT_DIR is located
2017-01-13 15:16:03 -08:00
i f n d e f T O O L S _ D I R
2016-09-02 19:56:15 -07:00
TOOLS_DIR := $( ROOT) /tools
2017-01-13 15:16:03 -08:00
e n d i f
2016-09-02 19:56:15 -07:00
BUILD_DIR := $( ROOT) /build
2016-12-30 02:17:56 -08:00
DL_DIR := $( ROOT) /downloads
2016-09-02 19:56:15 -07:00
export RM := rm
# import macros that are OS specific
i n c l u d e $( ROOT ) / m a k e / $( OSFAMILY ) . m k
# include the tools makefile
i n c l u d e $( ROOT ) / m a k e / t o o l s . m k
2016-06-12 22:20:33 -07:00
# default xtal value for F4 targets
2017-01-13 15:16:03 -08:00
HSE_VALUE ?= 8000000
2016-06-12 22:20:33 -07:00
# used for turning on features like VCP and SDCARD
2016-06-13 04:55:14 -07:00
FEATURES =
2016-06-07 12:37:43 -07:00
2019-01-16 04:38:04 -08:00
# used to disable features based on flash space shortage (larger number => more features disabled)
FEATURE_CUT_LEVEL_SUPPLIED := $( FEATURE_CUT_LEVEL)
FEATURE_CUT_LEVEL =
2019-01-15 17:33:08 -08:00
2019-02-26 00:02:45 -08:00
# The list of targets to build for 'pre-push'
2020-07-16 07:11:22 -07:00
PRE_PUSH_TARGET_LIST ?= STM32F405 STM32F411 STM32F7X2 STM32F745 NUCLEOH743 SITL STM32F4DISCOVERY_DEBUG test-representative
2019-02-26 00:02:45 -08:00
2017-07-20 12:34:04 -07:00
i n c l u d e $( ROOT ) / m a k e / t a r g e t s . m k
2016-10-29 09:57:19 -07:00
2020-01-25 01:01:52 -08:00
REVISION := norevision
i f e q ( $( shell git diff --shortstat ) , )
2016-12-12 23:32:57 -08:00
REVISION := $( shell git log -1 --format= "%h" )
2020-01-25 01:01:52 -08:00
e n d i f
2014-09-03 16:27:28 -07:00
2016-08-07 03:46:47 -07:00
FC_VER_MAJOR := $( shell grep " FC_VERSION_MAJOR" src/main/build/version.h | awk '{print $$3}' )
FC_VER_MINOR := $( shell grep " FC_VERSION_MINOR" src/main/build/version.h | awk '{print $$3}' )
FC_VER_PATCH := $( shell grep " FC_VERSION_PATCH" src/main/build/version.h | awk '{print $$3}' )
2016-01-28 07:21:32 -08:00
FC_VER := $( FC_VER_MAJOR) .$( FC_VER_MINOR) .$( FC_VER_PATCH)
2014-08-01 06:12:39 -07:00
# Search path for sources
2016-06-12 00:26:52 -07:00
VPATH := $( SRC_DIR) :$( SRC_DIR) /startup
USBFS_DIR = $( ROOT) /lib/main/STM32_USB-FS-Device_Driver
USBPERIPH_SRC = $( notdir $( wildcard $( USBFS_DIR) /src/*.c) )
FATFS_DIR = $( ROOT) /lib/main/FatFS
FATFS_SRC = $( notdir $( wildcard $( FATFS_DIR) /*.c) )
2014-08-01 06:12:39 -07:00
2016-06-12 00:26:52 -07:00
CSOURCES := $( shell find $( SRC_DIR) -name '*.c' )
2014-08-01 06:12:39 -07:00
2019-06-24 07:13:09 -07:00
LD_FLAGS :=
EXTRA_LD_FLAGS :=
2014-08-01 06:12:39 -07:00
2016-06-13 10:00:24 -07:00
#
2017-07-20 12:34:04 -07:00
# Default Tool options - can be overridden in {mcu}.mk files.
2016-06-13 10:00:24 -07:00
#
2017-07-20 12:34:04 -07:00
i f e q ( $( DEBUG ) , G D B )
OPTIMISE_DEFAULT := -Og
2016-09-09 23:51:18 -07:00
2017-07-20 12:34:04 -07:00
LTO_FLAGS := $( OPTIMISE_DEFAULT)
DEBUG_FLAGS = -ggdb3 -DDEBUG
2016-09-09 23:51:18 -07:00
e l s e
2018-04-16 04:08:33 -07:00
i f e q ( $( DEBUG ) , I N F O )
2018-04-12 00:03:50 -07:00
DEBUG_FLAGS = -ggdb3
e n d i f
2017-07-20 12:34:04 -07:00
OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math
OPTIMISE_DEFAULT := -O2
OPTIMISE_SPEED := -Ofast
OPTIMISE_SIZE := -Os
2015-01-25 04:59:48 -08:00
2017-07-20 12:34:04 -07:00
LTO_FLAGS := $( OPTIMISATION_BASE) $( OPTIMISE_SPEED)
2015-01-25 04:59:48 -08:00
e n d i f
2017-07-20 12:34:04 -07:00
VPATH := $( VPATH) :$( ROOT) /make/mcu
VPATH := $( VPATH) :$( ROOT) /make
2016-06-12 22:38:22 -07:00
2017-07-20 12:34:04 -07:00
# start specific includes
i n c l u d e $( ROOT ) / m a k e / m c u / $( TARGET_MCU ) . m k
2014-08-01 06:12:39 -07:00
2017-07-25 14:24:16 -07:00
# openocd specific includes
i n c l u d e $( ROOT ) / m a k e / o p e n o c d . m k
2017-07-20 12:34:04 -07:00
# Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
2020-01-18 09:01:26 -08:00
i f e q ( $( TARGET_FLASH_SIZE ) , )
i f n e q ( $( MCU_FLASH_SIZE ) , )
TARGET_FLASH_SIZE := $( MCU_FLASH_SIZE)
2017-07-20 12:34:04 -07:00
e l s e
2020-01-18 09:01:26 -08:00
$( error MCU_FLASH_SIZE not configured for target $ ( TARGET ) )
2014-08-01 06:12:39 -07:00
e n d i f
2016-06-13 18:26:09 -07:00
e n d i f
2014-08-01 06:12:39 -07:00
2020-01-18 09:01:26 -08:00
DEVICE_FLAGS := $( DEVICE_FLAGS) -DTARGET_FLASH_SIZE= $( TARGET_FLASH_SIZE)
2015-03-25 14:00:58 -07:00
2016-07-30 22:42:03 -07:00
i f n e q ( $( HSE_VALUE ) , )
DEVICE_FLAGS := $( DEVICE_FLAGS) -DHSE_VALUE= $( HSE_VALUE)
e n d i f
2019-01-16 04:38:04 -08:00
i f n e q ( $( FEATURE_CUT_LEVEL_SUPPLIED ) , )
DEVICE_FLAGS := $( DEVICE_FLAGS) -DFEATURE_CUT_LEVEL= $( FEATURE_CUT_LEVEL_SUPPLIED)
e l s e i f n e q ( $( FEATURE_CUT_LEVEL ) , )
DEVICE_FLAGS := $( DEVICE_FLAGS) -DFEATURE_CUT_LEVEL= $( FEATURE_CUT_LEVEL)
2019-01-15 17:33:08 -08:00
e n d i f
2016-06-13 18:26:09 -07:00
TARGET_DIR = $( ROOT) /src/main/target/$( BASE_TARGET)
2016-06-12 22:20:33 -07:00
TARGET_DIR_SRC = $( notdir $( wildcard $( TARGET_DIR) /*.c) )
2014-08-01 06:12:39 -07:00
2015-11-24 16:18:10 -08:00
i f e q ( $( OPBL ) , y e s )
2016-06-12 00:26:52 -07:00
TARGET_FLAGS := -DOPBL $( TARGET_FLAGS)
2016-06-13 04:35:46 -07:00
.DEFAULT_GOAL := binary
2016-06-13 18:26:09 -07:00
e l s e
.DEFAULT_GOAL := hex
2015-11-24 16:18:10 -08:00
e n d i f
2019-07-21 01:55:18 -07:00
i f e q ( $( CUSTOM_DEFAULTS_EXTENDED ) , y e s )
TARGET_FLAGS += -DUSE_CUSTOM_DEFAULTS=
EXTRA_LD_FLAGS += -Wl,--defsym= USE_CUSTOM_DEFAULTS_EXTENDED = 1
2019-06-24 07:13:09 -07:00
e n d i f
2016-10-17 00:24:42 -07:00
INCLUDE_DIRS := $( INCLUDE_DIRS) \
$( ROOT) /lib/main/MAVLink
2016-06-12 09:31:46 -07:00
INCLUDE_DIRS := $( INCLUDE_DIRS) \
2016-06-13 21:36:22 -07:00
$( TARGET_DIR)
2014-08-01 06:12:39 -07:00
2016-06-12 09:31:46 -07:00
VPATH := $( VPATH) :$( TARGET_DIR)
2014-08-01 06:12:39 -07:00
2017-07-20 12:34:04 -07:00
i n c l u d e $( ROOT ) / m a k e / s o u r c e . m k
2014-08-01 06:12:39 -07:00
###############################################################################
# Things that might need changing to use different tools
#
2016-07-14 02:33:05 -07:00
# Find out if ccache is installed on the system
CCACHE := ccache
2016-07-15 06:05:38 -07:00
RESULT = $( shell ( which $( CCACHE) > /dev/null 2>& 1; echo $$ ?) )
i f n e q ( $( RESULT ) , 0 )
2016-07-14 02:33:05 -07:00
CCACHE :=
e n d i f
2014-08-01 06:12:39 -07:00
# Tool names
2016-11-09 12:28:14 -08:00
CROSS_CC := $( CCACHE) $( ARM_SDK_PREFIX) gcc
CROSS_CXX := $( CCACHE) $( ARM_SDK_PREFIX) g++
2017-07-25 14:24:16 -07:00
CROSS_GDB := $( ARM_SDK_PREFIX) gdb
2016-09-13 20:14:55 -07:00
OBJCOPY := $( ARM_SDK_PREFIX) objcopy
2017-07-03 11:57:00 -07:00
OBJDUMP := $( ARM_SDK_PREFIX) objdump
2016-09-13 20:14:55 -07:00
SIZE := $( ARM_SDK_PREFIX) size
2019-11-23 18:27:36 -08:00
DFUSE-PACK := src/utils/dfuse-pack.py
2014-08-01 06:12:39 -07:00
#
# Tool options.
#
2017-07-20 12:34:04 -07:00
CC_DEBUG_OPTIMISATION := $( OPTIMISE_DEFAULT)
2016-12-13 02:41:29 -08:00
CC_DEFAULT_OPTIMISATION := $( OPTIMISATION_BASE) $( OPTIMISE_DEFAULT)
CC_SPEED_OPTIMISATION := $( OPTIMISATION_BASE) $( OPTIMISE_SPEED)
CC_SIZE_OPTIMISATION := $( OPTIMISATION_BASE) $( OPTIMISE_SIZE)
2019-02-18 07:11:38 -08:00
CC_NO_OPTIMISATION :=
2016-12-13 02:41:29 -08:00
2019-11-23 16:56:16 -08:00
#
# Added after GCC version update, remove once the warnings have been fixed
#
2019-11-28 06:54:14 -08:00
TEMPORARY_FLAGS :=
2019-11-23 16:56:16 -08:00
2017-07-20 12:34:04 -07:00
CFLAGS += $( ARCH_FLAGS) \
2016-06-13 21:36:22 -07:00
$( addprefix -D,$( OPTIONS) ) \
$( addprefix -I,$( INCLUDE_DIRS) ) \
$( DEBUG_FLAGS) \
2018-08-18 15:33:32 -07:00
-std= gnu11 \
2016-06-13 21:36:22 -07:00
-Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
-ffunction-sections \
-fdata-sections \
2018-08-30 05:10:15 -07:00
-fno-common \
2016-08-10 04:25:37 -07:00
-pedantic \
2019-11-23 16:56:16 -08:00
$( TEMPORARY_FLAGS) \
2016-06-13 21:36:22 -07:00
$( DEVICE_FLAGS) \
2018-03-14 06:17:23 -07:00
-D_GNU_SOURCE \
2016-06-13 21:36:22 -07:00
-DUSE_STDPERIPH_DRIVER \
2016-08-10 04:25:37 -07:00
-D$( TARGET) \
2016-06-13 21:36:22 -07:00
$( TARGET_FLAGS) \
-D'__FORKNAME__="$(FORKNAME)"' \
-D'__TARGET__="$(TARGET)"' \
-D'__REVISION__="$(REVISION)"' \
-save-temps= obj \
2017-05-23 16:07:13 -07:00
-MMD -MP \
$( EXTRA_FLAGS)
2016-06-12 00:26:52 -07:00
ASFLAGS = $( ARCH_FLAGS) \
2018-12-10 10:07:47 -08:00
$( DEBUG_FLAGS) \
2016-06-13 21:36:22 -07:00
-x assembler-with-cpp \
$( addprefix -I,$( INCLUDE_DIRS) ) \
-MMD -MP
2016-06-12 00:26:52 -07:00
2017-07-20 12:34:04 -07:00
i f e q ( $( LD_FLAGS ) , )
LD_FLAGS = -lm \
2016-06-13 21:36:22 -07:00
-nostartfiles \
--specs= nano.specs \
-lc \
-lnosys \
$( ARCH_FLAGS) \
$( LTO_FLAGS) \
$( DEBUG_FLAGS) \
-static \
-Wl,-gc-sections,-Map,$( TARGET_MAP) \
-Wl,-L$( LINKER_DIR) \
-Wl,--cref \
2016-07-01 03:10:45 -07:00
-Wl,--no-wchar-size-warning \
2018-05-16 04:17:03 -07:00
-Wl,--print-memory-usage \
2019-06-24 07:13:09 -07:00
-T$( LD_SCRIPT) \
$( EXTRA_LD_FLAGS)
2017-03-30 11:35:06 -07:00
e n d i f
2014-08-01 06:12:39 -07:00
###############################################################################
# No user-serviceable parts below
###############################################################################
2016-06-12 00:26:52 -07:00
CPPCHECK = cppcheck $( CSOURCES) --enable= all --platform= unix64 \
2016-06-13 21:36:22 -07:00
--std= c99 --inline-suppr --quiet --force \
$( addprefix -I,$( INCLUDE_DIRS) ) \
-I/usr/include -I/usr/include/linux
2015-08-21 21:23:55 -07:00
2020-01-25 01:01:52 -08:00
TARGET_BASENAME = $( BIN_DIR) /$( FORKNAME) _$( FC_VER) _$( TARGET) _$( REVISION)
2014-08-01 06:12:39 -07:00
#
# Things we will build
#
2020-01-25 01:01:52 -08:00
TARGET_BIN = $( TARGET_BASENAME) .bin
TARGET_HEX = $( TARGET_BASENAME) .hex
TARGET_DFU = $( TARGET_BASENAME) .dfu
TARGET_ZIP = $( TARGET_BASENAME) .zip
2016-06-12 00:26:52 -07:00
TARGET_ELF = $( OBJECT_DIR) /$( FORKNAME) _$( TARGET) .elf
2019-05-07 23:38:31 -07:00
TARGET_EXST_ELF = $( OBJECT_DIR) /$( FORKNAME) _$( TARGET) _EXST.elf
TARGET_UNPATCHED_BIN = $( OBJECT_DIR) /$( FORKNAME) _$( TARGET) _UNPATCHED.bin
2017-07-03 11:57:00 -07:00
TARGET_LST = $( OBJECT_DIR) /$( FORKNAME) _$( TARGET) .lst
2017-03-06 18:40:14 -08:00
TARGET_OBJS = $( addsuffix .o,$( addprefix $( OBJECT_DIR) /$( TARGET) /,$( basename $( SRC) ) ) )
TARGET_DEPS = $( addsuffix .d,$( addprefix $( OBJECT_DIR) /$( TARGET) /,$( basename $( SRC) ) ) )
2016-06-12 00:26:52 -07:00
TARGET_MAP = $( OBJECT_DIR) /$( FORKNAME) _$( TARGET) .map
2014-08-01 06:12:39 -07:00
2019-05-07 23:38:31 -07:00
TARGET_EXST_HASH_SECTION_FILE = $( OBJECT_DIR) /$( TARGET) /exst_hash_section.bin
2015-10-23 09:48:43 -07:00
CLEAN_ARTIFACTS := $( TARGET_BIN)
2016-04-26 09:58:05 -07:00
CLEAN_ARTIFACTS += $( TARGET_HEX)
2016-06-12 00:26:52 -07:00
CLEAN_ARTIFACTS += $( TARGET_ELF) $( TARGET_OBJS) $( TARGET_MAP)
2017-07-03 11:57:00 -07:00
CLEAN_ARTIFACTS += $( TARGET_LST)
2019-11-23 18:27:36 -08:00
CLEAN_ARTIFACTS += $( TARGET_DFU)
2015-10-23 09:48:43 -07:00
2016-12-08 16:05:08 -08:00
# Make sure build date and revision is updated on every incremental build
2017-03-06 18:40:14 -08:00
$(OBJECT_DIR)/$(TARGET)/build/version.o : $( SRC )
2016-12-08 16:05:08 -08:00
2014-08-01 06:12:39 -07:00
# List of buildable ELF files and their object dependencies.
# It would be nice to compute these lists, but that seems to be just beyond make.
2017-07-03 11:57:00 -07:00
$(TARGET_LST) : $( TARGET_ELF )
$( V0) $( OBJDUMP) -S --disassemble $< > $@
2019-05-07 23:38:31 -07:00
i f e q ( $( EXST ) , n o )
$(TARGET_BIN) : $( TARGET_ELF )
@echo " Creating BIN $( TARGET_BIN) " " $( STDOUT) "
$( V1) $( OBJCOPY) -O binary $< $@
2014-08-01 06:12:39 -07:00
$(TARGET_HEX) : $( TARGET_ELF )
2017-12-30 07:28:52 -08:00
@echo " Creating HEX $( TARGET_HEX) " " $( STDOUT) "
2017-12-21 14:20:40 -08:00
$( V1) $( OBJCOPY) -O ihex --set-start 0x8000000 $< $@
2014-08-01 06:12:39 -07:00
2019-11-23 18:27:36 -08:00
$(TARGET_DFU) : $( TARGET_HEX )
@echo " Creating DFU $( TARGET_DFU) " " $( STDOUT) "
$( V1) $( DFUSE-PACK) -i $< $@
2019-05-07 23:38:31 -07:00
e l s e
CLEAN_ARTIFACTS += $( TARGET_UNPATCHED_BIN) $( TARGET_EXST_HASH_SECTION_FILE) $( TARGET_EXST_ELF)
$(TARGET_UNPATCHED_BIN) : $( TARGET_ELF )
@echo " Creating BIN (without checksum) $( TARGET_UNPATCHED_BIN) " " $( STDOUT) "
2017-12-21 14:20:40 -08:00
$( V1) $( OBJCOPY) -O binary $< $@
2014-09-26 06:10:12 -07:00
2019-05-07 23:38:31 -07:00
$(TARGET_BIN) : $( TARGET_UNPATCHED_BIN )
@echo " Creating EXST $( TARGET_BIN) " " $( STDOUT) "
# Linker script should allow .bin generation from a .elf which results in a file that is the same length as the FIRMWARE_SIZE.
# These 'dd' commands will pad a short binary to length FIRMWARE_SIZE.
$( V1) dd if = /dev/zero ibs = 1k count = $( FIRMWARE_SIZE) of = $( TARGET_BIN)
$( V1) dd if = $( TARGET_UNPATCHED_BIN) of = $( TARGET_BIN) conv = notrunc
@echo "Generating MD5 hash of binary" " $( STDOUT) "
$( V1) openssl dgst -md5 $( TARGET_BIN) > $( TARGET_UNPATCHED_BIN) .md5
@echo "Patching MD5 hash into binary" " $( STDOUT) "
$( V1) cat $( TARGET_UNPATCHED_BIN) .md5 | awk '{printf("%08x: %s",(1024*$(FIRMWARE_SIZE))-16,$$2);}' | xxd -r - $( TARGET_BIN)
$( V1) echo $( FIRMWARE_SIZE) | awk '{printf("-s 0x%08x -l 16 -c 16 %s",(1024*$$1)-16,"$(TARGET_BIN)");}' | xargs xxd
2019-08-09 11:53:26 -07:00
# Note: From the objcopy manual "If you do not specify outfile, objcopy creates a temporary file and destructively renames the result with the name of infile"
# Due to this a temporary file must be created and removed, even though we're only extracting data from the input file.
# If this temporary file is NOT used the $(TARGET_ELF) is modified, and running make a second time will result in
# a) regeneration of $(TARGET_BIN), and
# b) the results of $(TARGET_BIN) will not be as expected.
@echo " Extracting HASH section from unpatched EXST elf $( TARGET_ELF) " " $( STDOUT) "
$( OBJCOPY) $( TARGET_ELF) $( TARGET_EXST_ELF) .tmp --dump-section .exst_hash= $( TARGET_EXST_HASH_SECTION_FILE)
rm $( TARGET_EXST_ELF) .tmp
@echo "Patching MD5 hash into HASH section" " $( STDOUT) "
2019-05-07 23:38:31 -07:00
$( V1) cat $( TARGET_UNPATCHED_BIN) .md5 | awk '{printf("%08x: %s",64-16,$$2);}' | xxd -r - $( TARGET_EXST_HASH_SECTION_FILE)
2019-08-09 11:53:26 -07:00
@echo " Patching updated HASH section into $( TARGET_EXST_ELF) " " $( STDOUT) "
2019-05-07 23:38:31 -07:00
$( OBJCOPY) $( TARGET_ELF) $( TARGET_EXST_ELF) --update-section .exst_hash= $( TARGET_EXST_HASH_SECTION_FILE)
$(TARGET_HEX) : $( TARGET_BIN )
2019-08-07 06:22:34 -07:00
$( if $( EXST_ADJUST_VMA) ,,$( error "EXST_ADJUST_VMA not specified" ) )
2019-08-09 11:53:26 -07:00
@echo " Creating EXST HEX from patched EXST BIN $( TARGET_BIN) , VMA Adjust $( EXST_ADJUST_VMA) " " $( STDOUT) "
$( V1) $( OBJCOPY) -I binary -O ihex --adjust-vma= $( EXST_ADJUST_VMA) $( TARGET_BIN) $@
2019-05-07 23:38:31 -07:00
e n d i f
2019-02-25 13:13:38 -08:00
$(TARGET_ELF) : $( TARGET_OBJS ) $( LD_SCRIPT )
2017-12-30 07:28:52 -08:00
@echo " Linking $( TARGET) " " $( STDOUT) "
2019-02-25 13:13:38 -08:00
$( V1) $( CROSS_CC) -o $@ $( filter-out %.ld,$^) $( LD_FLAGS)
2017-12-21 14:20:40 -08:00
$( V1) $( SIZE) $( TARGET_ELF)
2014-08-01 06:12:39 -07:00
# Compile
2019-03-18 15:25:00 -07:00
## compile_file takes two arguments: (1) optimisation description string and (2) optimisation compiler flag
d e f i n e c o m p i l e _ f i l e
echo " %% ( $( 1) ) $< " " $( STDOUT) " && \
$( CROSS_CC) -c -o $@ $( CFLAGS) $( 2) $<
e n d e f
2017-07-20 12:34:04 -07:00
i f e q ( $( DEBUG ) , G D B )
$(OBJECT_DIR)/$(TARGET)/%.o : %.c
$( V1) mkdir -p $( dir $@ )
2019-02-18 07:11:38 -08:00
$( V1) $( if $( findstring $<,$( NOT_OPTIMISED_SRC) ) , \
2019-03-18 15:25:00 -07:00
$( call compile_file,not optimised, $( CC_NO_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
, \
2019-03-18 15:25:00 -07:00
$( call compile_file,debug,$( CC_DEBUG_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
)
2017-07-20 12:34:04 -07:00
e l s e
2014-08-01 06:12:39 -07:00
$(OBJECT_DIR)/$(TARGET)/%.o : %.c
2016-08-10 15:00:26 -07:00
$( V1) mkdir -p $( dir $@ )
2019-02-18 07:11:38 -08:00
$( V1) $( if $( findstring $<,$( NOT_OPTIMISED_SRC) ) , \
2019-03-18 15:25:00 -07:00
$( call compile_file,not optimised,$( CC_NO_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
, \
$( if $( findstring $( subst ./src/main/,,$<) ,$( SPEED_OPTIMISED_SRC) ) , \
2019-03-18 15:25:00 -07:00
$( call compile_file,speed optimised,$( CC_SPEED_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
, \
$( if $( findstring $( subst ./src/main/,,$<) ,$( SIZE_OPTIMISED_SRC) ) , \
2019-03-18 15:25:00 -07:00
$( call compile_file,size optimised,$( CC_SIZE_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
, \
2019-03-18 15:25:00 -07:00
$( call compile_file,optimised,$( CC_DEFAULT_OPTIMISATION) ) \
2019-02-18 07:11:38 -08:00
) \
) \
)
2016-12-13 02:41:29 -08:00
e n d i f
2014-08-01 06:12:39 -07:00
# Assemble
$(OBJECT_DIR)/$(TARGET)/%.o : %.s
2016-08-10 15:00:26 -07:00
$( V1) mkdir -p $( dir $@ )
2017-12-30 07:28:52 -08:00
@echo " %% $( notdir $<) " " $( STDOUT) "
2016-11-09 12:28:14 -08:00
$( V1) $( CROSS_CC) -c -o $@ $( ASFLAGS) $<
2014-10-23 05:10:45 -07:00
2014-09-16 04:46:27 -07:00
$(OBJECT_DIR)/$(TARGET)/%.o : %.S
2016-08-10 15:00:26 -07:00
$( V1) mkdir -p $( dir $@ )
2017-12-30 07:28:52 -08:00
@echo " %% $( notdir $<) " " $( STDOUT) "
2016-11-09 12:28:14 -08:00
$( V1) $( CROSS_CC) -c -o $@ $( ASFLAGS) $<
2014-08-01 06:12:39 -07:00
2015-08-17 05:37:09 -07:00
2019-10-16 06:36:25 -07:00
## all : Build all currently built targets
all : $( CI_TARGETS )
2017-10-17 13:46:02 -07:00
2019-10-16 06:36:25 -07:00
## all_all : Build all targets (including legacy / unsupported)
all_all : $( VALID_TARGETS )
2020-01-15 03:47:51 -08:00
## unified : build all Unified Targets
unified : $( UNIFIED_TARGETS )
## unified_zip : build all Unified Targets as zip files (for posting on GitHub)
2020-05-02 20:55:46 -07:00
unified_zip : $( addsuffix _clean ,$ ( UNIFIED_TARGETS ) ) $( addsuffix _zip ,$ ( UNIFIED_TARGETS ) )
2020-01-15 03:47:51 -08:00
2019-10-16 06:36:25 -07:00
## legacy : Build legacy targets
legacy : $( LEGACY_TARGETS )
2017-10-17 13:46:02 -07:00
## unsupported : Build unsupported targets
unsupported : $( UNSUPPORTED_TARGETS )
2016-06-14 03:28:11 -07:00
2019-02-24 18:56:42 -08:00
## pre-push : The minimum verification that should be run before pushing, to check if CI has a chance of succeeding
pre-push :
2019-06-21 15:50:59 -07:00
$( MAKE) $( addsuffix _clean,$( PRE_PUSH_TARGET_LIST) ) $( PRE_PUSH_TARGET_LIST) EXTRA_FLAGS = -Werror
2019-02-24 18:56:42 -08:00
2017-03-20 14:55:53 -07:00
## targets-group-1 : build some targets
targets-group-1 : $( GROUP_ 1_TARGETS )
## targets-group-2 : build some targets
targets-group-2 : $( GROUP_ 2_TARGETS )
2020-02-17 03:42:08 -08:00
## targets-group-rest: build the rest of the targets (not listed in the other groups)
2017-03-20 14:55:53 -07:00
targets-group-rest : $( GROUP_OTHER_TARGETS )
2016-06-14 03:28:11 -07:00
$(VALID_TARGETS) :
2017-07-20 12:34:04 -07:00
$( V0) @echo " Building $@ " && \
2017-07-17 05:39:25 -07:00
$( MAKE) binary hex TARGET = $@ && \
2017-07-20 12:34:04 -07:00
echo " Building $@ succeeded. "
2018-05-04 08:08:13 -07:00
$(NOBUILD_TARGETS) :
2017-07-20 12:34:04 -07:00
$( MAKE) TARGET = $@
2016-06-12 15:12:21 -07:00
2020-01-15 03:47:51 -08:00
TARGETS_CLEAN = $( addsuffix _clean,$( VALID_TARGETS) )
2016-06-27 09:38:43 -07:00
## clean : clean up temporary / machine-generated files
2014-08-01 06:12:39 -07:00
clean :
2017-12-30 07:28:52 -08:00
@echo " Cleaning $( TARGET) "
2016-08-10 15:00:26 -07:00
$( V0) rm -f $( CLEAN_ARTIFACTS)
$( V0) rm -rf $( OBJECT_DIR) /$( TARGET)
2017-12-30 07:28:52 -08:00
@echo " Cleaning $( TARGET) succeeded. "
2016-06-13 10:45:32 -07:00
2019-06-21 15:50:59 -07:00
## test_clean : clean up temporary / machine-generated files (tests)
test-%_clean :
$( MAKE) test_clean
2019-02-26 00:02:45 -08:00
2019-06-21 15:50:59 -07:00
test_clean :
2016-08-10 15:00:26 -07:00
$( V0) cd src/test && $( MAKE) clean || true
2014-08-01 06:12:39 -07:00
2016-06-27 09:38:43 -07:00
## <TARGET>_clean : clean up one specific target (alias for above)
2017-07-20 12:34:04 -07:00
$(TARGETS_CLEAN) :
2016-08-10 15:00:26 -07:00
$( V0) $( MAKE) -j TARGET = $( subst _clean,,$@ ) clean
2016-06-27 09:38:43 -07:00
## clean_all : clean all valid targets
2019-06-21 15:50:59 -07:00
clean_all : $( TARGETS_CLEAN ) test_clean
2016-06-27 09:38:43 -07:00
2020-01-15 03:47:51 -08:00
TARGETS_FLASH = $( addsuffix _flash,$( VALID_TARGETS) )
2016-06-13 10:45:32 -07:00
2019-06-21 15:50:59 -07:00
## <TARGET>_flash : build and flash a target
$(TARGETS_FLASH) :
2019-11-23 18:27:36 -08:00
$( V0) $( MAKE) hex TARGET = $( subst _flash,,$@ )
2019-06-28 23:38:35 -07:00
i f n e q ( , $( findstring /dev /ttyUSB ,$ ( SERIAL_DEVICE ) ) )
2019-06-21 15:50:59 -07:00
$( V0) $( MAKE) tty_flash TARGET = $( subst _flash,,$@ )
2019-05-12 01:22:22 -07:00
e l s e
2019-06-21 15:50:59 -07:00
$( V0) $( MAKE) dfu_flash TARGET = $( subst _flash,,$@ )
2019-05-12 01:22:22 -07:00
e n d i f
## tty_flash : flash firmware (.hex) onto flight controller via a serial port
tty_flash :
2016-08-10 15:00:26 -07:00
$( V0) stty -F $( SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
2019-05-12 01:22:22 -07:00
$( V0) echo -n 'R' > $( SERIAL_DEVICE)
2016-08-10 15:00:26 -07:00
$( V0) stm32flash -w $( TARGET_HEX) -v -g 0x0 -b 115200 $( SERIAL_DEVICE)
2014-08-01 06:12:39 -07:00
2019-05-12 01:22:22 -07:00
## dfu_flash : flash firmware (.bin) onto flight controller via a DFU mode
dfu_flash :
i f n e q ( n o - p o r t - f o u n d , $( SERIAL_DEVICE ) )
# potentially this is because the MCU already is in DFU mode, try anyway
$( V0) echo -n 'R' > $( SERIAL_DEVICE)
$( V0) sleep 1
e n d i f
2019-11-23 18:27:36 -08:00
$( V0) $( MAKE) $( TARGET_DFU)
$( V0) dfu-util -a 0 -D $( TARGET_DFU) -s :leave
2014-08-01 06:12:39 -07:00
2015-07-19 06:52:15 -07:00
st-flash_$(TARGET) : $( TARGET_BIN )
2016-08-10 15:00:26 -07:00
$( V0) st-flash --reset write $< 0x08000000
2015-07-19 06:52:15 -07:00
2016-06-13 10:45:32 -07:00
## st-flash : flash firmware (.bin) onto flight controller
2015-07-19 06:52:15 -07:00
st-flash : st -flash_ $( TARGET )
2017-07-25 14:24:16 -07:00
i f n e q ( $( OPENOCD_COMMAND ) , )
openocd-gdb : $( TARGET_ELF )
$( V0) $( OPENOCD_COMMAND) & $( CROSS_GDB) $( TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
e n d i f
2020-01-15 03:47:51 -08:00
TARGETS_ZIP = $( addsuffix _zip,$( VALID_TARGETS) )
2020-01-15 03:34:10 -08:00
## <TARGET>_zip : build target and zip it (useful for posting to GitHub)
$(TARGETS_ZIP) :
$( V0) $( MAKE) hex TARGET = $( subst _zip,,$@ )
$( V0) $( MAKE) zip TARGET = $( subst _zip,,$@ )
zip :
$( V0) zip $( TARGET_ZIP) $( TARGET_HEX)
2016-06-24 02:06:40 -07:00
binary :
2016-08-10 15:00:26 -07:00
$( V0) $( MAKE) -j $( TARGET_BIN)
2016-06-24 02:06:40 -07:00
hex :
2016-08-10 15:00:26 -07:00
$( V0) $( MAKE) -j $( TARGET_HEX)
2014-08-01 06:12:39 -07:00
unbrick_$(TARGET) : $( TARGET_HEX )
2016-08-10 15:00:26 -07:00
$( V0) stty -F $( SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
$( V0) stm32flash -w $( TARGET_HEX) -v -g 0x0 -b 115200 $( SERIAL_DEVICE)
2014-08-01 06:12:39 -07:00
2016-06-13 10:45:32 -07:00
## unbrick : unbrick flight controller
2014-08-01 06:12:39 -07:00
unbrick : unbrick_ $( TARGET )
2016-06-13 10:45:32 -07:00
## cppcheck : run static analysis on C source code
2015-08-21 21:23:55 -07:00
cppcheck : $( CSOURCES )
2016-08-10 15:00:26 -07:00
$( V0) $( CPPCHECK)
2015-08-21 21:23:55 -07:00
cppcheck-result.xml : $( CSOURCES )
2016-08-10 15:00:26 -07:00
$( V0) $( CPPCHECK) --xml-version= 2 2> cppcheck-result.xml
2015-08-21 21:23:55 -07:00
2016-10-22 05:47:26 -07:00
# mkdirs
2016-09-02 19:56:15 -07:00
$(DL_DIR) :
mkdir -p $@
$(TOOLS_DIR) :
mkdir -p $@
$(BUILD_DIR) :
mkdir -p $@
2017-03-20 14:12:33 -07:00
## version : print firmware version
version :
@echo $( FC_VER)
2016-06-13 10:45:32 -07:00
## help : print this help message and exit
2016-10-22 05:47:26 -07:00
help : Makefile make /tools .mk
2017-12-30 07:28:52 -08:00
@echo ""
@echo " Makefile for the $( FORKNAME) firmware "
@echo ""
@echo "Usage:"
@echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
@echo "Or:"
@echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
@echo ""
@echo " Valid TARGET values are: $( VALID_TARGETS) "
@echo ""
@sed -n 's/^## //p' $?
2014-10-23 05:10:45 -07:00
2016-06-13 10:45:32 -07:00
## targets : print a list of all valid target platforms (for consumption by scripts)
2016-06-12 15:12:21 -07:00
targets :
2017-12-30 07:28:52 -08:00
@echo " Valid targets: $( VALID_TARGETS) "
2019-10-16 06:36:25 -07:00
@echo " Built targets: $( CI_TARGETS) "
2020-01-15 03:47:51 -08:00
@echo " Unified targets: $( UNIFIED_TARGETS) "
2019-10-16 06:36:25 -07:00
@echo " Legacy targets: $( LEGACY_TARGETS) "
2017-12-30 07:28:52 -08:00
@echo " Unsupported targets: $( UNSUPPORTED_TARGETS) "
@echo " Target: $( TARGET) "
@echo " Base target: $( BASE_TARGET) "
@echo " targets-group-1: $( GROUP_1_TARGETS) "
@echo " targets-group-2: $( GROUP_2_TARGETS) "
@echo " targets-group-rest: $( GROUP_OTHER_TARGETS) "
2016-06-12 15:12:21 -07:00
2018-07-16 09:10:03 -07:00
@echo " targets-group-1: $( words $( GROUP_1_TARGETS) ) targets "
@echo " targets-group-2: $( words $( GROUP_2_TARGETS) ) targets "
@echo " targets-group-rest: $( words $( GROUP_OTHER_TARGETS) ) targets "
2019-10-16 06:36:25 -07:00
@echo " total in all groups $( words $( CI_TARGETS) ) targets "
2018-07-16 09:10:03 -07:00
2018-07-21 16:24:07 -07:00
## target-mcu : print the MCU type of the target
target-mcu :
@echo $( TARGET_MCU)
## targets-by-mcu : make all targets that have a MCU_TYPE mcu
targets-by-mcu :
2019-10-16 06:36:25 -07:00
$( V1) for target in $$ { TARGETS} ; do \
2018-07-21 16:24:07 -07:00
TARGET_MCU_TYPE = $$ ( $( MAKE) -s TARGET = $$ { target} target-mcu) ; \
if [ " $$ {TARGET_MCU_TYPE} " = " $$ {MCU_TYPE} " ] ; then \
2019-01-18 18:09:26 -08:00
if [ " $$ {DO_BUILD} " = 1 ] ; then \
echo " Building target $$ {target}... " ; \
$( MAKE) TARGET = $$ { target} ; \
if [ $$ ? -ne 0 ] ; then \
echo " Building target $$ {target} failed, aborting. " ; \
exit 1; \
fi ; \
else \
echo -n " $$ {target} " ; \
2018-07-21 16:24:07 -07:00
fi ; \
fi ; \
done
2019-01-18 18:09:26 -08:00
@echo
2018-07-21 16:24:07 -07:00
## targets-f3 : make all F3 targets
targets-f3 :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F3 TARGETS = " $( VALID_TARGETS) " DO_BUILD = 1
2019-01-18 18:09:26 -08:00
targets-f3-print :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F3 TARGETS = " $( VALID_TARGETS) "
2018-07-21 16:24:07 -07:00
## targets-f4 : make all F4 targets
targets-f4 :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F4 TARGETS = " $( VALID_TARGETS) " DO_BUILD = 1
2019-01-18 18:09:26 -08:00
targets-f4-print :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F4 TARGETS = " $( VALID_TARGETS) "
targets-ci-f4-print :
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F4 TARGETS = " $( CI_TARGETS) "
2018-07-21 16:24:07 -07:00
## targets-f7 : make all F7 targets
targets-f7 :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F7 TARGETS = " $( VALID_TARGETS) " DO_BUILD = 1
2019-01-18 18:09:26 -08:00
targets-f7-print :
2019-10-16 06:36:25 -07:00
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F7 TARGETS = " $( VALID_TARGETS) "
targets-ci-f7-print :
$( V1) $( MAKE) -s targets-by-mcu MCU_TYPE = STM32F7 TARGETS = " $( CI_TARGETS) "
2018-07-21 16:24:07 -07:00
2019-01-27 03:05:52 -08:00
## test : run the Betaflight test suite
## junittest : run the Betaflight test suite, producing Junit XML result files.
## 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)
2019-02-02 18:54:26 -08:00
## test-all: run the Betaflight test suite including all per-target expanded tests
test junittest test-all test-representative :
2016-11-09 12:28:14 -08:00
$( V0) cd src/test && $( MAKE) $@
2015-08-17 06:02:19 -07:00
2019-01-27 23:36:33 -08:00
## test_help : print the help message for the test suite (including a list of the available tests)
test_help :
$( V0) cd src/test && $( MAKE) help
2020-05-31 20:30:18 -07:00
## test_versions : print the compiler versions used for the test suite
test_versions :
$( V0) cd src/test && $( MAKE) versions
2019-01-27 23:36:33 -08:00
## test_% : run test 'test_%' from the test suite
test_% :
$( V0) cd src/test && $( MAKE) $@
2018-05-12 02:56:58 -07:00
2014-10-23 05:10:45 -07:00
# rebuild everything when makefile changes
2019-02-18 07:12:53 -08:00
$(TARGET_OBJS) : Makefile $( TARGET_DIR ) /target .mk $( wildcard make /*)
2019-01-18 03:36:58 -08:00
2014-10-23 05:10:45 -07:00
# include auto-generated dependencies
- i n c l u d e $( TARGET_DEPS )