# $Id$
#
# Makefile for libcomm
# Copyright (C) 2001 Roland F. Smith
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along 
# with this library; if not, write to the Free Software Foundation, Inc., 
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# You can contact the author at the following addresses:
#     e-mail: rsmith@xs4all.nl
# snail-mail: R.F. Smith
#             Dr. Hermansweg 36
#             5624 HR  Eindhoven
#             The Netherlands
#

# Compilation flags for optimized libraries.
#CFLAGS = -O2 -Wall -Wstrict-prototypes -m486 -fomit-frame-pointer -fPIC

# The next line is for building debugging libraries.
CFLAGS = -g -Wall -Wstrict-prototypes -fPIC

# This line is for building Athlon optimized libraries with gcc 2.95.2
#CFLAGS = -s -O3 -fomit-frame-pointer -Wall -mpentiumpro -march=pentiumpro -malign-functions=4 -funroll-loops -fexpensive-optimizations -malign-double -fschedule-insns2 -mwide-multiply -fPIC

# Extra libraries to be linked.
LIBS = 

# Where to search for additional header files.
#HDIRS = -I/usr/X11R6/include

# Where to install the library and header file.
# THESE SHOULD BE CHECKED AND CHANGED IF NECESSARY!
LDEST = /usr/local/lib
HDEST = /usr/local/include
MDEST = /usr/man/man3

# 'lib' wil be prepended automagically to the basename.
# for libfoo.so.0.1.2 the basename is foo, version_major is 0,
# version_minor is 1 and patchlevel is 2.
BASENAME      = comm
VERSION_MAJOR = 1
VERSION_MINOR = 0
PATCHLEVEL    = 0

# Name of the library header file.
HDRS = comm.h

# list of source files
SRC = comm.c

# Name of the manual pages.
MPAGES = comm_read_text.3  comm_write_text.3

# Regular files to include in the distribution
DISTFILES = Makefile README COPYING $(MPAGES)

# Name and source of the test program
TEST = com_test
TESTSRC = $(TEST).c

# Extra stuff to add into the distribution.
XTRA_DIST = 

##### No editing necessary beyond this point ####
.SUFFIXES:
.SUFFIXES: .c .o

.PHONY:	clean help all shared static install_shared install_static \
install uninstall dist backup is_root check_dir log

NUM2      = $(VERSION_MAJOR).$(VERSION_MINOR)
NUM3      = $(VERSION_MAJOR).$(VERSION_MINOR).$(PATCHLEVEL)
SHARED	 = lib$(BASENAME).so.$(NUM3)
STATIC	 = lib$(BASENAME).a
PKGDIR    = lib$(BASENAME)-$(NUM3)
TARFILE	 = lib$(BASENAME)-$(NUM3).tar.gz
BACKUP	 = lib$(BASENAME)-backup-$(NUM3).tar.gz
OBJS		 = $(SRC:.c=.o)
LOG 		 = ChangeLog

INSTALL   = install

# This is the default target.
help:;	@echo "Use one of the following invocations:";\
		echo "  \"make shared\"         to build the shared library";\
		echo "  \"make static\"         to build a static library";\
		echo "  \"make all\"            to build both libraries";\
		echo "  \"make test\"           to build test programs";\
		echo "  \"make depend\"         to update the dependancies";\
		echo "  \"make clean\"          to remove all generated files";\
		echo "  \"make ps\"             to build man-pages in PostScript";\
		echo "  \"make txt\"            to build text man-pages";\
		echo "  \"make html\"           to build man-pages in html";\
		echo "NOTE: you must be root to use the following targets:";\
		echo "  \"make install_shared\" to install the shared library";\
		echo "  \"make install_static\" to install the static library";\
		echo "  \"make install_man\"    to install the manual-page";\
		echo "  \"make install\"        to install libraries and man-pages";\
		echo "  \"make uninstall\"      to remove the installed libraries";\
		echo "";\
		echo "The usual invocation would be to do \"make all\",";\
		echo "\"make test\", login as root, and then do a \"make install\".";\
		echo ""

shared:		depend $(SHARED)
static:		depend $(STATIC)
all:		depend $(SHARED) $(STATIC)
test: $(TEST)

# if the file depend doesn't exist, run 'make depend' first.
depend:	$(SRC) $(TESTSRC)
	gcc -MM $(SRC) >depend

