#                             Plugins/Makefile.plugins
#
# This file is the backend that does all of the compiling for the plugins.
# The default behavior of these files is to compile all of the .cpp files
# into .o files, and then link all of the .cpp files together into the
# resultant .so file, which is later copied to the Install directory.
# In addition, there may be a number of "resource" files that are to be
# copied with the plugin.  These may be graphics or .class files used by
# the output of the plugin.
#
# There are three ways for a Plugin to organize it's build structure:
#   1. If only the defaults are needed, just create a makefile that
#      includes "../Makefile.plugins".
#   2. Define a new "Makefile", possibly overriding the "PluginSubDirs"
#      and "ResourceFiles" variables, and then include ../Makefile.plugins 
#      into the Makefile.  See the FSGZipped and PPPathBreakout plugins as
#      examples of this.
#   3. Define a completely custom makefile and not use this one.  This
#      is not at all recommended.
#
# The following functionality may be overridden by defining variables:
#   1. $(PluginSubDirs) may be defined to hold subdirectories that the
#      plugin needs to have built.  The format of the variable is a list
#      of directories with the target specified after the directory.  An
#      example would be "zlib/libz.a".  In this case, the "libz.a" target
#      is built in the "zlib" subdirectory.  When it is done building,
#      that file is linked into the .so file.
#
#   2. $(PLink) is appended to the link command, so extra libraries that
#      are needed can be put here and stuff.
#
#   3. $(ResourceFiles) may contain a list of files to be copied to the
#      destination directory.  If these files do not exist, "make" will
#      stop.
#

LEVEL = ../..

# Include the "Common" files...
include $(LEVEL)/Makefile.common

# Plugin can override "PluginName" if they wanted to for some reason...
ifndef PluginName
PluginName    = $(notdir $(shell pwd)).so
PluginNameG   = $(notdir $(shell pwd))_g.so
endif


# Find out if we have documentation for the plugin...
#
DocSource = $(wildcard *.sdf)

# if we have documentation for the plugin...
#
ifneq ($(DocSource),)

# Create TXT and HTML output.
#ResourceFiles += $(addprefix $(basename $(DocSource)), .txt .html)

# If we can find sgml2ps, create postscript output...
#HASSGML2PS = bin,NO,$(shell which tcsh))

#ifneq ($(HASSGML2PS),NO)
#ResourceFiles += $(addprefix $(basename $(DocSource)), .ps)
#endif

endif   # End of if we have documentation...


# DestDirX is the directory to copy the resulting .so file and resources
# into...
#
DestDirO = $(LEVEL)/Install/Plugins/$(basename $(PluginName))/
DestDirG = $(LEVEL)/Install/Plugins_g/$(basename $(PluginName))/
DestDirs = $(DestDirO) $(DestDirG)

DestResourcesO = $(addprefix $(DestDirO),$(ResourceFiles))
DestResourcesG = $(addprefix $(DestDirG),$(ResourceFiles))
DestResources  = $(DestResourcesO) $(DestResourcesG)

# Define the rule to copy files over to the destination directory...
#
$(DestDirO)%: % $(DestDirO).dir
	cp $< $@
$(DestDirG)%: % $(DestDirG).dir
	cp $< $@

all:: $(DestDirG)$(PluginNameG) $(DestDirO)$(PluginName) $(DestResources)

$(DestDirO)$(PluginName): $(ObjectsO) $(PluginSubDirs) $(DestDirO).dir
	@echo ======= Building $(PluginName) Optimized Plugin. =======
	$(MakeSOO) -I../.. -o $@ $(ObjectsO) $(PluginSubDirs) $(PLink)

$(DestDirG)$(PluginNameG): $(ObjectsG) $(PluginSubDirs) $(DestDirG).dir
	@echo ======= Building $(PluginName) Debug Plugin. =======
	$(MakeSOG) -I../.. -o $@ $(ObjectsG) $(PluginSubDirs) $(PLink)

# Only include these rules if the plugin overrode the $(PluginSubDirs) var
ifneq ($(PluginSubDirs),)
$(PluginSubDirs):  # Build the subdirectory
	@echo ======= Cleaning $(PluginName:%.so=%)::$@ directory =======
	cd $(dir $@); $(MAKE) $(notdir $@)

# Clean all of the subdirectories...
MPCleanCommand = cd $(dir $P); echo;echo ======= Cleaning $(PluginName:%.so=%) ::
MPCleanCommand +=$(dir $(P)) directory. =======; $(MAKE) clean; cd ..;
clean::
	@$(foreach P,$(PluginSubDirs), $(MPCleanCommand) )
endif

