-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
312 lines (255 loc) · 9.6 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
300
301
302
303
304
305
306
307
308
309
310
311
312
# Makefile to build doom64
N64_SDK ?= /opt/crashsdk
N64_LIBGCCDIR ?= $(N64_SDK)/lib/gcc/mips64-elf/12.2.0
N64_BINDIR ?= $(N64_SDK)/bin
TARGET_STRING := doom64
TARGET := $(TARGET_STRING)
DEBUG ?= 0
DEFINES :=
OPTIONS :=
ifdef GDB
OPTIONS += USB_GDB
USB = 1
DEBUG = 1
endif
ifdef DEBUGOPT
DEFINES += DEBUGOPT=1
DEBUG = 1
endif
ifdef DEBUG_DISPLAY
OPTIONS += DEBUG_DISPLAY=$(DEBUG_DISPLAY)
endif
ifdef DEBUG_MEM
DEBUG = 1
DEFINES += DEBUG_MEM
endif
ifeq ($(DEBUG),0)
DEFINES += NDEBUG=1
endif
ifdef REGION
DEFINES += REGION=REGION_$(REGION)
endif
ifeq ($(SOUND),0)
OPTIONS += SOUND=0
endif
ifdef FORCE_NO_EXPANSION_PAK
DEFINES += FORCE_NO_EXPANSION_PAK
endif
ifneq ($(REQUIRE_EXPANSION_PAK),0)
OPTIONS += REQUIRE_EXPANSION_PAK
endif
ifdef BENCHMARK_MAP_LOAD
OPTIONS += BENCHMARK_MAP_LOAD
endif
ifdef USB
OPTIONS += USB
endif
ifeq ($(INTRO),0)
OPTIONS += SKIP_INTRO
endif
ifdef WARP
OPTIONS += DEVWARP="$(shell printf "%02d" $(WARP))"
endif
ifdef SKILL
OPTIONS += DEVSKILL=$(SKILL)
endif
ifdef CHEATS
OPTIONS += DEVCHEATS=$(CHEATS)
endif
ifeq ($(DEBUGOPT),1)
LIBULTRA_VER=libultra
LIBNAUDIO_VER=n_audio_sc
else
ifeq ($(DEBUG),1)
LIBULTRA_VER=libultra_d
LIBNAUDIO_VER=n_audio_sc_d
else
LIBULTRA_VER=libultra_rom
LIBNAUDIO_VER=n_audio_sc
endif
endif
# Whether to hide commands or not
VERBOSE ?= 0
ifeq ($(VERBOSE),0)
V := @
endif
# Whether to colorize build messages
COLOR ?= 1
#ifeq (, $(shell which lzop))
#$(error "No lzop in $(PATH), consider doing apt-get install lzop")
#endif
#==============================================================================#
# Target Executable and Sources #
#==============================================================================#
# BUILD_DIR is the location where all build artifacts are placed
BUILD_DIR ?= build
ROM := $(BUILD_DIR)/$(TARGET_STRING).z64
ELF := $(BUILD_DIR)/$(TARGET_STRING).elf
LD_SCRIPT := $(TARGET_STRING).ld
BOOT := /usr/lib/n64/PR/bootcode/boot.6102
BOOT_OBJ := $(BUILD_DIR)/boot.6102.o
# Directories containing source files
SRC_DIRS = src
ALL_DIRS := $(BUILD_DIR) $(addprefix $(BUILD_DIR)/,$(SRC_DIRS))
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
S_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.s))
BOOT_S_FILES := src/rom_header.s src/entry.s
S_FILES := $(filter-out $(BOOT_S_FILES),$(S_FILES))
# Files compiled with -Ofast
FAST_C_FILES := src/i_sram.c
# Files compiled with -Os
SIZE_C_FILES := $(filter-out $(FAST_C_FILES),$(C_FILES))
# Object files
FAST_O_FILES := $(foreach file,$(FAST_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
SIZE_O_FILES := $(foreach file,$(SIZE_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
O_FILES := $(FAST_O_FILES) $(SIZE_O_FILES) \
$(foreach file,$(S_FILES),$(BUILD_DIR)/$(file:.s=.o)) \
$(BUILD_DIR)/DOOM64.WAD.o $(BUILD_DIR)/DOOM64.WDD.o \
$(BUILD_DIR)/DOOM64.WMD.o $(BUILD_DIR)/DOOM64.WSD.o \
$(BOOT_OBJ)
BOOT_O_FILES := $(foreach file,$(BOOT_S_FILES),$(BUILD_DIR)/$(file:.s=.o))
# Automatic dependency files
DEP_FILES := $(O_FILES:.o=.d) $(BUILD_DIR)/$(LD_SCRIPT).d
LIBDOOM64 = $(BUILD_DIR)/libdoom64.a
LIBULTRA = $(BUILD_DIR)/$(LIBULTRA_VER)/$(LIBULTRA_VER).a
LIBNAUDIO = $(BUILD_DIR)/$(LIBNAUDIO_VER)/$(LIBNAUDIO_VER).a
# Make sure build directory exists before compiling anything
$(shell mkdir -p $(ALL_DIRS))
# Ensure submodules are checked out
$(shell if [ ! -d libultra_modern/src -o ! -d n_audio_sc/src ]; then git submodule update -i -r; fi)
define nl
endef
# cache build options
CONFIG_H := $(BUILD_DIR)/config.h
C_OPTIONS = $(foreach d,$(OPTIONS),#define $(subst =, ,$(d))$(nl))
ifeq (,$(wildcard $(CONFIG_H)))
$(file > $(CONFIG_H),$(C_OPTIONS))
endif
ifneq ($(strip $(file < $(CONFIG_H))),$(strip $(C_OPTIONS)))
$(file > $(CONFIG_H),$(C_OPTIONS))
endif
DEFINES_TXT := $(BUILD_DIR)/defines.txt
ifeq (,$(wildcard $(DEFINES_TXT)))
$(file > $(DEFINES_TXT),$(DEFINES))
endif
ifneq ($(strip $(file < $(DEFINES_TXT))),$(strip $(DEFINES)))
$(file > $(DEFINES_TXT),$(DEFINES))
endif
#==============================================================================#
# Compiler Options #
#==============================================================================#
AS := $(N64_BINDIR)/mips-n64-as
CC := $(N64_BINDIR)/mips-n64-gcc
CPP := cpp
LD := $(N64_BINDIR)/mips-n64-ld
AR := $(N64_BINDIR)/mips-n64-gcc-ar
OBJDUMP := $(N64_BINDIR)/mips-n64-objdump
OBJCOPY := $(N64_BINDIR)/mips-n64-objcopy
INCLUDE_DIRS += libultra_modern/include libultra_modern/include/PR n_audio_sc/include \
$(BUILD_DIR) $(BUILD_DIR)/include src src/asm
DEFINES += _FINALROM=1 F3DEX_GBI_2=1
C_DEFINES := $(foreach d,$(DEFINES),-D$(d))
LD_DEFINES := $(C_DEFINES) $(foreach d,$(OPTIONS),"-D$(d)") -DLIBULTRA=$(LIBULTRA_VER) -DLIBNAUDIO=$(LIBNAUDIO_VER)
DEF_INC_CFLAGS := $(foreach i,$(INCLUDE_DIRS),-I$(i)) $(C_DEFINES)
CFLAGS = -Wall -march=vr4300 -mtune=vr4300 \
-mabi=eabi -mno-abicalls -mgp32 \
-D_LANGUAGE_C -D_ULTRA64 -D__EXTENSIONS__ \
-fno-common -G0 -D_MIPS_SZLONG=32 -D_MIPS_SZINT=32 -D_MIPS_SIM=2 -g -ggdb \
-ffreestanding -fuse-linker-plugin \
-mfix4300 -mhard-float -mno-check-zero-division \
$(DEF_INC_CFLAGS)
ASFLAGS := -march=vr4300 -mtune=vr4300 -D_MIPS_SIM=2 -mno-check-zero-division \
-mabi=eabi -mno-abicalls -mgp32 \
$(foreach i,$(INCLUDE_DIRS),-I$(i))
LDFLAGS := -mabi=eabi -mno-abicalls -mgp32 -nostartfiles
SIZE_CFLAGS :=
FAST_CFLAGS :=
LIBULTRA_CFLAGS :=
# $(foreach d,$(DEFINES),--defsym $(d))
#
ifneq (,$(filter 0,$(DEBUG))$(filter 1,$(DEBUGOPT)))
FAST_CFLAGS += -Ofast -fno-unroll-loops -fno-peel-loops -flto=auto --param case-values-threshold=20 \
-fno-inline -finline-functions-called-once --param max-completely-peeled-insns=8
SIZE_CFLAGS += -Oz -finline-functions-called-once -ffast-math -falign-functions=32 -flto=auto
LIBULTRA_CFLAGS += -Oz -ffast-math -falign-functions=32 -flto=auto -fuse-linker-plugin
LDFLAGS += -Wl,--gc-sections -flto=auto -fuse-linker-plugin -Oz
else
CFLAGS += -Og
LIBULTRA_CFLAGS += -Og
endif
OPT_CFLAGS :=
# C preprocessor flags
CPPFLAGS := -P -Wno-trigraphs $(DEF_INC_CFLAGS)
# tools
PRINT = printf
ifeq ($(COLOR),1)
NO_COL := \033[0m
RED := \033[0;31m
GREEN := \033[0;32m
BLUE := \033[0;34m
YELLOW := \033[0;33m
BLINK := \033[33;5m
endif
# Common build print status function
define print
@$(PRINT) "$(GREEN)$(1) $(YELLOW)$(2)$(GREEN) -> $(BLUE)$(3)$(NO_COL)\n"
endef
#==============================================================================#
# Main Targets #
#==============================================================================#
# Default target
default: libultra n_audio_sc $(ROM)
clean:
$(RM) -r $(BUILD_DIR)
#==============================================================================#
# Compilation Recipes #
#==============================================================================#
$(BUILD_DIR)/DOOM64.%.o: data/DOOM64.%
$(call print,Packing data:,$<,$@)
$(V)$(OBJCOPY) -I binary -B mips:3000 -O elf32-bigmips $< $@
# Compile C code
$(FAST_O_FILES): OPT_CFLAGS := $(FAST_CFLAGS)
$(SIZE_O_FILES): OPT_CFLAGS := $(SIZE_CFLAGS)
$(BUILD_DIR)/%.o: %.c $(DEFINES_TXT)
$(call print,Compiling:,$<,$@)
$(V)$(CC) -c $(CFLAGS) $(OPT_CFLAGS) -MMD -MF $(BUILD_DIR)/$*.d -o $@ $<
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c $(DEFINES_TXT)
$(call print,Compiling:,$<,$@)
$(V)$(CC) -c $(CFLAGS) $(FAST_CFLAGS) -MMD -MF $(BUILD_DIR)/$*.d -o $@ $<
# Assemble assembly code
$(BUILD_DIR)/src/%.o: src/%.s $(DEFINES_TXT)
$(call print,Assembling:,$<,$@)
$(V)$(CC) -c $(ASFLAGS) $(foreach i,$(INCLUDE_DIRS),-Wa,-I$(i)) -x assembler-with-cpp -MMD -MF $(BUILD_DIR)/$*.d -o $@ $<
# Run linker script through the C preprocessor
$(BUILD_DIR)/$(LD_SCRIPT): src/$(LD_SCRIPT) $(CONFIG_H) $(DEFINES_TXT)
$(call print,Preprocessing linker script:,$<,$@)
$(V)$(CPP) $(CPPFLAGS) -DBUILD_DIR=$(BUILD_DIR) $(LD_DEFINES) -MMD -MP -MT $@ -MF $@.d -o $@ $<
$(BOOT_OBJ): $(BOOT)
$(V)$(OBJCOPY) -I binary -B mips:3000 -O elf32-bigmips --rename-section .data=.text $< $@
# Link final ELF file
$(LIBDOOM64): $(O_FILES)
$(call print,Archiving:,$@)
$(V)$(AR) rcs -o $@ $(O_FILES)
libultra:
$(V)$(MAKE) -s -C libultra_modern VERSION=$(LIBULTRA_VER) "EXT_CFLAGS=$(LIBULTRA_CFLAGS)" VERBOSE=$(VERBOSE) "BUILD_DIR_BASE=$(realpath $(BUILD_DIR))"
n_audio_sc:
$(V)$(MAKE) -s -C n_audio_sc VERSION=$(LIBNAUDIO_VER) "EXT_CFLAGS=$(LIBULTRA_CFLAGS)" VERBOSE=$(VERBOSE) "BUILD_DIR_BASE=$(realpath $(BUILD_DIR))"
$(LIBULTRA): libultra
$(LIBNAUDIO): n_audio_sc
# Link final ELF file
$(ELF): $(LIBULTRA) $(LIBNAUDIO) $(BOOT_O_FILES) $(LIBDOOM64) $(BUILD_DIR)/$(LD_SCRIPT)
@$(PRINT) "$(GREEN)Linking ELF file: $(BLUE)$@ $(NO_COL)\n"
$(V)$(CC) $(LDFLAGS) "-L$(BUILD_DIR)" "-Wl,-T,$(BUILD_DIR)/$(LD_SCRIPT)" "-Wl,-Map,$(BUILD_DIR)/$(TARGET).map" \
-o $@ $(BOOT_O_FILES) $(LIBDOOM64) $(LIBULTRA) $(LIBNAUDIO) -L$(N64_LIBGCCDIR)
# Build ROM
$(ROM): $(ELF)
$(call print,Building ROM:,$<,$@)
$(V)$(OBJCOPY) --pad-to=0x100000 --gap-fill=0xFF $< $@ -O binary
$(V)makemask $@
.PHONY: clean default libultra n_audio_sc
# with no prerequisites, .SECONDARY causes no intermediate target to be removed
.SECONDARY:
# Remove built-in rules, to improve performance
MAKEFLAGS += --no-builtin-rules
-include $(DEP_FILES)
print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true