###############################################################################
# Makefile for the DMalloc library.
# $Id: Makefile.all.in,v 1.83 1999/10/13 22:50:54 gray Exp $
###############################################################################

# default root installation directory
prefix = @prefix@
exec_prefix = @exec_prefix@

srcdir = @srcdir@
VPATH = @srcdir@

bindir = $(exec_prefix)/bin
libdir = $(exec_prefix)/lib
shlibdir = $(exec_prefix)/lib
incdir = $(prefix)/include
infodir = $(prefix)/share/info

# default c-compiler
CC = @CC@

DEFS	= -DHAVE_STDARG_H=@HAVE_STDARG_H@ \
	-DHAVE_STDLIB_H=@HAVE_STDLIB_H@ \
	-DHAVE_STRING_H=@HAVE_STRING_H@ \
	-DHAVE_UNISTD_H=@HAVE_UNISTD_H@ \
	$(DEFINES)
INCS = -I. -I$(srcdir) $(INCLUDES)
LIBS = -L. @LIBS@

LIBNAME	= dmalloc
SONAME = 4.2.0
LIBRARY	= lib$(LIBNAME).a
SHLIBRARY = lib$(LIBNAME).so.$(SONAME)

# thread version of the library
LIB_TH	= lib$(LIBNAME)th.a
SHLIB_TH = lib$(LIBNAME)th.so.$(SONAME)

# library with dmalloc disabled
LIB_DIS	= lib$(LIBNAME)lp.a
SHLIB_DIS = lib$(LIBNAME)lp.so.$(SONAME)

UTIL = dmalloc
INFOFILE = dmalloc.info

CCFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

###############################################################################
# End of system configuration section.
###############################################################################

SHELL = /bin/sh

HFLS = dmalloc.h
OBJS = arg_check.o compat.o dmalloc_lp.o dmalloc_tab.o env.o heap.o
NORMAL_OBJS = chunk.o error.o malloc.o
THREAD_OBJS = chunk_th.o error_th.o malloc_th.o

CFLAGS = $(CCFLAGS)
TEST = dmalloc_t

all : dmalloc.h $(LIBRARY) $(LIB_DIS) $(UTIL)
	@echo "To make the thread version of the library type 'make threads'"

clean :
	rm -f a.out core *.o *.t
	rm -f $(LIBRARY) $(LIB_TH) $(LIB_DIS) $(TEST) $(UTIL) dmalloc.h

realclean : clean

distclean : clean
	rm -f confdefs.h config.status config.log config.cache conf.h
	rm -f Makefile settings.h dmalloc.h.2
#	rm -f configure

installdirs :
	$(srcdir)/mkinstalldirs $(incdir) $(libdir) $(bindir)
# may want to add $(shlibdir) to this list

installincs : $(HFLS)
	$(srcdir)/mkinstalldirs $(incdir)
	$(INSTALL_DATA) $(HFLS) $(incdir)

installlib : installincs $(LIBRARY) $(LIB_DIS)
	$(srcdir)/mkinstalldirs $(libdir)
	$(INSTALL_DATA) $(LIBRARY) $(libdir)
	@RANLIB@ $(libdir)/$(LIBRARY)
	$(INSTALL_DATA) $(LIB_DIS) $(libdir)
	@RANLIB@ $(libdir)/$(LIB_DIS)

install : installdirs installlib $(UTIL)
	$(srcdir)/mkinstalldirs $(bindir)
	$(INSTALL_PROGRAM) -s $(UTIL) $(bindir)
	@echo "Enter 'make installinfo' to install $(INFOFILE) in $(infodir)"
	@echo "Enter 'make installth' to install thread library versions"
	@echo "Enter 'make installshlib' to install $(SHLIBRARY) in $(shlibdir)"

installth : $(LIB_TH)
	$(srcdir)/mkinstalldirs $(libdir)
	$(INSTALL_DATA) $(LIB_TH) $(libdir)

installinfo : $(INFOFILE)
	$(srcdir)/mkinstalldirs $(infodir)
	$(INSTALL_DATA) $(srcdir)/$(INFOFILE) $(infodir)

installshlib : $(SHLIBRARY)
	$(srcdir)/mkinstalldirs $(shlibdir)
	$(INSTALL_DATA) $(SHLIBRARY) $(shlibdir)
	$(INSTALL_DATA) $(SHLIB_DIS) $(shlibdir)
	$(INSTALL_DATA) $(SHLIB_TH) $(shlibdir)

dmalloc.h.2 : configure
	$(SHELL) configure

settings.h : $(srcdir)/settings.dist $(srcdir)/configure
	$(SHELL) $(srcdir)/configure

dmalloc.h : $(srcdir)/dmalloc.h.1 dmalloc.h.2 $(srcdir)/dmalloc.h.3
	rm -f $@ $@.t
	cat $(srcdir)/dmalloc.h.1 dmalloc.h.2 $(srcdir)/dmalloc.h.3 > $@.t
	mv $@.t $@

shlib : ${SHLIBRARY} ${SHLIB_DIS} ${SHLIB_TH}

# NOTE: you may have to edit the following to have it work on your system
${SHLIBRARY} : $(LIBRARY)
	rm -f $@ $@.t
	gcc -shared -Wl,-soname,$@ -o $@.t $(OBJS) $(NORMAL_OBJS) -lc
	mv $@.t $@

