# Build Makefile for GNU Make

# Notes:
#   Turning on any level of optimisation with GCC 2.95 results in a binary
#   which has bugs. Example: photocopier in Pigsty of Episode 4 of Duke.
#   Turning on any level of optimisation with GCC 3.x results in a binary
#   which instantly crashes when run.
#   Moral: don't enable optimisations
#
# New notes as of 25 August 2004:
#   Apparently GCC 2.95 now produces acceptable code if you enable -O2, and
#   GCC 3.3.1 MinGW32 is OK with -O too, but I'm still not excited about
#   trusting it though, so the moral is probably best still "don't enable
#   optimisations".
#
# Compilation on Linux:
#   * GCC 3 objects to something in the GCC inline pragmas, so disable
#     USE_GCC_PRAGMAS. GCC also didn't like to link to NASM objects
#     compiled with the -g switch, so remove -g from ASFLAGS.
#   

SMC		= /mnt/flash

#SYSTEMLIBS=	-lSDLx -lx_gp32  -lgcc
SYSTEMLIBS=	-lSDL_mixer -lSDL -lx_gp32 -lgcc
SCRIPT=		arm-gp32bin.x

AR=		arm-elf-ar
RANLIB=		arm-elf-ranlib
AS=		arm-elf-as
CC=		arm-elf-gcc
LD=		arm-elf-ld
OBJCOPY=	arm-elf-objcopy

B2FXEC =        b2fxe

INCLUDES=	-I/home/ian/gp32/dev/sdl/include

# filename extensions
o=o
res=o
asm=nasm

#DXROOT=c:/sdks/msc/dx61
#FMODROOT=c:/sdks/fmodapi374win/api

# debugging enabled
#debug=-ggdb #-DDEBUGGINGAIDS
# debugging disabled
#debug=-fomit-frame-pointer

# -D these to enable certain features of the port's compile process
# USE_A_C   This uses a C version of the classic renderer code rather
#           than the assembly version in A.ASM.
#           If this is defined, alter the $(OBJ)a.$o in the
#           ENGINEOBJS declaration to be $(OBJ)a-c.$o
# NOASM     When defined, uses C code instead of GCC inline
#           assembly for the features in PRAGMAS.H
TARGETOPTS=-DUSE_A_C -DNOASM

CFLAGS=$(debug) -c -march=armv4t -marm -mno-thumb-interwork -msoft-float \
	-ffast-math -nostdlib -fno-common -ffreestanding \
	-fno-builtin -fno-exceptions -mstructure-size-boundary=8 -O1 \
	-fomit-frame-pointer -Wall -DGP32 -W \
	-Wno-char-subscripts -Wno-unused \
	-funsigned-char -fno-strict-aliasing -DNO_GCC_BUILTINS \
	-I./ -I$(EINC) -I$(SRC)jmact \
	-DNOCOPYPROTECT -DPLATFORM_UNIX -DSTUB_NETWORKING $(INCLUDES) \


LDFLAGS		=	-nostartfiles --script $(SCRIPT) \
					-L/usr/arm-elf/libs \
					-L/usr/arm-elf/lib/gcc/arm-elf/3.4.1\
					-L/home/ian/gp32/dev/sdl/lib

LIBS=$(SYSTEMLIBS) -lm #-lfmod # ../jfaud/jfaud.a
ASFLAGS=-s #-g
EXESUFFIX=.elf

BUILDOBJS := \
	buildengine/cache1d.o \
	buildengine/engine.o \
	buildengine/a.o \
	buildengine/sdl_driver.o \
	buildengine/mmulti.o \
	buildengine/pragmas.o \
	buildengine/unix_compat.o


EDITOROBJS=crt0x_gp32.$o \
	$(OBJ)build.$o

GAMEEXEOBJS=crt0x_gp32.o \
	actors.o \
	animlib.o \
	control.o \
	config.o \
	game.o \
	gamedef.o \
	global.o \
	keyboard.o \
	menues.o \
	player.o \
	premap.o \
	rts.o \
	scriplib.o \
	sector.o \
	sounds.o \
	fx_man.o \
	xlatgp32.o

GAMEEXEOBJS+=$(BUILDOBJS)

EDITOREXEOBJS=$(OBJ)bstub.$o \
	$(OBJ)$(EDITORLIB)

EDITOREXEOBJS+=$(ENGINEOBJS)

.PHONY: clean all utils writeengineinfo enginelib editorlib

# TARGETS
#all: game$(EXESUFFIX) build$(EXESUFFIX) $(OBJ)$(ENGINELIB) $(OBJ)$(EDITORLIB)
all: duke3d.fxe build.fxe
utils: kextract$(EXESUFFIX) kgroup$(EXESUFFIX) transpal$(EXESUFFIX)

