-
Notifications
You must be signed in to change notification settings - Fork 3
/
makefile.board
260 lines (230 loc) · 8.7 KB
/
makefile.board
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
# Compiles firmware written in C and assembler for NXP's LPC chips
# Copyright (C) 2013 Richard Meadows
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# The primary targets in this file are:
#
# [none] Compiles the source to create an .elf in the output directory
# sources Creates sources.mk from all the .c files in the src directory
# download Compiles and downloads over lpc-link
# lpc-link Blocking - Initialises an lpc-link device and acts as a debug server
# clean Removes generated files
#
# This makefile is intended to be run from the root of the project.
#
# Directories
#
# These define the locations of the source and output trees.
include dir_list.mk
# External makefile.conf
#
# Edit the project name, chip, includes directories and so on in this file.
# Nucleo build uses 'marble' settings but with NUCLEO macro defined
ifeq ($(BOARD), nucleo)
include $(BUILD_DIR)/marble.conf
USR_CFLAGS += -DNUCLEO
PROJECT_NAME:=$(PROJECT_NAME)_nucleo
else
include $(BUILD_DIR)/$(BOARD).conf
endif
OUTPUT_DIR := out_$(BOARD)
# Shell Commands
#
# Listed here for portability.
#
CAT := cat
ECHO := echo
FIND := find
GREP := grep
MKDIR := mkdir -p
RM := rm -rf
SED := sed
SHUF := shuf
# ARM GNU Toolchain
#
# These tools are available from https://launchpad.net/gcc-arm-embedded/ and
# should be placed on your path. ALternatively you could compile your own.
#
TARGET := arm-none-eabi
AS := $(TARGET)-as
CC := $(TARGET)-gcc
CXX := $(TARGET)-g++
OBJCOPY := $(TARGET)-objcopy
SIZE := $(TARGET)-size
# An alternative to gdb-multiarch is arm-none-eabi-gdb distributed with gcc-arm-none-eabi-xxxx
GDB=gdb-multiarch
# Download Tools
#
# Binaries supplied with LPCXpresso used for downloading.
#
OPENOCD = openocd
# Compilation Flags
#
# Display all warnings. Compile functions and data into their own sections so
# they can be discarded if unused. The linker performs garbage collection of
# unused input sections.
CFLAGS = $(FLAGS) -Os -std=c11 -ffunction-sections -fdata-sections $(ARCH_FLAGS) $(USR_CFLAGS)
CFLAGS += -Wall -Wextra -Wshadow -Wundef -pedantic
CFLAGS += -Wstrict-prototypes -Wwrite-strings
CFLAGS += -Wpointer-arith -Wredundant-decls -Wunreachable-code
# to do: -Wcast-align -Wcast-qual -Wmissing-prototypes
ASFLAGS = $(FLAGS_AS) $(ARCH_FLAGS)
LDFLAGS = $(FLAGS) $(LINKER_FLAGS) -Wextra $(ARCH_FLAGS)
OUTPUT_ELF=$(OUTPUT_DIR)/$(PROJECT_NAME).elf
# Default target
all: $(OUTPUT_ELF)
$(info OUTPUT_ELF=$(OUTPUT_ELF))
# Create a definitive list of sources for the project by
# combining OTHER_SOURCES with sources.mk
SOURCES := $(OTHER_SOURCES)
include $(BUILD_DIR)/sources.mk
# Translate this list of sources into a list of required objects
# in the output directory
objects = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(patsubst %.s,%.o,$(SOURCES))))
OBJECTS = $(addprefix $(OUTPUT_DIR)/,$(objects))
# Version
# I'd rather have real dependencies than abuse the .PHONY flag
src/rev.h: $(SOURCES)
(git rev-parse --short=8 HEAD | awk '{print "#define GIT_REV \"" $$1 "\""}'; \
! git diff | grep -q .; echo "#define GIT_DIRTY $$?") > $@
git rev-parse --short=8 HEAD | awk '{print "#define GIT_REV_32BIT 0x" $$1 ""}' >> $@
$(OUTPUT_DIR)/src/main.o $(OUTPUT_DIR)/src/mailbox.o: src/rev.h
# Rule for generating object and dependancy files from source files
#
# Creates a directory in the output tree if nessesary. File is only compiled,
# not linked. Dependancy generation is automatic, but only for user header
# files. Every depandancy in the .d is appended to the .d as a target, so that
# if they stop existing the corresponding object file will be re-compiled.
#
$(OUTPUT_DIR)/%.o: %.c
@$(ECHO)
@$(ECHO) 'Compiling $<...'
@$(MKDIR) $(OUTPUT_DIR)/$(dir $<)
$(CC) -c -MMD $(CPPFLAGS) $(CFLAGS) $(addprefix -I,$(INCLUDES)) -o $@ $<
@$(SED) -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' -e '/^$$/ d' -e 's/$$/ :/' < $(OUTPUT_DIR)/$*.d >> $(OUTPUT_DIR)/$*.d;
# Attempt to include the dependancy makefiles for every object in this makefile.
#
# This means that object files depend on the header files they include.
#
-include $(OBJECTS:.o=.d)
# =============== Auto-Generated Mailbox Interface ================
MKMBOX=scripts/mkmbox.py
MBOX_DEF=mailbox_def
MBOX_DOC=mailbox.md
MBOX_JSON=mailbox_map.json
MBOX_VH=mailbox_map.vh
MBOX_H=mailbox_map.h
OFFSET ?= 0
$(OUTPUT_DIR)/src/mailbox.o: $(SOURCE_DIR)/$(MBOX_DEF).c $(INCLUDE_DIR)/$(MBOX_DEF).h
$(INCLUDE_DIR)/$(MBOX_DEF).h: $(INCLUDE_DIR)/mbox.def $(MKMBOX)
python3 $(MKMBOX) -d $< -o $@
$(SOURCE_DIR)/$(MBOX_DEF).c: $(INCLUDE_DIR)/mbox.def
python3 $(MKMBOX) -d $< -o $@
$(DOC_DIR)/$(MBOX_DOC): $(INCLUDE_DIR)/mbox.def
python3 $(MKMBOX) -d $< -o $@
$(DOC_DIR)/$(MBOX_JSON): $(INCLUDE_DIR)/mbox.def
python3 $(MKMBOX) --map -d $< -o $@ --offset $(OFFSET)
$(DOC_DIR)/$(MBOX_VH): $(INCLUDE_DIR)/mbox.def
python3 $(MKMBOX) --map -d $< -o $@ --offset $(OFFSET)
$(DOC_DIR)/$(MBOX_H): $(INCLUDE_DIR)/mbox.def
python3 $(MKMBOX) --map -d $< -o $@ --offset $(OFFSET)
# Rule for generating object files from assembler files
#
# Creates a directory in the output tree if nessesary. The file is only
# assembled, not linked.
#
$(OUTPUT_DIR)/%.o: %.s
@$(ECHO)
@$(ECHO) 'Assembling $<...'
@$(MKDIR) $(OUTPUT_DIR)/$(dir $<)
$(AS) $(ASFLAGS) -o $@ $<
# Generate the main build artifact.
#
# A .elf containing all the symbols (i.e. debugging information if the compiler
# / linker was run with -g) is created, alongside .hex and .bin files. A just
# about human-readable .map is also created.
#
$(OUTPUT_ELF): $(OBJECTS) $(LINKERS) gdbscript
@$(SED) -i 's/^file.*$$/file $(OUTPUT_DIR)\/$(PROJECT_NAME)\.elf/' gdbscript
@$(ECHO)
@$(ECHO) 'Linking $@...'
$(CC) $(LDFLAGS) $(addprefix -T,$(LINKERS)) -Wl,-Map,$(@:.elf=.map) -o $@ $(OBJECTS)
@$(OBJCOPY) -O binary $@ $(@:.elf=.bin)
@$(OBJCOPY) -O ihex $@ $(@:.elf=.hex)
@$(ECHO)
$(SIZE) $@
@$(ECHO)
@$(SIZE) $@|tail -1 -|awk '{print "ROM Usage: "int(($$1+$$2)/10.24)/100"K / $(ROM_SIZE)"}'
@$(SIZE) $@|tail -1 -|awk '{print "RAM Usage: "int(($$2+$$3)/10.24)/100"K / $(RAM_SIZE)"}'
# Creates sources.mk
#
# All C and S files in the sources directory are compiled into a makefile. This
# makefile should be audited to check that only the required code is linked into
# the build.
#
.PHONY: sources
sources:
@$(ECHO) 'Building sources.mk...'
@$(ECHO)
@$(FIND) $(SOURCE_DIR)/ | $(GREP) \\.[cS]$ > sources.mk
@$(CAT) sources.mk
@$(SED) -i '1s/^/SOURCES += /' sources.mk
@$(SED) -i 's/$$/ \\/' sources.mk
# Downloads the firmware over J-Link with openocd
#
# The 'symbol-file' command in the .gdbscript has the correct symbol file
# written to it.
#
.PHONY: download
download: all
$(OPENOCD) -f $(PROG_DIR)/ocd/$(OCD_CONF) -c "program $(OUTPUT_ELF) reset exit"
.PHONY: reset
reset:
$(OPENOCD) -f $(PROG_DIR)/ocd/$(OCD_CONF) -c "init" -c "reset" -c "exit"
.PHONY: doc
doc: $(DOC_DIR)/$(MBOX_DOC) $(DOC_DIR)/$(MBOX_JSON) $(DOC_DIR)/$(MBOX_VH) $(DOC_DIR)/$(MBOX_H)
# Creates a gdb script if required
#
#
#
gdbscript:
@$(ECHO) "# Load our .elf file into GDB" >> gdbscript
@$(ECHO) "file" >> gdbscript
@$(ECHO) "# Define a target description to override the lpc-link default" >> gdbscript
@$(ECHO) "set tdesc filename arm-core.xml" >> gdbscript
@$(ECHO) "# Required for semihosting" >> gdbscript
@$(ECHO) "set mem inaccessible-by-default off" >> gdbscript
@$(ECHO) "# Connect to the debug server launched by make lpc-link" >> gdbscript
@$(ECHO) "target extended-remote" >> gdbscript
@$(ECHO) "# Enable semihosting" >> gdbscript
@$(ECHO) "monitor semihosting enable" >> gdbscript
.PHONY: gdb
gdb:
$(GDB) -ex "target remote | $(OPENOCD) -f $(PROG_DIR)/ocd/$(OCD_CONF) -c 'gdb_port pipe; log_output $(PROG_DIR)/openocd.log'" \
-x $(PROG_DIR)/gdbscript_stm32 $(OUTPUT_ELF)
# Removes everything in the output directory
#
#
#
.PHONY: clean
clean:
$(RM) $(OUTPUT_DIR)/*
$(RM) gdbscript $(SOURCE_DIR)/rev.h