From efbd931cbcbb79103ce88d4d2899763782ba43fe Mon Sep 17 00:00:00 2001 From: someone2639 Date: Mon, 17 Feb 2025 11:28:31 -0500 Subject: [PATCH] Set up Makefile for modern building (#1) * set build options * remove COMPARE and MDOERN_* switches * remove tools makefile * AR patching is gone too since we want a fullly decomped version * AR is modern * remove cwd changes * edit my own tool to fix compile errors * compile files generated with my own tool instead of the originals * inline modern_gcc makefile * port mips toolchain detection logic * add util.mk for find-command * remove forced AR order and strip/mdebug removal commands * add -mabi=32 to as flags --------- Co-authored-by: someone2639 --- Makefile | 234 +++++++++++----------------------------- include/sgidefs.h | 2 +- makefiles/gcc.mk | 66 ------------ makefiles/ido.mk | 58 ---------- makefiles/modern_gcc.mk | 21 ---- tools/Makefile | 46 -------- tools/compile_sjis.py | 13 +-- util.mk | 16 +++ 8 files changed, 84 insertions(+), 372 deletions(-) delete mode 100644 makefiles/gcc.mk delete mode 100644 makefiles/ido.mk delete mode 100644 makefiles/modern_gcc.mk delete mode 100644 tools/Makefile create mode 100644 util.mk diff --git a/Makefile b/Makefile index 401c3d4..08191ac 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,39 @@ -COMPARE ?= 1 -MODERN_LD ?= 0 -MODERN_GCC ?= 0 - -ifneq ($(MODERN_LD),0) -COMPARE := 0 -endif - # One of: # libgultra_rom, libgultra_d, libgultra # libultra_rom, libultra_d, libultra TARGET ?= libgultra_rom VERSION ?= L -CROSS ?= mips-linux-gnu- +VERBOSE ?= 0 -ifeq ($(findstring libgultra,$(TARGET)),libgultra) -COMPILER := gcc -else ifeq ($(findstring libultra,$(TARGET)),libultra) -COMPILER := ido +include util.mk + +ifeq ($(VERBOSE), 0) +V=@ else -$(error Invalid Target) +V= endif -ifneq ($(MODERN_GCC),0) -COMPILER := modern_gcc -COMPARE := 0 -MODERN_LD := 0 +# detect prefix for MIPS toolchain +ifneq ($(call find-command,mips64-elf-ld),) + CROSS := mips64-elf- +else ifneq ($(call find-command,mips-n64-ld),) + CROSS := mips-n64- +else ifneq ($(call find-command,mips64-ld),) + CROSS := mips64- +else ifneq ($(call find-command,mips-linux-gnu-ld),) + CROSS := mips-linux-gnu- +else ifneq ($(call find-command,mips64-linux-gnu-ld),) + CROSS := mips64-linux-gnu- +else ifneq ($(call find-command,mips64-none-elf-ld),) + CROSS := mips64-none-elf- +else ifneq ($(call find-command,mips-ld),) + CROSS := mips- +else ifneq ($(call find-command,mips-suse-linux-ld ),) + CROSS := mips-suse-linux- +else + $(error Unable to detect a suitable MIPS toolchain installed) endif -BASE_DIR := extracted/$(VERSION)/$(TARGET) -BASE_AR := base/$(VERSION)/$(TARGET).a BUILD_ROOT := build BUILD_DIR := $(BUILD_ROOT)/$(VERSION)/$(TARGET) BUILD_AR := $(BUILD_DIR)/$(TARGET).a @@ -46,17 +51,24 @@ else DEBUGFLAG := -DNDEBUG endif -ifeq ($(COMPILER),gcc) --include makefiles/gcc.mk -else ifeq ($(COMPILER),ido) --include makefiles/ido.mk -else ifeq ($(COMPILER),modern_gcc) --include makefiles/modern_gcc.mk -else -$(error Invalid Compiler) -endif +AS := $(CROSS)gcc -x assembler-with-cpp +CC := $(CROSS)gcc -export COMPILER_PATH := $(COMPILER_DIR) +WARNINGS := -Wall -Wextra -Wno-format-security -Wno-unused-function -Wno-unused-parameter -Wno-unused-variable -Wno-builtin-declaration-mismatch +WARNINGS += -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-implicit-function-declaration # TODO: Try adjusting code to remove these +CFLAGS := -G 0 -c -nostdinc -march=vr4300 -mfix4300 -mabi=32 -mno-abicalls -mdivide-breaks -fno-PIC -fno-common -ffreestanding -fbuiltin -fno-builtin-sinf -fno-builtin-cosf -funsigned-char $(WARNINGS) +CFLAGS += -fno-strict-aliasing # TODO: Try adjusting code to remove this +ASFLAGS := -w -nostdinc -c -G 0 -march=vr4300 -mabi=32 -mgp32 -mfp32 -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_MIPS_SIM=1 -D_ULTRA64 +CPPFLAGS = -DMODERN_CC -D_MIPS_SZLONG=32 -D__USE_ISOC99 $(GBIDEFINE) $(VERSION_DEFINE) $(DEBUGFLAG) +IINC = -I . -I $(WORKING_DIR)/include -I $(WORKING_DIR)/include/compiler/modern_gcc -I $(WORKING_DIR)/include/PR +MIPS_VERSION := -mips3 +ASOPTFLAGS := + +ifeq ($(findstring _d,$(TARGET)),_d) +OPTFLAGS := -Og -ggdb3 -ffast-math -fno-unsafe-math-optimizations +else +OPTFLAGS := -Os -ggdb3 -ffast-math -fno-unsafe-math-optimizations +endif ifeq ($(findstring _rom,$(TARGET)),_rom) CPPFLAGS += -D_FINALROM @@ -77,166 +89,44 @@ endif C_O_FILES := $(foreach f,$(C_FILES:.c=.o),$(BUILD_DIR)/$f) S_O_FILES := $(foreach f,$(S_FILES:.s=.o),$(BUILD_DIR)/$f) O_FILES := $(S_O_FILES) $(C_O_FILES) -# Because we patch the object file timestamps, we can't use them as the targets since they'll always be older than the C file -# Therefore instead we use marker files that have actual timestamps as the dependencies for the archive -C_MARKER_FILES := $(C_O_FILES:.o=.marker) -S_MARKER_FILES := $(S_O_FILES:.o=.marker) -S_MARKER_FILES := $(filter-out $(MDEBUG_FILES),$(S_MARKER_FILES)) -MARKER_FILES := $(C_MARKER_FILES) $(S_MARKER_FILES) $(MDEBUG_FILES) - -ifneq ($(COMPARE),0) -COMPARE_OBJ = cmp $(BASE_DIR)/$(@F:.marker=.o) $(@:.marker=.o) && echo "$(@:.marker=.o): OK" -COMPARE_AR = cmp $(BASE_AR) $@ && echo "$@: OK" -ifeq ($(COMPILER),ido) -COMPARE_OBJ = $(CROSS)objcopy -p --strip-debug $(WORKING_DIR)/$(@:.marker=.o) $(WORKING_DIR)/$(@:.marker=.cmp.o) && \ - cmp $(BASE_DIR)/.cmp/$(@F:.marker=.cmp.o) $(WORKING_DIR)/$(@:.marker=.cmp.o) && echo "$(@:.marker=.o): OK" -COMPARE_AR = echo "$@: Cannot compare archive currently" -endif -else -COMPARE_OBJ := -COMPARE_AR := -AR_OLD := $(AR) -endif - -BASE_OBJS := $(wildcard $(BASE_DIR)/*.o) - -# Check to make sure the current version has been set up -ifneq ($(COMPARE),0) -ifeq ($(BASE_OBJS),) -# Ignore this check if the user is currently running setup, clean or distclean -ifeq ($(filter $(MAKECMDGOALS),setup clean distclean),) -$(error Current version ($(TARGET) 2.0$(VERSION)) has not been setup!) -endif -endif -endif AR_OBJECTS := $(shell cat base/$(VERSION)/$(TARGET).txt) -# If the version and target doesn't have a text file yet, resort back to using the base archive to get objects -ifeq ($(AR_OBJECTS),) -AR_OBJECTS := $(shell ar t $(BASE_AR)) -endif - -# Try to find a file corresponding to an archive file in src/ or the base directory, prioritizing src then the original file -AR_ORDER = $(foreach f,$(AR_OBJECTS),$(shell find $(BUILD_DIR)/src $(BASE_DIR) -iname $f -type f -print -quit)) -MATCHED_OBJS = $(filter-out $(BASE_DIR)/%,$(AR_ORDER)) -UNMATCHED_OBJS = $(filter-out $(MATCHED_OBJS),$(AR_ORDER)) -NUM_OBJS = $(words $(AR_ORDER)) -NUM_OBJS_MATCHED = $(words $(MATCHED_OBJS)) -NUM_OBJS_UNMATCHED = $(words $(UNMATCHED_OBJS)) - -$(shell mkdir -p $(BASE_DIR) src $(foreach dir,$(SRC_DIRS),$(BUILD_DIR)/$(dir))) +$(shell mkdir -p src $(foreach dir,$(SRC_DIRS),$(BUILD_DIR)/$(dir))) .PHONY: all clean distclean setup all: $(BUILD_AR) -$(BUILD_AR): $(MARKER_FILES) - $(AR_OLD) rcs $@ $(AR_ORDER) -ifneq ($(COMPARE),0) -# patch archive creation time and individual files' ownership & permissions - dd bs=1 skip=24 seek=24 count=12 conv=notrunc if=$(BASE_AR) of=$@ status=none - python3 tools/patch_ar_meta.py $@ $(BASE_AR) $(PATCH_AR_FLAGS) - @$(COMPARE_AR) - @echo "Matched: $(NUM_OBJS_MATCHED)/$(NUM_OBJS)" -endif +$(BUILD_AR): $(O_FILES) + @printf " [AR] $@\n" + $(V)$(AR) rcs $@ $^ clean: $(RM) -rf $(BUILD_DIR) distclean: - $(MAKE) -C tools distclean $(RM) -rf extracted/ $(BUILD_ROOT) -setup: - $(MAKE) -C tools -ifneq ($(COMPARE),0) - cd $(BASE_DIR) && $(AR) xo $(WORKING_DIR)/$(BASE_AR) - chmod -R +rw $(BASE_DIR) -ifeq ($(COMPILER),ido) - export CROSS=$(CROSS) && ./tools/strip_debug.sh $(BASE_DIR) -endif -endif - -$(BUILD_DIR)/$(BASE_DIR)/%.marker: $(BASE_DIR)/%.o - cp $< $(@:.marker=.o) -ifneq ($(COMPARE),0) -# change file timestamps to match original - @touch -r $(BASE_DIR)/$(@F:.marker=.o) $(@:.marker=.o) - @$(COMPARE_OBJ) - @touch $@ -endif - GBIDEFINE := -DF3DEX_GBI -$(BUILD_DIR)/src/gu/parse_gbi.marker: GBIDEFINE := -DF3D_GBI -$(BUILD_DIR)/src/gu/us2dex_emu.marker: GBIDEFINE := -$(BUILD_DIR)/src/gu/us2dex2_emu.marker: GBIDEFINE := -$(BUILD_DIR)/src/sp/sprite.marker: GBIDEFINE := -DF3D_GBI -$(BUILD_DIR)/src/sp/spriteex.marker: GBIDEFINE := -$(BUILD_DIR)/src/sp/spriteex2.marker: GBIDEFINE := -$(BUILD_DIR)/src/voice/%.marker: OPTFLAGS += -DLANG_JAPANESE -I$(WORKING_DIR)/src -I$(WORKING_DIR)/src/voice -$(BUILD_DIR)/src/voice/%.marker: CC := $(WORKING_DIR)/tools/compile_sjis.py -D__CC=$(CC) -D__BUILD_DIR=$(BUILD_DIR) +$(BUILD_DIR)/src/gu/parse_gbi.o: GBIDEFINE := -DF3D_GBI +$(BUILD_DIR)/src/gu/us2dex_emu.o: GBIDEFINE := +$(BUILD_DIR)/src/gu/us2dex2_emu.o: GBIDEFINE := +$(BUILD_DIR)/src/sp/sprite.o: GBIDEFINE := -DF3D_GBI +$(BUILD_DIR)/src/sp/spriteex.o: GBIDEFINE := +$(BUILD_DIR)/src/sp/spriteex2.o: GBIDEFINE := +$(BUILD_DIR)/src/voice/%.o: OPTFLAGS += -DLANG_JAPANESE -I$(WORKING_DIR)/src -I$(WORKING_DIR)/src/voice +$(BUILD_DIR)/src/voice/%.o: CC := $(WORKING_DIR)/tools/compile_sjis.py -D__CC=$(CC) -D__BUILD_DIR=$(BUILD_DIR) -$(C_MARKER_FILES): $(BUILD_DIR)/%.marker: %.c - cd $(/dev/null)