
default: opt 

opt: \
    $(EXEC:=.opt) \
	$(LIB:=.opt) \
	$(WRAPPER:=.opt) \
	$(PLUGIN:=.opt) \

debug: \
    $(EXEC:=.dbg) \
	$(LIB:=.dbg) \
	$(WRAPPER:=.dbg) \
	$(PLUGIN:=.dbg) \

static: \
	$(LIB:.$(LIB_EXT)=.static) \
	$(PLUGIN:.$(PLUGIN_EXT)=.static) \

staticdebug: \
	$(LIB:.$(LIB_EXT)=.staticdbg) \
	$(PLUGIN:.$(LIB_EXT)=.staticdbg) \

cleandepend: cleandependopt cleandependdbg

cleandependopt:
	@[ -d $(OPTDIR) ] && cd $(OPTDIR); rm -f Makedepend; rm -rf .depend

cleandependdbg:
	@[ -d $(DEBUGDIR) ] && cd $(DEBUGDIR); rm -f Makedepend; rm -rf .depend

cleantarget: cleantargetopt cleantargetdbg

cleantargetopt: 
	@[ -d $(OPTDIR) ] && cd $(OPTDIR); rm -f $(EXEC)$(EXE_EXT) $(LIB) ${WRAPPER} $(PLUGIN)

cleantargetdbg: 
	@[ -d $(DEBUGDIR) ] && cd $(DEBUGDIR); rm -f $(EXEC)$(EXE_EXT) $(LIB) ${WRAPPER} $(PLUGIN)

clean: cleanopt cleandbg

cleanopt: 
	@[ -d $(OPTDIR) ] && cd $(OPTDIR); rm -f $(OBJS) Makedepend; rm -rf .depend

cleandbg: 
	@[ -d $(DEBUGDIR) ] && cd $(DEBUGDIR); rm -f $(OBJS) Makedepend; rm -rf .depend

clobber: clobberdbg clobberopt

clobberopt: 
	rm -rf $(OPTDIR)

clobberdbg: 
	rm -rf $(DEBUGDIR)


beautify: 
	for f in *.cpp ; do mv $$f $$f.bak; bcpp $$f.bak $$f; rm $$f.bak; done
	for f in *.h ; do mv $$f $$f.bak; bcpp $$f.bak $$f; rm $$f.bak; done

