#################################################################
# Generic makefile for Unix and company                         #
#################################################################
#

#################################################################
# Programs used
######

# The most important stuff.

# Gnu CC.
CC         =  gcc

# Vendor-supplied compilers are often named 'cc'.
#CC        =  cc

# Gnu C++.
CXX        =  g++
LD         =  ld
MAKEDEPEND =  makedepend

# Various utilities.
AR         =  ar
GZIP       =  gzip
MAKE       =  gmake
RANLIB     =  ranlib
RM         =  rm
TAR        =  tar
TOUCH      =  touch

#################################################################
# Flags
######
DBGFLAGS = -g 
OPTFLAGS = -O3

INCFLAGS = -I.
LDFLAGS  = -L.

# GCC.
SPECIAL  = -ansi -pedantic -Wall

# GCC
#SPECIAL = -ansi -pedantic -Wall -D

# e.g. Sun's supplied compiler for Solaris.
# SPECIAL  = -v -Xc -xtransition

COMMONFLAGS = $(INCFLAGS)    $(LDFLAGS) $(SPECIAL)
CFLAGS      = $(COMMONFLAGS) $(OPTFLAGS)
CXXFLAGS    = $(COMMONFLAGS) $(OPTFLAGS)


GZIPFLAGS   = -9

#################################################################
# Default rules
#####

# Static libraries.
%.a: 
	$(AR) r $@ $?
	$(RANLIB) $@

# Shared libraries.
%.so:
	$(LD) -shared -o $@ $(LDFLAGS) $^

# C objects.
%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

# The next few cover common C++ extensions.
%.o: %.cc
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o: %.cxx
	$(CXX) $(CXXFLAGS) -c -o $@ $<

%.o: %.C
	$(CXX) $(CXXFLAGS) -c -o $@ $<

#################################################################
# Files
######

DEPEND  = depend.mk
ALLSRC  = list.c array.cc tug.cc wrapper.cc main.cc template.cc
ALLHDR  = adt.h list.h array.h  tug.h  wrapper.h
ALLOBJ  = list.o tug.o wrapper.o main.o template.o

#################################################################
# Standard Targets
#####

all:    depend libtug.a fractug

depend:  .depend

archive:
	$(RM) -f archive.tar.gz
	$(TAR) -cvf archive.tar $(ALLSRC) $(ALLHDR) *akefile*
	$(GZIP) $(GZIPFLAGS) archive.tar

clean:
	$(RM) -f $(DEPEND)
	$(RM) -f *.o
	$(RM) -f *.a
	$(RM) -f *.so
	$(RM) -f depend.mk.bak
	$(RM) -f .depend
	$(RM) -f fractug
	$(RM) -f *diag*.inp
	$(RM) -f *.eps
	$(RM) -f koch7.inp
	$(RM) -f Line.10000
	$(RM) -f Sierpinski.5000

.depend: $(ALLSRC) $(ALLHDR)
	$(RM) -f $(DEPEND)
	$(TOUCH) $(DEPEND)
	$(MAKEDEPEND) -f $(DEPEND) -- $(CFLAGS) -- *.c
	$(MAKEDEPEND) -f $(DEPEND) -a -- $(CXXFLAGS) -- \
                         *.cpp *.cc *.cxx
	touch .depend

rebuild:  clean all

#################################################################
# Other targets
#####

libtug.a:  list.o tug.o wrapper.o template.o

fractug:  libtug.a main.o
	$(CXX) $(LDFLAGS) $(CXXFLAGS) -o fractug main.o -lm -ltug

#################################################################
# For automatic dependency generation
#####

# This is why we use .depend; 'gmake clean' will not auto-regen
# depend.mk...
sinclude depend.mk

