# Common rules for tools

# Define the root for the RenderWare build.

BASEDIR         = ../..

# This target must be defined before makeopt is included. It contains a single
# forward reference to the main target

all: rwplugin $(OPTTARGET)
 
# Include the shared RenderWare make definitions

include $(BASEDIR)/makeopt

# derive the library, header and error filename from the plugin name
RPLIB           = $(SDKLIBDIR)/$(LIBPREFIX)$(PLUGIN)$(OPTEXT).$(LIBEXT)
RPHEAD          = $(SDKINCDIR)/$(PLUGIN).h
RPERR           = $(SDKINCDIR)/$(PLUGIN).rpe
RPDOCDIR        = $(SDKDOCDIR)/tool
RPHLPDIR        = $(SDKHLPDIR)/tool
RPHTMDIR        = $(SDKHTMDIR)/tool

# and what object files are required. Note, if the RenderWare error & 
# debug mechnism is to be used then rpdbgerr.$(O) must be included
# because of the needs for some compilers for the module names to be unique
# we generate a unique name for this file based on the name of the plugin

OBJS           += $(OBJDIR)/$(PLUGIN).$(O) 
TOOLSRC        := $(notdir $(addsuffix .c, $(basename $(subst $(OBJDIR), ., $(OBJS)))))
ifeq ($(RWDEBUG), 1)
OBJS           += $(OBJDIR)/u$(PLUGIN).$(O)
endif
DOCSRC         := $(PLUGIN).h $(PLUGIN).c $(DOCSRC)

# Need to include the archive directory in the include path

CFLAGS += -I$(SDKINCDIR)/archive

# Add any platform specific files defined in the main Makefile
ifneq ("$($(RWTARGET)OBJS)", "")
PSOBJS = $($(RWTARGET)OBJS)
else
PSOBJS = $(genericOBJS)
endif

ifneq ("$($(RWTARGET)DOCSRC)", "")
PSDOCSRC = $($(RWTARGET)DOCSRC)
else
PSDOCSRC = $(genericDOCSRC)
endif

OBJS += $(PSOBJS)
DOCSRC += $(PSDOCSRC)

# The main entry point - checkopt is defined in makeopt and 
# checks that valid values for target, compiler and os have been defined

rwplugin: header checkopt dirsetup $(RPLIB)

header: $(RPHEAD) $(RPERR) $($(RWTARGET)HDRS)

# The plugin library defined above
$(RPLIB): $(OBJS)
	$(RM) $@
	$(AR) $(ARCREATEFLAGS)
	$(BINDIR)/patchlib $@

# Install the headers
$(RPHEAD): $(notdir $(RPHEAD))
	$(CP) $< $@

# Generate the documentation

$(RPDOCDIR) $(RPHLPDIR) $(RPHTMDIR) $(RPHLPDIR)/$(PLUGIN):
	$(MD) $@

doxygen:
	echo $(DOCSRC) |                                \
	$(BINDIR)/doxycfg				\
	  -b$(BASEDIR)					\
	  -d$(RPHLPDIR)/$(PLUGIN)/			\
	  -o$(RWOS)					\
	  -s$(BASEDIR)/misc				\
	  -t$(RWTARGET)					\
	> $(RPHLPDIR)/$(PLUGIN)/Doxyfile

doc: 
	echo $(addprefix $(CURDIR)/,  $(DOCSRC)) \
	>> $(SDKHLPDIR)/$(RWTARGET)/Docfile

defgroup:
	-$(BINDIR)/egrep --line-number --with-filename \
	"\\\defgroup" $(addprefix $(CURDIR)/, $(DOCSRC))

plugindoc:
	doxygen $(RPHLPDIR)/$(PLUGIN)/Doxyfile
	-$(CP) $(BASEDIR)/misc/rwlogo.jpg $(RPHLPDIR)/$(PLUGIN)/html/rwlogo.jpg
	$(BINDIR)/UNIX2DOS $(RPHLPDIR)/$(PLUGIN)/html/index.hhc
	-$(BINDIR)/hhc $(RPHLPDIR)/$(PLUGIN)/html/index.hhp

doxy: $(RPHLPDIR)/$(PLUGIN) $(RPHEAD) doxygen plugindoc

# Create the directory for the object files
dirsetup: $(OBJDIR)

$(OBJDIR):
	$(MD) $@

verify:
	rwcheck $(RWCHECKFLAGS) $(TOOLSRC)

longline:
	-egrep --line-number --with-filename "................................................................................" $(TOOLSRC)

tabs:
	-egrep --line-number --with-filename "	" $(TOOLSRC)

maintainers:
	@echo -n $(PLUGIN) 'maintainers are:'
	@for maintainer in $(MAINTAINERS); do \
		echo -n ' '$$maintainer ; \
	done
	@echo '.'

# delete all intermediate files
clean: checkopt
	$(RM) $(OBJS)
	$(RM) -r $(OBJDIR)
	$(RM) *.ipp
	$(RM) *.err
	$(RM) *.cmd
	$(RM) *.pdb

# delete all files in preparation for a release
distclean: clean
	$(RM) $(RPLIB)
	$(RM) $(RPHEAD)
	$(RM) $(RPERR)
	$(RM) -r obj
	$(RM) -r $(PLUGIN).lib
	$(RM) -r $(PLUGIN).rtf
	$(RM) -r stddata.sed

# dependencies

$(RPHEAD): $(RPERR)

$(PLUGIN).c: $(RPHEAD)

$(RPERR): rperror.def

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

#########################################################################
