forked from labmicro/plantilla
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
205 lines (165 loc) · 5.14 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -------- Project path and name default values ------------------------
# Project path
# relative to this folder, leave void if the project is in this folder
PROJECT_PATH =
# Project name
PROJECT_NAME = app
# -------- Project path and name from an external projec.mk file -------
-include project.mk
# Compose project path and name
ifeq ($(PROJECT_PATH),)
PROJECT_PATH_AND_NAME=$(PROJECT_NAME)
else
PROJECT_PATH_AND_NAME=$(PROJECT_PATH)/$(PROJECT_NAME)
endif
# -------- Include config.mk file fom project --------------------------
include $(PROJECT_PATH_AND_NAME)/config.mk
# ----------------------------------------------------------------------
MODULES=$(sort $(dir $(wildcard libs/*/)))
MODULES+=$(sort $(dir $(wildcard modules/*/)))
SRC=$(wildcard $(PROJECT_PATH_AND_NAME)/src/*.c)
SRC+=$(foreach m, $(MODULES), $(wildcard $(m)/src/*.c))
CXXSRC=$(wildcard $(PROJECT_PATH_AND_NAME)/src/*.cpp)
CXXSRC+=$(foreach m, $(MODULES), $(wildcard $(m)/src/*.cpp))
ASRC=$(wildcard $(PROJECT_PATH_AND_NAME)/src/*.s)
ASRC+=$(foreach m, $(MODULES), $(wildcard $(m)/src/*.s))
OUT=$(PROJECT_PATH_AND_NAME)/out
OBJECTS=$(CXXSRC:%.cpp=$(OUT)/%.o) $(ASRC:%.s=$(OUT)/%.o) $(SRC:%.c=$(OUT)/%.o)
DEPS=$(OBJECTS:%.o=%.d)
OOCD_SCRIPT=scripts/openocd/lpc4337.cfg
TARGET=$(OUT)/$(PROJECT_NAME).elf
TARGET_BIN=$(basename $(TARGET)).bin
TARGET_LST=$(basename $(TARGET)).lst
TARGET_MAP=$(basename $(TARGET)).map
TARGET_NM=$(basename $(TARGET)).names.csv
INCLUDE_FLAGS=$(foreach m, $(MODULES), -I$(m)/inc) -I$(PROJECT_PATH_AND_NAME)/inc $(INCLUDES)
DEFINES_FLAGS=$(foreach m, $(DEFINES), -D$(m))
OPT_FLAGS=-ggdb3 -O$(OPT) -ffunction-sections -fdata-sections
LIBSDEPS=$(addprefix $(OUT)/, $(addsuffix .a, $(basename $(foreach l, $(LIBS), $(foreach m, $(MODULES), $(wildcard $(m)/lib/lib$(l).hexlib) ) ))))
COMMON_FLAGS=$(ARCH_FLAGS) $(DEFINES_FLAGS) $(INCLUDE_FLAGS) $(OPT_FLAGS)
CFLAGS=$(COMMON_FLAGS) -std=c99
CXXFLAGS=$(COMMON_FLAGS) -fno-rtti -fno-exceptions -std=c++11
LDFLAGS=$(ARCH_FLAGS)
LDFLAGS+=$(addprefix -L, $(foreach m, $(MODULES), $(wildcard $(m)/lib)))
LDFLAGS+=$(addprefix -L, $(wildcard $(dir $(LIBSDEPS))))
LDFLAGS+=$(addprefix -l, $(LIBS))
LDFLAGS+=-T$(LDSCRIPT)
LDFLAGS+=-nostartfiles -Wl,-gc-sections -Wl,-Map=$(TARGET_MAP) -Wl,--cref
ifeq ($(USE_NANO),y)
LDFLAGS+=--specs=nano.specs
endif
ifeq ($(SEMIHOST),y)
DEFINES+=USE_SEMIHOST
LDFLAGS+=--specs=rdimon.specs
endif
CROSS=arm-none-eabi-
CC=$(CROSS)gcc
CXX=$(CROSS)g++
ifeq ($(CXXSRC),)
LD=$(CROSS)gcc
else
LD=$(CROSS)g++
endif
SIZE=$(CROSS)size
LIST=$(CROSS)objdump -xdS
OBJCOPY=$(CROSS)objcopy
NM=$(CROSS)nm
GDB=$(CROSS)gdb
OOCD=openocd
ifeq ($(VERBOSE),y)
Q=
else
Q=@
endif
all: $(TARGET) $(TARGET_BIN) $(TARGET_LST) $(TARGET_NM) size
-include $(foreach m, $(MODULES), $(wildcard $(m)/module.mk))
-include $(DEPS)
$(OUT)/%.o: %.c
@echo CC $(notdir $<)
@mkdir -p $(dir $@)
$(Q)$(CC) -MMD $(CFLAGS) -c -o $@ $<
$(OUT)/%.o: %.cpp
@echo CXX $(notdir $<)
@mkdir -p $(dir $@)
$(Q)$(CXX) -MMD $(CXXFLAGS) -c -o $@ $<
$(OUT)/%.o: %.s
@echo AS $(notdir $<)
@mkdir -p $(dir $@)
$(Q)$(CC) -MMD $(CFLAGS) -c -o $@ $<
$(OUT)/%.a: %.hexlib
@echo DEBLOB $(notdir $<)
@mkdir -p $(dir $@)
$(Q)$(OBJCOPY) -I ihex -O binary $< $@
$(OUT)/linker-params: $(OBJECTS) $(LIBSDEPS) Makefile
@echo LD params
@mkdir -p $(dir $@)
$(Q)echo "-Wl,-( $(OBJECTS) -Wl,-) $(LDFLAGS)" > $@
$(TARGET): $(OUT)/linker-params
@echo LD $@...
$(Q)$(LD) -o $@ @$(OUT)/linker-params
$(TARGET_BIN): $(TARGET)
@echo COPY $(notdir $<) TO $(notdir $@)
@mkdir -p $(dir $@)
$(Q)$(OBJCOPY) -O binary $< $@
$(TARGET_LST): $(TARGET)
@echo LIST
$(Q)$(LIST) $< > $@
# If you have sed
#$(TARGET_NM): $(TARGET)
# @echo NAME
# $(Q)$(NM) -nAsSCp $< \
# | sed -r 's/(.+?\:[^ ]+) ([a-zA-Z\?] [a-zA-Z_].*)/\1 00000000 \2/' \
# | sed -r 's/(.+?)\:([a-fA-F0-9]+) ([a-fA-F0-9]+) ([a-zA-Z\?]) (.*)/\1\t0x\2\t0x\3\t\4\t\5/' \
# > $@
# If you doesn't have sed
$(TARGET_NM): $(TARGET)
@echo NAME
$(Q)$(NM) -nAsSCp $< > $@
size: $(TARGET)
$(Q)$(SIZE) $<
.download_flash: $(TARGET_BIN)
@echo DOWNLOAD to FLASH
$(Q)$(OOCD) -f $(OOCD_SCRIPT) \
-c "init" \
-c "halt 0" \
-c "flash write_image erase unlock $< 0x1A000000 bin" \
-c "reset run" \
-c "shutdown" 2>&1
.download_ram: $(TARGET_BIN)
@echo DOWNLOAD to RAM
$(Q)$(OOCD) -f $(OOCD_SCRIPT) \
-c "init" \
-c "halt 0" \
-c "load_image $< 0x20000000 bin" \
-c "reset run" \
-c "shutdown" 2>&1
ifeq ($(LOAD_INRAM),y)
download: .download_ram
else
download: .download_flash
endif
info:
@echo $(MODULES)
@echo $(INCLUDES)
erase:
@echo ERASE
$(Q)$(OOCD) -f $(OOCD_SCRIPT) \
-c "init" \
-c "halt 0" \
-c "flash erase_sector 0 0 last" \
-c "shutdown" 2>&1
debug:
@echo DEBUG
$(Q)$(OOCD) -f $(OOCD_SCRIPT) 2>&1
run: $(TARGET)
$(Q)$(OOCD) -f $(OOCD_SCRIPT) &
$(Q)socketwaiter :3333 && arm-none-eabi-gdb -batch $(TARGET) -x scripts/openocd/gdbinit
hwtest: $(TARGET)
$(Q)$(OOCD) -f $(OOCD_SCRIPT) > $(TARGET).log &
$(Q)sleep 3 && arm-none-eabi-gdb -batch $(TARGET) -x scripts/openocd/gdbinit
$(Q)cat $(TARGET).log
$(Q)cat $(TARGET).log | grep FAIL -o >/dev/null && exit 1 || exit 0
clean:
@echo CLEAN
$(Q)rm -fR $(OBJECTS) $(TARGET) $(TARGET_BIN) $(TARGET_LST) $(DEPS) $(OUT)
.PHONY: all size download erase debug clean