# This MUST be processed by GNU make
#
# Glide64 Makefile
# Version:  1.0
#
# this is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# this is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Make; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

#
#  Available options:
#
#    Environment variables:
#	DEBUG=1		enable debugging checks and messages
#			default = no
#
#    Environment variables:
#
#    Targets:
#	all:		build dynamic module
#	clean:		remove object files
#	realclean:	remove all generated files
#
#
#  Requirements:
#
#    Compiler:
#       GCC 4.2
#
#    Libraries:
#       glide3x - build Glitch64 first, copy the result dll to 'lib' folder
#       wx-widgets 2.8 (http://www.wxwidgets.org)
#

#
# GCC does not have SEH (structured exception handling)
#

.PHONY: all clean realclean ostype

OS=$(shell uname)
ifeq ($(findstring MINGW,$(OS)),MINGW)
OS := WINDOWS
endif
ifeq ($(findstring CYGWIN,$(OS)),CYGWIN)
OS=WINDOWS
endif
ifeq ($(findstring WINNT,$(OS)),WINNT)
OS=WINDOWS
endif
ifeq ($(OS),Darwin)
OS=MACOSX
endif

ifeq ($(OS), WINDOWS)
DLLNAME = Glide64.dll
else
DLLNAME = Glide64.so
endif

EXT_INC = ./inc
EXT_LIB = ./lib

CC = g++ 
STRIP = strip

CFLAGS = -DBUILDING_DLL=1  -fexceptions 
LDLIBS = -L"." -L"lib" `wx-config --libs` 

ifeq ($(OS), Linux)
CC += -V 4.2
LDFLAGS = -shared -lstdc++ 
CFLAGS += -D__unix__
LDLIBS += $(EXT_LIB)/glide3x.so
endif
ifeq ($(OS), MACOSX)
LDFLAGS = -dynamiclib -lstdc++ 
CFLAGS += -D__unix__ -Dmacintosh
LDLIBS += $(EXT_LIB)/glide3x.dylib
endif
ifeq ($(OS), WINDOWS)
LDFLAGS = -shared -mwindows 
CFLAGS += -D__WIN32__ -DWIN32 -D_WIN32
LDLIBS += $(EXT_LIB)/glide3x.lib
endif

CFLAGS += -ffast-math -funroll-loops
#CFLAGS += -fexpensive-optimizations -march=k6
CFLAGS += -I. -I$(EXT_INC) `wx-config --cppflags`

ifdef DEBUG
CFLAGS += -g -DDEBUG
endif

LD = g++

AS = nasm
ifeq ($(OS), Linux)
ASFLAGS = -O6 -felf -D__linux__ 
ASM_OBJ = \
  3dmathSIMD.o \
  FixedPoint.o \
  Texture.o
else 
  ifeq ($(OS), MACOSX)
    ASFLAGS = -O6 -fmacho --prefix _
    ASM_OBJ = \
     3dmathSIMD.o \
     FixedPoint.o \
     Texture.o
  else 
    ifeq ($(OS), WINDOWS)
      ASFLAGS = -O6 -fwin32 -D__WIN32__ --prefix _
      ASM_OBJ = \
       3dmathSIMD.obj \
       FixedPoint.obj \
       Texture.obj
    endif
  endif
endif

RM = rm

SOURCES = \
  3dmath.cpp \
  Combine.cpp \
  Config.cpp \
  CRC.cpp \
  Debugger.cpp \
  DepthBufferRender.cpp \
  Ext_TxFilter.cpp \
  FBtoScreen.cpp \
  Main.cpp \
  Keys.cpp \
  rdp.cpp \
  TexBuffer.cpp \
  TexCache.cpp \
  Util.cpp

OBJECTS = $(SOURCES:.cpp=.o)

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

all: $(DLLNAME)

$(DLLNAME): $(OBJECTS) $(ASM_OBJ)
	$(LD) -o $@ $(LDFLAGS) $^ $(LDLIBS)
ifeq ($(OS), Linux)
	$(STRIP) $@
endif

ifneq ($(OS), WINDOWS)
3dmathSIMD.o: 3dmathSIMD.asm
	$(AS) -o $@ $(ASFLAGS) $<
FixedPoint.o: FixedPoint.asm
	$(AS) -o $@ $(ASFLAGS) $<
Texture.o: Texture.asm
	$(AS) -o $@ $(ASFLAGS) $<
else
3dmathSIMD.obj: 3dmathSIMD.asm
	$(AS) -o $@ $(ASFLAGS) $<
FixedPoint.obj: FixedPoint.asm
	$(AS) -o $@ $(ASFLAGS) $<
Texture.obj: Texture.asm
	$(AS) -o $@ $(ASFLAGS) $<
endif

clean:
	-$(RM) *.o

realclean: clean
	-$(RM) $(DLLNAME) 

ostype:
	echo $(OS)

-include depend
