# DOS/DJGPP makefile for Glide3/H5 and Texus2
#
#  Copyright (c) 2003 - Daniel Borca
#  Email : dborca@users.sourceforge.net
#  Web   : http://www.geocities.com/dborca
#
# $Header: /cvsroot/glide/glide3x/h5/glide3/src/Attic/Makefile.DJ,v 1.1.2.4 2005/11/29 22:05:11 dborca Exp $
#


#
#  Available options:
#
#    Environment variables:
#	FX_GLIDE_HW	build for the given ASIC (h5).
#			default = h5
#	H4=1		High speed Avenger/Napalm.
#			default = no
#	OPTFLAGS	pass given optimization flags to compiler
#			default = -O1 -ffast-math
#			-O2 has problems in SLI configurations and GCC3
#	DEBUG=1		enable debugging checks and messages
#			default = no
#	USE_X86=1	use assembler triangle specializations!
#			default = no
#	USE_3DNOW=1	allow 3DNow! specializations. However, the true CPU
#			capabilities are still checked at run-time to avoid
#			crashes.
#			default = no
#	USE_MMX=1	(see USE_3DNOW)
#			default = no
#	USE_SSE=1	(see USE_3DNOW)
#			default = no
#	USE_SSE2=1	(see USE_3DNOW)
#			default = no
#	TEXUS2=1	embed Texus2 functions into Glide3.
#			default = no
#
#    Targets:
#	all:		build everything
#	glide3x:	build Glide3x lib
#	clean:		remove object files
#	realclean:	remove all generated files
#



.PHONY: all glide3x clean realclean
.INTERMEDIATE: fxgasm.exe gendate.exe fxbldno.exe

###############################################################################
#	general defines (user settable?)
###############################################################################

GLIDE_LIB = libglide3x.a
GLIDE_DXE = glide3x.dxe
GLIDE_IMP = libglide3i.a
TEXUS_EXE = texus2.exe

FX_GLIDE_HW ?= h5
FX_GLIDE_SW = ../../../swlibs
GLIDE_LIBDIR = ../../lib
TEXUS_EXEDIR = $(FX_GLIDE_SW)/bin

###############################################################################
#	tools
###############################################################################

CC = gcc
HOST_CC = gcc
AS = nasm
AR = ar
DXE3GEN = dxe3gen

UNLINK = rm -f $(1)

###############################################################################
#	defines
###############################################################################

# platform
CDEFS = -D__DOS__ -D__DOS32__

# general
CDEFS += -DGLIDE3 -DGLIDE3_ALPHA -DGLIDE_HW_TRI_SETUP=1 -DGLIDE_INIT_HWC -DGLIDE_PACKED_RGB=0 -DGLIDE_PACKET3_TRI_SETUP=1 -DGLIDE_TRI_CULLING=1 -DUSE_PACKET_FIFO=1
#CDEFS += -DGLIDE_CHECK_CONTEXT

# subsystem
CDEFS += -DH3
ifdef H4
CDEFS += -DH4
endif
CDEFS += -DFX_GLIDE_H5_CSIM=1 -DFX_GLIDE_NAPALM=1

# debug
ifdef DEBUG
CDEFS += -DGDBG_INFO_ON -DGLIDE_DEBUG -DGLIDE_SANITY_ASSERT -DGLIDE_SANITY_SIZE
endif

# shameless plug and splash screen
#CDEFS += -DGLIDE_PLUG -DGLIDE_SPLASH

ifeq ($(TEXUS2),1)
CDEFS += -DHAVE_TEXUS2
endif

###############################################################################
#	flags
###############################################################################

# librarian
ARFLAGS = rus

# assembler
ASFLAGS = -O2 -fcoff -D__DJGPP__ --prefix _
ASFLAGS += $(CDEFS)

# compiler
CFLAGS = -Wall -fno-strict-aliasing
CFLAGS += -I. -I../../incsrc -I../../minihwc -I../../cinit
CFLAGS += -I$(FX_GLIDE_SW)/fxmisc -I$(FX_GLIDE_SW)/newpci/pcilib -I$(FX_GLIDE_SW)/fxmemmap
CFLAGS += -I$(FX_GLIDE_SW)/texus2/lib
CFLAGS += $(CDEFS)

ifeq ($(USE_3DNOW),1)
CFLAGS += -DGL_AMD3D
override USE_X86 = 1
endif
ifeq ($(USE_MMX),1)
CFLAGS += -DGL_MMX
override USE_X86 = 1
endif
ifeq ($(USE_SSE),1)
CFLAGS += -DGL_SSE
override USE_X86 = 1
endif
ifeq ($(USE_SSE2),1)
CFLAGS += -DGL_SSE2
override USE_X86 = 1
endif

