-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
251 lines (189 loc) · 8.63 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
MAKEFLAGS += --no-builtin-rules
# Build options can either be changed by modifying the makefile, or by building with 'make SETTING=value'
GAME ?= oot
VERSION ?= ne0
# If COMPARE is 1, check the output md5sum after building
COMPARE ?= 1
# If NON_MATCHING is 1, define the NON_MATCHING C flag when building
NON_MATCHING ?= 0
# if WERROR is 1, pass -Werror to CC_CHECK, so warnings would be treated as errors
WERROR ?= 0
# Keep .mdebug section in build
KEEP_MDEBUG ?= 0
ifeq ($(NON_MATCHING),1)
CFLAGS := -DNON_MATCHING
CPPFLAGS := -DNON_MATCHING
COMPARE := 0
endif
MAKE = make
CPPFLAGS += -P
ifeq ($(OS),Windows_NT)
DETECTED_OS=windows
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
DETECTED_OS=linux
endif
ifeq ($(UNAME_S),Darwin)
DETECTED_OS=macos
MAKE=gmake
CPPFLAGS += -xc++
endif
endif
#### Tools ####
ifeq ($(shell type mips-linux-gnu-ld >/dev/null 2>/dev/null; echo $$?), 0)
MIPS_BINUTILS_PREFIX := mips-linux-gnu-
else
$(error Please install or build mips-linux-gnu)
endif
CC := tools/ido_recomp/$(DETECTED_OS)/7.1/cc
CC_OLD := tools/ido_recomp/$(DETECTED_OS)/5.3/cc
AS := $(MIPS_BINUTILS_PREFIX)as
LD := $(MIPS_BINUTILS_PREFIX)ld
OBJCOPY := $(MIPS_BINUTILS_PREFIX)objcopy
OBJDUMP := $(MIPS_BINUTILS_PREFIX)objdump
MIPS_GCC := $(MIPS_BINUTILS_PREFIX)gcc
IINC := -Iinclude -Isrc
ifeq ($(KEEP_MDEBUG),0)
RM_MDEBUG = $(OBJCOPY) --remove-section .mdebug $@
else
RM_MDEBUG = @:
endif
ifeq ($(VERSION),ne0)
VERSION_DEFINE := NTSC_1_0
else ifeq ($(VERSION),ne1)
VERSION_DEFINE := NTSC_1_1
else ifeq ($(VERSION),ne2)
VERSION_DEFINE := NTSC_1_2
else ifeq ($(VERSION),np0)
VERSION_DEFINE := PAL_1_0
else ifeq ($(VERSION),np1)
VERSION_DEFINE := PAL_1_1
endif
# Check code syntax with host compiler
CHECK_WARNINGS := -Wall -Wextra -Wno-format-security -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-variable -Wno-missing-braces
CHECK_WARNINGS += -Werror=implicit-int -Werror=implicit-function-declaration -Werror=int-conversion -Werror=incompatible-pointer-types
CC_CHECK := gcc -fno-builtin -fsyntax-only -funsigned-char -fdiagnostics-color -std=gnu89 -D _LANGUAGE_C -D NON_MATCHING -DOOT_VERSION=$(VERSION_DEFINE) $(IINC) -nostdinc $(CHECK_WARNINGS)
CC_CHECK += -m32
CPP := cpp
PYTHON := python3
ELF2ROM ?= tools/elf2rom
MKLDSCRIPT ?= tools/mkldscript
DISASSEMBLER ?= $(PYTHON) tools/py_mips_disasm/simpleDisasm.py
ASM_PROCESSOR ?= $(PYTHON) tools/asm_processor/build.py
ASMPROCFLAGS :=
OPTFLAGS := -O2
ASFLAGS := -march=vr4300 -32 -Iinclude
MIPS_VERSION := -mips2
# we support Microsoft extensions such as anonymous structs, which the compiler does support but warns for their usage. Surpress the warnings with -woff.
CFLAGS += -DOOT_VERSION=$(VERSION_DEFINE) -G 0 -non_shared -Xfullwarn -Xcpluscomm $(IINC) -nostdinc -Wab,-r4300_mul -woff 624,649,838,712
ifneq ($(WERROR), 0)
CC_CHECK += -Werror
endif
#### Files ####
BASE_DIR := $(GAME)/$(VERSION)
# ROM image
BASE_ROM := $(GAME)/$(GAME)_$(VERSION).z64
ROM := $(GAME)_$(VERSION)_uncompressed.z64
ELF := $(BASE_DIR)/build/$(ROM:.z64=.elf)
# description of ROM segments
SPEC := $(BASE_DIR)/spec_$(VERSION)
LDSCRIPT := $(BASE_DIR)/build/ldscript_$(VERSION).txt
# create asm directories
$(shell mkdir -p $(BASE_DIR)/baserom/ $(BASE_DIR)/asm/text $(BASE_DIR)/asm/data)
SRC_DIRS := $(shell find src -type d)
ASM_DIRS := $(shell find $(BASE_DIR)/asm -type d -not -path "$(BASE_DIR)/asm/functions*")
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
S_FILES := $(foreach dir,$(ASM_DIRS),$(wildcard $(dir)/*.s))
BASEROM_FILES := $(wildcard $(BASE_DIR)/baserom/*)
O_FILES := $(subst src/,$(BASE_DIR)/build/src/,$(C_FILES:.c=.o)) \
$(subst $(BASE_DIR)/,$(BASE_DIR)/build/,$(S_FILES:.s=.o)) \
$(subst $(BASE_DIR)/,$(BASE_DIR)/build/,$(BASEROM_FILES:.bin=.o))
# Automatic dependency files
# (Only asm_processor dependencies are handled for now)
DEP_FILES := $(O_FILES:.o=.asmproc.d)
DISASM_LIST := $(shell cat $(GAME)/tables/disasm_list.txt) \
$(shell [ -f $(BASE_DIR)/tables/disasm_list.txt ] && cat $(BASE_DIR)/tables/disasm_list.txt)
CSV_FILES := $(DISASM_LIST:%=$(BASE_DIR)/tables/files_%.csv) \
$(BASE_DIR)/tables/functions.csv $(BASE_DIR)/tables/variables.csv
DISASM_TARGETS := $(DISASM_LIST:%=$(BASE_DIR)/asm/text/%/.disasm)
# create build directories
$(shell mkdir -p $(BASE_DIR)/build/baserom $(foreach dir,$(ASM_DIRS),$(subst $(BASE_DIR)/,$(BASE_DIR)/build/,$(dir))))
$(shell mkdir -p $(subst src/,$(BASE_DIR)/build/src/,$(SRC_DIRS)))
# directory flags
$(BASE_DIR)/build/src/libleo/%.o: OPTFLAGS := -O2
$(BASE_DIR)/build/src/n64dd_O2_g3/%.o: OPTFLAGS := -O2 -g3
# file flags
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_801C9B70.o: ASMPROCFLAGS := --input-enc=utf-8 --output-enc=euc-jp
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_error_headers.o: ASMPROCFLAGS := --input-enc=utf-8 --output-enc=euc-jp
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_error_bodies.o: ASMPROCFLAGS := --input-enc=utf-8 --output-enc=euc-jp
# cc & asm-processor
$(BASE_DIR)/build/src/libleo/%.o: CC := $(ASM_PROCESSOR) $(ASMPROCFLAGS) $(CC_OLD) -- $(AS) $(ASFLAGS) --
$(BASE_DIR)/build/src/%.o: CC := $(ASM_PROCESSOR) $(ASMPROCFLAGS) $(CC) -- $(AS) $(ASFLAGS) --
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_801C9B70.o: CC := $(ASM_PROCESSOR) $(ASMPROCFLAGS) $(CC) -- $(AS) $(ASFLAGS) --
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_error_headers.o: CC := $(ASM_PROCESSOR) $(ASMPROCFLAGS) $(CC) -- $(AS) $(ASFLAGS) --
$(BASE_DIR)/build/src/n64dd_O2_g3/n64dd_error_bodies.o: CC := $(ASM_PROCESSOR) $(ASMPROCFLAGS) $(CC) -- $(AS) $(ASFLAGS) --
#### Main Targets ####
uncompressed: $(ROM)
ifeq ($(COMPARE),1)
@md5sum $(ROM)
@md5sum -c $(BASE_DIR)/checksum_uncompressed_$(VERSION).md5
endif
.PHONY: all splitcsvs disasm clean
.DEFAULT_GOAL := all
all: uncompressed
$(ROM): $(ELF)
$(ELF2ROM) -cic 6105 $< $@
$(ELF): $(O_FILES) $(LDSCRIPT) $(BASE_DIR)/build/undefined_syms_$(VERSION).txt $(BASE_DIR)/build/libultra_syms.txt $(BASE_DIR)/build/hardware_regs.txt
$(LD) -T $(BASE_DIR)/build/undefined_syms_$(VERSION).txt -T $(BASE_DIR)/build/libultra_syms.txt -T $(BASE_DIR)/build/hardware_regs.txt -T $(LDSCRIPT) --no-check-sections --accept-unknown-input-arch --emit-relocs -Map $(BASE_DIR)/build/$(GAME)_$(VERSION).map -o $@
#### Main commands ####
## Cleaning
clean:
$(RM) -rf $(ROM) $(ELF) $(BASE_DIR)/build
asmclean:
$(RM) -rf $(BASE_DIR)/asm $(BASE_DIR)/context
## Extraction step
setup:
$(MAKE) -C tools
$(PYTHON) decompress_baserom.py $(GAME) $(VERSION)
$(PYTHON) extract_baserom.py $(GAME) $(VERSION)
## Assembly generation
disasm: splitcsvs $(DISASM_TARGETS)
$(PYTHON) tools/automators/gen_undefined_syms.py --version $(VERSION) > $(BASE_DIR)/undefined_syms_$(VERSION).txt
@echo "Disassembly done!"
splitcsvs: $(CSV_FILES)
diff-init: uncompressed
$(RM) -rf $(BASE_DIR)/expected/
mkdir -p $(BASE_DIR)/expected/
cp -r $(BASE_DIR)/build $(BASE_DIR)/expected/build
#### Various Recipes ####
## Linker Scripts
$(BASE_DIR)/build/undefined_syms_$(VERSION).txt: $(BASE_DIR)/undefined_syms_$(VERSION).txt
$(CPP) $(CPPFLAGS) $< > $@
$(BASE_DIR)/build/libultra_syms.txt: libultra_syms.txt
$(CPP) $(CPPFLAGS) $< > $@
$(BASE_DIR)/build/hardware_regs.txt: hardware_regs.txt
$(CPP) $(CPPFLAGS) $< > $@
$(LDSCRIPT): $(SPEC)
$(CPP) $(CPPFLAGS) $< > $(BASE_DIR)/build/spec_$(VERSION)
$(MKLDSCRIPT) $(BASE_DIR)/build/spec_$(VERSION) $@
## Baserom
$(BASE_DIR)/build/baserom/%.o: $(BASE_DIR)/baserom/%.bin
$(OBJCOPY) -I binary -O elf32-big $< $@
## Build assembly
$(BASE_DIR)/build/asm/text/%.o: $(BASE_DIR)/asm/text/%.s
$(AS) $(ASFLAGS) $< -o $@
$(BASE_DIR)/build/asm/data/%.o: $(BASE_DIR)/asm/data/%.s
iconv --from UTF-8 --to EUC-JP $< | $(AS) $(ASFLAGS) -o $@
## Build C files
$(BASE_DIR)/build/src/%.o: src/%.c
$(CC_CHECK) $<
$(CC) -c $(CFLAGS) $(MIPS_VERSION) $(OPTFLAGS) -o $@ $<
@$(OBJDUMP) -d $@ > $(@:.o=.s)
$(RM_MDEBUG)
## Disassembly
$(BASE_DIR)/asm/text/%/.disasm: $(BASE_DIR)/baserom/%.bin $(BASE_DIR)/tables/variables.csv $(BASE_DIR)/tables/functions.csv $(BASE_DIR)/tables/files_%.csv
$(RM) -rf $(BASE_DIR)/asm/text/$* $(BASE_DIR)/asm/data/$* $(BASE_DIR)/asm/functions/$* $(BASE_DIR)/context/$*.txt
$(DISASSEMBLER) $< $(BASE_DIR)/asm/text/$* -q --data-output $(BASE_DIR)/asm/data/$* --split-functions $(BASE_DIR)/asm/functions/$* --variables $(BASE_DIR)/tables/variables.csv --functions $(BASE_DIR)/tables/functions.csv --constants $(GAME)/tables/constants.csv --file-splits $(BASE_DIR)/tables/files_$*.csv --save-context $(BASE_DIR)/context/$*.txt --constants $(BASE_DIR)/tables/constants_$*.csv
@touch $@
-include $(DEP_FILES)