#
# Quake2 gamei386.so Makefile for Linux 2.0
#
# Jan '98 by Zoid <zoid@idsoftware.com>
#
# ELF only
#
# Probably requires GNU make
#
# Modified by QuDos at http://qudos.quakedev.com
#

# Check OS type.
OSTYPE := $(shell uname -s)

# this nice line comes from the linux kernel makefile
ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc/ -e s/sparc64/sparc/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/alpha/axp/)

ifneq ($(ARCH),i386)
ifneq ($(ARCH),axp)
ifneq ($(ARCH),ppc)
ifneq ($(ARCH),sparc)
$(error arch $(ARCH) is currently not supported)
endif
endif
endif
endif

BUILD_DEBUG_DIR=debug$(ARCH)
BUILD_RELEASE_DIR=release$(ARCH)


CC=gcc
BASE_CFLAGS=-Dstricmp=strcasecmp 

RELEASE_CFLAGS=$(BASE_CFLAGS) -ffast-math -funroll-loops \
	-fomit-frame-pointer -fexpensive-optimizations
	
DEBUG_CFLAGS=$(BASE_CFLAGS) -g -ggdb -Wall -pipe

ifeq ($(OSTYPE),FreeBSD)
  LDFLAGS=-lm
endif

ifeq ($(OSTYPE),Linux)
  LDFLAGS=-lm -ldl
endif

SHLIBEXT=so

SHLIBCFLAGS=-fPIC

SHLIBLDFLAGS=-shared

DO_CC=$(CC) $(CFLAGS) $(SHLIBCFLAGS) -o $@ -c $<
DO_SHLIB_CC=$(CC) $(CFLAGS) $(SHLIBCFLAGS) -o $@ -c $<

TARGETS=$(BUILDDIR)/matrix/game$(ARCH).$(SHLIBEXT)
	
all:
	@echo
	@echo Possible targets:
	@echo
	@echo ">> make release	       build the binary for a release."
	@echo ">> make debug	       build the binary for debuging mode."
	@echo
debug:
	@-mkdir -p $(BUILD_DEBUG_DIR)/matrix
	$(MAKE) targets BUILDDIR=$(BUILD_DEBUG_DIR) CFLAGS="$(DEBUG_CFLAGS)"
	
release:
	@-mkdir -p $(BUILD_RELEASE_DIR)/matrix
	$(MAKE) targets BUILDDIR=$(BUILD_RELEASE_DIR) CFLAGS="$(RELEASE_CFLAGS)"
	
targets: $(TARGETS)

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


GAME_OBJS = \
        $(BUILDDIR)/g_ai.o \
        $(BUILDDIR)/g_chase.o \
        $(BUILDDIR)/g_cmds.o \
        $(BUILDDIR)/g_combat.o \
        $(BUILDDIR)/g_func.o \
        $(BUILDDIR)/g_items.o \
        $(BUILDDIR)/g_main.o \
        $(BUILDDIR)/g_misc.o \
        $(BUILDDIR)/g_monster.o \
        $(BUILDDIR)/g_phys.o \
        $(BUILDDIR)/g_save.o \
        $(BUILDDIR)/g_spawn.o \
        $(BUILDDIR)/g_svcmds.o \
        $(BUILDDIR)/g_target.o \
        $(BUILDDIR)/g_trigger.o \
        $(BUILDDIR)/g_turret.o \
        $(BUILDDIR)/g_utils.o \
        $(BUILDDIR)/g_weapon.o \
        $(BUILDDIR)/leper_think.o \
        $(BUILDDIR)/matrix_autobuy.o \
        $(BUILDDIR)/matrix_cam.o \
        $(BUILDDIR)/matrix_cmds.o \
        $(BUILDDIR)/matrix_findtarget.o \
        $(BUILDDIR)/matrix_gweap.o \
        $(BUILDDIR)/matrix_items.o \
        $(BUILDDIR)/matrix_kungfu.o \
        $(BUILDDIR)/matrix_locadam.o \
        $(BUILDDIR)/matrix_main.o \
        $(BUILDDIR)/matrix_match.o \
        $(BUILDDIR)/matrix_menu.o \
        $(BUILDDIR)/matrix_misc.o \
        $(BUILDDIR)/matrix_movement.o \
        $(BUILDDIR)/matrix_pweap.o \
        $(BUILDDIR)/matrix_scanner.o \
        $(BUILDDIR)/matrix_spells.o \
        $(BUILDDIR)/matrix_stamina.o \
        $(BUILDDIR)/matrix_tank.o \
        $(BUILDDIR)/matrix_team.o \
        $(BUILDDIR)/m_actor.o \
        $(BUILDDIR)/m_berserk.o \
        $(BUILDDIR)/m_boss2.o \
        $(BUILDDIR)/m_boss3.o \
        $(BUILDDIR)/m_boss31.o \
        $(BUILDDIR)/m_boss32.o \
        $(BUILDDIR)/m_brain.o \
        $(BUILDDIR)/m_chick.o \
        $(BUILDDIR)/m_flash.o \
        $(BUILDDIR)/m_flipper.o \
        $(BUILDDIR)/m_float.o \
        $(BUILDDIR)/m_flyer.o \
        $(BUILDDIR)/m_gladiator.o \
        $(BUILDDIR)/m_gunner.o \
        $(BUILDDIR)/m_hover.o \
        $(BUILDDIR)/m_infantry.o \
        $(BUILDDIR)/m_insane.o \
        $(BUILDDIR)/m_medic.o \
        $(BUILDDIR)/m_move.o \
        $(BUILDDIR)/m_mutant.o \
        $(BUILDDIR)/m_parasite.o \
        $(BUILDDIR)/m_soldier.o \
        $(BUILDDIR)/m_supertank.o \
        $(BUILDDIR)/m_tank.o \
        $(BUILDDIR)/p_chase.o \
        $(BUILDDIR)/p_client.o \
        $(BUILDDIR)/p_hud.o \
        $(BUILDDIR)/p_menu.o \
        $(BUILDDIR)/p_trail.o \
        $(BUILDDIR)/p_view.o \
        $(BUILDDIR)/p_weapon.o \
        $(BUILDDIR)/q_shared.o