ifeq ($(USE_X86),1)
CFLAGS += -DGL_X86
OPTFLAGS ?= -O1 -ffast-math
else
CFLAGS += -DGLIDE_USE_C_TRISETUP
OPTFLAGS ?= -O1 -ffast-math
endif

# optflags
CFLAGS += $(OPTFLAGS)
HOST_CFLAGS=$(filter-out -mcpu=% -mtune=% -march=%,$(CFLAGS))

###############################################################################
#	objects
###############################################################################

GLIDE_OBJECTS = \
	fifo.o \
	distate.o \
	gstrip.o \
	distrip.o \
	diget.o \
	gsplash.o \
	g3df.o \
	gu.o \
	gpci.o \
	diglide.o \
	disst.o \
	ditex.o \
	gbanner.o \
	gerror.o \
	gaa.o \
	gdraw.o \
	gglide.o \
	glfb.o \
	gsst.o \
	gtex.o \
	gtexdl.o \
	xtexdl_def.o

ifeq ($(USE_X86),1)
GLIDE_OBJECTS += \
	cpuid.o \
	xdraw2_def.o \
	xdraw3_def.o
ifeq ($(USE_3DNOW),1)
GLIDE_OBJECTS += \
	xdraw2_3dnow.o \
	xdraw3_3dnow.o \
	xtexdl_3dnow.o
endif
ifeq ($(USE_MMX),1)
GLIDE_OBJECTS += \
	xtexdl_mmx.o
endif
ifeq ($(USE_SSE),1)
GLIDE_OBJECTS += \
	xdraw2_sse.o \
	xdraw3_sse.o
endif
ifeq ($(USE_SSE2),1)
GLIDE_OBJECTS += \
	xtexdl_sse2.o
endif
else
GLIDE_OBJECTS += \
	gxdraw.o
endif

GLIDE_OBJECTS += \
	gthread.o \
	$(FX_GLIDE_SW)/newpci/pcilib/fxpci.o \
	$(FX_GLIDE_SW)/newpci/pcilib/fxdpmi2.o \
	../../minihwc/hwcio.o \
	../../minihwc/gdebug.o \
	../../minihwc/minihwc.o \
	../../minihwc/dos_mode.o \
	../../minihwc/gpio.o \
	../../cinit/h3cinit.o

TEXUS_SOURCES = \
	$(FX_GLIDE_SW)/texus2/lib/texuslib.c \
	$(FX_GLIDE_SW)/texus2/lib/clamp.c \
	$(FX_GLIDE_SW)/texus2/lib/read.c \
	$(FX_GLIDE_SW)/texus2/lib/resample.c \
	$(FX_GLIDE_SW)/texus2/lib/mipmap.c \
	$(FX_GLIDE_SW)/texus2/lib/quantize.c \
	$(FX_GLIDE_SW)/texus2/lib/ncc.c \
	$(FX_GLIDE_SW)/texus2/lib/nccnnet.c \
	$(FX_GLIDE_SW)/texus2/lib/pal256.c \
	$(FX_GLIDE_SW)/texus2/lib/pal6666.c \
	$(FX_GLIDE_SW)/texus2/lib/dequant.c \
	$(FX_GLIDE_SW)/texus2/lib/view.c \
	$(FX_GLIDE_SW)/texus2/lib/util.c \
	$(FX_GLIDE_SW)/texus2/lib/diffuse.c \
	$(FX_GLIDE_SW)/texus2/lib/write.c \
	$(FX_GLIDE_SW)/texus2/lib/tga.c \
	$(FX_GLIDE_SW)/texus2/lib/3df.c \
	$(FX_GLIDE_SW)/texus2/lib/ppm.c \
	$(FX_GLIDE_SW)/texus2/lib/rgt.c \
	$(FX_GLIDE_SW)/texus2/lib/txs.c \
	$(FX_GLIDE_SW)/texus2/lib/codec.c \
	$(FX_GLIDE_SW)/texus2/lib/eigen.c \
	$(FX_GLIDE_SW)/texus2/lib/bitcoder.c

ifeq ($(TEXUS2),1)
GLIDE_OBJECTS += $(TEXUS_SOURCES:.c=.o)
endif

###############################################################################
#	rules
###############################################################################

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

###############################################################################
#	main
###############################################################################
all: glide3x $(TEXUS_EXEDIR)/$(TEXUS_EXE)

