#
# QuakeWorld/MVDSV and QWDTools Makefile for FreeBSD/OpenBSD/NetBSD/DragonFly BSD/Darwin
#
#	- now should build on non-x86
#	- no longer requires gmake(1)
#	- debug targets support axed out
#	- couple of useful knobs added
#
# Created on Wednesday, May 21 2003 by Alexey Dokuchaev <danfe@regency.nsu.ru>
#
#	$Id: Makefile.BSD 746 2007-12-30 19:34:29Z qqshka $
#

#MAINDIR			!=	/bin/pwd
MAINDIR			=	.
SV_DIR			=	${MAINDIR}/source
QWDTOOLS_DIR	=	${MAINDIR}/source/qwdtools

# to compile mvdsv as 32bit on 64bit target platform use next:
# for gcc its like: make mvdsv FORCE32BITFLAGS=-m32
FORCE32BITFLAGS =

DO_CFLAGS		=	${CFLAGS} -Wall -pipe -funsigned-char -DUSE_PR2 -D${BYTE_ORDER}Q__ ${FORCE32BITFLAGS}

.if !defined(NOKQUEUE) && (${UNAME} == "FreeBSD" || ${UNAME} == "DragonFly")
DO_CFLAGS		+=	-DKQUEUE
.endif

WITH_OPTIMIZED_CFLAGS = YES

USE_ASM=-Did386
.if defined(WITH_OPTIMIZED_CFLAGS)
DO_CFLAGS		+=	-O2 -fno-strict-aliasing -s -ffast-math -funroll-loops
. if ${MACHINE_ARCH} == "i386" && !defined(WITHOUT_X86_ASM)
ASM=${USE_ASM}
DO_CFLAGS		+=	${ASM}
. endif
.endif

STRIP			=	strip
.if ${UNAME} != "Darwin" && ${UNAME} != "MacOSX"
STRIP_FLAGS		=	--strip-unneeded
.endif
STRIP_FLAGS		+=	--remove-section=.comment

LDFLAGS			=	-lm

#############################################################################
# SERVER
#############################################################################

SV_OBJS = \
		${SV_DIR}/pr_cmds.o \
		${SV_DIR}/pr_edict.o \
		${SV_DIR}/pr_exec.o \
\
		${SV_DIR}/pr2_cmds.o \
		${SV_DIR}/pr2_edict.o \
		${SV_DIR}/pr2_exec.o \
		${SV_DIR}/pr2_vm.o \
\
		${SV_DIR}/sv_ccmds.o \
		${SV_DIR}/sv_demo.o \
		${SV_DIR}/sv_demo_misc.o \
		${SV_DIR}/sv_demo_qtv.o \
		${SV_DIR}/sv_ents.o \
		${SV_DIR}/sv_init.o \
		${SV_DIR}/sv_login.o \
		${SV_DIR}/sv_main.o \
		${SV_DIR}/sv_master.o \
		${SV_DIR}/sv_mod_frags.o \
		${SV_DIR}/sv_move.o \
		${SV_DIR}/sv_nchan.o \
		${SV_DIR}/sv_phys.o \
		${SV_DIR}/sv_send.o \
		${SV_DIR}/sv_sys_unix.o \
		${SV_DIR}/sv_user.o \
\
		${SV_DIR}/bothtools.o \
		${SV_DIR}/cmd.o \
		${SV_DIR}/common.o \
		${SV_DIR}/cmodel.o \
		${SV_DIR}/crc.o \
		${SV_DIR}/cvar.o \
		${SV_DIR}/fs.o \
		${SV_DIR}/mathlib.o \
		${SV_DIR}/md4.o \
		${SV_DIR}/net_chan.o \
		${SV_DIR}/net.o \
		${SV_DIR}/pmove.o \
		${SV_DIR}/pmovetst.o \
		${SV_DIR}/sha1.o \
		${SV_DIR}/build.o \
		${SV_DIR}/world.o \
		${SV_DIR}/zone.o \
\
		${SV_DIR}/pcre/get.o \
		${SV_DIR}/pcre/pcre.o

.if ${USE_ASM} == ${ASM}
SV_ASM_OBJS = \
		${SV_DIR}/bothtoolsa.o \
		${SV_DIR}/math.o
.endif

#############################################################################
# QWDTOOLS
#############################################################################

QWDTOOLS_OBJS = \
		${SV_DIR}/bothtools.o \
		${QWDTOOLS_DIR}/dem_parse.o \
		${QWDTOOLS_DIR}/dem_send.o \
		${QWDTOOLS_DIR}/ini.o \
		${QWDTOOLS_DIR}/init.o \
		${QWDTOOLS_DIR}/main.o \
		${QWDTOOLS_DIR}/marge.o \
		${QWDTOOLS_DIR}/qwz.o \
		${QWDTOOLS_DIR}/sync.o \
		${QWDTOOLS_DIR}/tools.o

.if ${USE_ASM} == ${ASM}
QWDTOOLS_ASM_OBJS = \
		${SV_DIR}/bothtoolsa.o
.endif

#############################################################################
# SETUP AND BUILD
#############################################################################

.c.o:
		${CC} ${DO_CFLAGS} -c $< -o $*.o

.s.o:
		${CC} ${DO_CFLAGS} -DELF -x assembler-with-cpp -c $< -o $*.o
		
all:	mvdsv qwdtools
		${STRIP} ${STRIP_FLAGS} mvdsv qwdtools

mvdsv:	${SV_OBJS} ${SV_ASM_OBJS}
		${CC} ${DO_CFLAGS} ${LDFLAGS} -o mvdsv ${SV_OBJS} ${SV_ASM_OBJS}

qwdtools:	${QWDTOOLS_OBJS} ${QWDTOOLS_ASM_OBJS}
		${CC} ${DO_CFLAGS} ${LDFLAGS} -o qwdtools ${QWDTOOLS_OBJS} ${QWDTOOLS_ASM_OBJS}

clean:
		-rm -f ${SV_DIR}/*.core ${SV_DIR}/*.o ${SV_DIR}/pcre/*.o ${QWDTOOLS_DIR}/*.o mvdsv qwdtools
