This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefile.gnucc
260 lines (207 loc) · 7.04 KB
/
makefile.gnucc
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#**********************************************************#
#file makefile
#author Rajmund Szymanski
#date 27.02.2021
#brief LM4F120H5QR makefile.
#**********************************************************#
GNUCC :=
OPENOCD := openocd
LMFLASH := lmflash -q ek-lm4f232
QEMU := qemu-system-gnuarmeclipse -semihosting -board EK-LM4F120XL
#----------------------------------------------------------#
PROJECT ?= $(notdir $(CURDIR))
DEFS ?=
DIRS ?=
INCS ?= ./.
LIBS ?=
KEYS ?=
OPTF ?= 2 # s
SCRIPT ?=
#----------------------------------------------------------#
DEFS += gcc TARGET_IS_BLIZZARD_RA2 PART_LM4F120H5QR
KEYS += .gnucc .cortexm .lm4f *
#----------------------------------------------------------#
AS := $(GNUCC)arm-none-eabi-gcc -x assembler-with-cpp
CC := $(GNUCC)arm-none-eabi-gcc
CXX := $(GNUCC)arm-none-eabi-g++
COPY := $(GNUCC)arm-none-eabi-objcopy
DUMP := $(GNUCC)arm-none-eabi-objdump
SIZE := $(GNUCC)arm-none-eabi-size
LD := $(GNUCC)arm-none-eabi-g++
AR := $(GNUCC)arm-none-eabi-ar
GDB := $(GNUCC)arm-none-eabi-gdb
RM ?= rm -f
#----------------------------------------------------------#
DTREE = $(foreach d,$(foreach k,$(KEYS),$(wildcard $1$k)),$(dir $d) $(call DTREE,$d/))
VPATH := $(sort $(call DTREE,) $(foreach d,$(DIRS),$(call DTREE,$d/)))
#----------------------------------------------------------#
AS_EXT := .S
C_EXT := .c
CXX_EXT := .cpp
INC_DIRS := $(sort $(dir $(foreach d,$(VPATH),$(wildcard $d*.h $d*.hpp))))
LIB_DIRS := $(sort $(dir $(foreach d,$(VPATH),$(wildcard $dlib*.a $d*.ld))))
OBJ_SRCS := $(foreach d,$(VPATH),$(wildcard $d*.o))
AS_SRCS := $(foreach d,$(VPATH),$(wildcard $d*$(AS_EXT)))
C_SRCS := $(foreach d,$(VPATH),$(wildcard $d*$(C_EXT)))
CXX_SRCS := $(foreach d,$(VPATH),$(wildcard $d*$(CXX_EXT)))
LIB_SRCS := $(notdir $(foreach d,$(VPATH),$(wildcard $dlib*.a)))
ifeq ($(strip $(SCRIPT)),)
SCRIPT := $(firstword $(foreach d,$(VPATH),$(wildcard $d*.ld)))
else
SCRIPT := $(firstword $(foreach d,$(VPATH),$(wildcard $d$(SCRIPT))))
endif
ifeq ($(strip $(PROJECT)),)
PROJECT := $(notdir $(CURDIR))
endif
AS_SRCS := $(AS_SRCS:%.s=)
#----------------------------------------------------------#
BIN := $(PROJECT).bin
ELF := $(PROJECT).elf
HEX := $(PROJECT).hex
LIB := lib$(PROJECT).a
LSS := $(PROJECT).lss
MAP := $(PROJECT).map
OBJS := $(AS_SRCS:%$(AS_EXT)=%.o)
OBJS += $(C_SRCS:%$(C_EXT)=%.o)
OBJS += $(CXX_SRCS:%$(CXX_EXT)=%.o)
DEPS := $(OBJS:.o=.d)
LSTS := $(OBJS:.o=.lst)
#----------------------------------------------------------#
COMMON_F = -mthumb -mcpu=cortex-m4
ifneq ($(MAKECMDGOALS),qemu)
COMMON_F += -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffast-math
endif
COMMON_F += -O$(OPTF) -ffunction-sections -fdata-sections
ifneq ($(filter USE_LTO,$(DEFS)),)
COMMON_F += -flto
endif
COMMON_F += -Wall -Wextra -Wshadow # -Wpedantic
COMMON_F += -MD -MP
COMMON_F += # -Wa,-amhls=$(@:.o=.lst)
COMMON_F += # -g -ggdb
AS_FLAGS =
C_FLAGS =
CXX_FLAGS = -fno-rtti -fno-exceptions # -fno-use-cxa-atexit
LD_FLAGS = -Wl,-T$(SCRIPT),-Map=$(MAP),--cref,--no-warn-mismatch,--gc-sections
ifneq ($(filter main_stack_size%,$(DEFS)),)
LD_FLAGS += -Wl,--defsym=$(filter main_stack_size%,$(DEFS))
endif
ifneq ($(filter proc_stack_size%,$(DEFS)),)
LD_FLAGS += -Wl,--defsym=$(filter proc_stack_size%,$(DEFS))
endif
#----------------------------------------------------------#
ifeq ($(strip $(CXX_SRCS)),)
ifeq ($(filter USE_CRT,$(DEFS)),)
DEFS += __NOSTARTFILES
LD_FLAGS += -nostartfiles
endif
endif
ifeq ($(filter USE_STDLIB,$(DEFS)),)
LD_FLAGS += -specs=nano.specs
endif
ifneq ($(filter USE_NOHOST,$(DEFS)),)
LD_FLAGS += -specs=nosys.specs
endif
ifneq ($(filter USE_SEMIHOST,$(DEFS)),)
LD_FLAGS += -specs=rdimon.specs
endif
#----------------------------------------------------------#
DEFS_F := $(DEFS:%=-D%)
LIBS += $(LIB_SRCS:lib%.a=%)
LIBS_F := $(LIBS:%=-l%)
OBJS_ALL := $(sort $(OBJ_SRCS) $(OBJS))
INC_DIRS += $(INCS:%=%/)
INC_DIRS_F := $(INC_DIRS:%=-I%)
LIB_DIRS_F := $(LIB_DIRS:%=-L%)
AS_FLAGS += $(COMMON_F) $(DEFS_F) $(INC_DIRS_F)
C_FLAGS += $(COMMON_F) $(DEFS_F) $(INC_DIRS_F)
CXX_FLAGS += $(COMMON_F) $(DEFS_F) $(INC_DIRS_F)
LD_FLAGS += $(COMMON_F)
#----------------------------------------------------------#
#openocd command-line
#interface and board/target settings (using the OOCD target-library here)
OOCD_INIT := -f board/ek-lm4f120xl.cfg
OOCD_INIT += -c init
OOCD_INIT += -c targets
#commands to enable semihosting
OOCD_DEBG := -c "arm semihosting enable"
#commands to prepare flash-write
OOCD_SAVE := -c "reset halt"
#flash-write and -verify
OOCD_SAVE += -c "flash write_image erase $(ELF)"
OOCD_SAVE += -c "verify_image $(ELF)"
#reset target
OOCD_EXEC := -c "reset run"
#terminate OOCD after programming
OOCD_EXIT := -c shutdown
#gdb command line
DEBUG_CMD := -ex "target remote localhost:3333"
DEBUG_CMD += -ex "mon reset halt"
DEBUG_CMD += -ex "tbreak main"
DEBUG_CMD += -ex "c"
#----------------------------------------------------------#
all : $(LSS) print_elf_size
lib : $(LIB) print_size
$(ELF) : $(OBJS_ALL) $(SCRIPT)
$(info Linking target: $(ELF))
ifeq ($(strip $(SCRIPT)),)
$(error No linker script in project)
endif
$(LD) $(LD_FLAGS) $(OBJS_ALL) $(LIBS_F) $(LIB_DIRS_F) -o $@
$(LIB) : $(OBJS_ALL)
$(info Building library: $(LIB))
$(AR) -r $@ $?
$(OBJS) : $(MAKEFILE_LIST)
%.o : %$(AS_EXT)
$(info Assembling file: $<)
$(AS) $(AS_FLAGS) -c $< -o $@
%.o : %$(C_EXT)
$(info Compiling file: $<)
$(CC) $(C_FLAGS) -c $< -o $@
%.o : %$(CXX_EXT)
$(info Compiling file: $<)
$(CXX) $(CXX_FLAGS) -c $< -o $@
$(BIN) : $(ELF)
$(info Creating BIN image: $(BIN))
$(COPY) -O binary $< $@
$(HEX) : $(ELF)
$(info Creating HEX image: $(HEX))
$(COPY) -O ihex $< $@
$(LSS) : $(ELF)
$(info Creating extended listing: $(LSS))
$(DUMP) --demangle -S $< > $@
print_size :
$(info Size of modules:)
$(SIZE) -B -t --common $(OBJS_ALL)
print_elf_size : # print_size
$(info Size of target file:)
$(SIZE) -B $(ELF)
GENERATED = $(BIN) $(ELF) $(HEX) $(LIB) $(LSS) $(MAP) $(DEPS) $(LSTS) $(OBJS)
clean :
$(info Removing all generated output files)
$(RM) $(BIN) $(ELF) $(HEX) $(LIB) $(LSS) $(MAP)
$(RM) $(DEPS)
$(RM) $(LSTS)
$(RM) $(OBJS)
flash : all $(BIN)
$(info Programing device...)
$(OPENOCD) $(OOCD_INIT) $(OOCD_SAVE) $(OOCD_EXEC) $(OOCD_EXIT)
# $(LMFLASH) -v -r $(BIN)
server : all
$(info Starting server...)
$(OPENOCD) $(OOCD_INIT) $(OOCD_SAVE)
debug : all
$(info Debugging device...)
$(GDB) --nx $(DEBUG_CMD) $(ELF)
monitor : all
$(info Monitoring device...)
$(OPENOCD) $(OOCD_INIT) $(OOCD_SAVE) $(OOCD_DEBG) $(OOCD_EXEC)
qemu : all
$(info Emulating device...)
$(QEMU) -image $(ELF)
reset :
$(info Reseting device...)
$(OPENOCD) $(OOCD_INIT) $(OOCD_EXEC) $(OOCD_EXIT)
# $(LMFLASH) --hreset
.PHONY : all lib clean flash server debug monitor qemu reset
-include $(DEPS)