-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
139 lines (105 loc) · 2.8 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
137
138
139
TARGET=elog-test
DEFINES:=
INCLUDES:=inc
SRCDIR=src
SPECS=nosys nano
VERBOSE=n
OPTIMIZE=g
DEST=out
ARCH:=-mcpu=cortex-m3 -mthumb
LDSCRIPTS:=armv7m-generic.ld
LIBPATH:=lib
LIB:=c m
ELOG_HEADER:=inc/elog-internal.h
################################################################
ELF=$(addsuffix .elf, $(addprefix $(DEST)/, $(TARGET)))
MAP=$(patsubst %.elf, %.map, $(ELF))
BIN=$(patsubst %.elf, %.bin, $(ELF))
HEX=$(patsubst %.elf, %.hex, $(ELF))
LST=$(patsubst %.elf, %.lst, $(ELF))
NMP=$(patsubst %.elf, %.nmp, $(ELF))
BINLOG=$(DEST)/log_text.bin
CFLAGS:=$(ARCH)
CFLAGS+=$(addprefix -I, $(INCLUDES))
CFLAGS+=$(addprefix -D, $(DEFINES))
CFLAGS+=-ffunction-sections -fdata-sections -nostartfiles
CFLAGS+=-O$(OPTIMIZE)
CFLAGS+=-g3
LD_FLAGS:=--no-print-map-discarded
LD_FLAGS+=-Map=$(strip $(MAP))
LD_FLAGS+=--gc-sections
LD_FLAGS+=--cref
LD_FLAGS+=--print-memory-usage
COMMA:=,
LDFLAGS:=$(ARCH)
LDFLAGS+=$(addprefix -L, $(LIBPATH))
LDFLAGS+=$(addprefix -l, $(LIBS))
LDFLAGS+=$(addprefix -T, $(LDSCRIPTS))
LDFLAGS+=$(addsuffix .specs, $(addprefix -specs=, $(SPECS)))
LDFLAGS+=$(addprefix -Wl$(COMMA), $(LD_FLAGS))
LDFLAGS+=-nostartfiles
LDSCRIPT_FILES:=$(foreach l, $(LDSCRIPTS), $(foreach L, $(LIBPATH), $(wildcard $(L)/$(l))))
LSTSECTIONS:=$(foreach l, $(LDSCRIPT_FILES), $(strip $(shell cat $l | grep -E '\s\.\S+\s+\:' | cut -f 1 -d ':') ))
LSTFLAGS:=-z -x -w -t -S $(addprefix -j, $(LSTSECTIONS))
NMFLAGS:=-C -f sysv -S --size-sort -t dec
CSRC=$(foreach d, $(SRCDIR), $(wildcard $(d)/*.c))
OBJ=$(patsubst %.c, $(DEST)/%.o, $(notdir $(CSRC)))
ifeq ($(VERBOSE),y)
Q:=
else
Q:=@
endif
CROSS=arm-none-eabi-
CC=$(CROSS)gcc
LD=$(CROSS)gcc
OD=$(CROSS)objdump
OCP=$(CROSS)objcopy
SZ=$(CROSS)size
NM=$(CROSS)nm
RM=rm -fr
MD=mkdir -p
all: $(ELF) $(LST) $(NMP) $(BINLOG) $(BIN) $(HEX) build-decoder
OBJS:=$(addprefix $(DEST)/, $(patsubst %.c, %.o, $(notdir $(CSRC))))
DEPS:=$(addsuffix %.dep.mk, $(OBJS))
$(DEST):
$(Q)$(MD) $@
define cc_rule
$(DEST)/$(notdir $(patsubst %.c, %.o, $1)): $1 | $(DEST)
@echo CC $$<
$(Q)$(CC) -MMD -MP -MF $$@.dep.mk -c $(CFLAGS) -o $$@ $$<
endef
define cc_rules
$(foreach f, $(CSRC), $(eval $(call cc_rule, $f)))
endef
$(eval $(cc_rules))
$(ELF): $(OBJ)
@echo LD $@
$(Q)$(LD) -o $@ $(LDFLAGS) $(OBJS)
$(BINLOG): $(ELF)
@echo EXTRACT LOGS $@
$(Q)$(OCP) --dump-section .elog=$@ $<
@echo LOGS NOLOAD ELF $<
@$(OCP) --set-section-flags .elog=noload $<
$(BIN): $(ELF)
@echo BIN $<
$(Q)$(OCP) -O binary $< $@
$(HEX): $(ELF)
@echo HEX $<
$(Q)$(OCP) -O ihex $< $@
$(LST): $(ELF)
@echo LIST $<
$(Q)$(OD) $(LSTFLAGS) $< > $@
$(NMP): $(ELF)
@echo NM $<
$(Q)$(NM) $(NMFLAGS) $< > $@
clean:
@echo CLEAN
$(Q)$(RM) $(DEST)
$(Q)$(MAKE) -C decoder clean
%.d:
%.h:
-include $(DEPS)
build-decoder:
$(Q)$(MAKE) -C decoder ELOG_HEADER=$(dir $(realpath $(ELOG_HEADER)))
.PHONY: all clean build-decoder
.DEFAULT: all