# (c) Copyright 1993 by Panagiotis Tsirigotis
# All rights reserved.  The file named COPYRIGHT specifies the terms 
# and conditions for redistribution.

#
# $Id: Makefile,v 3.8 93/11/19 20:04:23 panos Exp $
#
# Based on Library makefile template: *Revision: 1.15 *
#

# 
# Available entries:
# 		lib 			--> creates the library
#		install		--> installs the library (archive, man page(s), header(s))
#		uninstall	--> uninstall the library
#		clean			--> removes all .o and .a files
#		spotless		--> clean + uninstall
# 		lint			--> lints a file (usage: make lint MODULE=foo.c)
#		tags			--> creates a tags file (from the SOURCES and HEADERS)
#		checkout 	--> checkout all files
#		dist			--> distribution support
#

NAME				= dict
VERSION			= 1.2.4

#
# Double-Linked-List implementation
#
DLL_INCLUDES	= dll.h
DLL_HEADERS		= $(DLL_INCLUDES) dllimpl.h
DLL_SOURCES		= dll.c
DLL_OBJECTS		= dll.o
DLL_MANFILES	= dll.3

#
# Hash-Table implementation
#
HT_INCLUDES		= ht.h
HT_HEADERS		= $(HT_INCLUDES) htimpl.h
HT_SOURCES		= ht.c
HT_OBJECTS		= ht.o
HT_MANFILES		= ht.3

#
# Binary search tree implementation
#
BST_INCLUDES	= bst.h
BST_HEADERS		= $(BST_INCLUDES) bstimpl.h
BST_SOURCES		= bst.c rbt.c
BST_OBJECTS		= bst.o rbt.o
BST_MANFILES	= bst.3

HEADERS			= dictimpl.h dict.h \
							$(DLL_HEADERS) $(HT_HEADERS) $(BST_HEADERS)
SOURCES			= dict.c \
							$(DLL_SOURCES) $(HT_SOURCES) $(BST_SOURCES)
OBJECTS			= dict.o \
							$(DLL_OBJECTS) $(HT_OBJECTS) $(BST_OBJECTS)

MANFILES			= dict.3 \
							$(DLL_MANFILES) $(HT_MANFILES) \
							$(BST_MANFILES)
INCLUDEFILES	= dict.h \
							$(DLL_INCLUDES) $(HT_INCLUDES) \
							$(BST_INCLUDES)

TESTPROGS		= dlltest htest bsttest bstcomp

# The following variables are used by the 'install' entry and
# should be customized:
#     LIBDIR:     where the library will be placed
#     INCUDEDIR:  where the include files will be placed
#     MANDIR:     where the man pages will be placed
#
LIBDIR			= $(HOME)/.links/libraries/$(ARCH)
MANDIR			= $(HOME)/.links/manpages/man3
INCLUDEDIR		= $(HOME)/.links/includes

#
# -DBST_DEBUG	:	enables 2 debugging functions: bst_getdepth and bst_traverse
#						bst_getdepth() returns the maximum and minimum depth of the
#						tree. In the case of a red-black tree, it verifies that
#						the balancing property holds for every subtree. It prints
#						a message for each unbalanced subtree it finds.
#						bst_traverse() does preorder/inorder/postorder traversals
#						of the tree and applies a user-provided function to each
#						node.
#						In order for these functions to be available, the BST_DEBUG
#						flag must be set, both when compiling the library and when
#						when compiling programs that use the library, since the
#						bst.h header file defines data types that are used by
#						these functions.
#
# -DDEBUG_RBT	:	enables code that verifies that left/right rotations do
#						not involve the anchor of the tree. If the assertion fails,
#						the program will be terminated.
#
DEFS				= -DBST_DEBUG -DDEBUG_RBT
DEBUG				= -g			# -g or -O
VERSION_DEF		= -DVERSION=\"DICT\ Version\ $(VERSION)\"

CPP_DEFS			= -I$(INCLUDEDIR) $(VERSION_DEF) $(DEFS)

#
# The following variables shouldn't need to be changed
#
LINT_FLAGS		= -hbux
CPP_FLAGS		= $(CPP_DEFS)
CC_FLAGS			= $(DEBUG)
CFLAGS			= $(CPP_FLAGS) $(CC_FLAGS)

