# This builds the library file of the 3D graphics API
#

all:  checkopt rw3 all-recursive

# include target specific make options

include makeopt
-include makedir/$(RWTARGET)

# Define pipe type (makedir/$(RWTARGET) had first go)
ifndef PIPETYPE
PIPETYPE = p2
endif

-include makedir/pipe/$(PIPETYPE)

# Find the pipeline files
VPATH += $(SRCDIR)/pipe/$(PIPETYPE)
C_INC += -I$(SRCDIR)/pipe/$(PIPETYPE)
CORE_INC += -I$(SRCDIR)/pipe/$(PIPETYPE)

ifneq ("$(PIPETYPE)", "generic")
VPATH += $(SRCDIR)/pipe/$(PIPETYPE)/$(RWTARGET)
C_INC += -I$(SRCDIR)/pipe/$(PIPETYPE)/$(RWTARGET)
CORE_INC += -I$(SRCDIR)/pipe/$(PIPETYPE)/$(RWTARGET)
endif

# define Object files which comprise the library
STUBDRVOBJ := $(DRVSPECIFICOBJDIR)/sbdevice.$(O)

OSOBJ := $(OBJDIR)/osintf.$(O)

CORECOBJ :=  					\
	$(OBJDIR)/babbox.$(O) 			\
	$(OBJDIR)/babincam.$(O) 		\
	$(OBJDIR)/babinfrm.$(O) 		\
	$(OBJDIR)/babintex.$(O) 		\
	$(OBJDIR)/bacamera.$(O) 		\
	$(OBJDIR)/badevice.$(O) 		\
	$(OBJDIR)/baframe.$(O) 			\
	$(OBJDIR)/baimage.$(O) 			\
	$(OBJDIR)/baimform.$(O) 		\
	$(OBJDIR)/baimras.$(O) 			\
	$(OBJDIR)/baraster.$(O) 		\
	$(OBJDIR)/baresamp.$(O) 		\
	$(OBJDIR)/basync.$(O) 			\
	$(OBJDIR)/batextur.$(O) 		\
	$(OBJDIR)/batypehf.$(O) 		\

PLCORECOBJ :=  					\
	$(OBJDIR)/babinary.$(O) 		\
	$(OBJDIR)/babinmtx.$(O) 		\
	$(OBJDIR)/bacolor.$(O) 			\
	$(OBJDIR)/badebug.$(O) 			\
	$(OBJDIR)/baerr.$(O) 			\
	$(OBJDIR)/bafsys.$(O) 			\
	$(OBJDIR)/baimmedi.$(O) 		\
	$(OBJDIR)/balist.$(O) 			\
	$(OBJDIR)/bamatrix.$(O) 		\
	$(OBJDIR)/bamemory.$(O) 		\
	$(OBJDIR)/baresour.$(O) 		\
	$(OBJDIR)/bastream.$(O) 		\
	$(OBJDIR)/batkbin.$(O) 			\
	$(OBJDIR)/batkreg.$(O) 			\
	$(OBJDIR)/bavector.$(O) 		\
	$(OBJDIR)/resmem.$(O) 			\
	$(OBJDIR)/rwdbgerr.$(O) 		\
	$(OBJDIR)/rwstring.$(O)

PLCOREINITOBJ = $(OBJDIR)/baplcore.$(O)

COREOBJ   := $(CORECOBJ) $(PLCORECOBJ) $(PIPEOBJ) $(PIPEPSOBJ) $(DRVOBJ) $(OSOBJ)
PLCOREOBJ := $(PLCORECOBJ) $(PLCOREINITOBJ) $(STUBDRVOBJ)  $(OSOBJ)

CORECSRC   = $(patsubst $(OBJDIR)/%.$(O),src/%.c, $(CORECOBJ))
PIPESRC    = $(patsubst $(OBJDIR)/%.$(O),src/pipe/$(PIPETYPE)/%.c, $(PIPEOBJ))

PIPEPSSRC  = $(patsubst $(OBJDIR)/%.$(O),src/pipe/$(PIPETYPE)/$(RWTARGET)/%.c, $(PIPEPSOBJ))
DRVSRC     = $(patsubst $(OBJDIR)/%.$(O),driver/$(RWTARGET)/%.c, $(DRVOBJ))
OSSRC      = $(patsubst $(OBJDIR)/%.$(O),os/$(RWOS)/%.c, $(OSOBJ))

PLCORECSRC = $(patsubst $(OBJDIR)/%.$(O),src/plcore/%.c,$(PLCORECOBJ))
STUBDRVSRC = $(patsubst $(OBJDIR)/%.$(O),driver/stub/%.c, $(DRVOBJ))

