#========================================================================
# This is a Makefile for the libdieharder library, part of the
# overall dieharder package.  It has a very abbreviated set of targets.
#
# make          alone should build the application.
#
# make clean    deletes the application and all object files
# make install  strips and installs application and a man page
# make printout prints out all source and include files
# 
# These two commands execute in the toplevel project directory only.
# make svn      does a svn commit and creates the timestamp $(SVN)
# make sync     does a svn commit and rsyncs to list of hosts
#========================================================================
PROJECT = wulfware
DIR = libwulf

#========================================================================
# This is essential.  The rpmbuild overrides it, but we have to make
# build "work" when executed only in the source tree directory itself.
# This isn't easy, since the dependences are more than a bit scattered.
# We therefore point to the directory one level up, where we should
# find a ./lib, ./include, ./share and ./bin directory tree from which
# the various dieharder files will actually be assembled into an rpm
# with PREFIX=/usr (for example).
#========================================================================
BUILDROOT=../buildroot
PREFIX=$(BUILDROOT)/usr
LIBDIR=$(PREFIX)/lib
INCDIR=$(PREFIX)/include
INCTIME=include.time

SVNTREE = $(HOME)/Src/svn-tree
SVNPATH = $(SVNTREE)/$(PROJECT)
SVNTIME = $(DIR:=.svn.time)


# This is the library from which both wulfstat and wulflogger (and maybe
# other stuff in the future) is built.
PROGMAN = libwulf.3
PROGLIB_A = libwulf.a
PROGLIB_SO = libwulf.so
PROGLIB_SO_VERSION = libwulf.so.$(VERSION_MAJOR).$(VERSION_MINOR)

# This is automagically set in the toplevel build.  Do not edit by
# hand.
VERSION_MAJOR=2
VERSION_MINOR=5.0
RELEASE=1

#========================================================================
# Define all sources.  Note that we choose to depend on ALL the includes
# in the include directory, which cannot be empty or this will barf.
#========================================================================
LIBSOURCES = $(shell ls *.c  2>&1 | sed -e "/\/bin\/ls:/d")
LIBOBJECTS = $(LIBSOURCES:.c=.o)

DEFINES = -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) \
          -DRELEASE=$(RELEASE)

#========================================================================
# Define parameters and directives needed in compile/link steps.
#========================================================================
# C Compiler
CC = gcc

# Compile flags.  -fpic creates "position independent code" for
# shared libraries!
CFLAGS = -O3 -fpic -std=c99 -I/usr/include/libxml2 $(DEFINES) -I $(INCDIR)

# Linker flags
LDFLAGS = --shared -Wl,-soname,$(PROGLIB_SO)

# Libraries
LIBS = -lpthread -lxml2 -lm 

#========================================================================
# List of variants one can make.  all is the default.  We always
# presume the simplest of dependencies and remake if includes change
# for example.  We MUST install the include files FIRST in a build.
#========================================================================
all: $(INCTIME) $(PROGLIB_SO)

# This has to ALWAYS run before a build.
$(INCTIME):
	(install -d $(INCDIR)/wulfware; \
	install -m 644 ../include/wulfware/*.h $(INCDIR)/wulfware;)
	
$(PROGLIB_A): $(INCTIME) $(LIBOBJECTS) $(LIBINCLUDES)
	ar r $(PROGLIB_A) $(LIBOBJECTS)
	ranlib $(PROGLIB_A)

$(PROGLIB_SO): $(INCTIME) $(LIBOBJECTS) $(LIBINCLUDES)
	gcc $(LDFLAGS) -o $(PROGLIB_SO) $(LIBOBJECTS)

#========================================================================
# The only safe place to do commits is in the toplevel directory
#========================================================================
svn:
	(cd ..;make svn)

sync:
	(cd ..;make sync)

#========================================================================
# printout makes an enscript -2r printout of SOURCES and
# and INCFILES.  Use lpr if you don't have enscript
#========================================================================
# LPR = enscript -2r
LPR = lpr
printout:
	$(LPR) $(LIBSOURCES) $(LIBINCLUDES)

#========================================================================
#  A standard cleanup target
#========================================================================
clean:
	rm -f core $(PROGLIB) $(PROGLIB_NAME) $(LIBOBJECTS) $(PROGMAN).gz

#========================================================================
# This is critical.  For the toplevel rpm build to succeed,
#  make PREFIX=/usr install
# (run by rpmbuild from the specfile) has to work, in the right order.
# This target has to install precisely the files required by the
# specfile for the dieharder package, in precisely the right locations.
#========================================================================
install: $(PROGLIB_SO) $(PROGMAN)
	(install -d $(LIBDIR); \
	install -m 755 $(PROGLIB_SO) $(LIBDIR); \
	install -m 755 $(PROGLIB_SO) $(LIBDIR)/$(PROGLIB_SO_VERSION); \
	install -d $(INCDIR)/wulfware; \
	install -m 644 ../include/wulfware/*.h $(INCDIR)/wulfware; \
	install -d $(PREFIX)/share/man/man3; \
	gzip -c $(PROGMAN) > $(PROGMAN).gz; \
	install -m 644 $(DIR).3.gz $(PREFIX)/share/man/man3 )

#	install -m 755 $(PROGLIB_A) $(LIBDIR); \
#========================================================================
# We give all generic rules below.  Currently we only need a rule for 
# objects.
#========================================================================
%.o:%.c $(LIBINCLUDES) Makefile
	$(CC) -c $(CFLAGS) $<

