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 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
26 # Compile-time options
29 # Debugger optons, must be empty or GDB
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.
43 ## V : Set verbosity level based on the V= parameter
55 export STDOUT
:= "> /dev/null"
56 export MAKE
:= $(MAKE
) --no-print-directory
63 ###############################################################################
64 # Things that need to be maintained as the source changes
68 SRC_DIR
:= $(ROOT
)/src
/main
69 OBJECT_DIR
:= $(ROOT
)/obj
/main
70 BIN_DIR
:= $(ROOT
)/obj
71 CMSIS_DIR
:= $(ROOT
)/lib
/main
/CMSIS
72 INCLUDE_DIRS
:= $(SRC_DIR
) \
73 $(ROOT
)/src
/main
/target
74 LINKER_DIR
:= $(ROOT
)/src
/main
/target
/link
76 # import macros common to all supported build systems
77 include $(ROOT
)/make
/system-id.mk
79 # default xtal value for F4 targets
83 # used for turning on features like VCP and SDCARD
86 include $(ROOT
)/make
/version.mk
87 include $(ROOT
)/make
/targets.mk
89 BUILD_DATE
= $(shell date
+%Y
%m
%d
)
91 # Search path for sources
92 FATFS_DIR
= $(ROOT
)/lib
/main
/FatFS
93 FATFS_SRC
= $(notdir $(wildcard $(FATFS_DIR
)/*.c
))
95 VPATH
:= $(SRC_DIR
):$(SRC_DIR
)/startup
96 VPATH
:= $(VPATH
):$(ROOT
)/make
/mcu
97 VPATH
:= $(VPATH
):$(ROOT
)/make
99 CSOURCES
:= $(shell find
$(SRC_DIR
) -name
'*.c')
101 # start specific includes
102 include $(ROOT
)/make
/mcu
/STM32.mk
103 include $(ROOT
)/make
/mcu
/$(TARGET_MCU_GROUP
).mk
105 # Configure default flash sizes for the targets (largest size specified gets hit first) if flash not specified already.
106 ifeq ($(FLASH_SIZE
),)
107 ifneq ($(TARGET_FLASH
),)
108 FLASH_SIZE
:= $(TARGET_FLASH
)
110 $(error FLASH_SIZE not configured for target
$(TARGET
))
114 # Configure devide and target-specific defines and compiler flags
115 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DFLASH_SIZE
=$(FLASH_SIZE
)
116 TARGET_FLAGS
:= $(TARGET_FLAGS
) -D
$(TARGET_MCU
) -D
$(TARGET_MCU_GROUP
) -D
$(TARGET
)
118 ifneq ($(HSE_VALUE
),)
119 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DHSE_VALUE
=$(HSE_VALUE
)
122 ifneq ($(MHZ_VALUE
),)
123 DEVICE_FLAGS
:= $(DEVICE_FLAGS
) -DMHZ_VALUE
=$(MHZ_VALUE
)
126 ifneq ($(BASE_TARGET
), $(TARGET
))
127 TARGET_FLAGS
:= $(TARGET_FLAGS
) -D
$(BASE_TARGET
)
130 TARGET_DIR
= $(ROOT
)/src
/main
/target
/$(BASE_TARGET
)
131 TARGET_DIR_SRC
= $(notdir $(wildcard $(TARGET_DIR
)/*.c
))
133 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
134 $(ROOT
)/lib
/main
/MAVLink
136 INCLUDE_DIRS
:= $(INCLUDE_DIRS
) \
139 VPATH
:= $(VPATH
):$(TARGET_DIR
)
143 include $(ROOT
)/make
/source.mk
144 include $(ROOT
)/make
/release.mk
146 ###############################################################################
148 # Toolchain installer
151 TOOLS_DIR
:= $(ROOT
)/tools
152 DL_DIR
:= $(ROOT
)/downloads
154 include $(ROOT
)/make
/tools.mk
159 CROSS_CC
= $(ARM_SDK_PREFIX
)gcc
160 OBJCOPY
= $(ARM_SDK_PREFIX
)objcopy
161 SIZE
= $(ARM_SDK_PREFIX
)size
167 # Save original CFLAGS before modifying them, so we don't
168 # add them twice when calling this Makefile recursively
170 SAVED_CFLAGS
:= $(CFLAGS
)
175 LTO_FLAGS
= -flto
-fuse-linker-plugin
178 ifneq ($(SEMIHOSTING
),)
179 SEMIHOSTING_CFLAGS
= -DSEMIHOSTING
180 SEMIHOSTING_LDFLAGS
= --specs
=rdimon.specs
-lc
-lrdimon
184 SEMIHOSTING_LDFLAGS
=
188 DEBUG_FLAGS
= -ggdb3
-DDEBUG
190 CFLAGS
+= $(ARCH_FLAGS
) \
192 $(addprefix -D
,$(OPTIONS
)) \
193 $(addprefix -I
,$(INCLUDE_DIRS
)) \
195 $(SEMIHOSTING_CFLAGS
) \
197 -Wall
-Wextra
-Wunsafe-loop-optimizations
-Wdouble-promotion \
198 -Wstrict-prototypes \
200 -ffunction-sections \
204 -DUSE_STDPERIPH_DRIVER \
206 -D
'__FORKNAME__="$(FORKNAME)"' \
207 -D
'__TARGET__="$(TARGET)"' \
208 -D
'__REVISION__="$(REVISION)"' \
212 ASFLAGS
= $(ARCH_FLAGS
) \
213 -x assembler-with-cpp \
214 $(addprefix -I
,$(INCLUDE_DIRS
)) \
226 $(SEMIHOSTING_LDFLAGS
) \
228 -Wl
,-gc-sections
,-Map
,$(TARGET_MAP
) \
229 -Wl
,-L
$(LINKER_DIR
) \
231 -Wl
,--no-wchar-size-warning \
232 -Wl
,--print-memory-usage \
235 ###############################################################################
236 # No user-serviceable parts below
237 ###############################################################################
239 CPPCHECK
= cppcheck
$(CSOURCES
) --enable
=all --platform
=unix64 \
240 --std
=c99
--inline-suppr
--quiet
--force \
241 $(addprefix -I
,$(INCLUDE_DIRS
)) \
242 -I
/usr
/include -I
/usr
/include/linux
245 # Things we will build
247 TARGET_BIN
:= $(BIN_DIR
)/$(FORKNAME
)_
$(FC_VER
)
248 TARGET_BIN
:= $(TARGET_BIN
)_
$(TARGET
)
249 ifneq ($(BUILD_SUFFIX
),)
250 TARGET_BIN
:= $(TARGET_BIN
)_
$(BUILD_SUFFIX
)
252 TARGET_BIN
:= $(TARGET_BIN
).bin
253 TARGET_HEX
= $(TARGET_BIN
:.bin
=.hex
)
255 TARGET_OBJ_DIR
= $(OBJECT_DIR
)/$(TARGET
)
256 TARGET_ELF
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).elf
257 TARGET_OBJS
= $(addsuffix .o
,$(addprefix $(TARGET_OBJ_DIR
)/,$(basename $(TARGET_SRC
))))
258 TARGET_DEPS
= $(addsuffix .d
,$(addprefix $(TARGET_OBJ_DIR
)/,$(basename $(TARGET_SRC
))))
259 TARGET_MAP
= $(OBJECT_DIR
)/$(FORKNAME
)_
$(TARGET
).map
262 CLEAN_ARTIFACTS
:= $(TARGET_BIN
)
263 CLEAN_ARTIFACTS
+= $(TARGET_HEX
)
264 CLEAN_ARTIFACTS
+= $(TARGET_ELF
)
265 CLEAN_ARTIFACTS
+= $(TARGET_OBJS
) $(TARGET_MAP
)
267 include $(ROOT
)/make
/stamp.mk
268 include $(ROOT
)/make
/settings.mk
269 include $(ROOT
)/make
/svd.mk
271 # Make sure build date and revision is updated on every incremental build
272 $(TARGET_OBJ_DIR
)/build
/version.o
: $(TARGET_SRC
)
274 # CFLAGS used for ASM generation. These can't include the LTO related options
275 # since they prevent proper ASM generation. Since $(LTO_FLAGS) includes the
276 # optization level, we have to add it back. -g is required to make interleaved
278 ASM_CFLAGS
=-g
$(OPTIMZE
) $(filter-out $(LTO_FLAGS
) -save-temps
=obj
, $(CFLAGS
))
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_HEX
): $(TARGET_ELF
)
284 $(V0
) $(OBJCOPY
) -O ihex
--set-start
$(FLASH_ORIGIN
) $< $@
286 $(TARGET_BIN
): $(TARGET_ELF
)
287 $(V0
) $(OBJCOPY
) -O binary
$< $@
289 $(TARGET_ELF
): $(TARGET_OBJS
)
290 $(V1
) echo Linking
$(TARGET
)
291 $(V1
) $(CROSS_CC
) -o
$@
$(filter %.o
, $^
) $(LDFLAGS
)
292 $(V0
) $(SIZE
) $(TARGET_ELF
)
294 OPTIMIZE_FLAG_SPEED
:= "-Os"
295 OPTIMIZE_FLAG_SIZE
:= "-Os"
296 OPTIMIZE_FLAG_NORMAL
:= "-Os"
298 ifneq ($(TARGET_MCU_GROUP
), STM32F3
)
299 OPTIMIZE_FLAG_SPEED
:= "-Ofast"
300 OPTIMIZE_FLAG_SIZE
:= "-Os"
301 OPTIMIZE_FLAG_NORMAL
:= "-O2"
305 echo
"%% $(1) $(2) $<" "$(STDOUT)" && \
306 $(CROSS_CC
) -c
-o
$@
$(CFLAGS
) $(2) $<
310 $(TARGET_OBJ_DIR
)/%.o
: %.c
311 $(V1
) mkdir
-p
$(dir $@
)
313 $(V1
) $(if
$(findstring $<,$(SIZE_OPTIMISED_SRC
)), \
314 $(call compile_file
,(size
),$(OPTIMIZE_FLAG_SIZE
)) \
316 $(if
$(findstring $<,$(SPEED_OPTIMISED_SRC
)), \
317 $(call compile_file
,(speed
),$(OPTIMIZE_FLAG_SPEED
)) \
319 $(call compile_file
,(normal
),$(OPTIMIZE_FLAG_NORMAL
)) \
322 ifeq ($(GENERATE_ASM
), 1)
323 $(V1
) $(CROSS_CC
) -S
-fverbose-asm
-Wa
,-aslh
-o
$(patsubst %.o
,%.txt.S
,$@
) -g
$(ASM_CFLAGS
) $<
327 $(TARGET_OBJ_DIR
)/%.o
: %.s
328 $(V1
) mkdir
-p
$(dir $@
)
329 $(V1
) echo
%% $(notdir $<) "$(STDOUT)"
330 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
332 $(TARGET_OBJ_DIR
)/%.o
: %.S
333 $(V1
) mkdir
-p
$(dir $@
)
334 $(V1
) echo
%% $(notdir $<) "$(STDOUT)"
335 $(V1
) $(CROSS_CC
) -c
-o
$@
$(ASFLAGS
) $<
346 ## all : Build all valid targets
347 all: $(VALID_TARGETS
)
349 ## targets-group-rest: build targets specified in release-targets list
350 release
: $(RELEASE_TARGETS
)
354 echo
"Building $@" && \
355 CFLAGS
=$(SAVED_CFLAGS
) $(MAKE
) -j
8 TARGET
=$@
&& \
356 echo
"Building $@ succeeded."
358 ## clean : clean up all temporary / machine-generated files
360 $(V0
) echo
"Cleaning $(TARGET)"
361 $(V0
) rm -f
$(CLEAN_ARTIFACTS
)
362 $(V0
) rm -rf
$(TARGET_OBJ_DIR
)
363 $(V0
) echo
"Cleaning $(TARGET) succeeded."
365 ## clean_test : clean up all temporary / machine-generated files (tests)
367 $(V0
) cd src
/test && $(MAKE
) clean
369 ## clean_<TARGET> : clean up one specific target
371 $(V0
) $(MAKE
) -j
8 TARGET
=$(subst clean_
,,$@
) clean
373 ## <TARGET>_clean : clean up one specific target (alias for above)
375 $(V0
) $(MAKE
) -j
8 TARGET
=$(subst _clean
,,$@
) clean
377 ## clean_all : clean all valid targets
378 clean_all
:$(CLEAN_TARGETS
)
380 ## all_clean : clean all valid targets (alias for above)
381 all_clean
:$(TARGETS_CLEAN
)
383 flash_
$(TARGET
): $(TARGET_HEX
)
384 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
385 $(V0
) echo
-n
'R' >$(SERIAL_DEVICE
)
386 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
388 ## flash : flash firmware (.hex) onto flight controller
389 flash
: flash_
$(TARGET
)
392 $(V0
) $(MAKE
) -j
8 TARGET
=$(subst st-flash_
,,$@
) st-flash
394 ## st-flash : flash firmware (.bin) onto flight controller
395 st-flash
: $(TARGET_BIN
)
396 $(V0
) st-flash
--reset write
$< $(FLASH_ORIGIN
)
399 binary
: $(TARGET_BIN
)
402 unbrick_
$(TARGET
): $(TARGET_HEX
)
403 $(V0
) stty
-F
$(SERIAL_DEVICE
) raw speed
115200 -crtscts cs8
-parenb
-cstopb
-ixon
404 $(V0
) stm32flash
-w
$(TARGET_HEX
) -v
-g
0x0 -b
115200 $(SERIAL_DEVICE
)
406 ## unbrick : unbrick flight controller
407 unbrick
: unbrick_
$(TARGET
)
409 ## cppcheck : run static analysis on C source code
410 cppcheck
: $(CSOURCES
)
413 cppcheck-result.xml
: $(CSOURCES
)
414 $(V0
) $(CPPCHECK
) --xml-version
=2 2> cppcheck-result.xml
416 ## help : print this help message and exit
419 $(V0
) @echo
"Makefile for the $(FORKNAME) firmware"
422 $(V0
) @echo
" make [TARGET=<target>] [OPTIONS=\"<options>\"]"
424 $(V0
) @echo
" make <target> [OPTIONS=\"<options>\"]"
426 $(V0
) @echo
"Valid TARGET values are: $(VALID_TARGETS)"
428 $(V0
) @sed
-n
's/^## //p' $<
430 ## test : run the cleanflight test suite
432 $(V0
) cd src
/test && $(MAKE
) test
434 # rebuild everything when makefile changes
435 # Make the generated files and the build stamp order only prerequisites,
436 # so they will be generated before TARGET_OBJS but regenerating them
437 # won't cause all TARGET_OBJS to be rebuilt.
438 $(TARGET_OBJS
) : Makefile |
$(GENERATED_FILES
) $(STAMP
)
440 # include auto-generated dependencies
441 -include $(TARGET_DEPS
)
444 include $(ROOT
)/make
/openocd.mk
445 include $(ROOT
)/make
/gdb.mk