CORESRC   := $(CORECSRC) $(PLCORECSRC) $(PIPESRC) $(PIPEPSSRC) $(DRVSRC) $(OSSRC)
PLCORESRC := $(PLCORECSRC) $(STUBDRVSRC) $(OSSRC)

DOCSRCPLCORE  = $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR)/plcore, $(PLCORECOBJ))))
DOCSRCPLCORE += $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR)/plcore, $(PLCOREINITOBJ))))

DOCSRCCORE    = $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR), $(CORECOBJ))))
DOCSRCPIPE    = $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR)/pipe/$(PIPETYPE), $(PIPEOBJ))))
DOCSRCPIPEPS  = $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR)/pipe/$(PIPETYPE)/$(RWTARGET), $(PIPEPSOBJ))))
DOCSRCOS      = $(addsuffix .c, $(basename $(subst $(OBJDIR),$(SRCDIR), $(CORECOBJ))))

DOCGENERICDRV   = $(addsuffix .c, $(basename $(subst $(DRVGENERICOBJDIR), $(DRVGENERICSRCDIR), $(GENERICDRVOBJ))))
DOCSPECIFICDRV  = $(addsuffix .c, $(basename $(subst $(DRVSPECIFICOBJDIR), $(DRVSPECIFICSRCDIR), $(SPECIFICDRVOBJ))))
DOCSRCDRV       = $(filter-out %.$(L), $(DOCGENERICDRV)  $(DOCSPECIFICDRV))

DOCSRCCORE += $(DOCSRCPIPE) $(DOCSRCPIPEPS) $(SRCDIR)/baim3doc.c

# HCORE is all of the .h files in the source directory - derived from the
# .c files so a few exceptions have to be removed
HCORE  = $(filter-out $(BASEDIR)/src/coredbg.h, $(subst .c,.h,$(DOCSRCCORE)))
ifeq ("$(PIPETYPE)", "p2")
HCORE += $(BASEDIR)/src/pipe/$(PIPETYPE)/$(RWTARGET)/pip2model.h
endif

HPLCORE  = $(addprefix $(BASEDIR)/, $(subst .c,.h,$(PLCORESRC)))
HPLCORE += $(BASEDIR)/src/plcore/balibtyp.h $(BASEDIR)/src/plcore/batypes.h

#determine total build list

LIBS = $(CORELIB) $(PLCORELIB)

ifeq ($(NOASM), 1)
BAEQUDEP=
else
BAEQUDEP=$(DEVDIR)/$(RWTARGET)/baequate.i
endif

# RWTARGETs

message:
	@echo Building RenderWare target - $(RWTARGET)
	@echo Using compiler - $(RWCOMPILER)
	@echo Using operating system - $(RWOS)
	@echo