INSTALL			= install -c
FMODE				= -m 640			# used by install
RANLIB			= ranlib

PAGER				= less


LIBNAME			= lib$(NAME).a

lib: $(LIBNAME)

libopt: clean
	make DEBUG=-O "DEFS=$(DEFS)" lib
	$(INSTALL) $(FMODE) $(LIBNAME) $(LIBDIR)-O

$(LIBNAME): $(OBJECTS)
	ar r $@ $?
	$(RANLIB) $@

lint:
	lint $(CPP_FLAGS) $(LINT_FLAGS) $(MODULE) 2>&1 | egrep -v "possible pointer alignment|RCSid" | $(PAGER)

install: $(LIBNAME)
	@if test "$(LIBDIR)" -a "$(INCLUDEDIR)" -a "$(MANDIR)" ;\
	then \
		$(INSTALL) $(FMODE) $(LIBNAME) $(LIBDIR) ;\
		echo "Installed $(LIBNAME) to $(LIBDIR)" ;\
		for i in $(INCLUDEFILES); do $(INSTALL) $(FMODE) $$i $(INCLUDEDIR) ; done ;\
		echo Installed $(INCLUDEFILES) to $(INCLUDEDIR) ;\
		for i in $(MANFILES) ; do $(INSTALL) $(FMODE) $$i $(MANDIR) ; done ;\
		echo Installed $(MANFILES) to $(MANDIR) ;\
	else \
		echo "You forgot to set one of the following variables: LIBDIR,INCLUDEDIR,MANDIR" ;\
	fi

uninstall:
	a=`pwd` ; cd $(INCLUDEDIR) ;\
	if test $$a != `pwd` ; then rm -f $(INCLUDEFILES) ; fi
	a=`pwd` ; cd $(LIBDIR) ;\
	if test $$a != `pwd` ; then rm -f $(LIBNAME) ; fi
	a=`pwd` ; cd $(MANDIR) ;\
	if test $$a != `pwd` ; then rm -f $(MANFILES) ; fi

clean:
	rm -f $(OBJECTS) $(LIBNAME) core $(TESTPROGS)

spotless: clean uninstall

tags: $(SOURCES) $(HEADERS)
	ctags -w $(SOURCES) $(HEADERS)

checkout:
	co $(SOURCES) $(HEADERS) $(MANFILES)

#
# Distribution section
# This section contains the 2 targets for distribution support: dist, dirs
# "dist" checks out all files to be distributed
# "dirs" prints a list of directories to be included in the distribution.
# These directories should have a Makefile with a "dist" target
#
DISTRIBUTION_FILES	= $(SOURCES) $(HEADERS) $(MANFILES) COPYRIGHT README \
								dlltest.c htest.c bsttest.c bstcomp.c \
								dlltest.out htest.out bsttest.out

DIRS						=

dist:
	-co -q $(DISTRIBUTION_FILES)

dirs:
	@echo $(DIRS)

#
# PUT HERE THE RULES TO MAKE THE OBJECT FILES
#
dll.o:		dll.h dllimpl.h dict.h dictimpl.h
ht.o:			ht.h htimpl.h dict.h dictimpl.h
bst.o:		bst.h bstimpl.h dict.h dictimpl.h
rbt.o:		bstimpl.h dict.h dictimpl.h
dict.o:		dictimpl.h dict.h

dlltest: dlltest.c $(LIBNAME)
	cc $(CFLAGS) dlltest.c -o $@ $(LIBNAME) -L$(LIBDIR) -lfsma

htest: htest.c $(LIBNAME)
	cc $(CFLAGS) -o $@ htest.c $(LIBNAME) -L$(LIBDIR) -lfsma

bsttest: bsttest.c $(LIBNAME)
	cc $(CFLAGS) -o $@ bsttest.c $(LIBNAME) -L$(LIBDIR) -lfsma

bstcomp: bstcomp.c $(LIBNAME)
	cc $(CFLAGS) -o $@ bstcomp.c $(LIBNAME) -L$(LIBDIR) -lfsma

