-
Notifications
You must be signed in to change notification settings - Fork 11
/
Makefile
136 lines (103 loc) · 4.07 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
###############################################################################
# SPDX-License-Identifier: GPL-3.0-or-later
###############################################################################
.PHONY: all build cubemx application menuconfig check cloc clean
-include .config
OUTDIR = build
CBMXDIR = application/cubemx
DEFINITIONS = -DDEBUG \
-DSTM32F103xB \
-DUSE_HAL_DRIVER
###############################################################################
PROJNAME := $(notdir $(shell pwd))
DIRS := $(shell ls -R components application -Icubemx | grep : | sed 's/://')
CBMXDIRS := $(shell ls -R $(CBMXDIR) | grep : | sed 's/://')
CBMXASRC := $(wildcard $(addsuffix /*.s, $(CBMXDIR)))
CBMXLD := $(wildcard $(addsuffix /*.ld, $(CBMXDIR)))
CBMXOBJS := $(wildcard $(addsuffix /*.o, $(CBMXDIR)/build))
APPLASRC := $(wildcard $(addsuffix /*.s, $(DIRS)))
APPLCSRC := $(wildcard $(addsuffix /*.c, $(DIRS)))
APPLLIBS := $(wildcard $(addsuffix /*.a, $(DIRS)))
APPLOBJS := $(addprefix $(OUTDIR)/, $(APPLASRC:.s=.o))
APPLOBJS += $(addprefix $(OUTDIR)/, $(APPLCSRC:.c=.o))
HEADERS := $(addprefix -I", $(addsuffix ", $(DIRS)))
HEADERS += $(addprefix -I", $(addsuffix ", $(CBMXDIRS)))
LINKERS := $(addprefix -T", $(addsuffix ", $(CBMXLD)))
LIBDIRS := $(addprefix -L", $(addsuffix ", $(dir $(APPLLIBS))))
LIBS := $(addprefix -l, $(subst lib, , $(subst .a, , $(notdir $(APPLLIBS)))))
###############################################################################
CC = arm-none-eabi-gcc
OBJCOPY = arm-none-eabi-objcopy
SIZE = arm-none-eabi-size
CPU = $(word 2, $(shell grep ".cpu" $(CBMXASRC)))
CORE = -mcpu=$(CPU) -mthumb -mfloat-abi=soft
DEPS = -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@"
OPTIMIZATION = $(subst ",,$(CONFIG_C_OPTIMIZATION_LEVEL)) \
$(subst ",,$(CONFIG_C_DEBUG_LEVEL))
FLAGS = $(subst ",,$(CONFIG_USE_PRINTF_FLOAT)) \
$(subst ",,$(CONFIG_USE_SCANF_FLOAT))
ASFLAGS = $(CORE) -x assembler-with-cpp
CFLAGS = $(CORE) \
$(FLAGS) \
$(OPTIMIZATION) \
$(DEFINITIONS) \
-std=$(subst ",,$(CONFIG_C_LANGUAGE_STANDARD)) \
$(subst ",,$(CONFIG_C_DATA_SECTIONS)) \
$(subst ",,$(CONFIG_C_FUNCTION_SECTIONS)) \
$(subst ",,$(CONFIG_C_STACK_USAGE)) \
$(subst ",,$(CONFIG_C_WARNING_ALL)) \
$(subst ",,$(CONFIG_C_WARNING_ERROR)) \
$(subst ",,$(CONFIG_C_WARNING_EXTRA)) \
$(subst ",,$(CONFIG_C_WARNING_NO_UNUSED_PARAMETER)) \
$(subst ",,$(CONFIG_C_WARNING_SWITCH_DEFAULT)) \
$(subst ",,$(CONFIG_C_WARNING_SWITCH_ENUM)) \
-specs=nano.specs \
-fmessage-length=0 \
$(HEADERS) \
$(DEPS)
LDFLAGS = $(CORE) \
$(OPTIMIZATION) \
$(LINKERS) \
-Wl,-Map=$(OUTDIR)/$(PROJNAME).map \
-specs=nosys.specs \
-specs=nano.specs \
-Wl,--start-group -lc -lm -Wl,--end-group \
-Wl,--gc-sections -static
MAKEFLAGS += --no-print-directory -j
###############################################################################
all: build
build: cubemx application
cubemx:
@cd $(CBMXDIR) && $(MAKE) -j -s
application: $(OUTDIR)/$(PROJNAME).bin \
$(OUTDIR)/$(PROJNAME).siz
# Rebuild project if Makefile changed
$(APPLOBJS): $(firstword $(MAKEFILE_LIST))
$(OUTDIR)/$(PROJNAME).elf: $(APPLOBJS)
@echo linker: $@
@$(CC) $(LDFLAGS) $(CBMXOBJS) $(APPLOBJS) $(LIBDIRS) $(LIBS) -o $@
$(OUTDIR)/$(PROJNAME).bin: $(OUTDIR)/$(PROJNAME).elf
@echo binary: $@
@$(OBJCOPY) -O binary $^ $@
$(OUTDIR)/$(PROJNAME).siz: $(OUTDIR)/$(PROJNAME).elf
@$(SIZE) --format=berkeley $^
$(OUTDIR)/%.o: %.s
@echo gcc: $(@F)
@mkdir -p $(@D)
@$(CC) -c $(ASFLAGS) '$<' -o '$@'
$(OUTDIR)/%.o: %.c
@echo gcc: $(@F)
@mkdir -p $(@D)
@$(CC) -c $(CFLAGS) '$<' -o '$@'
menuconfig:
@$(MAKE) -f scripts/Makefile $@
check:
@cppcheck --force --quiet -icubemx application
cloc:
@cloc --quiet --exclude-dir=cubemx application
clean:
rm -rf $(OUTDIR)
# Dependencies
-include $(CBMXOBJS:.o=.d)
-include $(APPLOBJS:.o=.d)
###############################################################################