PLUGINDIR = plugin
ifndef PLUGINS
PLUGINS = $(notdir $(wildcard $(PLUGINDIR)/*))
endif

APLUGINDIR = plugin.archive
ifndef APLUGINS
APLUGINS = $(notdir $(wildcard $(APLUGINDIR)/*))
endif

DPLUGINDIR = plugin.dev
#ifndef DPLUGINS
#DPLUGINS = $(notdir $(wildcard $(DPLUGINDIR)/*))
#endif

TOOLDIR = tool
ifndef TOOLS
TOOLS = $(notdir $(wildcard $(TOOLDIR)/*))
endif

.PHONY : all-recursive clean-recursive
.PHONY : doxy-recursive doc-recursive defgroup-recursive
.PHONY : header-recursive distclean-recursive
.PHONY : verify-recursive longline-recursive tabs-recursive

all-recursive clean-recursive header-recursive distclean-recursive \
doxy-recursive doc-recursive defgroup-recursive \
verify-recursive longline-recursive tabs-recursive :
	-@cd $(WORLDDIR); \
	$(MAKE) $(subst -recursive,,$@) RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE)
	-@cd $(PLUGINDIR); \
	for plugin in $(PLUGINS); do \
		$(MAKE) -C $$plugin $(subst -recursive,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done
	-@cd $(APLUGINDIR); \
	for aplugin in $(APLUGINS); do \
		$(MAKE) -C $$aplugin $(subst -recursive,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done
ifdef DPLUGINS
	-@cd $(DPLUGINDIR); \
	for dplugin in $(DPLUGINS); do \
		$(MAKE) -C $$dplugin $(subst -recursive,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done
endif
	-@cd $(TOOLDIR); \
	for tool in $(TOOLS); do \
		$(MAKE) -C $$tool $(subst -recursive,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done

rw3: message header dirsetup $(BAEQUDEP) $(LIBS) $(PLUGINDIRS)

# make the directories required by the build process

SDKDIRS =						\
	$(SDKLIBDIR) $(SDKINCDIR)			\
	$(SDKLIBDIR)/archive $(SDKINCDIR)/archive

ifdef DPLUGINS
SDKDIRS +=						\
	$(SDKLIBDIR)/dev $(SDKINCDIR)/dev
endif

$(OBJDIR) $(SDKDIRS) $(DEPDIR):
	$(MD) $@

dirsetup: $(OBJDIR) $(SDKDIRS)

depend: $(DEPDIR)

# AR up the driver bit separately.

# The list of dependancies for the core library is too long so it is split
# by using a make filter

$(PLCORELIB): $(PLCOREOBJ)
	$(RM) $(PLCORELIB)
	$(AR) $(ARCREATEFLAGS)
	$(BINDIR)/patchlib $(PLCORELIB)

$(CORELIB): $(COREOBJ)
	$(RM) $(CORELIB)
	$(AR) $(ARCREATEFLAGS)
	$(BINDIR)/patchlib $(CORELIB)

COREHFILES =					\
	$(SDKINCDIR)/rpcriter.h 		\
	$(SDKINCDIR)/rpdbgerr.c			\
	$(SDKINCDIR)/rpdbgerr.h 		\
	$(SDKINCDIR)/rperror.h			\
	$(SDKINCDIR)/errcom.def 		\
	$(SDKINCDIR)/errcore.def

$(COREHFILES): $(SDKINCDIR)/% : $(BASEDIR)/src/plcore/%
	$(CP) $^ $@

ifeq ($(RWDEPEND), 1)
ifdef WITH_DEPEND
HCOREDEPEND = $(HCORE)
endif
endif

$(SDKINCDIR)/rwplcore.h: $(HCOREDEPEND)
	$(BUILDTOOLDIR)/incgen/incgen $(INCGENFLAGS) $(PLCORE_INC) $(HPLCORE) \
	-sstdlib \
	-sstdio \
	-sstddef \
	-sstdarg \
	-o$@ -lrwplcore.lst

$(SDKINCDIR)/rwcore.h: $(HCOREDEPEND)
	$(BUILDTOOLDIR)/incgen/incgen $(INCGENFLAGS) $(CORE_INC) $(HCORE) \
	-srwplcore \
	-o$@ -lrwcore.lst

rwcore_h:: $(SDKINCDIR)/rwcore.h

header: dirsetup $(COREHFILES) $(SDKINCDIR)/rwplcore.h $(SDKINCDIR)/rwcore.h header-recursive

$(SDKDOCDIR) $(SDKHLPDIR) $(SDKHTMDIR) \
$(SDKDOCDIR)/platform $(SDKHLPDIR)/platform $(SDKHTMDIR)/platform \
$(SDKHLPDIR)/$(RWTARGET) $(SDKHLPDIR)/rwcore $(SDKHLPDIR)/rwplcore \
$(SDKHLPDIR)/ppipe:
	$(MD) $@

PLCOREDOXY=							\
	$(BASEDIR)/src/plcore/rpcriter.h			\
	$(BASEDIR)/include/$(RWTARGET)/rwplcore.h		\
	$(DOCSRCPLCORE)						\

COREDOXY=							\
	$(BASEDIR)/src/plcore/rpcriter.h			\
	$(BASEDIR)/include/$(RWTARGET)/rwplcore.h		\
	$(BASEDIR)/include/$(RWTARGET)/rwcore.h			\
	$(DOCSRCPLCORE)						\
	$(DOCSRCCORE)						\
	$(DOCSRCDRV)				

rwplcoredoxy: $(SDKHLPDIR)/rwplcore
	echo  $(PLCOREDOXY) |			\
	$(BINDIR)/doxycfg			\
	  -DRWPLCORE				\
	  -b$(BASEDIR)				\
	  -d$(SDKHLPDIR)/rwplcore/              \
	  -o$(RWOS)				\
	  -s$(BASEDIR)/misc			\
	  -i$(BASEDIR)/misc			\
	  -t$(RWTARGET)                         \
          > $(SDKHLPDIR)/rwplcore/Doxyfile
	$(BINDIR)/doxygen $(SDKHLPDIR)/rwplcore/Doxyfile
	-$(CP) $(BASEDIR)/misc/rwlogo.jpg $(SDKHLPDIR)/rwplcore/html/rwlogo.jpg
	$(BINDIR)/UNIX2DOS $(SDKHLPDIR)/rwplcore/html/index.hhc
	-hhc autodocs/hlp/rwplcore/html/index.chm

rwcoredoxy: $(SDKHLPDIR)/rwcore
	echo  $(COREDOXY) |			\
	$(BINDIR)/doxycfg			\
	  -b$(BASEDIR)				\
	  -d$(SDKHLPDIR)/rwcore/                \
	  -o$(RWOS)				\
	  -s$(BASEDIR)/misc			\
	  -i$(BASEDIR)/misc			\
	  -t$(RWTARGET)                         \
          > $(SDKHLPDIR)/rwcore/Doxyfile
	$(BINDIR)/doxygen $(SDKHLPDIR)/rwcore/Doxyfile
	-$(CP) $(BASEDIR)/misc/rwlogo.jpg $(SDKHLPDIR)/rwcore/html/rwlogo.jpg
	$(BINDIR)/UNIX2DOS $(SDKHLPDIR)/rwcore/html/index.hhc
	-hhc autodocs/hlp/rwcore/html/index.chm

doxy: header $(SDKHLPDIR)/rwplcore $(SDKHLPDIR)/rwcore rwcoredoxy rwplcoredoxy doxy-recursive

docfile: $(COREDOXY)
	echo $(COREDOXY)  \
	> $(SDKHLPDIR)/$(RWTARGET)/Docfile

rwdocbuild:
	$(BINDIR)/doxycfg			\
	  -b$(BASEDIR)				\
	  -d$(SDKHLPDIR)/$(RWTARGET)/		\
	  -o$(RWOS)				\
	  -s$(BASEDIR)/misc			\
	  -i$(BASEDIR)/misc			\
	  -t$(RWTARGET)				\
	<  $(SDKHLPDIR)/$(RWTARGET)/Docfile	\
	>  $(SDKHLPDIR)/$(RWTARGET)/Doxygen
	$(BINDIR)/doxygen $(SDKHLPDIR)/$(RWTARGET)/Doxygen
	-$(CP) $(BASEDIR)/misc/rwlogo.jpg $(SDKHLPDIR)/$(RWTARGET)/html/rwlogo.jpg
	$(BINDIR)/UNIX2DOS $(SDKHLPDIR)/$(RWTARGET)/html/index.hhc
	-hhc autodocs/hlp/$(RWTARGET)/html/index.chm

doc: header $(SDKHLPDIR)/$(RWTARGET)  docfile doc-recursive  rwdocbuild

defgroup: defgroup-recursive
	-$(BINDIR)/egrep --line-number --with-filename \
	"\\\defgroup" $(COREDOXY)

offsets_message:
	@echo building [$(BAEQUDEP)] ...

offsets_clean:
	$(RM) $(DEVDIR)/$(RWTARGET)/baequate.i $(OBJDIR)/baequ.s

offsets: offsets_message offsets_clean $(BAEQUDEP)

# This compiles to assembly then parses for the offsets (using target compiler!!! :-)
$(DEVDIR)/$(RWTARGET)/baequate.i: $(OBJDIR)/baequ.s
	$(BUILDTOOLDIR)/findsyms/findsyms -Fi$(OBJDIR)/baequ.s -Fo$(DEVDIR)/$(RWTARGET)/baequate.i -A$(AS)

$(OBJDIR)/baequ.s: $(DEVDIR)/common/baequ.c
	cd $(OBJDIR); $(CC) $(CFLAGS) $(CODEGENCFLAGS) $(SOUT) $^

# devclean implemented in makedir
clean: checkopt clean-recursive
	$(RM) -r $(OBJDIR)
	$(RM) $(DEVDIR)/$(RWTARGET)/baequate.i
	$(RM) *.obj
	$(RM) *.err
	$(RM) *.cmd
	$(RM) *.pdb
	$(RM) *.lst
	$(RM) *.ilk

distclean: distclean-recursive clean
	$(RM) -r $(BASEDIR)/lib
	$(RM) -r $(BASEDIR)/include
	$(RM) -r $(BASEDIR)/obj
	$(RM) -r $(BASEDIR)/autodocs

verify-core:
	rwcheck $(RWCHECKFLAGS) \
	$(filter-out %.$(L), $(filter-out $(DRVSRC) $(OSSRC), $(CORESRC)) $(DOCSRCDRV))

verify: verify-core verify-recursive

longline: longline-recursive
	-$(BINDIR)/egrep --line-number --with-filename "................................................................................" $(CORESRC)

tabs: tabs-recursive
	-$(BINDIR)/egrep --line-number --with-filename "	" $(CORESRC)

maintainers-ext:
	-@cd $(PLUGINDIR); \
	for plugin in $(PLUGINS); do \
		$(MAKE) -C $$plugin $(subst -ext,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done
	-@cd $(TOOLDIR); \
	for tool in $(TOOLS); do \
		$(MAKE) -C $$tool $(subst -ext,,$@) \
		RWOPTIONS=$(RWOPTIONS) PIPETYPE=$(PIPETYPE); \
	done

maintainers: maintainers-ext

HDEPEND = $(patsubst %.h,$(OBJDIR)/%.hd, $(notdir $(HCORE)))

ifeq ($(RWDEPEND), 1)
ifdef WITH_DEPEND
-include $(COREOBJ:.$(O)=.d)
-include $(HDEPEND)
endif
endif
