# This Makefile is set up to make a debugging malloc called libmalloc_d.a
# and a non-debugging fast malloc called libmalloc.a.
# Also generates testmalloc, simumalloc, teststomp and maltrace (together
# with _d versions)
#
# 'make veryclean' cleans out lib*.a as well.
#
# 'make .lint' runs lint.
#
# 'make install' puts libmalloc.a and libmalloc_d.a in $LIBDIR and
# puts malloc.h in $INCDIR.
#

srcdir = @srcdir@
VPATH = @srcdir@
@SET_MAKE@

SHELL=		/bin/sh
CC=		@CC@
COPTS=		@CFLAGS@
CPPFLAGS=	@CPPFLAGS@
CPPDEP=		@CPPDEP@
TOPDIR=		..
DEFS=		@DEFS@
RANLIB=		@RANLIB@
AR=		ar
LIBC_INCL=	@GENINCL@
LIBDIR=		@libdir@
INSTALL=	@INSTALL@
INCDIR=		@incdir@

# neutralize SystemV genius
SHELL=		/bin/sh

# DEBUGDEFS are set for libmalloc_d.a. Say 'make libmalloc' for nondebug
# version. (DEBUGDEFS=$(FASTDEFS))
#
DEBUGDEFS=-DDEBUG -DTRACE -DPROFILESIZES

# FASTDEFS are used when the 'libmalloc' target is made.
#
# -DTRACE and -DPROFILESIZES shouldn't introduce very much overhead since the
# former is turned off (one 'if'), and the latter is one 'if' + increment.
# So you can keep them even in the fast production version. 
# You may want to define -DSVID_MALLOC_0 if you want malloc(0) to
# return NULL, per the SVID.  Otherwise, I return a block that
# has zero size so it can then be passed to realloc().
#
FASTDEFS=#-DSVID_MALLOC_0 -DTRACE -DPROFILESIZES


#  NORMALDEFS are used for both debugging and non-debugging versions
# -DSHORTNAMES makes sure all internal global symbols are unique within 6
# characters. Avoid defining unless your linker is braindead.
# -DUSESTDIO is to make this use fputs() instead of write() for trace
# and debugging output. write() is preferable since it is unbuffered,
# and does not call malloc() or suchlike. Avoid defining if possible.
# -DSTDHEADERS if you have ANSI standard header files (stdlib.h, string.h)
# This can be defined on Solaris2.1, Irix3.3.x, BSD3.3,
# 386BSD, BSD386 and other sufficiently Posix systems.
# -DHAVE_MMAP should be defined for SunOS4.x and other systems
# that have a general purpose mmap call that allows memory-mapped files.
#
NORMALDEFS= @CFLAGS@

EXTRAINCLUDES=-I$(HOME)/include
LINT=lint
LN= ln -s
AR= ar
ARFLAGS= ruv
TOPDIR= ..
RANLIB=		@RANLIB@

LDFLAGS=#-Bstatic

EXT=	@MALLOCEXT@
MALLOC= @MALLOC@

# only developers should have to change stuff below this line

PROGS=	testmalloc$(EXT) simumalloc$(EXT) teststomp$(EXT) maltrace$(EXT)

DEFINES= $(CPPFLAGS) $(NORMALDEFS) $(DEBUGDEFS)

SPLAYOBJ = splay/sptree.o
SPLAYSRC = splay/sptree.c
SPLAYHDR = splay/sptree.h

SRCS =  _emalloc.c _malloc.c _memalign.c \
	_strdup.c _strsave.c botch.c \
	dumpheap.c emalloc.c getmem.c leak.c \
	malloc.c memalign.c setopts.c \
	stats.c strdup.c strsave.c verify.c

OBJS =  _emalloc.o _malloc.o _memalign.o \
	_strdup.o _strsave.o botch.o \
	dumpheap.o emalloc.o getmem.o leak.o \
	malloc.o memalign.o setopts.o \
	stats.o strdup.o strsave.o verify.o

# HDRS, DOCS, TESTS and EXTRAS are used when making distributions.
# so please keep them uptodate.
# bundle is smart enough not to include object files, RCS, executables,
# etc, and does subdirectories right, but there's often other files
# in the development directory...

# globals.c, version.c are included in malloc.c.
HDRS = align.h assert.h defs.h externs.h globals.c globals.h globrename.h \
	malloc.h trace.h version.c

DOCS = README NOTE TODO CHANGES malloc.doc Makefile

TESTS = testmalloc.c test.out teststomp.c tests regress \
	simumalloc.c

EXTRAS = splay

INCLUDES=-I./splay -I.. $(EXTRAINCLUDES)

CFLAGS= $(COPTS) $(INCLUDES) $(DEFS) $(DEFINES)

zmailer: lib$(MALLOC).a

all: pass clean lib$(MALLOC).a

pass: lib$(MALLOC).a $(PROGS) out$(EXT)

system:
	# Nothing to do!

malloc:
	$(MAKE) -f Makefile $(MFLAGS) CC="$(CC)" DEBUGDEFS="$(FASTDEFS)" \
		EXT="" CDEBUGFLAGS="$(CDEBUGFLAGS)" pass

malloc_d:
	$(MAKE) -f Makefile $(MFLAGS) CC="$(CC)" DEBUGDEFS="$(DEBUGDEFS)" \
		EXT="_d" CDEBUGFLAGS="$(CDEBUGFLAGS)" pass

testmalloc$(EXT): testmalloc.c .lib$(EXT)
	$(CC) $(CFLAGSM) -o $@ testmalloc.c -L. $(LIBMALLOC) ${LDFLAGS}

teststomp$(EXT): teststomp.c .lib$(EXT)
	$(CC) $(CFLAGSM) -o $@ teststomp.c -L. $(LIBMALLOC) ${LDFLAGS}