glide3x: $(GLIDE_LIBDIR)/$(GLIDE_LIB) $(GLIDE_LIBDIR)/$(GLIDE_DXE) $(GLIDE_LIBDIR)/$(GLIDE_IMP)

$(GLIDE_LIBDIR)/$(GLIDE_LIB): $(GLIDE_OBJECTS)
	$(AR) $(ARFLAGS) $@ $^

$(GLIDE_LIBDIR)/$(GLIDE_DXE) $(GLIDE_LIBDIR)/$(GLIDE_IMP): $(GLIDE_OBJECTS)
	-$(DXE3GEN) -o $(GLIDE_LIBDIR)/$(GLIDE_DXE) -Y $(GLIDE_LIBDIR)/$(GLIDE_IMP) -D "Glide3(h5) DJGPP" -E _gr -E _gu -E _tx -U $^

$(TEXUS_EXEDIR)/$(TEXUS_EXE): $(FX_GLIDE_SW)/texus2/cmd/cmd.c $(GLIDE_LIBDIR)/$(GLIDE_LIB)
ifeq ($(TEXUS2),1)
	$(CC) -o $@ -s $(CFLAGS) -L$(GLIDE_LIBDIR) $< -lglide3x
else
	$(warning Texus2 not enabled... Skipping $(TEXUS_EXE))
endif

###############################################################################
#	rules(2)
###############################################################################

xdraw2_def.o: xdraw2.asm
	$(AS) -o $@ $(ASFLAGS) $<
xdraw3_def.o: xdraw3.asm
	$(AS) -o $@ $(ASFLAGS) $<
xdraw2_3dnow.o: xdraw2.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_AMD3D=1 $<
xdraw3_3dnow.o: xdraw3.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_AMD3D=1 $<
xtexdl_3dnow.o: xtexdl.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_AMD3D=1 $<
xtexdl_mmx.o: xtexdl.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_MMX=1 $<
xdraw2_sse.o: xdraw2.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_SSE=1 $<
xdraw3_sse.o: xdraw3.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_SSE=1 $<
xtexdl_sse2.o: xtexdl.asm
	$(AS) -o $@ $(ASFLAGS) -DGL_SSE2=1 $<

$(GLIDE_OBJECTS): fxinline.h fxgasm.h fxbldno.h

# cross-compile workaround:
ifneq ($(COMSPEC),)
RUN_FXGASM=fxgasm.exe
RUN_FXBLDNO=fxbldno.exe
RUN_GENDATE=gendate.exe
else
RUN_FXGASM=./fxgasm.exe
RUN_FXBLDNO=./fxbldno.exe
RUN_GENDATE=./gendate.exe
endif

fxinline.h: fxgasm.exe
	$(RUN_FXGASM) -inline > $@

fxgasm.h: fxgasm.exe
	$(RUN_FXGASM) -hex > $@

fxgasm.exe: fxgasm.c ../../incsrc/gendate.h
	$(HOST_CC) -o $@ $(HOST_CFLAGS) $<

../../incsrc/gendate.h: gendate.exe
	$(RUN_GENDATE) > $@

fxbldno.h: fxbldno.exe
	$(RUN_FXBLDNO) > $@

gendate.exe: ../../incsrc/gendate.c
	$(HOST_CC) -o $@ $(HOST_CFLAGS) $<

fxbldno.exe: fxbldno.c
	$(HOST_CC) -o $@ $(HOST_CFLAGS) $<

###############################################################################
#	clean, realclean
###############################################################################

clean:
	-$(call UNLINK,*.o)
	-$(call UNLINK,../../cinit/*.o)
	-$(call UNLINK,../../minihwc/*.o)
	-$(call UNLINK,$(FX_GLIDE_SW)/newpci/pcilib/*.o)
	-$(call UNLINK,fxinline.h)
	-$(call UNLINK,fxgasm.h)
	-$(call UNLINK,../../incsrc/gendate.h)
	-$(call UNLINK,fxbldno.h)
	-$(call UNLINK,$(FX_GLIDE_SW)/texus2/lib/*.o)

realclean: clean
	-$(call UNLINK,$(GLIDE_LIBDIR)/$(GLIDE_LIB))
	-$(call UNLINK,$(GLIDE_LIBDIR)/$(GLIDE_DXE))
	-$(call UNLINK,$(GLIDE_LIBDIR)/$(GLIDE_IMP))
	-$(call UNLINK,$(TEXUS_EXEDIR)/$(TEXUS_EXE))
