Conditionalize SimonK handling from escserial and drop it from F3
[betaflight.git] / Makefile
blob035fc2943a271c2f4d119c2036cd4a51206f6433
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
22 OPTIONS ?=
24 # compile for OpenPilot BootLoader support
25 OPBL ?= no
27 # Debugger optons:
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
31 DEBUG ?=
33 # Insert the debugging hardfault debugger
34 # releases should not be built with this flag as it does not disable pwm output
35 DEBUG_HARDFAULTS ?=
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.
41 FLASH_SIZE ?=
44 ###############################################################################
45 # Things that need to be maintained as the source changes
48 FORKNAME = betaflight
50 # Working directories
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
61 ## V=0 Low
62 ## V=1 High
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
73 ifndef TOOLS_DIR
74 TOOLS_DIR := $(ROOT)/tools
75 endif
76 BUILD_DIR := $(ROOT)/build
77 DL_DIR := $(ROOT)/downloads
79 export RM := rm
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
88 HSE_VALUE ?= 8000000
90 # used for turning on features like VCP and SDCARD
91 FEATURES =
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')
112 LD_FLAGS :=
115 # Default Tool options - can be overridden in {mcu}.mk files.
117 ifeq ($(DEBUG),GDB)
118 OPTIMISE_DEFAULT := -Og
120 LTO_FLAGS := $(OPTIMISE_DEFAULT)
121 DEBUG_FLAGS = -ggdb3 -DDEBUG
122 else
123 ifeq ($(DEBUG),INFO)
124 DEBUG_FLAGS = -ggdb3
125 endif
126 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math
127 OPTIMISE_DEFAULT := -O2
128 OPTIMISE_SPEED := -Ofast
129 OPTIMISE_SIZE := -Os
131 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
132 endif
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)
147 else
148 $(error FLASH_SIZE not configured for target $(TARGET))
149 endif
150 endif
152 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
154 ifneq ($(HSE_VALUE),)
155 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
156 endif
158 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
159 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
161 ifeq ($(OPBL),yes)
162 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
163 .DEFAULT_GOAL := binary
164 else
165 .DEFAULT_GOAL := hex
166 endif
168 INCLUDE_DIRS := $(INCLUDE_DIRS) \
169 $(ROOT)/lib/main/MAVLink
171 INCLUDE_DIRS := $(INCLUDE_DIRS) \
172 $(TARGET_DIR)
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
183 CCACHE := ccache
184 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
185 ifneq ($(RESULT),0)
186 CCACHE :=
187 endif
189 # Tool names
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
198 # Tool options.
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)) \
208 $(DEBUG_FLAGS) \
209 -std=gnu11 \
210 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
211 -ffunction-sections \
212 -fdata-sections \
213 -fno-common \
214 -pedantic \
215 $(DEVICE_FLAGS) \
216 -D_GNU_SOURCE \
217 -DUSE_STDPERIPH_DRIVER \
218 -D$(TARGET) \
219 $(TARGET_FLAGS) \
220 -D'__FORKNAME__="$(FORKNAME)"' \
221 -D'__TARGET__="$(TARGET)"' \
222 -D'__REVISION__="$(REVISION)"' \
223 -save-temps=obj \
224 -MMD -MP \
225 $(EXTRA_FLAGS)
227 ASFLAGS = $(ARCH_FLAGS) \
228 $(DEBUG_FLAGS) \
229 -x assembler-with-cpp \
230 $(addprefix -I,$(INCLUDE_DIRS)) \
231 -MMD -MP
233 ifeq ($(LD_FLAGS),)
234 LD_FLAGS = -lm \
235 -nostartfiles \
236 --specs=nano.specs \
237 -lc \
238 -lnosys \
239 $(ARCH_FLAGS) \
240 $(LTO_FLAGS) \
241 $(DEBUG_FLAGS) \
242 -static \
243 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
244 -Wl,-L$(LINKER_DIR) \
245 -Wl,--cref \
246 -Wl,--no-wchar-size-warning \
247 -Wl,--print-memory-usage \
248 -T$(LD_SCRIPT)
249 endif
251 ###############################################################################
252 # No user-serviceable parts below
253 ###############################################################################
255 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
256 --std=c99 --inline-suppr --quiet --force \
257 $(addprefix -I,$(INCLUDE_DIRS)) \
258 -I/usr/include -I/usr/include/linux
261 # Things we will build
263 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
264 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
265 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
266 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
267 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
268 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
269 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
272 CLEAN_ARTIFACTS := $(TARGET_BIN)
273 CLEAN_ARTIFACTS += $(TARGET_HEX)
274 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
275 CLEAN_ARTIFACTS += $(TARGET_LST)
277 # Make sure build date and revision is updated on every incremental build
278 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
280 # List of buildable ELF files and their object dependencies.
281 # It would be nice to compute these lists, but that seems to be just beyond make.
283 $(TARGET_LST): $(TARGET_ELF)
284 $(V0) $(OBJDUMP) -S --disassemble $< > $@
286 $(TARGET_HEX): $(TARGET_ELF)
287 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
288 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
290 $(TARGET_BIN): $(TARGET_ELF)
291 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
292 $(V1) $(OBJCOPY) -O binary $< $@
294 $(TARGET_ELF): $(TARGET_OBJS)
295 @echo "Linking $(TARGET)" "$(STDOUT)"
296 $(V1) $(CROSS_CC) -o $@ $^ $(LD_FLAGS)
297 $(V1) $(SIZE) $(TARGET_ELF)
299 # Compile
300 ifeq ($(DEBUG),GDB)
301 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
302 $(V1) mkdir -p $(dir $@)
303 $(V1) echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
304 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<
305 else
306 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
307 $(V1) mkdir -p $(dir $@)
308 $(V1) $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
309 echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
310 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
311 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
312 echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
313 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
314 echo "%% $(notdir $<)" "$(STDOUT)" && \
315 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))
316 endif
318 # Assemble
319 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
320 $(V1) mkdir -p $(dir $@)
321 @echo "%% $(notdir $<)" "$(STDOUT)"
322 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
324 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
325 $(V1) mkdir -p $(dir $@)
326 @echo "%% $(notdir $<)" "$(STDOUT)"
327 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
330 ## all : Build all targets (excluding unsupported)
331 all: $(SUPPORTED_TARGETS)
333 ## all_with_unsupported : Build all targets (including unsupported)
334 all_with_unsupported: $(VALID_TARGETS)
336 ## unsupported : Build unsupported targets
337 unsupported: $(UNSUPPORTED_TARGETS)
339 ## official : Build all official (travis) targets
340 official: $(OFFICIAL_TARGETS)
342 ## targets-group-1 : build some targets
343 targets-group-1: $(GROUP_1_TARGETS)
345 ## targets-group-2 : build some targets
346 targets-group-2: $(GROUP_2_TARGETS)
348 ## targets-group-3 : build some targets
349 targets-group-3: $(GROUP_3_TARGETS)
351 ## targets-group-3 : build some targets
352 targets-group-4: $(GROUP_4_TARGETS)
354 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
355 targets-group-rest: $(GROUP_OTHER_TARGETS)
357 $(VALID_TARGETS):
358 $(V0) @echo "Building $@" && \
359 $(MAKE) binary hex TARGET=$@ && \
360 echo "Building $@ succeeded."
362 $(NOBUILD_TARGETS):
363 $(MAKE) TARGET=$@
365 CLEAN_TARGETS = $(addprefix clean_,$(VALID_TARGETS) )
366 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS) )
368 ## clean : clean up temporary / machine-generated files
369 clean:
370 @echo "Cleaning $(TARGET)"
371 $(V0) rm -f $(CLEAN_ARTIFACTS)
372 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
373 @echo "Cleaning $(TARGET) succeeded."
375 ## clean_test : clean up temporary / machine-generated files (tests)
376 clean_test:
377 $(V0) cd src/test && $(MAKE) clean || true
379 ## clean_<TARGET> : clean up one specific target
380 $(CLEAN_TARGETS):
381 $(V0) $(MAKE) -j TARGET=$(subst clean_,,$@) clean
383 ## <TARGET>_clean : clean up one specific target (alias for above)
384 $(TARGETS_CLEAN):
385 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
387 ## clean_all : clean all valid targets
388 clean_all: $(CLEAN_TARGETS)
390 ## all_clean : clean all valid targets (alias for above)
391 all_clean: $(TARGETS_CLEAN)
394 flash_$(TARGET): $(TARGET_HEX)
395 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
396 $(V0) echo -n 'R' >$(SERIAL_DEVICE)
397 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
399 ## flash : flash firmware (.hex) onto flight controller
400 flash: flash_$(TARGET)
402 st-flash_$(TARGET): $(TARGET_BIN)
403 $(V0) st-flash --reset write $< 0x08000000
405 ## st-flash : flash firmware (.bin) onto flight controller
406 st-flash: st-flash_$(TARGET)
408 ifneq ($(OPENOCD_COMMAND),)
409 openocd-gdb: $(TARGET_ELF)
410 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
411 endif
413 binary:
414 $(V0) $(MAKE) -j $(TARGET_BIN)
416 hex:
417 $(V0) $(MAKE) -j $(TARGET_HEX)
419 unbrick_$(TARGET): $(TARGET_HEX)
420 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
421 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
423 ## unbrick : unbrick flight controller
424 unbrick: unbrick_$(TARGET)
426 ## cppcheck : run static analysis on C source code
427 cppcheck: $(CSOURCES)
428 $(V0) $(CPPCHECK)
430 cppcheck-result.xml: $(CSOURCES)
431 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
433 # mkdirs
434 $(DL_DIR):
435 mkdir -p $@
437 $(TOOLS_DIR):
438 mkdir -p $@
440 $(BUILD_DIR):
441 mkdir -p $@
443 ## version : print firmware version
444 version:
445 @echo $(FC_VER)
447 ## help : print this help message and exit
448 help: Makefile make/tools.mk
449 @echo ""
450 @echo "Makefile for the $(FORKNAME) firmware"
451 @echo ""
452 @echo "Usage:"
453 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
454 @echo "Or:"
455 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
456 @echo ""
457 @echo "Valid TARGET values are: $(VALID_TARGETS)"
458 @echo ""
459 @sed -n 's/^## //p' $?
461 ## targets : print a list of all valid target platforms (for consumption by scripts)
462 targets:
463 @echo "Valid targets: $(VALID_TARGETS)"
464 @echo "Supported targets: $(SUPPORTED_TARGETS)"
465 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
466 @echo "Target: $(TARGET)"
467 @echo "Base target: $(BASE_TARGET)"
468 @echo "targets-group-1: $(GROUP_1_TARGETS)"
469 @echo "targets-group-2: $(GROUP_2_TARGETS)"
470 @echo "targets-group-3: $(GROUP_3_TARGETS)"
471 @echo "targets-group-4: $(GROUP_4_TARGETS)"
472 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
474 @echo "targets-group-1: $(words $(GROUP_1_TARGETS)) targets"
475 @echo "targets-group-2: $(words $(GROUP_2_TARGETS)) targets"
476 @echo "targets-group-3: $(words $(GROUP_3_TARGETS)) targets"
477 @echo "targets-group-4: $(words $(GROUP_4_TARGETS)) targets"
478 @echo "targets-group-rest: $(words $(GROUP_OTHER_TARGETS)) targets"
479 @echo "total in all groups $(words $(SUPPORTED_TARGETS)) targets"
481 ## target-mcu : print the MCU type of the target
482 target-mcu:
483 @echo $(TARGET_MCU)
485 ## targets-by-mcu : make all targets that have a MCU_TYPE mcu
486 targets-by-mcu:
487 @echo "Building all $(MCU_TYPE) targets..."
488 $(V1) for target in $(VALID_TARGETS); do \
489 TARGET_MCU_TYPE=$$($(MAKE) -s TARGET=$${target} target-mcu); \
490 if [ "$${TARGET_MCU_TYPE}" = "$${MCU_TYPE}" ]; then \
491 echo "Building target $${target}..."; \
492 $(MAKE) TARGET=$${target}; \
493 if [ $$? -ne 0 ]; then \
494 echo "Building target $${target} failed, aborting."; \
495 exit 1; \
496 fi; \
497 fi; \
498 done
500 ## targets-f3 : make all F3 targets
501 targets-f3:
502 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F3
504 ## targets-f4 : make all F4 targets
505 targets-f4:
506 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F4
508 ## targets-f7 : make all F7 targets
509 targets-f7:
510 $(V1) $(MAKE) -s targets-by-mcu MCU_TYPE=STM32F7
512 ## test : run the cleanflight test suite
513 ## junittest : run the cleanflight test suite, producing Junit XML result files.
514 test junittest:
515 $(V0) cd src/test && $(MAKE) $@
518 check-target-independence:
519 $(V1) for test_target in $(VALID_TARGETS); do \
520 FOUND=$$(grep -rE "\W$${test_target}\W?" src/main | grep -vE "(//)|(/\*).*\W$${test_target}\W?" | grep -vE "^src/main/target"); \
521 if [ "$${FOUND}" != "" ]; then \
522 echo "Target dependencies found:"; \
523 echo "$${FOUND}"; \
524 exit 1; \
525 fi; \
526 done
528 check-fastram-usage-correctness:
529 $(V1) NON_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM_ZERO_INIT\W.*=.*" src/main/ | grep -Ev "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
530 if [ "$${NON_TRIVIALLY_INITIALIZED}" != "" ]; then \
531 echo "Non-trivially initialized FAST_RAM_ZERO_INIT variables found, use FAST_RAM instead:"; \
532 echo "$${NON_TRIVIALLY_INITIALIZED}"; \
533 exit 1; \
534 fi; \
535 TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -v "="); \
536 EXPLICITLY_TRIVIALLY_INITIALIZED=$$(grep -Ern "\W?FAST_RAM\W.*;" src/main/ | grep -E "=\s*(false|NULL|0(\.0*f?)?)\s*[,;]"); \
537 if [ "$${TRIVIALLY_INITIALIZED}$${EXPLICITLY_TRIVIALLY_INITIALIZED}" != "" ]; then \
538 echo "Trivially initialized FAST_RAM variables found, use FAST_RAM_ZERO_INIT instead to save FLASH:"; \
539 echo "$${TRIVIALLY_INITIALIZED}\n$${EXPLICITLY_TRIVIALLY_INITIALIZED}"; \
540 exit 1; \
543 # rebuild everything when makefile changes
544 $(TARGET_OBJS) : Makefile
546 # include auto-generated dependencies
547 -include $(TARGET_DEPS)