forked from IOsetting/py32f0-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.mk
139 lines (116 loc) · 4.38 KB
/
rules.mk
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
# 'make V=1' will show all compiler calls.
V ?= 0
ifeq ($(V),0)
Q := @
NULL := 2>/dev/null
endif
PREFIX ?= $(ARM_TOOCHAIN)/arm-none-eabi-
CC = $(PREFIX)gcc
XX = $(PREFIX)g++
AS = $(PREFIX)as
LD = $(PREFIX)ld
OBJCOPY = $(PREFIX)objcopy
# `$(shell pwd)` or `.`, both works
TOP = $(PWD)
BDIR = $(TOP)/$(BUILD_DIR)
# For each direcotry, add it to csources
CSOURCES := $(foreach dir, $(CDIRS), $(shell find $(TOP)/$(dir) -maxdepth 1 -name '*.c'))
# Add single c source files to csources
CSOURCES += $(addprefix $(TOP)/, $(CFILES))
# C++ files
CPPSOURCES := $(foreach dir, $(CDIRS), $(shell find $(TOP)/$(dir) -maxdepth 1 -name '*.cpp'))
CPPSOURCES += $(addprefix $(TOP)/, $(CPPFILES))
# Then assembly source folders and files
ASOURCES := $(foreach dir, $(ADIRS), $(shell find $(TOP)/$(dir) -maxdepth 1 -name '*.s'))
ASOURCES += $(addprefix $(TOP)/, $(AFILES))
# Fill object files with c and asm files (keep source directory structure)
OBJS = $(CSOURCES:$(TOP)/%.c=$(BDIR)/%.o)
OBJS += $(CPPSOURCES:$(TOP)/%.cpp=$(BDIR)/%.o)
OBJS += $(ASOURCES:$(TOP)/%.s=$(BDIR)/%.o)
# d files for detecting h file changes
DEPS=$(CSOURCES:$(TOP)/%.c=$(BDIR)/%.d)
# Arch and target specified flags
ARCH_FLAGS := -mthumb -mcpu=cortex-m0plus
# Debug options, -gdwarf-2 for debug, -g0 for release
# https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Debugging-Options.html
# -g: system’s native format, -g0:off, -g/g1,-g2,-g3 -> more verbosely
# -ggdb: for gdb, -ggdb0:off, -ggdb/ggdb1,-ggdb2,-ggdb3 -> more verbosely
# -gdwarf: in DWARF format, -gdwarf-2,-gdwarf-3,-gdwarf-4,-gdwarf-5
DEBUG_FLAGS ?= -gdwarf-3
OPT ?= -Og
# C flags
TGT_CFLAGS ?= $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) -std=c99 $(addprefix -D, $(LIB_FLAGS)) -Wall -ffunction-sections -fdata-sections
# C++ flags
TGT_CPPFLAGS ?= $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) -std=c++11 $(addprefix -D, $(LIB_FLAGS)) -Wall -ffunction-sections -fdata-sections
# ASM flags
TGT_ASFLAGS ?= $(ARCH_FLAGS) $(DEBUG_FLAGS) $(OPT) -Wa,--warn
# LD flags
TGT_LDFLAGS ?= $(ARCH_FLAGS) -specs=nano.specs -specs=nosys.specs -static -lc -lm \
-Wl,-Map=$(BDIR)/$(PROJECT).map \
-Wl,--gc-sections \
-Wl,--print-memory-usage
GCC_VERSION := $(shell $(CC) -dumpversion)
IS_GCC_ABOVE_12 := $(shell expr "$(GCC_VERSION)" ">=" "12")
ifeq "$(IS_GCC_ABOVE_12)" "1"
TGT_LDFLAGS += -Wl,--no-warn-rwx-segments
endif
ifeq ($(ENABLE_PRINTF_FLOAT),y)
TGT_LDFLAGS += -u _printf_float
endif
# include paths
TGT_INCFLAGS := $(addprefix -I $(TOP)/, $(INCLUDES))
.PHONY: all clean flash echo
all: fullcheck $(BDIR)/$(PROJECT).elf $(BDIR)/$(PROJECT).bin $(BDIR)/$(PROJECT).hex
fullcheck:
@if [ '$(findstring PY32F07,$(MCU_TYPE))' = 'PY32F07' ] && [ '$(USE_LL_LIB)' = 'y' ]; then \
echo "LL for PY32F07x is not supported yet"; \
return 1; \
fi
# for debug
echo:
$(info 1. $(AFILES))
$(info 2. $(ASOURCES))
$(info 3. $(CSOURCES))
$(info 4. $(OBJS))
$(info 5. $(TGT_INCFLAGS))
# include d files without non-exist warning
-include $(DEPS)
# Compile c to obj -- should be `$(BDIR)/%.o: $(TOP)/%.c`, but since $(TOP) is base folder so non-path also works
$(BDIR)/%.o: %.c
@printf " CC\t$<\n"
@mkdir -p $(dir $@)
$(Q)$(CC) $(TGT_CFLAGS) $(TGT_INCFLAGS) -MT $@ -o $@ -c $< -MD -MF $(BDIR)/$*.d -MP
$(BDIR)/%.o: %.cpp
@printf " XX\t$<\n"
@mkdir -p $(dir $@)
$(Q)$(XX) $(TGT_CPPFLAGS) $(TGT_INCFLAGS) -MT $@ -o $@ -c $< -MD -MF $(BDIR)/$*.d -MP
# Compile asm to obj
$(BDIR)/%.o: %.s
@printf " AS\t$<\n"
@mkdir -p $(dir $@)
$(Q)$(CC) $(TGT_ASFLAGS) -o $@ -c $<
# Link object files to elf
$(BDIR)/$(PROJECT).elf: $(OBJS) $(TOP)/$(LDSCRIPT)
@printf " LD\t$(LDSCRIPT) -> $@\n"
$(Q)$(CC) $(TGT_LDFLAGS) -T$(TOP)/$(LDSCRIPT) $(OBJS) -o $@
# Convert elf to bin
%.bin: %.elf
@printf " OBJCP BIN\t$@\n"
$(Q)$(OBJCOPY) -I elf32-littlearm -O binary $< $@
# Convert elf to hex
%.hex: %.elf
@printf " OBJCP HEX\t$@\n"
$(Q)$(OBJCOPY) -I elf32-littlearm -O ihex $< $@
clean:
rm -rf $(BDIR)/*
flash:
ifeq ($(FLASH_PROGRM),jlink)
$(JLINKEXE) -device $(JLINK_DEVICE) -if swd -speed 4000 -JLinkScriptFile $(TOP)/Misc/jlink-script -CommanderScript $(TOP)/Misc/jlink-command
else ifeq ($(FLASH_PROGRM),pyocd)
$(PYOCD_EXE) erase -t $(PYOCD_DEVICE) --chip --config $(TOP)/Misc/pyocd.yaml
$(PYOCD_EXE) load $(BDIR)/$(PROJECT).hex -t $(PYOCD_DEVICE) --config $(TOP)/Misc/pyocd.yaml
else ifeq ($(FLASH_PROGRM),stm32flash)
$(FLASH_PROGRM) -w $(BDIR)/$(PROJECT).hex -vg 0x00 $(SERIAL_DEV)
else
@echo "FLASH_PROGRM is invalid\n"
endif