${SHLIB_TH} : $(LIB_TH)
	rm -f $@ $@.t
	gcc -shared -Wl,-soname,$@ -o $@.t $(OBJS) $(THREAD_OBJS) -lc
	mv $@.t $@

${SHLIB_DIS} : $(LIB_DIS)
	rm -f $@ $@.t
	gcc -shared -Wl,-soname,$@ -o $@.t dmalloc_lp.o -lc
	mv $@.t $@

$(LIBRARY) : $(OBJS) $(NORMAL_OBJS)
	ar cr $@ $?
	@RANLIB@ $@

$(LIB_TH) : $(OBJS) $(THREAD_OBJS)
	ar cr $@ $?
	ranlib $@

$(LIB_DIS) : dmalloc_lp.o
	ar cr $@ $?
	@RANLIB@ $@

noansi : dmalloc.h Deansify.pl
	perl $(srcdir)/Deansify.pl *.[ch]

utils : $(UTIL)

$(UTIL) : $(UTIL).o dmalloc_argv.o compat.o env.o
	rm -f $@
	$(CC) $(LDFLAGS) $(UTIL).o dmalloc_argv.o compat.o env.o $(LIBS)
	mv a.out $@

threads : $(LIB_TH)

# special _th versions of objects with the LOCK_THREADS variable defined to 1
chunk_th.o : $(srcdir)/chunk.c
	rm -f $@
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(INCS) -DLOCK_THREADS=1 \
		-c $(srcdir)/chunk.c -o ./$@

error_th.o : $(srcdir)/error.c
	rm -f $@
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(INCS) -DLOCK_THREADS=1 \
		-c $(srcdir)/error.c -o ./$@

malloc_th.o : $(srcdir)/malloc.c
	rm -f $@
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(INCS) -DLOCK_THREADS=1 \
		-c $(srcdir)/malloc.c -o ./$@

tests : $(TEST)

$(TEST) : $(TEST).o dmalloc_argv.o $(LIBRARY)
	rm -f $@
	$(CC) $(LDFLAGS) $(TEST).o dmalloc_argv.o $(LIBS) -l$(LIBNAME)
	mv a.out $@

check light : $(TEST)
	./$(TEST) -s -t 10000
	./$(TEST) -s -t 10000
	./$(TEST) -s -t 10000
	./$(TEST) -s -t 10000
	./$(TEST) -s -t 10000

heavy : $(TEST) light
	./$(TEST) -s -t 100000
	./$(TEST) -s -t 100000
	./$(TEST) -s -t 100000

.c.o :
	rm -f $@
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(INCS) -c $< -o ./$@

.texi.info :
	makeinfo --no-split $<

#
# auto configure settings - uncomment if you are doing configure
# development on the library
#

#Makefile : $(srcdir)/Makefile.in config.status
#	$(SHELL) config.status

#conf.h : $(srcdir)/conf.h.in config.status
#	$(SHELL) config.status

#config.status : $(srcdir)/configure
#	$(SHELL) config.status --recheck

# $(srcdir)/configure : $(srcdir)/configure.in
#	cd $(srcdir); autoconf

###############################################################################
#
# These dependencies are automatically generated.  Do not edit by hand.
#

arg_check.o: arg_check.c dmalloc.h conf.h settings.h chunk.h \
 debug_val.h dmalloc_loc.h error.h arg_check.h
chunk.o: chunk.c conf.h settings.h dmalloc.h chunk.h chunk_loc.h \
 dmalloc_loc.h compat.h debug_val.h dmalloc_tab.h error.h error_val.h \
 heap.h
compat.o: compat.c dmalloc.h conf.h settings.h compat.h dmalloc_loc.h
dmalloc.o: dmalloc.c dmalloc_argv.h conf.h settings.h dmalloc.h \
 compat.h debug_tok.h dmalloc_loc.h debug_val.h env.h error_str.h \
 error_val.h version.h
dmalloc_argv.o: dmalloc_argv.c conf.h settings.h dmalloc_argv.h \
 dmalloc_argv_loc.h compat.h
dmalloc_lp.o: dmalloc_lp.c dmalloc.h conf.h settings.h error_val.h \
 dmalloc_loc.h dmalloc_lp.h malloc.h return.h
dmalloc_t.o: dmalloc_t.c conf.h settings.h dmalloc_argv.h dmalloc.h \
 debug_val.h dmalloc_loc.h error_val.h
dmalloc_tab.o: dmalloc_tab.c conf.h settings.h chunk.h compat.h \
 dmalloc.h dmalloc_loc.h error.h error_val.h dmalloc_tab.h \
 dmalloc_tab_loc.h
env.o: env.c conf.h settings.h dmalloc.h compat.h dmalloc_loc.h \
 debug_tok.h debug_val.h env.h error.h
error.o: error.c conf.h settings.h dmalloc.h compat.h debug_val.h \
 dmalloc_loc.h env.h error.h error_val.h version.h
heap.o: heap.c dmalloc.h conf.h settings.h chunk.h compat.h \
 debug_val.h dmalloc_loc.h error.h error_val.h heap.h
malloc.o: malloc.c conf.h settings.h dmalloc.h chunk.h compat.h \
 debug_val.h dmalloc_loc.h env.h error.h error_str.h error_val.h \
 heap.h dmalloc_lp.h return.h
