Implement minimalistic strcasestr (#5411)
[betaflight.git] / Makefile
blob96f214de837a7c1c5991ec40522c6f4f672c5177
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, must be empty or GDB
28 DEBUG ?=
30 # Insert the debugging hardfault debugger
31 # releases should not be built with this flag as it does not disable pwm output
32 DEBUG_HARDFAULTS ?=
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.
38 FLASH_SIZE ?=
41 ###############################################################################
42 # Things that need to be maintained as the source changes
45 FORKNAME = betaflight
47 # Working directories
48 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
49 SRC_DIR := $(ROOT)/src/main
50 OBJECT_DIR := $(ROOT)/obj/main
51 BIN_DIR := $(ROOT)/obj
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
58 ## V=0 Low
59 ## V=1 High
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
70 ifndef TOOLS_DIR
71 TOOLS_DIR := $(ROOT)/tools
72 endif
73 BUILD_DIR := $(ROOT)/build
74 DL_DIR := $(ROOT)/downloads
76 export RM := rm
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
85 HSE_VALUE ?= 8000000
87 # used for turning on features like VCP and SDCARD
88 FEATURES =
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')
109 LD_FLAGS :=
112 # Default Tool options - can be overridden in {mcu}.mk files.
114 ifeq ($(DEBUG),GDB)
115 OPTIMISE_DEFAULT := -Og
117 LTO_FLAGS := $(OPTIMISE_DEFAULT)
118 DEBUG_FLAGS = -ggdb3 -DDEBUG
119 else
120 OPTIMISATION_BASE := -flto -fuse-linker-plugin -ffast-math
121 OPTIMISE_DEFAULT := -O2
122 OPTIMISE_SPEED := -Ofast
123 OPTIMISE_SIZE := -Os
125 LTO_FLAGS := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
126 endif
128 VPATH := $(VPATH):$(ROOT)/make/mcu
129 VPATH := $(VPATH):$(ROOT)/make
131 # start specific includes
132 include $(ROOT)/make/mcu/$(TARGET_MCU).mk
134 # openocd specific includes
135 include $(ROOT)/make/openocd.mk
137 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
138 ifeq ($(FLASH_SIZE),)
139 ifneq ($(TARGET_FLASH),)
140 FLASH_SIZE := $(TARGET_FLASH)
141 else
142 $(error FLASH_SIZE not configured for target $(TARGET))
143 endif
144 endif
146 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
148 ifneq ($(HSE_VALUE),)
149 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
150 endif
152 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
153 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
155 ifeq ($(OPBL),yes)
156 TARGET_FLAGS := -DOPBL $(TARGET_FLAGS)
157 .DEFAULT_GOAL := binary
158 else
159 .DEFAULT_GOAL := hex
160 endif
162 INCLUDE_DIRS := $(INCLUDE_DIRS) \
163 $(ROOT)/lib/main/MAVLink
165 INCLUDE_DIRS := $(INCLUDE_DIRS) \
166 $(TARGET_DIR)
168 VPATH := $(VPATH):$(TARGET_DIR)
170 include $(ROOT)/make/source.mk
172 ###############################################################################
173 # Things that might need changing to use different tools
176 # Find out if ccache is installed on the system
177 CCACHE := ccache
178 RESULT = $(shell (which $(CCACHE) > /dev/null 2>&1; echo $$?) )
179 ifneq ($(RESULT),0)
180 CCACHE :=
181 endif
183 # Tool names
184 CROSS_CC := $(CCACHE) $(ARM_SDK_PREFIX)gcc
185 CROSS_CXX := $(CCACHE) $(ARM_SDK_PREFIX)g++
186 CROSS_GDB := $(ARM_SDK_PREFIX)gdb
187 OBJCOPY := $(ARM_SDK_PREFIX)objcopy
188 OBJDUMP := $(ARM_SDK_PREFIX)objdump
189 SIZE := $(ARM_SDK_PREFIX)size
192 # Tool options.
194 CC_DEBUG_OPTIMISATION := $(OPTIMISE_DEFAULT)
195 CC_DEFAULT_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_DEFAULT)
196 CC_SPEED_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SPEED)
197 CC_SIZE_OPTIMISATION := $(OPTIMISATION_BASE) $(OPTIMISE_SIZE)
199 CFLAGS += $(ARCH_FLAGS) \
200 $(addprefix -D,$(OPTIONS)) \
201 $(addprefix -I,$(INCLUDE_DIRS)) \
202 $(DEBUG_FLAGS) \
203 -std=gnu99 \
204 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
205 -ffunction-sections \
206 -fdata-sections \
207 -pedantic \
208 $(DEVICE_FLAGS) \
209 -D_GNU_SOURCE \
210 -DUSE_STDPERIPH_DRIVER \
211 -D$(TARGET) \
212 $(TARGET_FLAGS) \
213 -D'__FORKNAME__="$(FORKNAME)"' \
214 -D'__TARGET__="$(TARGET)"' \
215 -D'__REVISION__="$(REVISION)"' \
216 -save-temps=obj \
217 -MMD -MP \
218 $(EXTRA_FLAGS)
220 ASFLAGS = $(ARCH_FLAGS) \
221 -x assembler-with-cpp \
222 $(addprefix -I,$(INCLUDE_DIRS)) \
223 -MMD -MP
225 ifeq ($(LD_FLAGS),)
226 LD_FLAGS = -lm \
227 -nostartfiles \
228 --specs=nano.specs \
229 -lc \
230 -lnosys \
231 $(ARCH_FLAGS) \
232 $(LTO_FLAGS) \
233 $(DEBUG_FLAGS) \
234 -static \
235 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
236 -Wl,-L$(LINKER_DIR) \
237 -Wl,--cref \
238 -Wl,--no-wchar-size-warning \
239 -T$(LD_SCRIPT)
240 endif
242 ###############################################################################
243 # No user-serviceable parts below
244 ###############################################################################
246 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
247 --std=c99 --inline-suppr --quiet --force \
248 $(addprefix -I,$(INCLUDE_DIRS)) \
249 -I/usr/include -I/usr/include/linux
252 # Things we will build
254 TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).bin
255 TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET).hex
256 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
257 TARGET_LST = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).lst
258 TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
259 TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(SRC))))
260 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
263 CLEAN_ARTIFACTS := $(TARGET_BIN)
264 CLEAN_ARTIFACTS += $(TARGET_HEX)
265 CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP)
266 CLEAN_ARTIFACTS += $(TARGET_LST)
268 # Make sure build date and revision is updated on every incremental build
269 $(OBJECT_DIR)/$(TARGET)/build/version.o : $(SRC)
271 # List of buildable ELF files and their object dependencies.
272 # It would be nice to compute these lists, but that seems to be just beyond make.
274 $(TARGET_LST): $(TARGET_ELF)
275 $(V0) $(OBJDUMP) -S --disassemble $< > $@
277 $(TARGET_HEX): $(TARGET_ELF)
278 @echo "Creating HEX $(TARGET_HEX)" "$(STDOUT)"
279 $(V1) $(OBJCOPY) -O ihex --set-start 0x8000000 $< $@
281 $(TARGET_BIN): $(TARGET_ELF)
282 @echo "Creating BIN $(TARGET_BIN)" "$(STDOUT)"
283 $(V1) $(OBJCOPY) -O binary $< $@
285 $(TARGET_ELF): $(TARGET_OBJS)
286 @echo "Linking $(TARGET)" "$(STDOUT)"
287 $(V1) $(CROSS_CC) -o $@ $^ $(LD_FLAGS)
288 $(V1) $(SIZE) $(TARGET_ELF)
290 # Compile
291 ifeq ($(DEBUG),GDB)
292 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
293 $(V1) mkdir -p $(dir $@)
294 $(V1) echo "%% (debug) $(notdir $<)" "$(STDOUT)" && \
295 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEBUG_OPTIMISATION) $<
296 else
297 $(OBJECT_DIR)/$(TARGET)/%.o: %.c
298 $(V1) mkdir -p $(dir $@)
299 $(V1) $(if $(findstring $(subst ./src/main/,,$<),$(SPEED_OPTIMISED_SRC)), \
300 echo "%% (speed optimised) $(notdir $<)" "$(STDOUT)" && \
301 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SPEED_OPTIMISATION) $<, \
302 $(if $(findstring $(subst ./src/main/,,$<),$(SIZE_OPTIMISED_SRC)), \
303 echo "%% (size optimised) $(notdir $<)" "$(STDOUT)" && \
304 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_SIZE_OPTIMISATION) $<, \
305 echo "%% $(notdir $<)" "$(STDOUT)" && \
306 $(CROSS_CC) -c -o $@ $(CFLAGS) $(CC_DEFAULT_OPTIMISATION) $<))
307 endif
309 # Assemble
310 $(OBJECT_DIR)/$(TARGET)/%.o: %.s
311 $(V1) mkdir -p $(dir $@)
312 @echo "%% $(notdir $<)" "$(STDOUT)"
313 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
315 $(OBJECT_DIR)/$(TARGET)/%.o: %.S
316 $(V1) mkdir -p $(dir $@)
317 @echo "%% $(notdir $<)" "$(STDOUT)"
318 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
321 ## all : Build all targets (excluding unsupported)
322 all: $(SUPPORTED_TARGETS)
324 ## all_with_unsupported : Build all targets (excluding unsupported)
325 all_with_unsupported: $(VALID_TARGETS)
327 ## unsupported : Build unsupported targets
328 unsupported: $(UNSUPPORTED_TARGETS)
330 ## official : Build all official (travis) targets
331 official: $(OFFICIAL_TARGETS)
333 ## targets-group-1 : build some targets
334 targets-group-1: $(GROUP_1_TARGETS)
336 ## targets-group-2 : build some targets
337 targets-group-2: $(GROUP_2_TARGETS)
339 ## targets-group-3 : build some targets
340 targets-group-3: $(GROUP_3_TARGETS)
342 ## targets-group-3 : build some targets
343 targets-group-4: $(GROUP_4_TARGETS)
345 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
346 targets-group-rest: $(GROUP_OTHER_TARGETS)
348 $(VALID_TARGETS):
349 $(V0) @echo "Building $@" && \
350 $(MAKE) binary hex TARGET=$@ && \
351 echo "Building $@ succeeded."
353 $(SKIP_TARGETS):
354 $(MAKE) TARGET=$@
356 CLEAN_TARGETS = $(addprefix clean_,$(VALID_TARGETS) )
357 TARGETS_CLEAN = $(addsuffix _clean,$(VALID_TARGETS) )
359 ## clean : clean up temporary / machine-generated files
360 clean:
361 @echo "Cleaning $(TARGET)"
362 $(V0) rm -f $(CLEAN_ARTIFACTS)
363 $(V0) rm -rf $(OBJECT_DIR)/$(TARGET)
364 @echo "Cleaning $(TARGET) succeeded."
366 ## clean_test : clean up temporary / machine-generated files (tests)
367 clean_test:
368 $(V0) cd src/test && $(MAKE) clean || true
370 ## clean_<TARGET> : clean up one specific target
371 $(CLEAN_TARGETS):
372 $(V0) $(MAKE) -j TARGET=$(subst clean_,,$@) clean
374 ## <TARGET>_clean : clean up one specific target (alias for above)
375 $(TARGETS_CLEAN):
376 $(V0) $(MAKE) -j TARGET=$(subst _clean,,$@) clean
378 ## clean_all : clean all valid targets
379 clean_all: $(CLEAN_TARGETS)
381 ## all_clean : clean all valid targets (alias for above)
382 all_clean: $(TARGETS_CLEAN)
385 flash_$(TARGET): $(TARGET_HEX)
386 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
387 $(V0) echo -n 'R' >$(SERIAL_DEVICE)
388 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
390 ## flash : flash firmware (.hex) onto flight controller
391 flash: flash_$(TARGET)
393 st-flash_$(TARGET): $(TARGET_BIN)
394 $(V0) st-flash --reset write $< 0x08000000
396 ## st-flash : flash firmware (.bin) onto flight controller
397 st-flash: st-flash_$(TARGET)
399 ifneq ($(OPENOCD_COMMAND),)
400 openocd-gdb: $(TARGET_ELF)
401 $(V0) $(OPENOCD_COMMAND) & $(CROSS_GDB) $(TARGET_ELF) -ex "target remote localhost:3333" -ex "load"
402 endif
404 binary:
405 $(V0) $(MAKE) -j $(TARGET_BIN)
407 hex:
408 $(V0) $(MAKE) -j $(TARGET_HEX)
410 unbrick_$(TARGET): $(TARGET_HEX)
411 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
412 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
414 ## unbrick : unbrick flight controller
415 unbrick: unbrick_$(TARGET)
417 ## cppcheck : run static analysis on C source code
418 cppcheck: $(CSOURCES)
419 $(V0) $(CPPCHECK)
421 cppcheck-result.xml: $(CSOURCES)
422 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
424 # mkdirs
425 $(DL_DIR):
426 mkdir -p $@
428 $(TOOLS_DIR):
429 mkdir -p $@
431 $(BUILD_DIR):
432 mkdir -p $@
434 ## version : print firmware version
435 version:
436 @echo $(FC_VER)
438 ## help : print this help message and exit
439 help: Makefile make/tools.mk
440 @echo ""
441 @echo "Makefile for the $(FORKNAME) firmware"
442 @echo ""
443 @echo "Usage:"
444 @echo " make [V=<verbosity>] [TARGET=<target>] [OPTIONS=\"<options>\"]"
445 @echo "Or:"
446 @echo " make <target> [V=<verbosity>] [OPTIONS=\"<options>\"]"
447 @echo ""
448 @echo "Valid TARGET values are: $(VALID_TARGETS)"
449 @echo ""
450 @sed -n 's/^## //p' $?
452 ## targets : print a list of all valid target platforms (for consumption by scripts)
453 targets:
454 @echo "Valid targets: $(VALID_TARGETS)"
455 @echo "Supported targets: $(SUPPORTED_TARGETS)"
456 @echo "Unsupported targets: $(UNSUPPORTED_TARGETS)"
457 @echo "Target: $(TARGET)"
458 @echo "Base target: $(BASE_TARGET)"
459 @echo "targets-group-1: $(GROUP_1_TARGETS)"
460 @echo "targets-group-2: $(GROUP_2_TARGETS)"
461 @echo "targets-group-3: $(GROUP_3_TARGETS)"
462 @echo "targets-group-4: $(GROUP_4_TARGETS)"
463 @echo "targets-group-rest: $(GROUP_OTHER_TARGETS)"
465 ## test : run the cleanflight test suite
466 ## junittest : run the cleanflight test suite, producing Junit XML result files.
467 test junittest:
468 $(V0) cd src/test && $(MAKE) $@
470 # rebuild everything when makefile changes
471 $(TARGET_OBJS) : Makefile
473 # include auto-generated dependencies
474 -include $(TARGET_DEPS)