enginelib: $(OBJ)$(ENGINELIB)
$(OBJ)$(ENGINELIB): $(ENGINEOBJS)
	$(AR) rc $@ $^
	$(RANLIB) $@

editorlib: $(OBJ)$(EDITORLIB)
$(OBJ)$(EDITORLIB): $(EDITOROBJS)
	$(AR) rc $@ $^
	$(RANLIB) $@

duke3d.fxe:  duke3d.gxb
	$(B2FXEC) $< $@

build.fxe:  build.gxb
	$(B2FXEC) $< $@

duke3d.gxb: duke3d$(EXESUFFIX)
	$(OBJCOPY) -v -O binary duke3d$(EXESUFFIX) duke3d.gxb

build.gxb: build$(EXESUFFIX)
	$(OBJCOPY) -v -O binary build$(EXESUFFIX) build.gxb


duke3d$(EXESUFFIX): $(GAMEEXEOBJS)
	$(LD) $(LDFLAGS) -Map=$@.map $^ $(LIBS) -o duke3d$(EXESUFFIX)
#	$(CC) $(CFLAGS) -o $(OBJ)$@ $^ $(LIBS)
#	cp -f $(OBJ)$@ $@
#	cp -f $@ game.sym$(EXESUFFIX)
#	strip $@
	
build$(EXESUFFIX): $(EDITOREXEOBJS)
	$(LD) $(LDFLAGS) -Map=$@.map $^ $(LIBS) -o build$(EXESUFFIX)
#	$(CC) $(CFLAGS) -o $(OBJ)$@ $^ $(LIBS)
#	cp -f $(OBJ)$@ $@
#	cp -f $@ build.sym$(EXESUFFIX)
#	strip $@

pragmacheck$(EXESUFFIX): $(OBJ)pragmacheck.$o $(OBJ)pragmas.$o
	$(CC) $(subst -Dmain=app_main,,$(CFLAGS)) -o $@ $^
	
kextract$(EXESUFFIX): $(OBJ)kextract.$o $(OBJ)compat.$o
	$(CC) -o $@ $^
kgroup$(EXESUFFIX): $(OBJ)kgroup.$o $(OBJ)compat.$o
	$(CC) -o $@ $^
transpal$(EXESUFFIX): $(OBJ)transpal.$o $(OBJ)pragmas.$o $(OBJ)compat.$o
	$(CC) -o $@ $^
wad2art$(EXESUFFIX): $(OBJ)wad2art.$o $(OBJ)pragmas.$o $(OBJ)compat.$o
	$(CC) -o $@ $^
wad2map$(EXESUFFIX): $(OBJ)wad2map.$o $(OBJ)pragmas.$o $(OBJ)compat.$o
	$(CC) -o $@ $^
generateicon$(EXESUFFIX): $(OBJ)generateicon.$o $(OBJ)kplib.$o
	$(CC) -o $@ $^


.PHONY: $(OBJ)engineinfo.$o
$(OBJ)engineinfo.$o:
	echo "const char _engine_cflags[] = \"$(CFLAGS)\";" > $(SRC)tmp/engineinfo.c
	echo "const char _engine_libs[] = \"$(LIBS)\";" >> $(SRC)tmp/engineinfo.c
	echo "const char _engine_uname[] = \"$(shell uname -a)\";" >> $(SRC)tmp/engineinfo.c
	echo "const char _engine_compiler[] = \"$(CC) $(shell $(CC) -dumpversion) $(shell $(CC) -dumpmachine)\";" >> $(SRC)tmp/engineinfo.c
	echo "const char _engine_date[] = __DATE__ \" \" __TIME__;" >> $(SRC)tmp/engineinfo.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $(SRC)tmp/engineinfo.c -o $@ 2>&1

# RULES
#$(OBJ)%.$o: $(SRC)%.nasm
#	$(AS) $(ASFLAGS) $< -o $@

$(OBJ)%.$o: $(SRC)%.s
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ 2>&1

$(OBJ)%.$o: $(SRC)%.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ 2>&1
	
$(OBJ)%.$o: $(SRC)tmp/%.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ 2>&1

$(OBJ)%.$o: $(SRC)misc/%.rc
	$(RC) -i $^ -o $@

$(OBJ)%.$o: $(SRC)util/%.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ 2>&1

$(OBJ)%.$o: $(RSRC)%.c
	$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ 2>&1


# PHONIES	
clean:
	-rm -f $(ENGINEOBJS) $(EDITOROBJS) $(GAMEEXEOBJS) $(EDITOREXEOBJS) game$(EXESUFFIX) game.sym$(EXESUFFIX) build$(EXESUFFIX) build.sym$(EXESUFFIX) core*