docs:
	[ "$(TARGET_BASENAME)" != "" ] && \
	mkdir -p $(TOPDIR)/doc/doc++/$(TARGET_BASENAME) && \
	doc++ -d $(TOPDIR)/doc/doc++/$(TARGET_BASENAME) -H -A \
	    $(TOPDIR)/include/$(TARGET_BASENAME)/* && \
	echo "$(TARGET_BASENAME) HTML documentation created in " \
	     "$(TOPDIR)/doc/doc++/$(TARGET_BASENAME)"


$(EXEC): $(OBJS) 
	$(C++) $(LDFLAGS) $(OBJS) $(LIBS) -o $@


## Mac OSX makes the distinction between -dynamiclib and -bundle
## causing us to have to make a special case here, rather than 
## just redefining the SHARED argument.

ifeq ($(OS),Darwin)

$(LIB): $(OBJS)
	$(C++) $(LDFLAGS) $(LIBVERSION) -dynamiclib $(OBJS) $(LIBS) -o $@

$(WRAPPER) $(PLUGIN): $(OBJS)
	$(C++) $(LDFLAGS) -bundle $(OBJS) $(LIBS) -o $@

$(PLUGIN): $(OBJS)
	$(C++) $(LDFLAGS) -bundle $(OBJS) $(LIBS) -o $@

else
  ifeq ($(OS),HP-UX)

# we need to explicitly tell the linker the library name - else it will
# include paths like ../../../libosg.sl into executables
$(LIB) $(PLUGIN) ${WRAPPER} : $(OBJS)
	$(C++) $(LDFLAGS) $(SHARED) $(OBJS) $(LIBS) -Wl,+h -Wl,$@ -o $@

  else

$(LIB) $(PLUGIN) ${WRAPPER} : $(OBJS)
	$(C++) $(LDFLAGS) $(SHARED) $(OBJS) $(LIBS) -o $@

$(STATICLIB) $(STATICPLUGIN): $(OBJS)
	ar rv $@ $?

  endif # not HP-UX
endif # not Darwin

$(EXEC:=.dbg) :
	@[ -d $(DEBUGDIR) ] || mkdir $(DEBUGDIR)
	@cd $(DEBUGDIR); \
	  $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)"  TOPDIR=../${TOPDIR} \
	      THISDIR=../$(THISDIR) \
	      MAKEDEPEND=Makedepend $(EXEC)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(EXEC)$(EXE_EXT) LNDEST=$(BININST)/$(EXEC)$(EXE_EXT) __link

$(LIB:=.dbg) : 
	@[ -d $(DEBUGDIR) ] || mkdir $(DEBUGDIR)
	@cd $(DEBUGDIR); \
	  $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)"  TOPDIR=../${TOPDIR} \
	      THISDIR=../$(THISDIR) \
	      MAKEDEPEND=Makedepend $(LIB)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(LIB) LNDEST=$(LIBINST)/$(LIB) __link
ifdef IMP_LIB_EXT
	@$(MAKE) LNSRC=$(DEBUGDIR)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) LNDEST=$(IMP_LIBINST)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) __link
endif

$(LIB:.$(LIB_EXT)=.staticdbg) :
	@[ -d $(DEBUGDIR) ] || mkdir $(DEBUGDIR)
	@cd $(DEBUGDIR); \
      $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)" TOPDIR=../${TOPDIR} \
            THISDIR=../$(THISDIR) \
            MAKEDEPEND=Makedepend $(STATICLIB)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(STATICLIB) LNDEST=$(LIBINST)/$(OS)$(ARCH)/$(STATICLIB) __link

$(WRAPPER:=.dbg) : 
	@[ -d $(DEBUGDIR) ] || mkdir $(DEBUGDIR)
	@cd $(DEBUGDIR); \
	  $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)"  TOPDIR=../${TOPDIR} \
	      THISDIR=../$(THISDIR) \
	      MAKEDEPEND=Makedepend $(WRAPPER)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(WRAPPER) LNDEST=$(PLUGININST)/$(WRAPPER) __link

$(PLUGIN:=.dbg) : 
	@[ -d $(DEBUGDIR) ] || mkdir $(DEBUGDIR)
	@cd $(DEBUGDIR); \
	  $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)"  TOPDIR=../${TOPDIR} \
	      THISDIR=../$(THISDIR) \
	      MAKEDEPEND=Makedepend $(PLUGIN)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(PLUGIN) LNDEST=$(PLUGININST)/$(PLUGIN) __link
ifdef IMP_LIB_EXT
	@$(MAKE) LNSRC=$(DEBUGDIR)/lib$(TARGET_BASENAME)$(IMP_LIB_EXT) LNDEST=$(IMP_PLUGININST)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) __link
endif

$(PLUGIN:.$(PLUGIN_EXT)=.staticdbg) : 
	@[ -d $(DEBUGDIR) ] || mkdir -p $(DEBUGDIR)
	@cd $(DEBUGDIR); \
	  $(MAKE) -f ../GNUmakefile "DOF=$(DBGF)" TOPDIR=../${TOPDIR} \
	    THISDIR=../$(THISDIR) \
	               MAKEDEPEND=Makedepend STATICPLUGIN=$(PLUGIN:.$(PLUGIN_EXT)=.a) $(STATICPLUGIN)
	@$(MAKE) LNSRC=$(DEBUGDIR)/$(STATICPLUGIN) LNDEST=$(PLUGININST)/$(STATICPLUGIN) __link

$(EXEC:=.opt) : 
	@[ -d $(OPTDIR) ] || mkdir $(OPTDIR)
	@cd $(OPTDIR); \
	  $(MAKE) -f ../GNUmakefile DOF=$(OPTF)  TOPDIR=../${TOPDIR} \
	      THISDIR=../$(THISDIR) \
	      MAKEDEPEND=Makedepend $(EXEC)
	@$(MAKE) LNSRC=$(OPTDIR)/$(EXEC)$(EXE_EXT) LNDEST=$(BININST)/$(EXEC)$(EXE_EXT) __link


$(LIB:=.opt) : 
	@[ -d $(OPTDIR) ] || mkdir -p $(OPTDIR)
	@cd $(OPTDIR); \
	  $(MAKE) -f ../GNUmakefile DOF=$(OPTF) TOPDIR=../${TOPDIR} \
	        THISDIR=../$(THISDIR) \
	        MAKEDEPEND=Makedepend $(LIB)
	@$(MAKE) LNSRC=$(OPTDIR)/$(LIB) LNDEST=$(LIBINST)/$(LIB) __link
ifdef IMP_LIB_EXT
	@$(MAKE) LNSRC=$(OPTDIR)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) LNDEST=$(IMP_LIBINST)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) __link
endif

$(LIB:.$(LIB_EXT)=.static) :
	[ -d $(OPTDIR) ] || mkdir $(OPTDIR)
	cd $(OPTDIR); \
      $(MAKE) -f ../GNUmakefile DOF=$(OPTF) TOPDIR=../${TOPDIR} \
            THISDIR=../$(THISDIR) \
            MAKEDEPEND=Makedepend $(STATICLIB)
	@$(MAKE) LNSRC=$(OPTDIR)/$(STATICLIB) LNDEST=$(LIBINST)/$(OS)$(ARCH)/$(STATICLIB) __link

$(WRAPPER:=.opt) : 
	@[ -d $(OPTDIR) ] || mkdir -p $(OPTDIR)
	@cd $(OPTDIR); \
	  $(MAKE) -f ../GNUmakefile DOF=$(OPTF) TOPDIR=../${TOPDIR} \
	    THISDIR=../$(THISDIR) \
	               MAKEDEPEND=Makedepend $(WRAPPER)
	@$(MAKE) LNSRC=$(OPTDIR)/$(WRAPPER) LNDEST=$(PLUGININST)/$(WRAPPER) __link

$(PLUGIN:=.opt) : 
	@[ -d $(OPTDIR) ] || mkdir -p $(OPTDIR)
	@cd $(OPTDIR); \
	  $(MAKE) -f ../GNUmakefile DOF=$(OPTF) TOPDIR=../${TOPDIR} \
	    THISDIR=../$(THISDIR) \
	               MAKEDEPEND=Makedepend $(PLUGIN)
	@$(MAKE) LNSRC=$(OPTDIR)/$(PLUGIN) LNDEST=$(PLUGININST)/$(PLUGIN) __link
ifdef IMP_LIB_EXT
	@$(MAKE) LNSRC=$(OPTDIR)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) LNDEST=$(IMP_PLUGININST)/lib$(TARGET_BASENAME).$(IMP_LIB_EXT) __link
endif

$(PLUGIN:.$(PLUGIN_EXT)=.static) : 
	@[ -d $(OPTDIR) ] || mkdir -p $(OPTDIR)
	@cd $(OPTDIR); \
	  $(MAKE) -f ../GNUmakefile DOF=$(OPTF) TOPDIR=../${TOPDIR} \
	    THISDIR=../$(THISDIR) \
	               MAKEDEPEND=Makedepend STATICPLUGIN=$(PLUGIN:.$(PLUGIN_EXT)=.a) $(STATICPLUGIN)
	@$(MAKE) LNSRC=$(OPTDIR)/$(STATICPLUGIN) LNDEST=$(PLUGININST)/$(STATICPLUGIN) __link



## Revamped method for making Makedepend files
## Under this method, Makedepend simply includes
## all dependency files which are built for each 
## individual source file.  At update, only the individual
## dependency file is updated rather than the whole of Makedepend

Makedepend : $(CXXFILES) $(CFILES)
ifeq ($(OS),IRIX)
	touch $@
else
	@ touch $@
	@if [ ! -d .depend ] ; then  \
		mkdir .depend; \
		depfiles="$+"; \
	else \
		depfiles="$?"; \
	fi; \
	if [ ! -z "$$depfiles" ]; then \
	for f in $$depfiles; do \
		echo "$(C++) $(DEPARG) $(INC) $$f  pipeto .depend/`basename $$f`"; \
		$(C++) $(DEPARG) $(INC) $$f > .depend/`basename $$f`; \
		grep $$f $@ || echo include .depend/`basename $$f` >> Makedepend ; \
	done; \
	fi
endif


.SUFFIXES: .cpp .o
.cpp.o:
	$(C++) $(CXXFLAGS) -c $<

depend : $(MAKEDEPEND)

__link : 
	@sh $(TOPDIR)/Make/dolink.sh $(TOPDIR) $(LNSRC) $(LNDEST) "$(LINK)"
		
__instfiles :
	@echo $(INSTFILES)

sinclude $(MAKEDEPEND)

