Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | # --------------------------------------- |
| 2 | # Common make definition for all examples |
| 3 | # --------------------------------------- |
| 4 | |
| 5 | # Build directory |
| 6 | BUILD := _build/$(BOARD) |
| 7 | |
| 8 | PROJECT := $(notdir $(CURDIR)) |
| 9 | BIN := $(TOP)/_bin/$(BOARD)/$(notdir $(CURDIR)) |
| 10 | |
| 11 | # Handy check parameter function |
| 12 | check_defined = \ |
| 13 | $(strip $(foreach 1,$1, \ |
| 14 | $(call __check_defined,$1,$(strip $(value 2))))) |
| 15 | __check_defined = \ |
| 16 | $(if $(value $1),, \ |
| 17 | $(error Undefined make flag: $1$(if $2, ($2)))) |
| 18 | |
| 19 | #-------------- Select the board to build for. ------------ |
| 20 | |
| 21 | # Board without family |
| 22 | ifneq ($(wildcard $(TOP)/hw/bsp/$(BOARD)/board.mk),) |
| 23 | BOARD_PATH := hw/bsp/$(BOARD) |
| 24 | FAMILY := |
| 25 | endif |
| 26 | |
| 27 | # Board within family |
| 28 | ifeq ($(BOARD_PATH),) |
| 29 | BOARD_PATH := $(subst $(TOP)/,,$(wildcard $(TOP)/hw/bsp/*/boards/$(BOARD))) |
| 30 | FAMILY := $(word 3, $(subst /, ,$(BOARD_PATH))) |
| 31 | FAMILY_PATH = hw/bsp/$(FAMILY) |
| 32 | endif |
| 33 | |
| 34 | ifeq ($(BOARD_PATH),) |
| 35 | $(info You must provide a BOARD parameter with 'BOARD=') |
| 36 | $(error Invalid BOARD specified) |
| 37 | endif |
| 38 | |
| 39 | ifeq ($(FAMILY),) |
| 40 | include $(TOP)/hw/bsp/$(BOARD)/board.mk |
| 41 | else |
| 42 | # Include Family and Board specific defs |
| 43 | include $(TOP)/$(FAMILY_PATH)/family.mk |
| 44 | |
| 45 | SRC_C += $(subst $(TOP)/,,$(wildcard $(TOP)/$(FAMILY_PATH)/*.c)) |
| 46 | endif |
| 47 | |
| 48 | # Fetch submodules depended by family |
| 49 | fetch_submodule_if_empty = $(if $(wildcard $(TOP)/$1/*),,$(info $(shell git -C $(TOP) submodule update --init $1))) |
| 50 | ifdef DEPS_SUBMODULES |
| 51 | $(foreach s,$(DEPS_SUBMODULES),$(call fetch_submodule_if_empty,$(s))) |
| 52 | endif |
| 53 | |
| 54 | #-------------- Cross Compiler ------------ |
| 55 | # Can be set by board, default to ARM GCC |
| 56 | CROSS_COMPILE ?= arm-none-eabi- |
| 57 | # Allow for -Os to be changed by board makefiles in case -Os is not allowed |
| 58 | CFLAGS_OPTIMIZED ?= -Os |
| 59 | |
| 60 | CC = $(CROSS_COMPILE)gcc |
| 61 | CXX = $(CROSS_COMPILE)g++ |
| 62 | GDB = $(CROSS_COMPILE)gdb |
| 63 | OBJCOPY = $(CROSS_COMPILE)objcopy |
| 64 | SIZE = $(CROSS_COMPILE)size |
| 65 | MKDIR = mkdir |
| 66 | |
| 67 | ifeq ($(CMDEXE),1) |
| 68 | CP = copy |
| 69 | RM = del |
| 70 | PYTHON = python |
| 71 | else |
| 72 | SED = sed |
| 73 | CP = cp |
| 74 | RM = rm |
| 75 | PYTHON = python3 |
| 76 | endif |
| 77 | |
| 78 | #-------------- Source files and compiler flags -------------- |
| 79 | |
| 80 | # Include all source C in family & board folder |
| 81 | SRC_C += hw/bsp/board.c |
| 82 | SRC_C += $(subst $(TOP)/,,$(wildcard $(TOP)/$(BOARD_PATH)/*.c)) |
| 83 | |
| 84 | INC += $(TOP)/$(FAMILY_PATH) |
| 85 | |
| 86 | # Compiler Flags |
| 87 | CFLAGS += \ |
| 88 | -ggdb \ |
| 89 | -fdata-sections \ |
| 90 | -ffunction-sections \ |
| 91 | -fsingle-precision-constant \ |
| 92 | -fno-strict-aliasing \ |
| 93 | -Wdouble-promotion \ |
| 94 | -Wstrict-prototypes \ |
| 95 | -Wstrict-overflow \ |
| 96 | -Wall \ |
| 97 | -Wextra \ |
| 98 | -Werror \ |
| 99 | -Wfatal-errors \ |
| 100 | -Werror-implicit-function-declaration \ |
| 101 | -Wfloat-equal \ |
| 102 | -Wundef \ |
| 103 | -Wshadow \ |
| 104 | -Wwrite-strings \ |
| 105 | -Wsign-compare \ |
| 106 | -Wmissing-format-attribute \ |
| 107 | -Wunreachable-code \ |
| 108 | -Wcast-align \ |
| 109 | -Wcast-function-type \ |
| 110 | -Wcast-qual \ |
| 111 | -Wnull-dereference |
| 112 | |
| 113 | # Debugging/Optimization |
| 114 | ifeq ($(DEBUG), 1) |
| 115 | CFLAGS += -Og |
| 116 | else |
| 117 | CFLAGS += $(CFLAGS_OPTIMIZED) |
| 118 | endif |
| 119 | |
| 120 | # Log level is mapped to TUSB DEBUG option |
| 121 | ifneq ($(LOG),) |
| 122 | CMAKE_DEFSYM += -DLOG=$(LOG) |
| 123 | CFLAGS += -DCFG_TUSB_DEBUG=$(LOG) |
| 124 | endif |
| 125 | |
| 126 | # Logger: default is uart, can be set to rtt or swo |
| 127 | ifneq ($(LOGGER),) |
| 128 | CMAKE_DEFSYM += -DLOGGER=$(LOGGER) |
| 129 | endif |
| 130 | |
| 131 | ifeq ($(LOGGER),rtt) |
| 132 | CFLAGS += -DLOGGER_RTT -DSEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL |
| 133 | RTT_SRC = lib/SEGGER_RTT |
| 134 | INC += $(TOP)/$(RTT_SRC)/RTT |
| 135 | SRC_C += $(RTT_SRC)/RTT/SEGGER_RTT.c |
| 136 | else ifeq ($(LOGGER),swo) |
| 137 | CFLAGS += -DLOGGER_SWO |
| 138 | endif |