simumalloc$(EXT): simumalloc.c .lib$(EXT)
	$(CC) $(CFLAGSM) -DMYMALLOC -o $@ simumalloc.c -L. $(LIBMALLOC) ${LDFLAGS}

maltrace$(EXT): maltrace.c .lib$(EXT)
	$(CC) $(CFLAGSM) -DMYMALLOC -o $@ maltrace.c -L. $(LIBMALLOC) ${LDFLAGS}

../libs/lib$(MALLOC).a: lib$(MALLOC).a

lib$(MALLOC).a: $(OBJS) $(SPLAYOBJ) 
	rm -f lib$(MALLOC).a
	$(AR) $(ARFLAGS) lib$(MALLOC).a $(OBJS) $(SPLAYOBJ)
	-$(RANLIB) lib$(MALLOC).a
	cp -p lib$(MALLOC).a ../libs/lib$(MALLOC).a
	-$(RANLIB) ../libs/lib$(MALLOC).a
	touch .lib$(EXT)

$(SPLAYOBJ): .foo
	cd splay; $(MAKE) $(MFLAGS) DEFINES="$(DEFINES)" DEFS="$(DEFS)" \
		LIBMALLOC=$(TOPDIR)/$(LIBMALLOC) COPTS="$(COPTS)" CC="$(CC)"

out$(EXT): $(PROGS)
	rm -f out$(EXT)
	EXT=$(EXT); export EXT; ./regress > out$(EXT) 2>&1
	@echo "Ok to get two core dumps if this is teststomp_d"
	-(./teststomp$(EXT) 5; ./teststomp$(EXT) 8) >>out$(EXT) 2>&1

one: onefile.o

onefile.c: $(SRCS) $(SPLAYSRC)
	rm -f onefile.c
	cat $(SRCS) $(SPLAYSRC) | sed '/RCSID/d' > onefile.c

.lint: $(SRCS)
	($(LINT) $(LINTFLAGS) $(DEFINES) $(INCLUDES) $(SRCS); \
		$(LINT) $(LINTFLAGS) $(DEFINES) $(INCLUDES) -I$(TOPDIR) $(SPLAYSRC)) \
		2>&1 | egrep -v 'pointer alignment|illegal pointer comb'
	touch .lint

.foo:

cleanprogs:
	-rm -f $(PROGS) $(LIBMALLOC) out$(EXT) trace$(EXT) .lib$(EXT)

clean:
	-rm -f *.a *.o \#* *~ core a.out gmon.out mon.out onefile.c *.sL prof.out
	cd splay; $(MAKE) clean

distclean veryclean: clean cleanprogs
	make EXT= cleanprogs
	-rm -f make.log make.out o core simumalloc_* Makefile */Makefile

install:
	$(INSTALL) -m 644 libmalloc.a $(LIBDIR)
	-$(RANLIB) $(LIBDIR)/libmalloc.a
	$(INSTALL) -m 644 libmalloc_d.a $(LIBDIR)
	-$(RANLIB) $(LIBDIR)/libmalloc_d.a
	$(INSTALL) -m 644 malloc.h $(INCDIR)

.id: $(SRCS)
	mkid $(SRCS) $(SPLAYSRC) $(HDRS) $(SPLAYHDR)
	touch .id

dist:
	@rm -f Makefile.bak
	@mv Makefile Makefile.bak;\
	sed '/^# DO NOT PUT ANYTHING/,$$d' Makefile.bak > Makefile; \
	(bundle -v $(DOCS) $(SRCS) $(HDRS) $(TESTS) $(EXTRAS)); \
	mv Makefile.bak Makefile

files:
	find * -type f -print | \
		egrep -v '(,v|\.o|core|make.log|simumalloc|testmalloc|teststomp)$$' | \
		egrep -v '(libmalloc.*\.a|res\..*)$$' > FILES

depend:   # An empty target -- manually generated..

.dep: $(SRCS)
	$(CC) $(CFLAGSM) -MM $(SRCS) > .dep

# append the contents of .dep after this line.
_emalloc.o : _emalloc.c defs.h externs.h assert.h align.h globals.h globrename.h \
  trace.h 
_malloc.o : _malloc.c defs.h externs.h assert.h align.h globals.h globrename.h \
  trace.h 
_memalign.o : _memalign.c defs.h externs.h assert.h align.h globals.h globrename.h \
  trace.h 
_strdup.o : _strdup.c defs.h externs.h assert.h align.h globals.h globrename.h \
  trace.h 
_strsave.o : _strsave.c defs.h externs.h assert.h align.h globals.h globrename.h \
  trace.h 
botch.o : botch.c defs.h externs.h assert.h align.h globals.h globrename.h 
dumpheap.o : dumpheap.c defs.h externs.h assert.h align.h globals.h globrename.h 
emalloc.o : emalloc.c defs.h externs.h assert.h align.h globals.h globrename.h 
getmem.o : getmem.c defs.h externs.h assert.h align.h globals.h globrename.h 
leak.o : leak.c defs.h externs.h assert.h align.h globals.h globrename.h ./splay/sptree.h 
malloc.o : malloc.c defs.h externs.h assert.h align.h globals.c globrename.h \
  version.c 
memalign.o : memalign.c defs.h externs.h assert.h align.h globals.h globrename.h 
setopts.o : setopts.c defs.h externs.h assert.h align.h globals.h globrename.h 
stats.o : stats.c defs.h externs.h assert.h align.h globals.h globrename.h 
strdup.o : strdup.c defs.h externs.h assert.h align.h 
strsave.o : strsave.c defs.h externs.h assert.h align.h 
verify.o : verify.c defs.h externs.h assert.h align.h globals.h globrename.h 
