COMPILER := gcc
AS := tools/gcc/as
CC := tools/gcc/gcc
AR_OLD := tools/gcc/ar
PATCH_AR_FLAGS := 0 0 37777700
STRIP =

export COMPILER_PATH := $(WORKING_DIR)/tools/gcc

CFLAGS := -w -nostdinc -c -G 0 -mgp32 -mfp32 -D_LANGUAGE_C
ASFLAGS := -w -nostdinc -c -G 0 -mgp32 -mfp32 -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_MIPS_SIM=1 -D_ULTRA64 -x assembler-with-cpp
CPPFLAGS = -D_MIPS_SZLONG=32 -D__USE_ISOC99 $(GBIDEFINE) $(VERSION_DEFINE) $(DEBUGFLAG)
IINC = -I . -I $(WORKING_DIR)/include -I $(WORKING_DIR)/include/gcc -I $(WORKING_DIR)/include/PR
MIPS_VERSION := -mips3
ASOPTFLAGS :=

ifneq ($(filter $(VERSION),D E F G H I I_P J),)
CFLAGS += -funsigned-char
endif

# 2.0I libgultra_rom was not compiled with -DNDEBUG and instead libgultra had -DNDEBUG
ifneq ($(filter $(VERSION),I I_P),)
ifeq ($(findstring _rom,$(TARGET)),_rom)
DEBUGFLAG :=
else ifeq ($(findstring _d,$(TARGET)),_d)
DEBUGFLAG := -D_DEBUG
else
DEBUGFLAG := -DNDEBUG
endif
endif

ifeq ($(findstring _d,$(TARGET)),_d)
OPTFLAGS := -O0
else
OPTFLAGS := -O3
endif

ifeq ($(findstring _d,$(TARGET)),_d)
$(BUILD_DIR)/src/rmon/%.marker: OPTFLAGS := -O0
endif

# KMC gcc has a custom flag, N64ALIGN, which forces 8 byte alignment on arrays. This can be used to match, but
# an explicit aligned(8) attribute can be used instead. We opted for the latter for better compatibilty with
# other versions of GCC that do not have this flag.
# export N64ALIGN := ON
export VR4300MUL := ON

$(BUILD_DIR)/src/os/initialize_isv.marker: OPTFLAGS := -O2
$(BUILD_DIR)/src/os/initialize_isv.marker: STRIP = && tools/gcc/strip-2.7 -N initialize_isv.c $(WORKING_DIR)/$(@:.marker=.o)
$(BUILD_DIR)/src/os/assert.marker: OPTFLAGS := -O0
ifeq ($(filter $(VERSION),D E F G H I I_P),)
$(BUILD_DIR)/src/os/seterrorhandler.marker: OPTFLAGS := -O0
endif
$(BUILD_DIR)/src/mgu/%.marker: export VR4300MUL := OFF
$(BUILD_DIR)/src/mgu/rotate.marker: export VR4300MUL := ON
$(BUILD_DIR)/src/debug/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/error/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/log/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/os/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/gu/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/libc/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/rmon/%.marker: ASFLAGS += -P
$(BUILD_DIR)/src/host/host_ptn64.marker: CFLAGS += -fno-builtin # Probably a better way to solve this

MDEBUG_FILES := $(BUILD_DIR)/src/monutil.marker
$(BUILD_DIR)/src/monutil.marker: CC := tools/ido/cc
$(BUILD_DIR)/src/monutil.marker: ASFLAGS := -non_shared -mips2 -fullwarn -verbose -Xcpluscomm -G 0 -woff 516,649,838,712 -Wab,-r4300_mul -nostdinc -o32 -c
