summaryrefslogtreecommitdiff
path: root/Makefile
blob: d51d2c10ffa9a52ce216ed1425714822119fb8b5 (plain)
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
########################################################################################################################
# Dependency directories
########################################################################################################################

CMSIS_DEVICE_DIR 	?= lib/cmsis_device
CMSIS_CORE_DIR 		?= lib/cmsis_core
HAL_DIR				?= lib/hal_driver
MUSL_DIR			?= lib/musl

########################################################################################################################
# Sources
########################################################################################################################

C_SOURCES		:= src/main.c
C_SOURCES		+= src/cobs.c
C_SOURCES		+= src/clocks.c
C_SOURCES		+= src/bootloader.c
C_SOURCES		+= src/interrupts.c
C_SOURCES		+= src/system_stm32f3xx.c
C_SOURCES		+= config/fe_config.c

MUSL_SOURCES		+= string/strlen.c

C_SOURCES += $(addprefix $(MUSL_DIR)/src/,$(MUSL_SOURCES))

CXX_SOURCES			+=

BUILDDIR 	?= build
BINARY 		:= fenris_f302r8tx.elf
LDSCRIPT 	:= stm32f302r8tx.ld

########################################################################################################################
# Build parameters
########################################################################################################################

PREFIX		?= arm-none-eabi-

DEBUG		?= 1

CC			:= $(PREFIX)gcc
CXX			:= $(PREFIX)g++
LD			:= $(PREFIX)gcc
AR			:= $(PREFIX)ar
AS			:= $(PREFIX)as
SIZE		:= $(PREFIX)size
NM			:= $(PREFIX)nm
OBJCOPY		:= $(PREFIX)objcopy
OBJDUMP		:= $(PREFIX)objdump
GDB			:= $(PREFIX)gdb

HOST_CC				?= $(HOST_PREFIX)gcc
HOST_CXX			?= $(HOST_PREFIX)g++
HOST_LD				?= $(HOST_PREFIX)gcc
HOST_AR				?= $(HOST_PREFIX)ar
HOST_AS				?= $(HOST_PREFIX)as
HOST_OBJCOPY		?= $(HOST_PREFIX)objcopy
HOST_OBJDUMP		?= $(HOST_PREFIX)objdump

PYTHON3 ?= python3
DOT		?= dot

CMSIS_DIR_ABS 		:= $(abspath $(CMSIS_DIR))

ARCH_FLAGS		?= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
SYSTEM_FLAGS	?= -nostdlib -ffreestanding -nostartfiles

CFLAGS				+= -Iinclude -Iconfig
CFLAGS				+= -I$(BUILDDIR)
CFLAGS				+= -I$(CMSIS_DEVICE_DIR)/Include -I$(CMSIS_CORE_DIR)/Include
CFLAGS				+= -I$(abspath include/musl_include_shims)

CFLAGS			+= -Os -std=gnu11 -g -DSTM32F302x8 -DSTM32F3 -DDEBUG=$(DEBUG)
CFLAGS			+= $(ARCH_FLAGS) $(SYSTEM_FLAGS)
CFLAGS			+= -fno-common -ffunction-sections -fdata-sections

# for musl
CFLAGS			+= -Dhidden=

CXXFLAGS	+= -Os -g
CXXFLAGS	+= $(ARCH_FLAGS) $(SYSTEM_FLAGS)
CXXFLAGS	+= -fno-common -ffunction-sections -fdata-sections
CXXFLAGS	+= -Wall -Wextra -Wshadow -Wundef -Wredundant-decls
CXXFLAGS	+= -I.

LDFLAGS			+= $(ARCH_FLAGS) $(SYSTEM_FLAGS)

LIBS		+= -lgcc
LDFLAGS		+= -Wl,--gc-sections

LINKMEM_FLAGS ?= --trim-stubs=startup_stm32f302x8.o --trace-sections .isr_vector --highlight-subdirs $(BUILDDIR)

OBJS := $(addprefix $(BUILDDIR)/,$(C_SOURCES:.c=.o) $(CXX_SOURCES:.cpp=.o))

ALL_OBJS := $(OBJS)
ALL_OBJS += $(BUILDDIR)/startup_stm32f302x8.o

########################################################################################################################
# Rules
########################################################################################################################

all: binsize

.PHONY: binsize
binsize: $(BUILDDIR)/$(BINARY) $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.pdf)
	$(LD) -T$(LDSCRIPT) $(LDFLAGS) -Wl,--print-memory-usage -o /dev/null $(ALL_OBJS) $(LIBS)
	@echo
	@echo "▐▬▬▬▌ SyMbOL sIzE HiGhScORe LiSt ▐▬▬▬▌"
	$(NM) --print-size --size-sort --radix=d $< | tail -n 20

$(BUILDDIR)/generated: ; mkdir -p $@

.PRECIOUS: $(BUILDDIR)/$(BINARY) 
$(BUILDDIR)/$(BINARY) $(BUILDDIR)/$(BINARY:.elf=.map) &: $(ALL_OBJS)
	$(LD) -T$(LDSCRIPT) $(LDFLAGS) -o $@ -Wl,-Map=$(BUILDDIR)/$(BINARY:.elf=.map) $^ $(LIBS)

build/$(BINARY:.elf=-symbol-sizes.dot): $(ALL_OBJS)
	$(PYTHON3) tools/linkmem.py $(LINKMEM_FLAGS) $(LD) -T$(LDSCRIPT) $(LDFLAGS) $^ $(LIBS) > $@

%.pdf: %.dot
	$(DOT) -T pdf $< -o $@

%.dot: %.elf
	r2 -a arm -qc 'aa;agRd' $< 2>/dev/null >$@

$(BUILDDIR)/%.o: %.s
	mkdir -p $(@D)
	$(CC) $(COMMON_CFLAGS) $(CFLAGS) $(INT_CFLAGS) -o $@ -c $<

$(BUILDDIR)/src/%.o: src/%.c
	mkdir -p $(@D)
	$(CC) $(COMMON_CFLAGS) $(CFLAGS) $(INT_CFLAGS) -o $@ -c $<

$(BUILDDIR)/src/%.o: src/%.cpp
	mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -o $@ -c $<

$(BUILDDIR)/generated/%.o: $(BUILDDIR)/generated/%.c
	mkdir -p $(@D)
	$(CC) $(COMMON_CFLAGS) $(CFLAGS) $(INT_CFLAGS) -o $@ -c $<

$(BUILDDIR)/%.o: %.c
	mkdir -p $(@D)
	$(CC) $(COMMON_CFLAGS) $(CFLAGS) $(EXT_CFLAGS) -o $@ -c $<

clean:
	rm -rf $(BUILDDIR)/src
	rm -rf $(BUILDDIR)/generated
	rm -f $(BUILDDIR)/$(BINARY)
	rm -f $(BUILDDIR)/$(BINARY:.elf=.map)
	rm -f $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.dot)
	rm -f $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.pdf)
	rm -f $(BUILDDIR)/tools/freq_meas_test

mrproper: clean
	rm -rf build

.PHONY: clean mrproper

-include $(OBJS:.o=.d)