$(BUILDDIR)/matrix/game$(ARCH).$(SHLIBEXT) : $(GAME_OBJS)
	$(CC) $(CFLAGS) $(SHLIBLDFLAGS) -o $@ $(GAME_OBJS) $(LDFLAGS)
	
$(BUILDDIR)/g_ai.o :        g_ai.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_chase.o :    p_chase.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_client.o :    p_client.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_menu.o :    p_menu.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_cmds.o :      g_cmds.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_svcmds.o :    g_svcmds.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_chase.o :     g_chase.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_combat.o :    g_combat.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_func.o :      g_func.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_items.o :     g_items.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_main.o :      g_main.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_misc.o :      g_misc.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_monster.o :   g_monster.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_phys.o :      g_phys.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_save.o :      g_save.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_spawn.o :     g_spawn.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_target.o :    g_target.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_trigger.o :   g_trigger.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_turret.o :    g_turret.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_utils.o :     g_utils.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/g_weapon.o :    g_weapon.c
	$(DO_SHLIB_CC)
	
$(BUILDDIR)/leper_think.o :    leper_think.c
	$(DO_SHLIB_CC)
	
$(BUILDDIR)/matrix_autobuy.o : matrix_autobuy.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_cam.o :     matrix_cam.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_cmds.o :    matrix_cmds.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_findtarget.o : matrix_findtarget.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_gweap.o :        matrix_gweap.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_items.o :      matrix_items.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_kungfu.o :     matrix_kungfu.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_locadam.o :    matrix_locadam.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_main.o :       matrix_main.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_match.o :      matrix_match.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_menu.o :       matrix_menu.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_misc.o :       matrix_misc.c
	$(DO_SHLIB_CC)
	
$(BUILDDIR)/matrix_movement.o :   matrix_movement.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_pweap.o :      matrix_pweap.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_scanner.o :    matrix_scanner.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_spells.o :     matrix_spells.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_stamina.o :    matrix_stamina.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_tank.o :       matrix_tank.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/matrix_team.o :       matrix_team.c
	$(DO_SHLIB_CC)
	
$(BUILDDIR)/m_actor.o :     m_actor.c
	$(DO_SHLIB_CC)
	
$(BUILDDIR)/m_berserk.o :   m_berserk.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_boss2.o :     m_boss2.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_boss3.o :     m_boss3.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_boss31.o :    m_boss31.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_boss32.o :    m_boss32.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_brain.o :     m_brain.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_chick.o :     m_chick.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_flipper.o :   m_flipper.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_float.o :     m_float.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_flyer.o :     m_flyer.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_gladiator.o : m_gladiator.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_gunner.o :    m_gunner.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_hover.o :     m_hover.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_infantry.o :  m_infantry.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_insane.o :    m_insane.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_medic.o :     m_medic.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_move.o :      m_move.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_mutant.o :    m_mutant.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_parasite.o :  m_parasite.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_soldier.o :   m_soldier.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_supertank.o : m_supertank.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_tank.o :      m_tank.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_hud.o :       p_hud.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_trail.o :     p_trail.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_view.o :      p_view.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/p_weapon.o :    p_weapon.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/q_shared.o :    q_shared.c
	$(DO_SHLIB_CC)

$(BUILDDIR)/m_flash.o :     m_flash.c
	$(DO_SHLIB_CC)

#############################################################################
# MISC
#############################################################################


clean: clean-debug clean-release

clean-debug:
	$(MAKE) clean2 BUILDDIR=$(BUILD_DEBUG_DIR) CFLAGS="$(DEBUG_CFLAGS)"
	
clean-release:
	$(MAKE) clean2 BUILDDIR=$(BUILD_RELEASE_DIR) CFLAGS="$(DEBUG_CFLAGS)"
	
clean2:
	-rm -f $(GAME_OBJS)


