-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
299 lines (237 loc) · 8.73 KB
/
Makefile
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
.SECONDEXPANSION:
.SECONDARY:
# Binaries
MAIN := main
GAME := game
OVL_AC := ac # The Ant Caves
OVL_AG := ag # The Asylum Grounds
OVL_CC := cc # The Crystal Caves
OVL_CH := ch # Cemetery Hill
OVL_CR := cr # Dan's Crypt
OVL_CREDITS := credits # Credits
OVL_DC := dc # Desecrated Church (The Hilltop Mausoleum)
OVL_EE := ee # The Enchanted Earth
OVL_EH := eh # The Entrance Hall
OVL_GG := gg # The Gallows Gauntlet
OVL_GS := gs # The Ghost Ship
OVL_GY1 := gy1 # The Graveyard
OVL_GY2 := gy2 # Return to the Graveyard
OVL_HH := hh # Hall of Heroes
OVL_HR := hr # The Haunted Ruins
OVL_IA := ia # Inside the Asylum
OLV_LA := la # The Lake
OVL_LANDMAP := landmap # The Map of Gallowmere
OVL_PD := pd # Pools of the Ancient Dead
OVL_PG := pg # Pumpkin Gorge
OVL_PS := ps # The Pumpkin Serpent
OVL_SF := sf # Scarecrow Fields
OVL_SV := sv # The Sleeping Village
OVL_TD := td # The Time Device
OVL_TL := tl # Title Level
OVL_ZL := zl # Zarok's Lair
# Compiler
CC1PSX := ./bin/cc1-2.8.1
CROSS := mipsel-linux-gnu-
AS := $(CROSS)as
CC := $(CC1PSX)
LD := $(CROSS)ld
CPP := $(CROSS)cpp
OBJCOPY := $(CROSS)objcopy
AS_FLAGS += -Iinclude -march=r3000 -mtune=r3000 -no-pad-sections -O1 -G0
PSXCC_FLAGS := -quiet -mcpu=3000 -fgnu-linker -mgas -gcoff
CC_FLAGS += -msplit-addresses -O2 -G0 -funsigned-char -w -fpeephole -ffunction-cse -fpcc-struct-return -fcommon -fverbose-asm -msoft-float -g
CPP_FLAGS += -Iinclude -undef -Wall -fno-builtin
CPP_FLAGS += -Dmips -D__GNUC__=2 -D__OPTIMIZE__ -D__mips__ -D__mips -Dpsx -D__psx__ -D__psx -D_PSYQ -D__EXTENSIONS__ -D_MIPSEL -D_LANGUAGE_C -DLANGUAGE_C -DHACKS -DUSE_INCLUDE_ASM
LD_FLAGS := -nostdlib --no-check-sections
# Directories
ASM_DIR := asm
SRC_DIR := src
ASSETS_DIR := assets
INCLUDE_DIR := include
BUILD_DIR := build
CONFIG_DIR := config
TOOLS_DIR := tools
# Tooling
MAKE := make
PYTHON := python3
SPLAT_DIR := $(TOOLS_DIR)/splat
SPLAT_APP := $(SPLAT_DIR)/split.py
SPLAT := $(PYTHON) $(SPLAT_APP)
ASMDIFFER_DIR := $(TOOLS_DIR)/asm-differ
ASMDIFFER_APP := $(ASMDIFFER_DIR)/diff.py
M2C_DIR := $(TOOLS_DIR)/m2c
M2C_APP := $(M2C_DIR)/m2c.py
M2C := $(PYTHON) $(M2C_APP)
M2C_ARGS := -P 4
MASPSX_DIR := $(TOOLS_DIR)/maspsx
MASPSX_APP := $(MASPSX_DIR)/maspsx.py
MASPSX := $(PYTHON) $(MASPSX_APP) --no-macro-inc --expand-div
# List source files
define list_src_files
$(foreach dir,$(ASM_DIR)/$(1),$(wildcard $(dir)/**.s))
$(foreach dir,$(ASM_DIR)/$(1)/data,$(wildcard $(dir)/**.s))
$(foreach dir,$(SRC_DIR)/$(1),$(wildcard $(dir)/**.c))
endef
# List object files
define list_o_files
$(foreach file,$(call list_src_files,$(1)),$(BUILD_DIR)/$(file).o)
endef
# Linking
define link
$(LD) $(LD_FLAGS) -o $(2) \
-Map $(BUILD_DIR)/$(1).map \
-T $(CONFIG_DIR)/ld/$(1).ld \
-T $(CONFIG_DIR)/symbols.txt \
-T $(CONFIG_DIR)/symbols.game.txt \
-T $(CONFIG_DIR)/undefined_syms.txt \
-T $(CONFIG_DIR)/undefined_syms_auto.$(1).txt \
-T $(CONFIG_DIR)/undefined_funcs_auto.$(1).txt
endef
# Build
all: build check
build: main game overlays
init:
$(MAKE) clean
$(MAKE) extract -j $(nproc)
$(MAKE) all
### Game Executables ###
# This is SCUS_942.27
main: main_dirs $(BUILD_DIR)/SCUS_942.27
$(BUILD_DIR)/SCUS_942.27: $(BUILD_DIR)/$(MAIN).elf
$(OBJCOPY) -O binary $< $@
$(BUILD_DIR)/$(MAIN).elf: $(call list_o_files,main)
$(call link,main,$@)
game: game_dirs $(BUILD_DIR)/MEDIEVIL.EXE
$(BUILD_DIR)/MEDIEVIL.EXE: $(BUILD_DIR)/$(GAME).elf
$(OBJCOPY) -O binary $< $@
$(BUILD_DIR)/$(GAME).elf: $(call list_o_files,game)
$(call link,game,$@)
%_dirs:
$(foreach dir,$(ASM_DIR)/$* $(ASM_DIR)/$*/data $(SRC_DIR)/$* $(ASSETS_DIR)/$*,$(shell mkdir -p $(BUILD_DIR)/$(dir)))
### Overlays ###
overlays: ac ag cc ch cr credits dc ee eh gg gs gy1 gy2 hh hr ia la landmap pd pg ps sf sv td tl zl
ac: ovlac_dirs $(BUILD_DIR)/AC.BIN
$(BUILD_DIR)/AC.BIN: $(BUILD_DIR)/ovlac.elf
$(OBJCOPY) -O binary $< $@
ag: ovlag_dirs $(BUILD_DIR)/AG.BIN
$(BUILD_DIR)/AG.BIN: $(BUILD_DIR)/ovlag.elf
$(OBJCOPY) -O binary $< $@
cc: ovlcc_dirs $(BUILD_DIR)/CC.BIN
$(BUILD_DIR)/CC.BIN: $(BUILD_DIR)/ovlcc.elf
$(OBJCOPY) -O binary $< $@
ch: ovlch_dirs $(BUILD_DIR)/CH.BIN
$(BUILD_DIR)/CH.BIN: $(BUILD_DIR)/ovlch.elf
$(OBJCOPY) -O binary $< $@
cr: ovlcr_dirs $(BUILD_DIR)/CR.BIN
$(BUILD_DIR)/CR.BIN: $(BUILD_DIR)/ovlcr.elf
$(OBJCOPY) -O binary $< $@
credits: ovlcredits_dirs $(BUILD_DIR)/CREDITS.BIN
$(BUILD_DIR)/CREDITS.BIN: $(BUILD_DIR)/ovlcredits.elf
$(OBJCOPY) -O binary $< $@
dc: ovldc_dirs $(BUILD_DIR)/DC.BIN
$(BUILD_DIR)/DC.BIN: $(BUILD_DIR)/ovldc.elf
$(OBJCOPY) -O binary $< $@
ee: ovlee_dirs $(BUILD_DIR)/EE.BIN
$(BUILD_DIR)/EE.BIN: $(BUILD_DIR)/ovlee.elf
$(OBJCOPY) -O binary $< $@
eh: ovleh_dirs $(BUILD_DIR)/EH.BIN
$(BUILD_DIR)/EH.BIN: $(BUILD_DIR)/ovleh.elf
$(OBJCOPY) -O binary $< $@
gg: ovlgg_dirs $(BUILD_DIR)/GG.BIN
$(BUILD_DIR)/GG.BIN: $(BUILD_DIR)/ovlgg.elf
$(OBJCOPY) -O binary $< $@
gs: ovlgs_dirs $(BUILD_DIR)/GS.BIN
$(BUILD_DIR)/GS.BIN: $(BUILD_DIR)/ovlgs.elf
$(OBJCOPY) -O binary $< $@
gy1: ovlgy1_dirs $(BUILD_DIR)/GY1.BIN
$(BUILD_DIR)/GY1.BIN: $(BUILD_DIR)/ovlgy1.elf
$(OBJCOPY) -O binary $< $@
gy2: ovlgy2_dirs $(BUILD_DIR)/GY2.BIN
$(BUILD_DIR)/GY2.BIN: $(BUILD_DIR)/ovlgy2.elf
$(OBJCOPY) -O binary $< $@
hh: ovlhh_dirs $(BUILD_DIR)/HH.BIN
$(BUILD_DIR)/HH.BIN: $(BUILD_DIR)/ovlhh.elf
$(OBJCOPY) -O binary $< $@
hr: ovlhr_dirs $(BUILD_DIR)/HR.BIN
$(BUILD_DIR)/HR.BIN: $(BUILD_DIR)/ovlhr.elf
$(OBJCOPY) -O binary $< $@
ia: ovlia_dirs $(BUILD_DIR)/IA.BIN
$(BUILD_DIR)/IA.BIN: $(BUILD_DIR)/ovlia.elf
$(OBJCOPY) -O binary $< $@
la: ovlla_dirs $(BUILD_DIR)/LA.BIN
$(BUILD_DIR)/LA.BIN: $(BUILD_DIR)/ovlla.elf
$(OBJCOPY) -O binary $< $@
landmap: ovllandmap_dirs $(BUILD_DIR)/LANDMAP.BIN
$(BUILD_DIR)/LANDMAP.BIN: $(BUILD_DIR)/ovllandmap.elf
$(OBJCOPY) -O binary $< $@
pd: ovlpd_dirs $(BUILD_DIR)/PD.BIN
$(BUILD_DIR)/PD.BIN: $(BUILD_DIR)/ovlpd.elf
$(OBJCOPY) -O binary $< $@
pg: ovlpg_dirs $(BUILD_DIR)/PG.BIN
$(BUILD_DIR)/PG.BIN: $(BUILD_DIR)/ovlpg.elf
$(OBJCOPY) -O binary $< $@
ps: ovlps_dirs $(BUILD_DIR)/PS.BIN
$(BUILD_DIR)/PS.BIN: $(BUILD_DIR)/ovlps.elf
$(OBJCOPY) -O binary $< $@
sf: ovlsf_dirs $(BUILD_DIR)/SF.BIN
$(BUILD_DIR)/SF.BIN: $(BUILD_DIR)/ovlsf.elf
$(OBJCOPY) -O binary $< $@
sv: ovlsv_dirs $(BUILD_DIR)/SV.BIN
$(BUILD_DIR)/SV.BIN: $(BUILD_DIR)/ovlsv.elf
$(OBJCOPY) -O binary $< $@
td: ovltd_dirs $(BUILD_DIR)/TD.BIN
$(BUILD_DIR)/TD.BIN: $(BUILD_DIR)/ovltd.elf
$(OBJCOPY) -O binary $< $@
tl: ovltl_dirs $(BUILD_DIR)/TL.BIN
$(BUILD_DIR)/TL.BIN: $(BUILD_DIR)/ovltl.elf
$(OBJCOPY) -O binary $< $@
zl: ovlzl_dirs $(BUILD_DIR)/ZL.BIN
$(BUILD_DIR)/ZL.BIN: $(BUILD_DIR)/ovlzl.elf
$(OBJCOPY) -O binary $< $@
ovl%_dirs:
$(foreach dir,$(ASM_DIR)/ovl/$* $(ASM_DIR)/ovl/$*/data $(SRC_DIR)/ovl/$* $(ASSETS_DIR)/ovl/$*,$(shell mkdir -p $(BUILD_DIR)/$(dir)))
$(BUILD_DIR)/ovl%.elf: $$(call list_o_files,ovl/$$*)
$(call link,ovl$*,$@)
# Assembly
$(BUILD_DIR)/%.s.o: %.s
$(AS) $(AS_FLAGS) -o $@ $<
$(BUILD_DIR)/%.c.o: %.c $(MASPSX_APP) $(CC1PSX)
$(CPP) $(CPP_FLAGS) -lang-c $< | $(CC) $(CC_FLAGS) $(PSXCC_FLAGS) | $(MASPSX) | $(AS) $(AS_FLAGS) -o $@
# Checksum
check:
@sha1sum --check config/medievil.check.sha
# asm-differ expected object files
expected: check
mkdir -p expected/build
rm -rf expected/build/
cp -r build/ expected/build/
# Assembly extraction
extract: extract_main extract_game extract_ovlac extract_ovlag extract_ovlcc extract_ovlch extract_ovlcr extract_ovlcredits extract_ovldc extract_ovlee extract_ovleh extract_ovlgg extract_ovlgs extract_ovlgy1 extract_ovlgy2 extract_ovlhh extract_ovlhr extract_ovlia extract_ovlla extract_ovllandmap extract_ovlpd extract_ovlpg extract_ovlps extract_ovlsf extract_ovlsv extract_ovltd extract_ovltl extract_ovlzl
## Main
extract_main:
cat $(CONFIG_DIR)/symbols.txt $(CONFIG_DIR)/symbols.main.txt > $(CONFIG_DIR)/generated.symbols.main.txt
$(SPLAT) $(CONFIG_DIR)/splat.main.yaml
## Game
extract_game:
cat $(CONFIG_DIR)/symbols.txt $(CONFIG_DIR)/symbols.game.txt > $(CONFIG_DIR)/generated.symbols.game.txt
$(SPLAT) $(CONFIG_DIR)/splat.game.yaml
## Overlays
extract_ovl%:
cat $(CONFIG_DIR)/symbols.txt $(CONFIG_DIR)/symbols.ovl$*.txt > $(CONFIG_DIR)/generated.symbols.ovl$*.txt
$(SPLAT) $(CONFIG_DIR)/splat.ovl$*.yaml
# Cleaning
clean:
@git clean -fdx asm/
@git clean -fdx config/
@git clean -fdx build/
# Formatting
format:
@./tools/format.py -j $(nproc)
checkformat:
@./tools/check_format.sh -j $(nproc)
# Phony
.PHONY: init, all, clean, format, checkformat, check, expected
.PHONY: list_src_files, list_o_files, link
.PHONY: main game ac ag cc ch cr credits dc ee eh gg gs gy1 gy2 hh hr ia la landmap pd pg ps sf sv td tl zl
.PHONY: %_dirs
.PHONY: extract, extract_%