-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
308 lines (247 loc) · 10.7 KB
/
Makefile
File metadata and controls
308 lines (247 loc) · 10.7 KB
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
300
301
302
303
304
305
306
307
308
MAKEFLAGS += --no-builtin-rules
# Set PACKAGE_NAME define for printing commit name
ifeq ($(origin PACKAGE_NAME), undefined)
PACKAGE_NAME := "$(shell git log -1 --pretty=%s | tr -d '()`"\n' | tr -d "'" | sed 's/\"/\\\"/g')"
ifeq ($(PACKAGE_NAME),"")
PACKAGE_NAME := "Unknown name"
endif
endif
# Set PACKAGE_COMMIT_AUTHOR for printing commit author
ifeq ($(origin PACKAGE_COMMIT_AUTHOR), undefined)
PACKAGE_COMMIT_AUTHOR := "$(shell git log -1 --pretty=format:'%an' | tr -d '\n' | sed 's/\"/\\\"/g')"
ifeq ($(PACKAGE_COMMIT_AUTHOR),"")
PACKAGE_COMMIT_AUTHOR := "Unknown author"
endif
endif
# Set PACKAGE_AUTHOR define for printing author's git name
ifeq ($(origin PACKAGE_AUTHOR), undefined)
PACKAGE_AUTHOR := "$(shell git config --get user.name | tr -d '\n' | sed 's/\"/\\\"/g')"
ifeq ($(PACKAGE_AUTHOR),"")
PACKAGE_AUTHOR := "Unknown author"
endif
endif
# Set PACKAGE_VERSION define for printing commit hash
ifeq ($(origin PACKAGE_VERSION), undefined)
PACKAGE_VERSION := "$(shell git log -1 --pretty=%h | tr -d '\n' | sed 's/\"/\\\"/g')"
ifeq ($(PACKAGE_VERSION),"")
PACKAGE_VERSION := "Unknown version"
endif
endif
-include tools/print_rules.mk
# disable ds-rom output
export RUST_LOG = ds_rom::rom::rom=warn
# Ensure the build fails if a piped command fails
SHELL = /usr/bin/env bash
.SHELLFLAGS = -o pipefail -c
# path to decomp, defaults to the submodule's path
STGZ_DECOMP_DIR ?= resources/decomp
STGZ_EMULATOR ?=
# game region, only eur is supported atm
REGION := eur
COMPARE ?= 1
### project tools ###
MAKE = make
MKDIR ?= mkdir
CMAKE ?= cmake
RM ?= rm
CP ?= cp -v
# python
VENV := .venv
PYTHON ?= $(VENV)/bin/python3
# download tool (took from st decomp)
DL_TOOL := $(PYTHON) tools/download_tool.py
# rom patcher tool
ROM_PATCHER := $(PYTHON) tools/rom_patcher.py
# ds-rom
DSROM := tools/dsrom
# armips setup
ARMIPS_DIR := tools/armips
ARMIPS ?= $(ARMIPS_DIR)/out/armips
# main source/objects
BUILD_DIR := build/$(REGION)
ALL_FILES := $(shell find src/ -path "src/thumb" -prune -o -print)
ASM_FILES := $(filter %.s, $(ALL_FILES))
C_FILES := $(filter %.c, $(ALL_FILES))
CPP_FILES := $(filter %.cpp, $(ALL_FILES))
OBJ := $(foreach f,$(ASM_FILES),$(BUILD_DIR)/$(f:.s=.o)) $(foreach f,$(C_FILES),$(BUILD_DIR)/$(f:.c=.o)) $(foreach f,$(CPP_FILES),$(BUILD_DIR)/$(f:.cpp=.o)) $(BUILD_DIR)/src/thumb/thumb-$(REGION).o
DEPS := $(foreach f,$(ASM_FILES),$(BUILD_DIR)/$(f:.s=.d)) $(foreach f,$(C_FILES),$(BUILD_DIR)/$(f:.c=.d)) $(foreach f,$(CPP_FILES),$(BUILD_DIR)/$(f:.cpp=.d))
# hooks source/objects
HOOKS_BUILD_DIR := hooks/build/$(REGION)
HOOKS_SRC := $(wildcard hooks/src/*.c)
HOOKS_OBJ := $(foreach f,$(HOOKS_SRC:hooks/%=%),$(HOOKS_BUILD_DIR)/$(f:.c=.o))
HOOKS_DEPS := $(foreach f,$(HOOKS_SRC:hooks/%=%),$(HOOKS_BUILD_DIR)/$(f:.c=.d))
HOOKS_GAME_SRC := $(wildcard hooks/src/*.cpp)
HOOKS_GAME_OBJ := $(foreach f,$(HOOKS_GAME_SRC:hooks/%=%),$(HOOKS_BUILD_DIR)/$(f:.cpp=.o))
HOOKS_GAME_DEPS := $(foreach f,$(HOOKS_GAME_SRC:hooks/%=%),$(HOOKS_BUILD_DIR)/$(f:.cpp=.d))
ALL_DEPS := $(DEPS) $(HOOKS_DEPS) $(HOOKS_GAME_DEPS)
# region addresses
ifeq ($(REGION),eur)
OVL018_ADDR := 0x020C4840
OVLGZ_ADDR := 0x0218A380
HOOK_INIT := 0x020C4DD0
HOOK_UPDATE := 0x02013464
MAIN_ADDR := 0x02000BC8
OVERLAY_0_SLOT_ADDR := 0x02043E50 # in reality this is the address of gOverlayManager
HOOKS_ADDR := 0x01FFFE20
HOOKS_GAME_ADDR := 0x02013394
else
$(error "Region not supported: $(REGION)")
endif
OVLGZ_SIZE := 0x20000
HOOKS_SIZE := 0x1E0
RESERVED_SIZE := 0x10
RESERVED_ADDR := $(shell $(PYTHON) -c "print(f'0x{$(OVLGZ_ADDR) + $(OVLGZ_SIZE) - $(RESERVED_SIZE):08X}')")
# compiler settings
CFLAGS_BASE := -marm -mthumb-interwork -march=armv5te -mtune=arm946e-s -nostdlib -nodefaultlibs -nostartfiles
CC := arm-none-eabi-gcc $(CFLAGS_BASE)
CXX := arm-none-eabi-g++ $(CFLAGS_BASE)
WARNINGS := -Wall -Wno-multichar -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-unused-variable
INCLUDES := -I include -I $(STGZ_DECOMP_DIR)/include -I $(STGZ_DECOMP_DIR)/libs/c/include
CPP_DEFINES := -DGZ_OVL_ID=114 -DPACKAGE_VERSION='$(PACKAGE_VERSION)' -DPACKAGE_NAME='$(PACKAGE_NAME)' -DPACKAGE_COMMIT_AUTHOR='$(PACKAGE_COMMIT_AUTHOR)' -DPACKAGE_AUTHOR='$(PACKAGE_AUTHOR)'
CFLAGS := -Os -fno-short-enums -fomit-frame-pointer -ffast-math -fno-builtin -fshort-wchar -MMD -MP $(WARNINGS) $(INCLUDES) $(CPP_DEFINES)
CPP_FLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=c++2c
ELF := $(BUILD_DIR)/ovgz.elf
BIN := $(ELF:.elf=.bin)
MAP := $(ELF:.elf=.map)
LD := $(CC)
LDFLAGS := -T libs/ovgz.ld -Llibs -lst-$(REGION) -Wl,-Map,$(MAP) -specs=nosys.specs -Wl,--gc-sections -Wl,--defsym=OVLGZ_ADDR=$(OVLGZ_ADDR) -Wl,--defsym=OVLGZ_SIZE=$(OVLGZ_SIZE) -Wl,--defsym=RESERVED_SIZE=$(RESERVED_SIZE)
OBJCOPY := tools/binutils/arm-none-eabi-objcopy
HOOKS_ELF := $(HOOKS_BUILD_DIR)/hooks.elf
HOOKS_BIN := $(HOOKS_ELF:.elf=.bin)
HOOKS_MAP := $(HOOKS_ELF:.elf=.map)
HOOKS_LD := $(CC)
HOOKS_LDFLAGS := -T hooks/hooks.ld -Llibs -lst-$(REGION) -lgz -specs=nosys.specs -Wl,--gc-sections
HOOKS_GAME_ELF := $(HOOKS_BUILD_DIR)/game.elf
HOOKS_GAME_BIN := $(HOOKS_GAME_ELF:.elf=.bin)
HOOKS_GAME_MAP := $(HOOKS_GAME_ELF:.elf=.map)
# create output directories
$(shell $(MKDIR) -p $(BUILD_DIR)/src/thumb)
$(shell $(MKDIR) -p $(HOOKS_BUILD_DIR)/src)
### project settings ###
EXTRACT_DIR := extract
EXTRACTED_DIR := $(EXTRACT_DIR)/$(REGION)
OUT_ROM := stgz.nds
EXTRACTED_REL := ../../../$(EXTRACTED_DIR)
ARMIPS_ARGS ?= \
-strequ OVL018_BIN "$(EXTRACTED_REL)/arm9_overlays/ov018.bin" \
-strequ OVL018_MOD_BIN "$(EXTRACTED_REL)/arm9_overlays/ov018_mod.bin" \
-strequ ARM9_BIN "$(EXTRACTED_REL)/arm9/arm9_patched.bin" \
-strequ ARM9_MOD_BIN "$(EXTRACTED_REL)/arm9/arm9_mod.bin" \
-strequ ITCM_BIN "$(EXTRACTED_REL)/arm9/itcm.bin" \
-strequ ITCM_MOD_BIN "$(EXTRACTED_REL)/arm9/itcm_mod.bin" \
-equ OVL018_ADDR $(OVL018_ADDR) \
-equ HOOKS_SIZE $(HOOKS_SIZE) \
-equ HOOKS_ADDR $(HOOKS_ADDR) \
-equ HOOKS_GAME_ADDR $(HOOKS_GAME_ADDR) \
-equ HOOK_UPDATE $(HOOK_UPDATE) \
-equ HOOK_INIT $(HOOK_INIT)
### project targets ###
all: build
$(call print_no_args,Assembling the rom...)
$(V)$(DSROM) build --config $(EXTRACTED_DIR)/config.yaml --rom $(OUT_ROM)
$(call print_no_args,Success!)
@$(PRINT) "==== Build Options ====$(NO_COL)\n"
@$(PRINT) "${GREEN}Game Region: $(BLUE)$(REGION)$(NO_COL)\n"
@$(PRINT) "${GREEN}Rom Path: $(BLUE)$(OUT_ROM)$(NO_COL)\n"
@$(PRINT) "${GREEN}Code Version: $(BLUE)$(PACKAGE_VERSION)$(NO_COL)\n"
@$(PRINT) "${GREEN}Build Author: $(BLUE)$(PACKAGE_AUTHOR)$(NO_COL)\n"
@$(PRINT) "${GREEN}Commit Author: $(BLUE)$(PACKAGE_COMMIT_AUTHOR)$(NO_COL)\n"
@$(PRINT) "${BLINK}Build succeeded.\n$(NO_COL)"
build: hooks
$(call print_no_args,Patching the game...)
$(V)$(ROM_PATCHER) -e $(EXTRACTED_DIR) -o $(OBJ) -m $(OVLGZ_SIZE) -j $(HOOKS_OBJ) -n $(HOOKS_SIZE) -a $(OVLGZ_ADDR) -d $(HOOKS_BUILD_DIR) --elf $(ELF) --map $(MAP) --hooks_bin $(HOOKS_BIN) --hooks_elf $(HOOKS_ELF) --hooks_game_bin $(HOOKS_GAME_BIN)
$(call print_no_args,Applying hooks and adding new code...)
$(V)$(ARMIPS) $(HOOKS_BUILD_DIR)/setup.asm $(ARMIPS_ARGS)
$(call print_no_args,Success!)
clean:
$(V)$(RM) -r $(BUILD_DIR) $(HOOKS_BUILD_DIR)
$(V)$(RM) $(OUT_ROM)
$(call print_no_args,Success!)
distclean: clean
$(V)$(RM) -r $(EXTRACTED_DIR)
$(call print_no_args,Success!)
extract:
$(call print_no_args,Extracting the rom...)
$(V)$(DSROM) extract --rom $(EXTRACT_DIR)/baserom_st_$(REGION).nds --path $(EXTRACTED_DIR)
$(call print_no_args,Success!)
hooks: overlay $(HOOKS_BIN) $(HOOKS_GAME_BIN)
init: venv
$(call print_no_args,Verifying baserom checksum...)
ifeq ($(COMPARE),1)
$(V)sha1sum -c $(EXTRACT_DIR)/baserom_st_$(REGION).sha1
endif
$(V)$(DL_TOOL) -p tools/ dsrom v0.6.1
$(V)$(DL_TOOL) -p tools/ binutils arm-2.42-0
ifeq ("$(wildcard $(ARMIPS_DIR))", "")
$(error armips not found!)
else
ifeq ("$(wildcard $(ARMIPS_DIR)/out)", "")
$(call print_no_args,Building armips...)
$(V)$(MKDIR) $(ARMIPS_DIR)/out && cd $(ARMIPS_DIR)/out && $(CMAKE) -DCMAKE_BUILD_TYPE=Release .. && $(CMAKE) --build .
endif
endif
libs:
$(call print_no_args,Generating game symbol library...)
$(V)$(PYTHON) tools/gen_libs.py -m libst -d $(STGZ_DECOMP_DIR)
$(call print_no_args,Success!)
overlay: $(BIN)
$(call print_no_args,Generating stgz symbol library...)
$(V)$(PYTHON) tools/gen_libs.py -m libgz -e $(ELF)
$(call print_no_args,Success!)
run: all
ifeq ($(STGZ_EMULATOR),)
$(error "Emulator path not set.")
endif
$(STGZ_EMULATOR) $(OUT_ROM)
setup: extract
venv:
# Create the virtual environment if it doesn't exist.
# Delete the virtual environment directory if creation fails.
$(call print_no_args,Creating python virtual environment...)
$(V)test -d $(VENV) || python3 -m venv $(VENV) || { rm -rf $(VENV); false; }
$(V)$(PYTHON) -m pip install -U pip
$(V)$(PYTHON) -m pip install -U -r tools/requirements.txt
$(call print_no_args,Success!)
.PHONY: all build clean distclean extract hooks init libs overlay run setup venv
### misc project recipes ###
# add dependencies
-include $(ALL_DEPS)
## process auto-generated thumb definitions (necessary to avoid crashes when calling thumb functions) ##
$(BUILD_DIR)/src/thumb/thumb-$(REGION).o: src/thumb/thumb-$(REGION).s
$(V)$(CC) $(CFLAGS) -fverbose-asm -Os -x assembler-with-cpp -fomit-frame-pointer -c "$<" -o "$@"
## process source files ##
$(BUILD_DIR)/src/%.o: src/%.s
$(call print_two_args,Assembling:,$<,$@)
$(V)$(CC) $(CFLAGS) -fverbose-asm -Os -x assembler-with-cpp -fomit-frame-pointer -c "$<" -o "$@"
$(BUILD_DIR)/src/%.o: src/%.c
$(call print_two_args,Compiling:,$<,$@)
$(V)$(CC) $(CFLAGS) -c "$<" -o "$@"
$(BUILD_DIR)/src/%.o: src/%.cpp
$(call print_two_args,Compiling:,$<,$@)
$(V)$(CXX) $(CPP_FLAGS) -c "$<" -o "$@"
$(HOOKS_BUILD_DIR)/src/%.o: hooks/src/%.c
$(call print_two_args,Compiling hooks:,$<,$@)
$(V)$(CC) $(CFLAGS) -DOVERLAY_0_SLOT_ADDR=$(OVERLAY_0_SLOT_ADDR) -c "$<" -o "$@"
$(HOOKS_BUILD_DIR)/src/%.o: hooks/src/%.cpp
$(call print_two_args,Compiling hooks:,$<,$@)
$(V)$(CXX) $(CPP_FLAGS) -c "$<" -o "$@"
## process build artifacts ##
$(ELF): $(OBJ)
$(call print_one_arg,Linking:,$@)
$(V)$(LD) -o $@ $^ $(LDFLAGS)
$(BIN): $(ELF)
$(call print_two_args,Wrapping binary to ELF:,$<,$@)
$(V)$(OBJCOPY) -S -O binary $< $@
$(V)$(CP) $@ $(EXTRACTED_DIR)/arm9_overlays/ovgz.bin
$(HOOKS_ELF): $(HOOKS_OBJ)
$(call print_one_arg,Linking hooks:,$@)
$(V)$(LD) -o $@ $^ $(HOOKS_LDFLAGS) -Wl,-Map,$(HOOKS_MAP) -Wl,--defsym=HOOKS_ADDR=$(HOOKS_ADDR)
$(HOOKS_BIN): $(HOOKS_ELF)
$(call print_two_args,Wrapping hooks binary to ELF:,$<,$@)
$(V)$(OBJCOPY) -S -O binary $< $@
$(HOOKS_GAME_ELF): $(HOOKS_GAME_OBJ)
$(call print_one_arg,Linking hooks:,$@)
$(V)$(LD) -o $@ $^ $(HOOKS_LDFLAGS) -Wl,-Map,$(HOOKS_GAME_MAP) -Wl,--defsym=HOOKS_ADDR=$(HOOKS_GAME_ADDR)
$(HOOKS_GAME_BIN): $(HOOKS_GAME_ELF)
$(call print_two_args,Wrapping hooks binary to ELF:,$<,$@)
$(V)$(OBJCOPY) -S -O binary $< $@