######################################################################
# Linux Makefile for Quake2 v1.3
# Robert "Arras" LeBlanc <rjl@renaissoft.com>
#
# This Makefile is based on the "authoritative" Makefile released
# by Zoid <zoid@idsoftware.com> and refined somewhat by Jet
# <jet@poboxes.com>, and now finally refined a bit more by me to
# make it more useful to mod authors.
#
# To use this Makefile to build your own mod, follow the steps below:
#
# Step 1: Get the original 12/11 source distribution from id Software
#         and copy the contents of the 'game' subdirectory to a
#         working directory.
#
# Step 2: Copy this Makefile and its accompanying game.diff file into
#         the working directory.
#
# Step 3: Go to the working directory and type "make patch".  This
#         will strip the carriage returns from all source files and
#         patch them to fix some syntactical items that gcc doesn't
#         like, even if Microsoft Visual C++ 5.0 lets them slide.
#
# Step 4: Copy any mod-related source files of yours into the working
#         directory.  Note that if you changed g_local.h, game.h, or
#         m_player.h, you'll have to make a few simple changes:
#
#         g_local.h: 
#         Line 754: change "typedef struct gclient_s" to "struct gclient_s"
#         Line 828: change "} gclient_t;" to "};"
#
#         game.h:
#         Line 4: change "GAME_API_VERSION 1" to "GAME_API_VERSION 2"
#
#         m_player.h:
#         Line 205: change CTRL-Z to a newline
#
# Step 5: If you developed your source files on a Win32 machine, type
#         "make stripcr" to get rid of any stray carriage returns that
#         may be lurking in your files.
#
# Step 6: Add your custom mod-related files to the C_OBJS list below,
#         so that the Makefile knows to build them.  For every *.c
#         file in your mod you should have a corresponding *.o file
#         listed under C_OBJS.  Don't list files that are already
#         part of id's sources (e.g. g_cmds.o, p_client.o, etc.),
#         they're already known to the Makefile; just list any *.c
#         files specific to your mod.
#
# Step 7: Type "make dep" to build a list of dependencies based on
#         the total set of source files in the working directory.
#
# Step 8: Type "make" to build your mod.
######################################################################


######################################################################
# C_OBJS (Custom Objects): Mod authors should use this group below to
# specify any custom files required by their mods that are not
# included in id's sources.  e.g. if your mod requires the addition
# of custom files "foo.c" and "bar.c", you'd add:
#
# C_OBJS = foo.o bar.o
#
# Leave this empty if you just want to build id's default gamei386.so.
######################################################################
C_OBJS = g_cfgserver.o g_hook.o g_motd.o g_teamplay.o g_voteexit.o \
         p_observer.o q_devels.o


######################################################################
# End of user-customizable section - you shouldn't have to touch
# anything below this point.
######################################################################

# Game-related objects
G_OBJS = g_ai.o g_cmds.o g_combat.o g_func.o g_items.o g_main.o \
         g_misc.o g_monster.o g_phys.o g_save.o g_spawn.o g_target.o \
         g_trigger.o g_turret.o g_utils.o g_weapon.o

# Monster-related objects
M_OBJS = m_actor.o m_berserk.o m_boss2.o m_boss3.o m_boss31.o \
         m_boss32.o m_brain.o m_chick.o m_flipper.o m_float.o \
         m_flyer.o m_gladiator.o m_gunner.o m_hover.o m_infantry.o \
         m_insane.o m_medic.o m_move.o m_mutant.o m_parasite.o \
         m_soldier.o m_supertank.o m_tank.o m_flash.o

# Player-related objects
P_OBJS = p_client.o p_hud.o p_trail.o p_view.o p_weapon.o

# Quake2-related objects
Q_OBJS = q_shared.o

# Note that the mod author's Custom Objects (C_OBJS) are built first,
# which should speed up the detection of compilation errors; if they
# build properly, the rest of id's code should build without complaint,
# at least until link time :)
#
OBJS = $(C_OBJS) $(G_OBJS) $(M_OBJS) $(P_OBJS) $(Q_OBJS)

TARGET = gamei386.so

CC = gcc

SHELL = /bin/sh

# This #define saves us from having to change stricmp() to
# strcasecmp() in the actual sources, which means we can leave
# the source code as portable as possible (Win32 doesn't like
# strcasecmp() any more than Linux likes stricmp()).  By doing
# it this way the same source code can be built under both
# platforms unchanged.
#
BASE_CFLAGS = -Dstricmp=strcasecmp

# This builds objects optimized for speed rather than size.
CFLAGS = $(BASE_CFLAGS) -m486 -O3 -DC_ONLY -ffast-math -funroll-loops \
	 -fomit-frame-pointer -fexpensive-optimizations -malign-loops=2 \
	 -malign-jumps=2 -malign-functions=2 -fno-strength-reduce

# Linker flags for building a shared library (*.so).
LDFLAGS = -ldl -lm
SHLIBCFLAGS = -fPIC
SHLIBLDFLAGS = -shared

######################################################################
# Targets
######################################################################

all: $(TARGET)

.c.o:
	$(CC) $(CFLAGS) $(SHLIBCFLAGS) -o $@ -c $<

$(TARGET):	$(OBJS)
		$(CC) $(CFLAGS) $(SHLIBLDFLAGS) -o $@ $(OBJS) $(LDFLAGS)
		strip $(TARGET)

dep:
	@echo "Updating dependencies..."
	@$(CC) -MM $(OBJS:.o=.c) > .depend

stripcr:	.
		@echo "Stripping carriage returns from source files..."
	 	@for f in *.[ch]; do \
		  cat $$f | tr -d '\015' > .stripcr; \
		  mv .stripcr $$f; \
		done; \
		rm -f .stripcr

patch:		stripcr
		@echo "Patching id's original source files..."
		@patch -s < game.diff
		@rm -f *.orig ~*

clean:	
		@echo "Deleting temporary files..."
		@rm -f $(OBJS) *.orig ~* core

distclean:	clean
		@echo "Deleting everything that can be rebuilt..."
		@rm -f $(TARGET) .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