# Build the shared library.
$(SHARED):	$(OBJS)
	gcc -shared -Wl,-soname,lib$(BASENAME).so.$(VERSION_MAJOR) \
	-o $(SHARED) $(OBJS) $(LIBS)
#	strip -g --strip-unneeded $(SHARED)

# Builds a static- (archive) library instead of a shared library.
$(STATIC): $(OBJS)
	ar crus $(STATIC) $(OBJS)
#	strip -g $(STATIC)

$(TEST): $(TESTSRC) $(STATIC)
	gcc -o $(TEST) $(TESTSRC) $(STATIC)


# Remove all files that can be created by this makefile.
clean:;
	-rm -f $(OBJS) core *~ $(SHARED) $(STATIC) $(TARFILE) \
	$(LOG) $(TEST) $(MPAGES:.3=.ps) \
	$(MPAGES:.3=.txt) $(MPAGES:.3=.html)

$(LOG):;
	rm -f $(LOG)
	rcs2log -i 2 -l 70 >$(LOG)

# Check if the user has root privileges.
is_root:;
	@if [ `id -u` != 0 ]; then \
		echo "You must be root to install the library!"; \
		exit 1; \
	fi

# Check if the install directory isn't the current directory.
check_dir:;
	@if [ $(LDEST) = . -o $(HDEST) = . ]; then \
		echo "Can't install in current directory!";\
		exit 1; \
	fi

# Install the header files
install_hdr: $(HDRS)
	list='$(HDRS)'; \
	for i in $$list; do \
		rm -f $(MDEST)/$$i; \
		$(INSTALL) -m 644 $$i $(MDEST); \
	done

install_man: $(MPAGES)
	list='$(MPAGES)'; \
	for i in $$list; do \
		rm -f $(MDEST)/$$i; \
		$(INSTALL) -m 644 $$i $(MDEST); \
	done

# Install the shared library. Must be run as root.
install_shared: $(SHARED) is_root check_dir install_hdr install_man
   # Remove the old library.
	rm -f $(LDEST)/$(SHARED)
	rm -f $(LDEST)/lib$(BASENAME).so.$(VERSION_MAJOR)
	rm -f $(LDEST)/lib$(BASENAME).so
   # Install new library and make the usual links.
	$(INSTALL) -m 755 $(SHARED) $(LDEST) ; cd $(LDEST) ;\
	ln -s $(SHARED) lib$(BASENAME).so.$(VERSION_MAJOR) ;\
	ln -s $(SHARED) lib$(BASENAME).so 

# Install the static library. Must be run as root.
install_static: $(STATIC) is_root check_dir install_hdr install_man
   # Remove the old library.
	rm -f $(LDEST)/$(STATIC)
   # Install new library.
	$(INSTALL) -m 644 $(STATIC) $(LDEST)

# Installs both libraries
install: install_shared install_static

# Remove the installed libraries, header and man-page. Must be run as root.
uninstall: is_root
	rm -f $(LDEST)/$(SHARED)
	rm -f $(LDEST)/$(STATIC)
	rm -f $(LDEST)/lib$(BASENAME).so.$(VERSION_MAJOR)
	rm -f $(LDEST)/lib$(BASENAME).so
	list='$(HDRS)'; \
	for i in $$list; do \
		rm -f $(HDEST)/$$i; \
	done
	list='$(MPAGES)'; \
	for i in $$list; do \
		rm -f $(MDEST)/$$i; \
	done

# Make a source distribution file.
dist: $(LOG)
	rm -rf $(PKGDIR)
	mkdir -p $(PKGDIR)
	cp $(DISTFILES) $(XTRA_DIST) *.c *.h $(PKGDIR)
	tar -czf $(TARFILE) $(PKGDIR)
	rm -rf $(PKGDIR)

# Make a complete backup of the source directory.
backup: clean $(LOG)
	tar -czf $(BACKUP) *

# postscript manual pages
ps: $(MPAGES:.3=.ps)

# text manual pages
txt: $(MPAGES:.3=.txt)

# html manual pages
html: $(MPAGES:.3=.html)

# Implicit rule (is needed because of HDIRS!)
.c.o:
	gcc $(CFLAGS) $(CPPFLAGS) $(HDIRS) -c -o $@ $<

%.ps: %.3
	groff -Tps -man $< >$@

%.txt: %.3
	groff -Tlatin1 -man $< >$@

%.html: %.3
	groff -Thtml -man $< >$@

# DO NOT DELETE THIS LINE 
include depend

