-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.mk
200 lines (153 loc) · 4.98 KB
/
build.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
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
.SUFFIXES:
#
# Functions
#
# String equality
eq = $(if $(and $(findstring x$(1),x$(2)),$(findstring x$(2),x$(1))),y,)
# Map relative directory names into build tree
pathmap = $(1)$(subst $(subst ,, ),/,$(foreach component,$(subst /, ,$(2)),$(if $(call eq,$(component),..),__,$(component))))$(3)
glob = $(foreach pat,$(1),$(wildcard $(pat)))
# Object and dependency filename functions
obj = $(call pathmap,$(BUILDDIR)/obj/,$(1),.o)
dep = $(call pathmap,$(BUILDDIR)/dep/,$(1),.d)
gen_s = $(call pathmap,$(BUILDDIR)/gen_src/,$(1),$(2))
gen_i = $(call pathmap,$(BUILDDIR)/gen_inc/,$(1),$(2))
gen_b = $(call pathmap,$(BUILDDIR)/gen_bin/,$(1),$(2))
# Language detection by filenames (taken from GCC docs)
is-c = $(if $(filter %.c %.i %.h,$(1)),y,)
is-cxx = $(if $(filter %.ii %.cc %.cp %.cxx %.cpp %.CPP %.c++ %.C \
%.hh %.H %.hp %.hxx %.hpp %.HPP %.h++ %.tcc,$(1)),y,)
is-asm = $(if $(filter %.s %.S %.sx,$(1)),y,)
# devkitPRO-style architecture flag overrides
arch = $(or \
$(if $(filter %.arm %.iwram,$(basename $(1))),-marm,),\
$(if $(filter %.thumb,$(basename $(1))),-mthumb,))
# Language-specific compiler flag list generation
flags = $(ALLFLAGS) $(or \
$(if $(call is-c,$(1)),$(CFLAGS),),\
$(if $(call is-cxx,$(1)),$(CXXFLAGS),),\
$(if $(call is-asm,$(1)),$(ASFLAGS),)) $(call arch,$(1))
#
# Internal Variables
#
# Verbosity
SILENT := $(if $(VERBOSE)$(V),,@)
# Toolchain prefix
TOOLCHAIN := $(DEVKITARM)/bin/arm-none-eabi
# Tools
CC := $(TOOLCHAIN)-gcc
CXX := $(TOOLCHAIN)-g++
AR := $(TOOLCHAIN)-ar
OBJCOPY := $(TOOLCHAIN)-objcopy
LD := $(if $(call is-cxx,$(SOURCES)),$(CXX),$(CC))
BIN2S := $(DEVKITPRO)/tools/bin/bin2s
GBAFIX := $(DEVKITPRO)/tools/bin/gbafix
MMUTIL := $(DEVKITPRO)/tools/bin/mmutil
GRIT := $(DEVKITPRO)/tools/bin/grit
RUNNER := mgba-qt
# Primary build artifacts
ELFFILE := $(BUILDDIR)/$(PROJECT).elf
ROMFILE := $(BUILDDIR)/$(PROJECT).gba
MAPFILE := $(BUILDDIR)/$(PROJECT).map
LIBFILE := $(BUILDDIR)/lib$(PROJECT).a
# Default compiler flags
ALLFLAGS += \
-mthumb \
-mcpu=arm7tdmi \
-mabi=aapcs \
-mfloat-abi=soft \
$(LIBDIRS:%=-I%/include) \
$(INCLUDES:%=-iquote %) \
$(if $(USE_LTO),-flto,-fno-lto) \
# Default linker flags
LDFLAGS += \
-Wl,-n \
-Wl,--gc-sections \
-Wl,-Map,$(MAPFILE) \
$(LIBDIRS:%=-L%/lib) \
$(LIBS:%=-l%) \
$(if $(USE_LTO),-flto,-fno-lto) \
# Gbafix flags
GFFLAGS := \
$(if $(strip $(ROM_TITLE)),'-t$(strip $(ROM_TITLE))',) \
$(if $(strip $(ROM_GAMECODE)),'-c$(strip $(ROM_GAMECODE))',) \
$(if $(strip $(ROM_MAKERCODE)),'-m$(strip $(ROM_MAKERCODE))',) \
$(if $(strip $(ROM_VERSION)),'-r$(strip $(ROM_VERSION))',)
SOURCES := $(call glob,$(SOURCES))
BINARY := $(call glob,$(BINARY_FILES))
AUDIO := $(call glob,$(AUDIO_FILES))
GRAPHICS:= $(filter-out %.grit,$(call glob,$(GRAPHICS)))
GEN_B := $(if $(strip $(AUDIO)),$(call gen_b,soundbank.bin,))
BINARY += $(GEN_B)
GEN_S := $(foreach bin,$(BINARY),$(call gen_s,$(bin),.S)) \
$(foreach gfx,$(GRAPHICS),$(call gen_s,$(gfx),.s))
SOURCES += $(GEN_S)
GEN_I := $(foreach bin,$(BINARY),$(call gen_i,$(bin),.h)) \
$(foreach gfx,$(GRAPHICS),$(call gen_i,$(gfx),.h)) \
$(if $(strip $(AUDIO)),$(call gen_i,soundbank.h,),)
ALLFLAGS += $(foreach dir,$(dir $(GEN_I)),-iquote $(dir))
# Build artifacts
OBJECTS := $(foreach source,$(SOURCES),$(call obj,$(source)))
DEPENDS := $(foreach source,$(SOURCES),$(call dep,$(source)))
DIRS := $(dir $(BUILDDIR) $(OBJECTS) $(DEPENDS) $(GEN_S) $(GEN_I) $(GEN_B))
#
# Targets
#
ifeq ($(strip $(BUILD_LIB)),)
$(ROMFILE): $(ELFFILE)
$(ELFFILE): $(OBJECTS)
else
$(LIBFILE): $(OBJECTS)
endif
$(OBJECTS): | dirs $(GEN_I)
define bin2s =
$(call gen_s,$(1),.S) $(call gen_i,$(1),.h) &: $(1)
@echo "bin2s $$<"
$$(SILENT)$$(BIN2S) -a 2 -H $(call gen_i,$(1),.h) $$< > $(call gen_s,$(1),.S)
endef
define grit =
$(call gen_s,$(1),.s) $(call gen_i,$(1),.h) &: $(1) $(1).grit
@echo "grit $$<"
$$(SILENT)$$(GRIT) -o$(call gen_s,$(1),.s) -ff$(basename $(1)).grit $$<
$$(SILENT)mv $(call gen_s,$(1),.h) $(call gen_i,$(1),.h)
endef
define compile =
$(call obj,$(1)): $(1)
endef
# Process binary files
$(foreach binary,$(BINARY),$(eval $(call bin2s,$(binary))))
# Process graphics files
$(foreach graphic,$(GRAPHICS),$(eval $(call grit,$(graphic))))
# Compile sources
$(foreach source,$(SOURCES),$(eval $(call compile,$(source))))
#
# Rules
#
$(BUILDDIR)/gen_bin/soundbank.bin $(BUILDDIR)/gen_inc/soundbank.h &: $(AUDIO)
@echo "mmutil $@"
$(SILENT)$(MMUTIL) $^ -o$(BUILDDIR)/gen_bin/soundbank.bin -h$(BUILDDIR)/gen_inc/soundbank.h
%.o:
@echo "compile $<"
$(SILENT)$(CC) -c -o $@ $(call flags,$<) -MMD -MP -MF $(call dep,$<) $<
%.a:
@echo "library $@"
$(SILENT)rm -f $@
$(SILENT)$(AR) rcs $@ $^
%.elf:
@echo "link $@"
$(SILENT)$(LD) -o $@ $^ $(LDFLAGS)
%.gba:
@echo "rom $@"
$(SILENT)$(OBJCOPY) -O binary $< $@
$(SILENT)$(GBAFIX) $@ $(GFFLAGS) >&-
dirs:
$(SILENT)mkdir -p $(DIRS)
$(SILENT)echo '*' > $(BUILDDIR)/.gitignore
clean:
@echo "clean $(BUILDDIR)"
$(SILENT)rm -rf $(BUILDDIR)
run: $(ELFFILE)
@echo "run $(ELFFILE)"
$(SILENT)$(RUNNER) $<
.PHONY: dirs clean run
-include $(DEPENDS)