forked from pret/pokeheartgold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.mk
235 lines (182 loc) · 7.87 KB
/
common.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
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
## Common defines for ARM9 and ARM7 Makefiles ##
COMPARE ?= 1
default: all
PROJECT_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# Because mwldarm expects absolute paths to be WIN32 paths,
# all paths referring up from BUILD_DIR must be relative.
WORK_DIR := $(shell realpath --relative-to $(CURDIR) $(PROJECT_ROOT))
$(shell mkdir -p $(BUILD_DIR))
BACK_REL := $(shell realpath --relative-to $(BUILD_DIR) $(CURDIR))
# Recursive wildcard function
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
TOOLSDIR := $(WORK_DIR)/tools
include $(WORK_DIR)/platform.mk
include $(WORK_DIR)/binutils.mk
# NitroSDK tools
MWCC = $(TOOLSDIR)/mwccarm/$(MWCCVER)/mwccarm.exe
MWAS = $(TOOLSDIR)/mwccarm/$(MWCCVER)/mwasmarm.exe
MWLD = $(BACK_REL)/$(TOOLSDIR)/mwccarm/$(MWCCVER)/mwldarm.exe
MAKEROM := $(TOOLSDIR)/bin/makerom.exe
MAKELCF := $(TOOLSDIR)/bin/makelcf.exe
MAKEBNR := $(TOOLSDIR)/bin/makebanner.exe
NTRCOMP := $(TOOLSDIR)/bin/ntrcomp.exe
export LM_LICENSE_FILE := $(TOOLSDIR)/mwccarm/license.dat
# Native tools
JSONPROC := $(TOOLSDIR)/jsonproc/jsonproc$(EXE)
GFX := $(TOOLSDIR)/nitrogfx/nitrogfx$(EXE)
FIXROM := $(TOOLSDIR)/fixrom/fixrom$(EXE)
KNARC := $(TOOLSDIR)/knarc/knarc$(EXE)
O2NARC := $(TOOLSDIR)/o2narc/o2narc$(EXE)
MSGENC := $(TOOLSDIR)/msgenc/msgenc$(EXE)
ASPATCH := $(TOOLSDIR)/mwasmarm_patcher/mwasmarm_patcher$(EXE)
CSV2BIN := $(TOOLSDIR)/csv2bin/csv2bin$(EXE)
MKFXCONST := $(TOOLSDIR)/gen_fx_consts/gen_fx_consts$(EXE)
MOD123ENCRY := $(TOOLSDIR)/mod123encry/mod123encry$(EXE)
# Decompiled NitroSDK tools
COMPSTATIC := $(TOOLSDIR)/compstatic/compstatic$(EXE)
NTRMERGE := $(TOOLSDIR)/ntr_merge_elf/ntr_merge_elf.sh
ASM_PROCESSOR := $(TOOLSDIR)/asm_processor/compile.sh
NATIVE_TOOLS := \
$(JSONPROC) \
$(GFX) \
$(FIXROM) \
$(KNARC) \
$(O2NARC) \
$(MSGENC) \
$(ASPATCH) \
$(CSV2BIN) \
$(MKFXCONST) \
$(COMPSTATIC) \
$(MOD123ENCRY)
TOOLDIRS := $(foreach tool,$(NATIVE_TOOLS),$(dir $(tool)))
# Directories
NITROSDK_SRC_SUBDIRS := os
LIB_SUBDIRS := cw NitroSDK NitroSystem NitroDWC NitroWiFi libCPS libVCT
SRC_SUBDIR := src
ASM_SUBDIR := asm
LIB_SRC_SUBDIR := lib/src $(LIB_SUBDIRS:%=lib/%/src) $(NITROSDK_SRC_SUBDIRS:%=lib/NitroSDK/src/%)
LIB_ASM_SUBDIR := lib/asm $(LIB_SUBDIRS:%=lib/%/asm)
ALL_SUBDIRS := $(SRC_SUBDIR) $(ASM_SUBDIR) $(LIB_SRC_SUBDIR) $(LIB_ASM_SUBDIR)
SRC_BUILDDIR := $(addprefix $(BUILD_DIR)/,$(SRC_SUBDIR))
ASM_BUILDDIR := $(addprefix $(BUILD_DIR)/,$(ASM_SUBDIR))
LIB_SRC_BUILDDIR := $(addprefix $(BUILD_DIR)/,$(LIB_SRC_SUBDIR))
LIB_ASM_BUILDDIR := $(addprefix $(BUILD_DIR)/,$(LIB_ASM_SUBDIR))
C_SRCS := $(call rwildcard,src,*.c)
ASM_SRCS := $(foreach dname,$(ASM_SUBDIR),$(wildcard $(dname)/*.s))
GLOBAL_ASM_SRCS != grep -rl 'GLOBAL_ASM(' $(C_SRCS)
LIB_C_SRCS := $(foreach dname,$(LIB_SRC_SUBDIR),$(wildcard $(dname)/*.c))
LIB_ASM_SRCS := $(foreach dname,$(LIB_ASM_SUBDIR),$(wildcard $(dname)/*.s))
ALL_SRCS := $(C_SRCS) $(ASM_SRCS) $(GLOBAL_ASM_SRCS) $(LIB_C_SRCS) $(LIB_ASM_SRCS)
C_OBJS = $(C_SRCS:%.c=$(BUILD_DIR)/%.o)
ASM_OBJS = $(ASM_SRCS:%.s=$(BUILD_DIR)/%.o)
GLOBAL_ASM_OBJS = $(GLOBAL_ASM_SRCS:%.c=$(BUILD_DIR)/%.o)
LIB_C_OBJS = $(LIB_C_SRCS:%.c=$(BUILD_DIR)/%.o)
LIB_ASM_OBJS = $(LIB_ASM_SRCS:%.s=$(BUILD_DIR)/%.o)
ALL_GAME_OBJS = $(C_OBJS) $(ASM_OBJS) $(GLOBAL_ASM_OBJS)
ALL_LIB_OBJS = $(LIB_C_OBJS) $(LIB_ASM_OBJS)
ALL_OBJS = $(ALL_GAME_OBJS) $(ALL_LIB_OBJS)
$(ALL_LIB_OBJS): DEFINES = $(GLB_DEFINES)
ALL_BUILDDIRS := $(sort $(ALL_BUILDDIRS) $(foreach obj,$(ALL_OBJS),$(dir $(obj))))
NEF := $(BUILD_DIR)/$(NEFNAME).nef
ELF := $(NEF:%.nef=%.elf)
LCF := $(NEF:%.nef=%.lcf)
RESPONSE := $(NEF:%.nef=%.response)
SBIN := $(NEF:%.nef=%.sbin)
XMAP := $(NEF).xMAP
EXCCFLAGS := -Cpp_exceptions off
MWCFLAGS = $(DEFINES) $(OPTFLAGS) -enum int -lang c99 $(EXCCFLAGS) -gccext,on -proc $(PROC) -msgstyle gcc -gccinc -i ./include -i ./include/library -i $(WORK_DIR)/files -I$(WORK_DIR)/lib/include -ipa file -interworking -inline on,noauto -char signed
MWASFLAGS = $(DEFINES) -proc $(PROC_S) -gccinc -i . -i ./include -i $(WORK_DIR)/files -I$(WORK_DIR)/lib/include -DSDK_ASM
MWLDFLAGS := -w off -proc $(PROC) -nopic -nopid -interworking -map closure,unused -symtab sort -m _start -msgstyle gcc
ARFLAGS := rcS
$(C_OBJS): MWCFLAGS += -include global.h
MW_COMPILE = $(WINE) $(MWCC) $(MWCFLAGS)
MW_ASSEMBLE = $(WINE) $(MWAS) $(MWASFLAGS)
export MWCIncludes := lib/include
LSF := $(addsuffix .lsf,$(NEFNAME))
ifneq ($(LSF),)
OVERLAYS := $(shell $(GREP) -o "^Overlay \w+" $(LSF) | cut -d' ' -f2)
else
OVERLAYS :=
endif
# Make sure build directories exist before compiling anything
DUMMY := $(shell mkdir -p $(ALL_BUILDDIRS))
.SECONDARY:
.SECONDEXPANSION:
.DELETE_ON_ERROR:
.PHONY: all tidy clean tools clean-tools $(TOOLDIRS)
.PRECIOUS: $(SBIN)
.NOTPARALLEL:
.PHONY: $(MWAS)
$(MWAS):
$(ASPATCH) -q $@
all: tools
ifeq ($(NODEP),)
ifneq ($(WINPATH),)
PROJECT_ROOT_NT := $(shell $(WINPATH) -w $(PROJECT_ROOT) | $(SED) 's/\\/\//g')
define fixdep
$(SED) -i 's/\r//g; s/\\/\//g; s/\/$$/\\/g; s#$(PROJECT_ROOT_NT)#$(PROJECT_ROOT)#g' $(1)
touch -r $(1:%.d=%.o) $(1)
endef
else
define fixdep
$(SED) -i 's/\r//g; s/\\/\//g; s/\/$$/\\/g' $(1)
touch -r $(1:%.d=%.o) $(1)
endef
endif
DEPFLAGS := -gccdep -MD
DEPFILES := $(ALL_OBJS:%.o=%.d)
MW_COMPILE += $(DEPFLAGS)
$(GLOBAL_ASM_OBJS): BUILD_C := $(ASM_PROCESSOR) "$(MW_COMPILE)" "$(MW_ASSEMBLE)"
BUILD_C ?= $(MW_COMPILE) -c -o
$(DEPFILES):
$(BUILD_DIR)/lib/NitroSDK/%.o: MWCCVER := 2.0/sp2p3
$(BUILD_DIR)/%.o: %.c
$(BUILD_DIR)/%.o: %.c $(BUILD_DIR)/%.d
$(BUILD_C) $@ $<
@$(call fixdep,$(BUILD_DIR)/$*.d)
$(BUILD_DIR)/%.o: %.s
$(BUILD_DIR)/%.o: %.s $(BUILD_DIR)/%.d
$(WINE) $(MWAS) $(MWASFLAGS) $(DEPFLAGS) -o $@ $<
@$(call fixdep,$(BUILD_DIR)/$*.d)
include $(wildcard $(DEPFILES))
else
$(GLOBAL_ASM_OBJS): BUILD_C := $(ASM_PROCESSOR) "$(MW_COMPILE)" "$(MW_ASSEMBLE)"
BUILD_C ?= $(MW_COMPILE) -c -o
$(BUILD_DIR)/%.o: %.c
$(BUILD_C) $@ $<
$(BUILD_DIR)/%.o: %.s
$(WINE) $(MWAS) $(MWASFLAGS) -o $@ $<
endif
$(NATIVE_TOOLS): tools
tools: $(TOOLDIRS) $(MWAS)
$(TOOLDIRS):
@$(MAKE) -C $@
clean-tools:
$(foreach tool,$(TOOLDIRS),$(MAKE) -C $(tool) clean;)
$(LCF): $(LSF) $(LCF_TEMPLATE)
$(WINE) $(MAKELCF) $(MAKELCF_FLAGS) $^ $@
ifeq ($(PROC),arm946e)
$(SED) -i '1i KEEP_SECTION\n{\n\t.exceptix\n}' $@
else
$(SED) -i '/\} > check\.WORKRAM/a SDK_SUBPRIV_ARENA_LO = SDK_SUBPRIV_ARENA_LO + SDK_AUTOLOAD.EXT_WRAM.SIZE + SDK_AUTOLOAD.EXT_WRAM.BSS_SIZE;' $@
endif
RESPONSE_TEMPLATE := $(PROJECT_ROOT)/mwldarm.response.template
RESPONSE_TEMPLATE_NT := $(PROJECT_ROOT_NT)/mwldarm.response.template
$(RESPONSE): $(LSF) $(RESPONSE_TEMPLATE)
$(WINE) $(MAKELCF) $(MAKELCF_FLAGS) $< $(RESPONSE_TEMPLATE_NT) $@
# Locate crt0.o
CRT0_OBJ := lib/asm/crt0.o
$(NEF): $(LCF) $(RESPONSE) $(ALL_OBJS)
cd $(BUILD_DIR) && LM_LICENSE_FILE=$(BACK_REL)/$(LM_LICENSE_FILE) $(WINE) $(MWLD) $(MWLDFLAGS) $(LIBS) -o $(BACK_REL)/$(NEF) $(LCF:$(BUILD_DIR)/%=%) @$(RESPONSE:$(BUILD_DIR)/%=%) $(CRT0_OBJ)
.INTERMEDIATE: $(BUILD_DIR)/obj.list
$(SBIN): build/%.sbin: build/%.nef
ifeq ($(SBIN),$(BUILD_DIR)/main.sbin)
# Overlay 123 is encrypted in the retail ROM, so we need to reencrypt it after building it
cd $(BUILD_DIR) && ../../$(MOD123ENCRY) encry main OVY_123_enc.sbin 123 && mv OVY_123_enc.sbin OVY_123.sbin
endif
ifeq ($(COMPARE),1)
$(SHA1SUM) --quiet -c $*.sha1
endif
$(ELF): %.elf: %.nef
$(NTRMERGE) $*
print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true