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 ###############################################################################
15 # Things that the user might override on the commandline
18 # The target to build, see VALID_TARGETS below
21 # Compile-time options
24 # Debugger optons, must be empty or GDB
30 # Serial port/Device for flashing
31 SERIAL_DEVICE ?
= $(firstword $(wildcard /dev
/ttyUSB
*) no-port-found
)
33 # Flash size (KB). Some low-end chips actually have more flash than advertised, use this to override.
36 ## V : Set verbosity level based on the V= parameter
48 export STDOUT
:= "> /dev/null"
49 export MAKE
:= $(MAKE
) --no-print-directory
56 ###############################################################################
57 # Things that need to be maintained as the source changes
63 ROOT
:= $(patsubst %/,%,$(dir $(lastword
$(MAKEFILE_LIST
))))
64 SRC_DIR
:= $(ROOT
)/src
/main
65 OBJECT_DIR
:= $(ROOT
)/obj
/main
66 BIN_DIR
:= $(ROOT
)/obj
67 CMSIS_DIR
:= $(ROOT
)/lib
/main
/CMSIS
68 INCLUDE_DIRS
:= $(SRC_DIR
) \
69 $(ROOT
)/src
/main
/target
70 LINKER_DIR
:= $(ROOT
)/src
/main
/target
/link
72 # default xtal value for F4 targets
75 # used for turning on features like VCP and SDCARD
78 ALT_TARGETS
= $(sort $(filter-out target
, $(basename $(notdir $(wildcard $(ROOT
)/src
/main
/target
/*/*.mk
)))))
80 VALID_TARGETS
= $(dir $(wildcard $(ROOT
)/src
/main
/target
/*/target.mk
))
81 VALID_TARGETS
:= $(subst /,, $(subst .
/src
/main
/target
/,, $(VALID_TARGETS
)))
82 VALID_TARGETS
:= $(VALID_TARGETS
) $(ALT_TARGETS
)
83 VALID_TARGETS
:= $(sort $(VALID_TARGETS
))
85 CLEAN_TARGETS
= $(addprefix clean_
,$(VALID_TARGETS
) )
86 TARGETS_CLEAN
= $(addsuffix _clean
,$(VALID_TARGETS
) )
88 ifeq ($(filter $(TARGET
),$(ALT_TARGETS
)), $(TARGET
))
89 BASE_TARGET
:= $(firstword $(subst /,, $(subst .
/src
/main
/target
/,, $(dir $(wildcard $(ROOT
)/src
/main
/target
/*/$(TARGET
).mk
)))))
90 -include $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)/$(TARGET
).mk
92 BASE_TARGET
:= $(TARGET
)
95 # silently ignore if the file is not present. Allows for target specific.
96 -include $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)/target.mk
98 F4_TARGETS
= $(F405_TARGETS
) $(F411_TARGETS
) $(F427_TARGETS
) $(F446_TARGETS
)
99 F7_TARGETS
= $(F7X2RE_TARGETS
) $(F7X5XE_TARGETS
) $(F7X5XG_TARGETS
) $(F7X5XI_TARGETS
) $(F7X6XG_TARGETS
)
101 ifeq ($(filter $(TARGET
),$(VALID_TARGETS
)),)
102 $(error Target
'$(TARGET)' is not valid
, must be one of
$(VALID_TARGETS
). Have you prepared a valid target.mk?
)
105 ifeq ($(filter $(TARGET
),$(F1_TARGETS
) $(F3_TARGETS
) $(F4_TARGETS
) $(F7_TARGETS
)),)
106 $(error Target
'$(TARGET)' has not specified a valid STM group
, must be one of F1
, F3
, F405
, F411
, F427 or F7x. Have you prepared a valid target.mk?
)
109 ifeq ($(TARGET
),$(filter $(TARGET
),$(F3_TARGETS
)))
110 TARGET_MCU
:= STM32F3
111 else ifeq ($(TARGET
),$(filter $(TARGET
), $(F4_TARGETS
)))
112 TARGET_MCU
:= STM32F4
113 else ifeq ($(TARGET
),$(filter $(TARGET
), $(F7_TARGETS
)))
114 TARGET_MCU
:= STM32F7
115 else ifeq ($(TARGET
),$(filter $(TARGET
), $(F1_TARGETS
)))
116 TARGET_MCU
:= STM32F1
118 $(error Unknown target MCU specified.
)
121 GROUP_1_TARGETS
:= ALIENFLIGHTF3 ALIENFLIGHTF4 AIRHEROF3 AIRHEROF3_QUAD COLIBRI_RACE LUX_RACE SPARKY REVO SPARKY2 COLIBRI KISSFC FALCORE FF_F35_LIGHTNING FF_FORTINIF4 FF_PIKOF4 FF_PIKOF4OSD
122 GROUP_2_TARGETS
:= SPRACINGF3 SPRACINGF3EVO SPRACINGF3EVO_1SS SPRACINGF3MINI SPRACINGF4EVO CLRACINGF4AIR CLRACINGF4AIRV2 BEEROTORF4 BETAFLIGHTF3 BETAFLIGHTF4 PIKOBLX
123 GROUP_3_TARGETS
:= OMNIBUS AIRBOTF4 BLUEJAYF4 OMNIBUSF4 OMNIBUSF4PRO OMNIBUSF4V3 FIREWORKSV2 SPARKY2 MATEKF405 OMNIBUSF7 DYSF4PRO OMNIBUSF4PRO_LEDSTRIPM5 OMNIBUSF7NXT OMNIBUSF7V2 ASGARD32F4
124 GROUP_4_TARGETS
:= ANYFC ANYFCF7 ANYFCF7_EXTERNAL_BARO ANYFCM7 ALIENFLIGHTNGF7 PIXRACER YUPIF4 YUPIF4MINI YUPIF4R2 YUPIF7 MATEKF405SE MATEKF411 MATEKF722 MATEKF405OSD MATEKF405_SERVOS6 NOX
125 GROUP_5_TARGETS
:= ASGARD32F7 CHEBUZZF3 CLRACINGF4AIRV3 DALRCF405 DALRCF722DUAL DYSF4PROV2 F4BY FISHDRONEF4 FOXEERF405 FOXEERF722DUAL FRSKYF3 FRSKYF4 FURYF3 FURYF3_SPIFLASH FURYF4OSD
126 GROUP_6_TARGETS
:= MAMBAF405
127 GROUP_OTHER_TARGETS
:= $(filter-out $(GROUP_1_TARGETS
) $(GROUP_2_TARGETS
) $(GROUP_3_TARGETS
) $(GROUP_4_TARGETS
) $(GROUP_5_TARGETS
) $(GROUP_6_TARGETS
), $(VALID_TARGETS
))
129 REVISION
= $(shell git rev-parse
--short HEAD
)
131 FC_VER_MAJOR
:= $(shell grep
" FC_VERSION_MAJOR" src
/main
/build
/version.h | awk
'{print $$3}' )
132 FC_VER_MINOR
:= $(shell grep
" FC_VERSION_MINOR" src
/main
/build
/version.h | awk
'{print $$3}' )
133 FC_VER_PATCH
:= $(shell grep
" FC_VERSION_PATCH" src
/main
/build
/version.h | awk
'{print $$3}' )
135 FC_VER
:= $(FC_VER_MAJOR
).
$(FC_VER_MINOR
).
$(FC_VER_PATCH
)
138 BUILD_DATE
= $(shell date
+%Y
%m
%d
)
140 # Search path for sources
141 FATFS_DIR
= $(ROOT
)/lib
/main
/FatFS
142 FATFS_SRC
= $(notdir $(wildcard $(FATFS_DIR
)/*.c
))
144 VPATH
:= $(SRC_DIR
):$(SRC_DIR
)/startup
145 VPATH
:= $(VPATH
):$(ROOT
)/make
/mcu
146 VPATH
:= $(VPATH
):$(ROOT
)/make
148 CSOURCES
:= $(shell find
$(SRC_DIR
) -name
'*.c')
150 # start specific includes
151 include $(ROOT
)/make
/mcu
/$(TARGET_MCU
).mk
153 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
154 ifeq ($(FLASH_SIZE
),)
155 ifneq ($(TARGET_FLASH
),)
156 FLASH_SIZE
:= $(TARGET_FLASH
)
158 $(error FLASH_SIZE not configured for target
$(TARGET
))
162 # Configure devide and target-specific defines and compiler flags
163 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
164 TARGET_FLAGS
:= $(TARGET_FLAGS
) -D
$(TARGET_MCU
) -D
$(TARGET
)
166 ifneq ($(HSE_VALUE
),)
167 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
170 ifneq ($(BASE_TARGET
), $(TARGET
))
171 TARGET_FLAGS
:= $(TARGET_FLAGS
) -D
$(BASE_TARGET
)
174 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
175 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
177 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
178 $(ROOT
)/lib
/main
/MAVLink
180 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
183 VPATH
:= $(VPATH
):$(TARGET_DIR
)
187 include $(ROOT
)/make
/source.mk
188 include $(ROOT
)/make
/release.mk
190 ###############################################################################
191 # Things that might need changing to use different tools
195 ifneq ($(TOOLCHAINPATH
),)
196 CROSS_CC
= $(TOOLCHAINPATH
)/arm-none-eabi-gcc
197 OBJCOPY
= $(TOOLCHAINPATH
)/arm-none-eabi-objcopy
198 SIZE
= $(TOOLCHAINPATH
)/arm-none-eabi-size
200 CROSS_CC
= arm-none-eabi-gcc
201 OBJCOPY
= arm-none-eabi-objcopy
202 SIZE
= arm-none-eabi-size
211 LTO_FLAGS
= $(OPTIMIZE
)
214 LTO_FLAGS
= -flto
-fuse-linker-plugin
$(OPTIMIZE
)
217 DEBUG_FLAGS
= -ggdb3
-DDEBUG
219 CFLAGS
+= $(ARCH_FLAGS
) \
221 $(addprefix -D
,$(OPTIONS
)) \
222 $(addprefix -I
,$(INCLUDE_DIRS
)) \
225 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
226 -Wstrict-prototypes \
228 -ffunction-sections \
232 -DUSE_STDPERIPH_DRIVER \
234 -D
'__FORKNAME__="$(FORKNAME)"' \
235 -D
'__TARGET__="$(TARGET)"' \
236 -D
'__REVISION__="$(REVISION)"' \
240 ASFLAGS
= $(ARCH_FLAGS
) \
241 -x assembler-with-cpp \
242 $(addprefix -I
,$(INCLUDE_DIRS
)) \
255 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
256 -Wl
,-L
$(LINKER_DIR
) \
258 -Wl
,--no-wchar-size-warning \
259 -Wl
,--print-memory-usage \
262 ###############################################################################
263 # No user-serviceable parts below
264 ###############################################################################
266 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
267 --std
=c99
--inline-suppr
--quiet
--force \
268 $(addprefix -I
,$(INCLUDE_DIRS
)) \
269 -I
/usr
/include -I
/usr
/include/linux
272 # Things we will build
274 TARGET_BIN
:= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)
275 ifneq ($(FC_VER_SUFFIX
),)
276 TARGET_BIN
:= $(TARGET_BIN
)-$(FC_VER_SUFFIX
)
278 TARGET_BIN
:= $(TARGET_BIN
)_
$(TARGET
)
279 ifneq ($(BUILD_SUFFIX
),)
280 TARGET_BIN
:= $(TARGET_BIN
)_
$(BUILD_SUFFIX
)
282 TARGET_BIN
:= $(TARGET_BIN
).bin
283 TARGET_HEX
= $(TARGET_BIN
:.bin
=.hex
)
285 TARGET_OBJ_DIR
= $(OBJECT_DIR
)/$(TARGET
)
286 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
287 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(TARGET_OBJ_DIR
)/,$(basename $(TARGET_SRC
))))
288 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(TARGET_OBJ_DIR
)/,$(basename $(TARGET_SRC
))))
289 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
292 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
293 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
294 CLEAN_ARTIFACTS
+= $(TARGET_ELF
) $(TARGET_OBJS
) $(TARGET_MAP
)
296 # Make sure build date and revision is updated on every incremental build
297 $(TARGET_OBJ_DIR
)/build
/version.o
: $(TARGET_SRC
)
300 .PHONY
: .FORCE settings clean-settings
301 UTILS_DIR
= $(ROOT
)/src
/utils
302 SETTINGS_GENERATOR
= $(UTILS_DIR
)/settings.rb
303 BUILD_STAMP
= $(UTILS_DIR
)/build_stamp.rb
304 STAMP
= $(TARGET_OBJ_DIR
)/build.stamp
306 GENERATED_SETTINGS
= $(TARGET_OBJ_DIR
)/settings_generated.h
$(TARGET_OBJ_DIR
)/settings_generated.c
307 SETTINGS_FILE
= $(SRC_DIR
)/fc/settings.yaml
308 GENERATED_FILES
= $(GENERATED_SETTINGS
)
309 $(GENERATED_SETTINGS
): $(SETTINGS_GENERATOR
) $(SETTINGS_FILE
) $(STAMP
)
311 # Make sure the generated files are in the include path
312 CFLAGS
+= -I
$(TARGET_OBJ_DIR
)
315 $(V1
) CFLAGS
="$(CFLAGS)" TARGET
=$(TARGET
) ruby
$(BUILD_STAMP
) $(SETTINGS_FILE
) $(STAMP
)
317 # Use a pattern rule, since they're different than normal rules.
318 # See https://www.gnu.org/software/make/manual/make.html#Pattern-Examples
319 %generated.h
%generated.c
:
320 $(V1
) echo
"settings.yaml -> settings_generated.h, settings_generated.c" "$(STDOUT)"
321 $(V1
) CFLAGS
="$(CFLAGS)" TARGET
=$(TARGET
) ruby
$(SETTINGS_GENERATOR
) .
$(SETTINGS_FILE
) -o
$(TARGET_OBJ_DIR
)
324 $(V0
) CFLAGS
="$(CFLAGS)" TARGET
=$(TARGET
) ruby
$(SETTINGS_GENERATOR
) .
$(SETTINGS_FILE
) --json settings.json
327 $(V1
) $(RM
) $(GENERATED_SETTINGS
)
329 # CFLAGS used for ASM generation. These can't include the LTO related options
330 # since they prevent proper ASM generation. Since $(LTO_FLAGS) includes the
331 # optization level, we have to add it back. -g is required to make interleaved
333 ASM_CFLAGS
=-g
$(OPTIMZE
) $(filter-out $(LTO_FLAGS
) -save-temps
=obj
, $(CFLAGS
))
335 # List of buildable ELF files and their object dependencies.
336 # It would be nice to compute these lists, but that seems to be just beyond make.
338 $(TARGET_HEX
): $(TARGET_ELF
)
339 $(V0
) $(OBJCOPY
) -O ihex
--set-start
0x8000000 $< $@
341 $(TARGET_BIN
): $(TARGET_ELF
)
342 $(V0
) $(OBJCOPY
) -O binary
$< $@
344 $(TARGET_ELF
): $(TARGET_OBJS
)
345 $(V1
) echo Linking
$(TARGET
)
346 $(V1
) $(CROSS_CC
) -o
$@
$(filter %.o
, $^
) $(LDFLAGS
)
347 $(V0
) $(SIZE
) $(TARGET_ELF
)
350 $(TARGET_OBJ_DIR
)/%.o
: %.c
351 $(V1
) mkdir
-p
$(dir $@
)
352 $(V1
) echo
%% $(notdir $<) "$(STDOUT)"
353 $(V1
) $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $<
354 ifeq ($(GENERATE_ASM
), 1)
355 $(V1
) $(CROSS_CC
) -S
-fverbose-asm
-Wa
,-aslh
-o
$(patsubst %.o
,%.txt.S
,$@
) -g
$(ASM_CFLAGS
) $<
360 $(TARGET_OBJ_DIR
)/%.o
: %.s
361 $(V1
) mkdir
-p
$(dir $@
)
362 $(V1
) echo
%% $(notdir $<) "$(STDOUT)"
363 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
365 $(TARGET_OBJ_DIR
)/%.o
: %.S
366 $(V1
) mkdir
-p
$(dir $@
)
367 $(V1
) echo
%% $(notdir $<) "$(STDOUT)"
368 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
371 ## all : Build all valid targets
372 all: $(VALID_TARGETS
)
374 ## targets-group-1 : build some targets
375 targets-group-1
: $(GROUP_1_TARGETS
)
377 ## targets-group-2 : build some targets
378 targets-group-2
: $(GROUP_2_TARGETS
)
380 ## targets-group-3 : build some targets
381 targets-group-3
: $(GROUP_3_TARGETS
)
383 ## targets-group-4 : build some targets
384 targets-group-4
: $(GROUP_4_TARGETS
)
386 ## targets-group-5 : build some targets
387 targets-group-5
: $(GROUP_5_TARGETS
)
389 ## targets-group-rest: build the rest of the targets (not listed in group 1, 2 or 3)
390 targets-group-rest
: $(GROUP_OTHER_TARGETS
)
392 ## targets-group-rest: build targets specified in release-targets list
393 release
: $(RELEASE_TARGETS
)
397 echo
"Building $@" && \
398 $(MAKE
) -j
8 TARGET
=$@
&& \
399 echo
"Building $@ succeeded."
401 ## clean : clean up all temporary / machine-generated files
403 $(V0
) echo
"Cleaning $(TARGET)"
404 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
405 $(V0
) rm -rf
$(TARGET_OBJ_DIR
)
406 $(V0
) echo
"Cleaning $(TARGET) succeeded."
408 ## clean_test : clean up all temporary / machine-generated files (tests)
410 $(V0
) cd src
/test && $(MAKE
) clean
412 ## clean_<TARGET> : clean up one specific target
414 $(V0
) $(MAKE
) -j
8 TARGET
=$(subst clean_
,,$@
) clean
416 ## <TARGET>_clean : clean up one specific target (alias for above)
418 $(V0
) $(MAKE
) -j
8 TARGET
=$(subst _clean
,,$@
) clean
420 ## clean_all : clean all valid targets
421 clean_all
:$(CLEAN_TARGETS
)
423 ## all_clean : clean all valid targets (alias for above)
424 all_clean
:$(TARGETS_CLEAN
)
426 flash_
$(TARGET
): $(TARGET_HEX
)
427 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
428 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
429 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
431 ## flash : flash firmware (.hex) onto flight controller
432 flash
: flash_
$(TARGET
)
434 st-flash_
$(TARGET
): $(TARGET_BIN
)
435 $(V0
) st-flash
--reset write
$< 0x08000000
437 ## st-flash : flash firmware (.bin) onto flight controller
438 st-flash
: st-flash_
$(TARGET
)
440 binary
: $(TARGET_BIN
)
443 unbrick_
$(TARGET
): $(TARGET_HEX
)
444 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
445 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
447 ## unbrick : unbrick flight controller
448 unbrick
: unbrick_
$(TARGET
)
450 ## cppcheck : run static analysis on C source code
451 cppcheck
: $(CSOURCES
)
454 cppcheck-result.xml
: $(CSOURCES
)
455 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
457 ## help : print this help message and exit
460 $(V0
) @echo
"Makefile for the $(FORKNAME) firmware"
463 $(V0
) @echo
" make [TARGET=<target>] [OPTIONS=\"<options>\"]"
465 $(V0
) @echo
" make <target> [OPTIONS=\"<options>\"]"
467 $(V0
) @echo
"Valid TARGET values are: $(VALID_TARGETS)"
469 $(V0
) @sed
-n
's/^## //p' $<
471 ## targets : print a list of all valid target platforms (for consumption by scripts)
473 $(V0
) @echo
"Valid targets: $(VALID_TARGETS)"
474 $(V0
) @echo
"Target: $(TARGET)"
475 $(V0
) @echo
"Base target: $(BASE_TARGET)"
476 $(V0
) @echo
"targets-group-1: $(GROUP_1_TARGETS)"
477 $(V0
) @echo
"targets-group-2: $(GROUP_2_TARGETS)"
478 $(V0
) @echo
"targets-group-3: $(GROUP_3_TARGETS)"
479 $(V0
) @echo
"targets-group-4: $(GROUP_4_TARGETS)"
480 $(V0
) @echo
"targets-group-5: $(GROUP_5_TARGETS)"
481 $(V0
) @echo
"targets-group-rest: $(GROUP_OTHER_TARGETS)"
482 $(V0
) @echo
"Release targets: $(RELEASE_TARGETS)"
484 ## test : run the cleanflight test suite
486 $(V0
) cd src
/test && $(MAKE
) test
488 # rebuild everything when makefile changes
489 # Make the generated files and the build stamp order only prerequisites,
490 # so they will be generated before TARGET_OBJS but regenerating them
491 # won't cause all TARGET_OBJS to be rebuilt.
492 $(TARGET_OBJS
) : Makefile |
$(GENERATED_FILES
) $(STAMP
)
494 # include auto-generated dependencies
495 -include $(TARGET_DEPS
)