Merge pull request #4446 from iNavFlight/dzikuvx-mag-gain-calibration
[inav.git] / Makefile
blob5b95e5a6acb1f87a7ef5886ccf7563c601156fe5
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 iNav firmware.
10 # Invoke this with 'make help' to see the list of supported targets.
12 ###############################################################################
14 # Root directory
15 ROOT := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
17 # developer preferences, edit these at will, they'll be gitignored
18 -include $(ROOT)/make/local.mk
20 # Things that the user might override on the commandline
23 # The target to build, see VALID_TARGETS below
24 TARGET ?= REVO
26 # Compile-time options
27 OPTIONS ?=
29 # Debugger optons, must be empty or GDB
30 DEBUG ?=
32 SEMIHOSTING ?=
34 # Build suffix
35 BUILD_SUFFIX ?=
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 ?=
43 ## V : Set verbosity level based on the V= parameter
44 ## V=0 Low
45 ## V=1 High
46 export AT := @
48 ifndef V
49 export V0 :=
50 export V1 := $(AT)
51 export STDOUT :=
52 else ifeq ($(V), 0)
53 export V0 := $(AT)
54 export V1 := $(AT)
55 export STDOUT:= "> /dev/null"
56 export MAKE := $(MAKE) --no-print-directory
57 else ifeq ($(V), 1)
58 export V0 :=
59 export V1 :=
60 export STDOUT :=
61 endif
63 ###############################################################################
64 # Things that need to be maintained as the source changes
67 # Working directories
68 SRC_DIR := $(ROOT)/src/main
69 BL_SRC_DIR := $(ROOT)/src/bl
70 OBJECT_DIR := $(ROOT)/obj/main
71 BL_OBJECT_DIR := $(ROOT)/obj/bl
72 BIN_DIR := $(ROOT)/obj
73 CMSIS_DIR := $(ROOT)/lib/main/CMSIS
74 INCLUDE_DIRS := $(SRC_DIR) \
75 $(ROOT)/src/main/target
76 LINKER_DIR := $(ROOT)/src/main/target/link
78 # import macros common to all supported build systems
79 include $(ROOT)/make/system-id.mk
81 # default xtal value for F4 targets
82 HSE_VALUE = 8000000
83 MHZ_VALUE ?=
85 # used for turning on features like VCP and SDCARD
86 FEATURES =
88 include $(ROOT)/make/version.mk
89 include $(ROOT)/make/targets.mk
91 BUILD_DATE = $(shell date +%Y%m%d)
93 # Search path for sources
94 FATFS_DIR = $(ROOT)/lib/main/FatFS
95 FATFS_SRC = $(notdir $(wildcard $(FATFS_DIR)/*.c))
97 VPATH := $(SRC_DIR):$(SRC_DIR)/startup
98 VPATH := $(VPATH):$(ROOT)/make/mcu
99 VPATH := $(VPATH):$(ROOT)/make
100 VPATH := $(VPATH):$(BL_SRC_DIR)
102 CSOURCES := $(shell find $(SRC_DIR) -name '*.c')
104 # start specific includes
105 include $(ROOT)/make/mcu/STM32.mk
106 include $(ROOT)/make/mcu/$(TARGET_MCU_GROUP).mk
108 BL_LD_SCRIPT := $(basename $(LD_SCRIPT))_bl.ld
110 ifneq ($(FOR_BL),)
111 LD_SCRIPT := $(basename $(LD_SCRIPT))_for_bl.ld
112 endif
114 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
115 ifeq ($(FLASH_SIZE),)
116 ifneq ($(TARGET_FLASH),)
117 FLASH_SIZE := $(TARGET_FLASH)
118 else
119 $(error FLASH_SIZE not configured for target $(TARGET))
120 endif
121 endif
123 # Configure devide and target-specific defines and compiler flags
124 DEVICE_FLAGS := $(DEVICE_FLAGS) -DFLASH_SIZE=$(FLASH_SIZE)
125 TARGET_FLAGS := $(TARGET_FLAGS) -D$(TARGET_MCU) -D$(TARGET_MCU_GROUP) -D$(TARGET)
127 ifneq ($(HSE_VALUE),)
128 DEVICE_FLAGS := $(DEVICE_FLAGS) -DHSE_VALUE=$(HSE_VALUE)
129 endif
131 ifneq ($(MHZ_VALUE),)
132 DEVICE_FLAGS := $(DEVICE_FLAGS) -DMHZ_VALUE=$(MHZ_VALUE)
133 endif
135 ifneq ($(BASE_TARGET), $(TARGET))
136 TARGET_FLAGS := $(TARGET_FLAGS) -D$(BASE_TARGET)
137 endif
139 TARGET_DIR = $(ROOT)/src/main/target/$(BASE_TARGET)
140 TARGET_DIR_SRC = $(notdir $(wildcard $(TARGET_DIR)/*.c))
142 INCLUDE_DIRS := $(INCLUDE_DIRS) \
143 $(ROOT)/lib/main/MAVLink
145 INCLUDE_DIRS := $(INCLUDE_DIRS) \
146 $(TARGET_DIR)
148 VPATH := $(VPATH):$(TARGET_DIR)
150 .DEFAULT_GOAL := hex
152 include $(ROOT)/make/source.mk
153 include $(ROOT)/make/release.mk
155 ###############################################################################
157 # Toolchain installer
160 TOOLS_DIR := $(ROOT)/tools
161 DL_DIR := $(ROOT)/downloads
163 include $(ROOT)/make/tools.mk
166 # Tool names
168 CROSS_CC = $(ARM_SDK_PREFIX)gcc
169 OBJCOPY = $(ARM_SDK_PREFIX)objcopy
170 SIZE = $(ARM_SDK_PREFIX)size
171 COMBINE_TOOL := src/utils/combine_tool
174 # Tool options.
177 # Save original CFLAGS before modifying them, so we don't
178 # add them twice when calling this Makefile recursively
179 # for each target
180 SAVED_CFLAGS := $(CFLAGS)
182 ifeq ($(DEBUG),GDB)
183 LTO_FLAGS =
184 else
185 LTO_FLAGS = -flto -fuse-linker-plugin
186 endif
188 ifneq ($(SEMIHOSTING),)
189 SEMIHOSTING_CFLAGS = -DSEMIHOSTING
190 SEMIHOSTING_LDFLAGS = --specs=rdimon.specs -lc -lrdimon
191 SYSLIB :=
192 else
193 SEMIHOSTING_CFLAGS =
194 SEMIHOSTING_LDFLAGS =
195 SYSLIB := -lnosys
196 endif
198 DEBUG_FLAGS = -ggdb3 -DDEBUG
200 CFLAGS += $(ARCH_FLAGS) \
201 $(LTO_FLAGS) \
202 $(addprefix -D,$(OPTIONS)) \
203 $(addprefix -I,$(INCLUDE_DIRS)) \
204 $(DEBUG_FLAGS) \
205 $(SEMIHOSTING_CFLAGS) \
206 -std=gnu99 \
207 -Wall -Wextra -Wunsafe-loop-optimizations -Wdouble-promotion \
208 -Wstrict-prototypes \
209 -Werror=switch \
210 -ffunction-sections \
211 -fdata-sections \
212 -fno-common \
213 $(DEVICE_FLAGS) \
214 -DUSE_STDPERIPH_DRIVER \
215 $(TARGET_FLAGS) \
216 -D'__FORKNAME__="$(FORKNAME)"' \
217 -D'__TARGET__="$(TARGET)"' \
218 -D'__REVISION__="$(REVISION)"' \
219 -save-temps=obj \
220 -MMD -MP
222 BL_CFLAGS = -DMSP_FIRMWARE_UPDATE -DBOOTLOADER
224 ifneq ($(FOR_BL),)
225 CFLAGS += -DMSP_FIRMWARE_UPDATE
226 endif
228 ASFLAGS = $(ARCH_FLAGS) \
229 -x assembler-with-cpp \
230 $(addprefix -I,$(INCLUDE_DIRS)) \
231 -D$(TARGET) \
232 -MMD -MP
234 LDFLAGS = -lm \
235 -nostartfiles \
236 --specs=nano.specs \
237 -lc \
238 $(SYSLIB) \
239 $(ARCH_FLAGS) \
240 $(LTO_FLAGS) \
241 $(DEBUG_FLAGS) \
242 $(SEMIHOSTING_LDFLAGS) \
243 -static \
244 -Wl,-gc-sections,-Map,$(TARGET_MAP) \
245 -Wl,-L$(LINKER_DIR) \
246 -Wl,--cref \
247 -Wl,--no-wchar-size-warning \
248 -Wl,--print-memory-usage \
249 -T$(LD_SCRIPT)
251 BL_LDFLAGS = -lm \
252 -nostartfiles \
253 --specs=nano.specs \
254 -lc \
255 $(SYSLIB) \
256 $(ARCH_FLAGS) \
257 $(LTO_FLAGS) \
258 $(DEBUG_FLAGS) \
259 $(SEMIHOSTING_LDFLAGS) \
260 -static \
261 -Wl,-gc-sections,-Map,$(TARGET_BL_MAP) \
262 -Wl,-L$(LINKER_DIR) \
263 -Wl,--cref \
264 -Wl,--no-wchar-size-warning \
265 -Wl,--print-memory-usage \
266 -T$(BL_LD_SCRIPT)
268 ###############################################################################
269 # No user-serviceable parts below
270 ###############################################################################
272 CPPCHECK = cppcheck $(CSOURCES) --enable=all --platform=unix64 \
273 --std=c99 --inline-suppr --quiet --force \
274 $(addprefix -I,$(INCLUDE_DIRS)) \
275 -I/usr/include -I/usr/include/linux
278 # Things we will build
280 TARGET_BIN := $(BIN_DIR)/$(FORKNAME)_$(FC_VER)
281 TARGET_BIN := $(TARGET_BIN)_$(TARGET)
282 TARGET_BL_BIN := $(TARGET_BIN)_bl
283 ifneq ($(BUILD_SUFFIX),)
284 TARGET_BIN := $(TARGET_BIN)_$(BUILD_SUFFIX)
285 TARGET_BL_BIN := $(TARGET_BL_BIN)_$(BUILD_SUFFIX)
286 endif
287 TARGET_BIN := $(TARGET_BIN).bin
288 TARGET_BL_BIN := $(TARGET_BL_BIN).bin
289 TARGET_HEX = $(TARGET_BIN:.bin=.hex)
290 TARGET_BL_HEX = $(TARGET_BL_BIN:.bin=.hex)
291 TARGET_COMBINED_HEX = $(basename $(TARGET_HEX))_combined.hex
293 TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET)
294 TARGET_BL_OBJ_DIR = $(BL_OBJECT_DIR)/$(TARGET)
295 TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
296 TARGET_BL_ELF = $(BL_OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf
297 TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(TARGET_SRC))))
298 TARGET_BL_OBJS = $(addsuffix .o,$(addprefix $(TARGET_BL_OBJ_DIR)/,$(basename $(TARGET_BL_SRC))))
299 TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(TARGET_SRC))))
300 TARGET_BL_DEPS = $(addsuffix .d,$(addprefix $(TARGET_BL_OBJ_DIR)/,$(basename $(TARGET_BL_SRC))))
301 TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map
302 TARGET_BL_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET)_bl.map
305 CLEAN_ARTIFACTS := $(TARGET_BIN)
306 CLEAN_ARTIFACTS += $(TARGET_HEX)
307 CLEAN_ARTIFACTS += $(TARGET_ELF)
308 CLEAN_ARTIFACTS += $(TARGET_OBJS) $(TARGET_MAP)
309 CLEAN_ARTIFACTS += $(TARGET_BL_BIN) $(TARGET_BL_HEX) $(TARGET_BL_ELF) $(TARGET_BL_OBJS) $(TARGET_BL_MAP)
311 include $(ROOT)/make/stamp.mk
312 include $(ROOT)/make/settings.mk
313 include $(ROOT)/make/svd.mk
315 # Make sure build date and revision is updated on every incremental build
316 $(TARGET_OBJ_DIR)/build/version.o $(TARGET_BL_OBJ_DIR)/build/version.o: $(TARGET_SRC)
318 # CFLAGS used for ASM generation. These can't include the LTO related options
319 # since they prevent proper ASM generation. Since $(LTO_FLAGS) includes the
320 # optization level, we have to add it back. -g is required to make interleaved
321 # source/ASM work.
322 ASM_CFLAGS=-g $(OPTIMZE) $(filter-out $(LTO_FLAGS) -save-temps=obj, $(CFLAGS))
324 # List of buildable ELF files and their object dependencies.
325 # It would be nice to compute these lists, but that seems to be just beyond make.
327 $(TARGET_HEX): $(TARGET_ELF)
328 $(V0) $(OBJCOPY) -O ihex --set-start $(FLASH_ORIGIN) $< $@
330 $(TARGET_BIN): $(TARGET_ELF)
331 $(V0) $(OBJCOPY) -O binary $< $@
333 $(TARGET_ELF): $(TARGET_OBJS)
334 $(V1) echo Linking $(TARGET)
335 $(V1) $(CROSS_CC) -o $@ $(filter %.o, $^) $(LDFLAGS)
336 $(V0) $(SIZE) $(TARGET_ELF)
338 $(TARGET_BL_HEX): $(TARGET_BL_ELF)
339 $(V0) $(OBJCOPY) -O ihex --set-start $(FLASH_ORIGIN) $< $@
341 $(TARGET_BL_BIN): $(TARGET_BL_ELF)
342 $(V0) $(OBJCOPY) -O binary $< $@
344 $(TARGET_BL_ELF): $(TARGET_BL_OBJS)
345 $(V1) echo Linking $(TARGET) bl
346 $(V1) $(CROSS_CC) -o $@ $(filter %.o, $^) $(BL_LDFLAGS)
347 $(V0) $(SIZE) $(TARGET_BL_ELF)
349 OPTIMIZE_FLAG_SPEED := "-Os"
350 OPTIMIZE_FLAG_SIZE := "-Os"
351 OPTIMIZE_FLAG_NORMAL := "-Os"
353 ifneq ($(TARGET_MCU_GROUP), STM32F3)
354 OPTIMIZE_FLAG_SPEED := "-Ofast"
355 OPTIMIZE_FLAG_SIZE := "-Os"
356 OPTIMIZE_FLAG_NORMAL := "-O2"
357 endif
359 define compile_file
360 echo "%% $(1) $(2) $<" "$(STDOUT)" && \
361 $(CROSS_CC) -c -o $@ $(CFLAGS) $(2) $<
362 endef
364 define compile_bl_file
365 echo "%% $(1) $(2) $<" "$(STDOUT)" && \
366 $(CROSS_CC) -c -o $@ $(CFLAGS) $(BL_CFLAGS) $(2) $<
367 endef
369 # Compile
370 $(TARGET_OBJ_DIR)/%.o: %.c
371 $(V1) mkdir -p $(dir $@)
373 $(V1) $(if $(findstring $<,$(SIZE_OPTIMISED_SRC)), \
374 $(call compile_file,(size),$(OPTIMIZE_FLAG_SIZE)) \
376 $(if $(findstring $<,$(SPEED_OPTIMISED_SRC)), \
377 $(call compile_file,(speed),$(OPTIMIZE_FLAG_SPEED)) \
379 $(call compile_file,(normal),$(OPTIMIZE_FLAG_NORMAL)) \
382 ifeq ($(GENERATE_ASM), 1)
383 $(V1) $(CROSS_CC) -S -fverbose-asm -Wa,-aslh -o $(patsubst %.o,%.txt.S,$@) -g $(ASM_CFLAGS) $<
384 endif
386 $(TARGET_BL_OBJ_DIR)/%.o: %.c
387 $(V1) mkdir -p $(dir $@)
389 $(V1) $(if $(findstring $<,$(SIZE_OPTIMISED_SRC)), \
390 $(call compile_bl_file,(size),$(OPTIMIZE_FLAG_SIZE)) \
392 $(if $(findstring $<,$(SPEED_OPTIMISED_SRC)), \
393 $(call compile_bl_file,(speed),$(OPTIMIZE_FLAG_SPEED)) \
395 $(call compile_bl_file,(normal),$(OPTIMIZE_FLAG_NORMAL)) \
398 ifeq ($(GENERATE_ASM), 1)
399 $(V1) $(CROSS_CC) -S -fverbose-asm -Wa,-aslh -o $(patsubst %.o,%.txt.S,$@) -g $(ASM_CFLAGS) $(BL_CFLAGS) $<
400 endif
402 # Assemble
403 $(TARGET_OBJ_DIR)/%.o $(TARGET_BL_OBJ_DIR)/%.o: %.s
404 $(V1) mkdir -p $(dir $@)
405 $(V1) echo %% $(notdir $<) "$(STDOUT)"
406 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
408 $(TARGET_OBJ_DIR)/%.o $(TARGET_BL_OBJ_DIR)/%.o: %.S
409 $(V1) mkdir -p $(dir $@)
410 $(V1) echo %% $(notdir $<) "$(STDOUT)"
411 $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $<
414 # mkdirs
415 $(DL_DIR):
416 mkdir -p $@
418 $(TOOLS_DIR):
419 mkdir -p $@
422 ## all : Build all valid targets
423 all: $(VALID_TARGETS)
425 ## targets-group-rest: build targets specified in release-targets list
426 release: $(RELEASE_TARGETS)
428 $(VALID_TARGETS):
429 $(V0) echo "" && \
430 echo "Building $@" && \
431 CFLAGS=$(SAVED_CFLAGS) $(MAKE) -j 8 TARGET=$@ && \
432 echo "Building $@ succeeded."
434 $(VALID_BL_TARGETS):
435 $(V0) echo "" && \
436 echo "Building $@" && \
437 CFLAGS=$(SAVED_CFLAGS) $(MAKE) -j 8 TARGET=$(@:%_bl=%) bl_binary bl_hex && \
438 echo "Building $(@:%_bl=%) bl succeeded."
440 $(VALID_TARGETS_FOR_BL):
441 $(V0) echo "" && \
442 echo "Building $@" && \
443 CFLAGS=$(SAVED_CFLAGS) $(MAKE) -j 8 TARGET=$(@:%_for_bl=%) FOR_BL=1 binary hex && \
444 echo "Building $(@:%_for_bl=%) for bl succeeded."
446 $(VALID_TARGETS_WITH_BL):
447 $(V0) echo "" && \
448 echo "Building $@ with bl" && \
449 CFLAGS=$(SAVED_CFLAGS) $(MAKE) TARGET=$(@:%_with_bl=%) combined_hex && \
450 echo "Building $(@:%_with_bl=%) with bl succeeded."
452 combined_hex:
453 $(V1) echo "Building combined BL+MAIN hex" && \
454 CFLAGS=$(SAVED_CFLAGS) $(MAKE) -j 8 TARGET=$(TARGET) bl_binary && \
455 CFLAGS=$(SAVED_CFLAGS) $(MAKE) -j 8 TARGET=$(TARGET) binary FOR_BL=1 && \
456 echo "Combining MAIN+BL in $(TARGET_COMBINED_HEX)" && \
457 $(COMBINE_TOOL) $(TARGET_BL_BIN) $(TARGET_BIN) $(TARGET_COMBINED_HEX)
459 ## clean : clean up all temporary / machine-generated files
460 clean:
461 $(V0) echo "Cleaning $(TARGET)"
462 $(V0) rm -f $(CLEAN_ARTIFACTS)
463 $(V0) rm -rf $(TARGET_OBJ_DIR)
464 $(V0) echo "Cleaning $(TARGET) succeeded."
466 ## clean_test : clean up all temporary / machine-generated files (tests)
467 clean_test:
468 $(V0) $(RM) -r src/test/build
470 ## clean_<TARGET> : clean up one specific target
471 $(CLEAN_TARGETS) :
472 $(V0) $(MAKE) -j 8 TARGET=$(subst clean_,,$@) clean
474 ## <TARGET>_clean : clean up one specific target (alias for above)
475 $(TARGETS_CLEAN) :
476 $(V0) $(MAKE) -j 8 TARGET=$(subst _clean,,$@) clean
478 ## clean_all : clean all valid targets
479 clean_all:$(CLEAN_TARGETS)
481 ## all_clean : clean all valid targets (alias for above)
482 all_clean:$(TARGETS_CLEAN)
484 flash_$(TARGET): $(TARGET_HEX)
485 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
486 $(V0) echo -n 'R' >$(SERIAL_DEVICE)
487 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
489 ## flash : flash firmware (.hex) onto flight controller
490 flash: flash_$(TARGET)
492 $(STFLASH_TARGETS) :
493 $(V0) $(MAKE) -j 8 TARGET=$(subst st-flash_,,$@) st-flash
495 ## st-flash : flash firmware (.bin) onto flight controller
496 st-flash: $(TARGET_BIN)
497 $(V0) st-flash --reset write $< $(FLASH_ORIGIN)
499 elf: $(TARGET_ELF)
500 binary: $(TARGET_BIN)
501 hex: $(TARGET_HEX)
502 bl_elf: $(TARGET_BL_ELF)
503 bl_binary: $(TARGET_BL_BIN)
504 bl_hex: $(TARGET_BL_HEX)
506 unbrick_$(TARGET): $(TARGET_HEX)
507 $(V0) stty -F $(SERIAL_DEVICE) raw speed 115200 -crtscts cs8 -parenb -cstopb -ixon
508 $(V0) stm32flash -w $(TARGET_HEX) -v -g 0x0 -b 115200 $(SERIAL_DEVICE)
510 ## unbrick : unbrick flight controller
511 unbrick: unbrick_$(TARGET)
513 ## cppcheck : run static analysis on C source code
514 cppcheck: $(CSOURCES)
515 $(V0) $(CPPCHECK)
517 cppcheck-result.xml: $(CSOURCES)
518 $(V0) $(CPPCHECK) --xml-version=2 2> cppcheck-result.xml
520 ## help : print this help message and exit
521 help: Makefile
522 $(V0) @echo ""
523 $(V0) @echo "Makefile for the $(FORKNAME) firmware"
524 $(V0) @echo ""
525 $(V0) @echo "Usage:"
526 $(V0) @echo " make [TARGET=<target>] [OPTIONS=\"<options>\"]"
527 $(V0) @echo "Or:"
528 $(V0) @echo " make <target> [OPTIONS=\"<options>\"]"
529 $(V0) @echo ""
530 $(V0) @echo "Valid TARGET values are: $(VALID_TARGETS)"
531 $(V0) @echo ""
532 $(V0) @sed -n 's/^## //p' $<
534 ## test : run the cleanflight test suite
535 test:
536 $(V0) mkdir -p src/test/build && cd src/test/build && cmake .. && $(MAKE) check
538 # rebuild everything when makefile changes
539 # Make the generated files and the build stamp order only prerequisites,
540 # so they will be generated before TARGET_OBJS but regenerating them
541 # won't cause all TARGET_OBJS to be rebuilt.
542 $(TARGET_OBJS) : Makefile | $(GENERATED_FILES) $(STAMP)
544 # include auto-generated dependencies
545 -include $(TARGET_DEPS)
547 # Developer tools
548 include $(ROOT)/make/openocd.mk
549 include $(ROOT)/make/gdb.mk