ogrebattle64/Makefile

190 lines
5.0 KiB
Makefile

### Build Options ###
BASEROM := baserom.z64
TARGET := ogrebattle64
COMPARE ?= 1
NON_MATCHING ?= 0
CHECK ?= 1
VERBOSE ?= 0
PRETTY_PRINTING ?= 1
# Fail early if baserom does not exist
ifeq ($(wildcard $(BASEROM)),)
$(error Baserom `$(BASEROM)' not found.)
endif
# NON_MATCHING=1 implies COMPARE=0
ifeq ($(NON_MATCHING),1)
override COMPARE=0
endif
ifeq ($(VERBOSE),0)
V := @
endif
ifeq ($(PRETTY_PRINTING),0)
P := true ||
endif
### Output ###
BUILD_DIR := build
TOOLS_DIR := tools
BUILD_TOOLS_DIR := $(TOOLS_DIR)/build
ROM := $(BUILD_DIR)/$(TARGET).z64
ELF := $(BUILD_DIR)/$(TARGET).elf
LD_SCRIPT := linker_scripts/$(TARGET).ld
LD_MAP := $(BUILD_DIR)/$(TARGET).map
PYTHON := python3
SPLAT_YAML := splat.yaml
SPLAT := $(PYTHON) $(TOOLS_DIR)/splat/split.py $(SPLAT_YAML)
DIFF := diff
CROSS := mips-linux-gnu-
LD := $(CROSS)ld
OBJDUMP := $(CROSS)objdump
AS := $(CROSS)as
ICONV := iconv
OBJCOPY := $(CROSS)objcopy
STRIP := $(CROSS)strip
CC := $(BUILD_TOOLS_DIR)/gcc2.7.2/gcc -B $(BUILD_TOOLS_DIR)/gcc2.7.2/
CXX := $(BUILD_TOOLS_DIR)/gcc2.7.2/g++ -P
PRINT := printf '
ENDCOLOR := \033[0m
WHITE := \033[0m
ENDWHITE := $(ENDCOLOR)
GREEN := \033[0;32m
ENDGREEN := $(ENDCOLOR)
BLUE := \033[0;34m
ENDBLUE := $(ENDCOLOR)
YELLOW := \033[0;33m
ENDYELLOW := $(ENDCOLOR)
ENDLINE := \n'
### Compiler Options ###
IINC := -I include -I include/ultra -I include/ultra/PR
ASFLAGS := -I include -EB -mtune=vr4300 -march=vr4300
CPPFLAGS := $(IINC) -DBUILD_VERSION=VERSION_J -D_LANGUAGE_C -D_FINALROM -DF3DEX_GBI_2 -D_MIPS_SZLONG=32 -nostdinc -mgp32 -mfp32 -mips2
CFLAGS := -c -G0 -mgp32 -mfp32 -mips2
LDFLAGS := -T undefined_syms.txt -T undefined_syms_auto.txt -T undefined_funcs_auto.txt -T undefined_funcs.txt -T $(LD_SCRIPT) -Map $(LD_MAP) --no-check-sections
ICONV_FLAGS := --from-code=UTF-8 --to-code=Shift-JIS
OPTFLAGS := -O2
### Sources ###
# Empty file used to track the time that splat was most recently run
SPLAT_TIMESTAMP := asm/splat_timestamp
# Object files
OBJECTS := $(shell $(PYTHON) tools/splat_objects.py $(SPLAT_YAML))
DEPENDS := $(OBJECTS:=.d)
### Targets ###
all: $(ROM)
-include $(DEPENDS)
clean:
$(V)rm -rf build
$(info $(MAKEFLAGS))
distclean: clean
$(V)rm -rf asm
$(V)rm -rf assets
$(V)rm -f *auto.txt
$(V)rm -f $(TARGET).ld
$(V)rm -f include/ld_addrs.h
setup: clean distclean split
diff-init: all
$(RM) -rf expected/
mkdir -p expected/
cp -r $(BUILD_DIR) expected/$(BUILD_DIR)
split:
$(V)$(SPLAT)
@touch $(SPLAT_TIMESTAMP)
context:
$(V)$(PYTHON) tools/m2ctx.py ctx_includes.h
$(BUILD_DIR)/src/boot.c.o: OPTFLAGS = -O0
# Run splat and update the timestamp
$(SPLAT_TIMESTAMP) : $(SPLAT_YAML) | $(BUILD_DIR)
@$(P)$(PRINT)$(GREEN)Running splat$(ENDGREEN)$(ENDLINE)
$(V)$(SPLAT)
@touch $@
# Disassemble asm files with splat (just update the timestamp so it's newer than the time splat was run)
asm/%.s: $(SPLAT_TIMESTAMP)
@touch $@
# Extract bin files with splat (same as above)
assets/%.bin: $(SPLAT_TIMESTAMP)
@touch $@
# Create the build directory
$(BUILD_DIR):
@$(P)$(PRINT)$(GREEN)Making build folder$(ENDGREEN)$(ENDLINE)
@mkdir -p $@
# Compile .c files
$(BUILD_DIR)/src/%.c.o: src/%.c $(SPLAT_TIMESTAMP) | $(BUILD_DIR)
@$(P)$(PRINT)$(GREEN)Compiling C file: $(ENDGREEN)$(BLUE)$<$(ENDBLUE)$(ENDLINE)
@mkdir -p $(shell dirname $@)
$(V)$(CC) $(CFLAGS) $(OPTFLAGS) $(CPPFLAGS) -E $< | $(ICONV) $(ICONV_FLAGS) | $(CC) -x c $(CFLAGS) $(OPTFLAGS) $(CPPFLAGS) -c -o $@ -
# Assemble .s files with modern gnu as
$(BUILD_DIR)/asm/%.s.o: asm/%.s | $(BUILD_DIR)
@$(P)$(PRINT)$(GREEN)Assembling asm file: $(ENDGREEN)$(BLUE)$<$(ENDBLUE)$(ENDLINE)
@mkdir -p $(shell dirname $@)
$(V)$(ICONV) $(ICONV_FLAGS) $< | $(AS) $(ASFLAGS) -o $@
# Create .o files from .bin files.
$(BUILD_DIR)/%.bin.o: %.bin | $(BUILD_DIR)
@$(P)$(PRINT)$(GREEN)Objcopying binary file: $(ENDGREEN)$(BLUE)$<$(ENDBLUE)$(ENDLINE)
@mkdir -p $(shell dirname $@)
$(V)$(LD) -r -b binary -o $@ $<
# Link the .o files into the .elf
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS)
@$(P)$(PRINT)$(GREEN)Linking elf file: $(ENDGREEN)$(BLUE)$@$(ENDBLUE)$(ENDLINE)
$(V)$(LD) $(LDFLAGS) -o $@
# Convert the .elf to the final rom
$(ROM): $(BUILD_DIR)/$(TARGET).elf
@$(P)$(PRINT)$(GREEN)Creating z64: $(ENDGREEN)$(BLUE)$@$(ENDBLUE)$(ENDLINE)
$(V)$(OBJCOPY) $< $@ -O binary
$(V)$(OBJCOPY) -O binary --gap-fill 0xFF --pad-to 0x1000000 $< $@
ifeq ($(COMPARE),1)
@$(DIFF) $(BASEROM) $(ROM) && $(PRINT)OK$(ENDLINE) || ($(PRINT)FAILED (ROM BUILT, BUT DIFFERS FROM BASEROM)$(ENDLINE) && false)
endif
### File-Specific Rules ###
# build/src/os/O1/%.o: OPTFLAGS := -O1
# build/src/%.o: CC := python3 tools/asm_processor/build.py $(CC) -- $(AS) $(ASFLAGS) --
### Make Settings ###
# Prevent removing intermediate files
.SECONDARY:
# Specify which targets don't have a corresponding file
.PHONY: all clean distclean test setup split
# Print target for debugging
print-% : ; $(info $* is a $(flavor $*) variable set to [$($*)]) @true