#	Makefile for:
#	----------------------------------------------------------------------
#	Bounds Checking Project, by Richard W.M. Jones.
#	Library of checking functions linked by modified GCC.
#	----------------------------------------------------------------------

# The following mods from Mark W. Snitily should solve cross-compilation
# problems.	(RWMJ)
# ---
#
#  This makefile is run by the gcc dir's makefile:
#  thisdir1=`pwd`; \
#  srcdir1=`cd $(srcdir); pwd`; \
#  cd bounds/lib; \
#  $(MAKE) $(MAKEFLAGS) -f $$srcdir1/bounds/lib/Makefile libcheck.a \
#    srcdir=$$srcdir1 tooldir=$(tooldir) AR="$(AR)" AR_FLAGS="$(AR_FLAGS)" \
#    GCC_FOR_TARGET="$$thisdir1/xgcc -B$$thisdir1/" \
#    GCC_CFLAGS="$(GCC_CFLAGS)" incinstalldir=$$thisdir1/include
#  Two targets are used by ../Makefile: `all' and `mostlyclean'.

# SHELL	= /bin/sh

OPTIMIZE= -O3

GCC_FOR_TARGET=gcc
GCC_CFLAGS=-funsigned-char
AR_FLAGS=r
srcdir=$(PWD:d:d:d).

# Always search these dirs when compiling.
SUBDIR_INCLUDES = -I../.. -I$(srcdir)/bounds/lib -I$(srcdir) -I$(srcdir)/config

.c.o:
	$(GCC_FOR_TARGET) -Wall $(OPTIMIZE) \
		-c $(GCC_CFLAGS) $(SUBDIR_INCLUDES) $<

# If we were not invoked from the gcc dir invoke make
# in the gcc dir and reinvoke this makefile.
# That's necessary to get the right values for srcdir, etc.
all:
	cd ../..; $(MAKE) CFLAGS="$(CFLAGS)" sublibcheck.a

OBJS	= alloca.o check.o error.o exstatic.o function.o globals.o init.o \
	  malloc/gmalloc.o objects.o print.o
HEADERS = alloca.h bounds-l.h check.h ext-tree.h function.h \
	  mutex.h objects.h version.h ../VERSION
LIBFUNCS = memcpy memmove bcopy memset bzero memcmp bcmp \
	   strcpy strncpy strlen strcmp strncmp strcat strncat strpbrk \
	   strrchr rindex strspn strcspn strstr strtok strchr strdup \
	   index strcoll strxfrm memchr memccpy strcasecmp strncasecmp

RM = -del

# This is where `libcheck.a' ends up (in the gcc-x.x.x home directory).
INSTDIR	= $(srcdir)

mostlyclean:
	$(RM) libcheck.a $(OBJS) core
	$(RM) -r func stmp_func
	cd malloc; $(MAKE) clean

distclean: clean
	$(RM) *~ *.BAK

clean: mostlyclean
extraclean: mostlyclean

install: libcheck.a
	cp libcheck.a $(INSTDIR)/libcheck.a

libcheck.a: $(OBJS) stmp_func
	$(RM) libcheck.a
	$(AR) $(AR_FLAGS) libcheck.a $(OBJS) func/*.o
# ranlib is run in the parent directory's makefile.

alloca.o: $(srcdir)/bounds/lib/alloca.c
	$(GCC_FOR_TARGET) -fno-builtin -Wall $(OPTIMIZE) \
		-c $(GCC_CFLAGS) $(SUBDIR_INCLUDES) \
		$(srcdir)/bounds/lib/alloca.c -o alloca.o

malloc/gmalloc.o:
	cd malloc & $(MAKE) srcdir=$(srcdir) gmalloc.o

stmp_func: $(srcdir)/bounds/lib/string.c $(HEADERS)
	-mkdir func 2>nul
	for %n in ($(LIBFUNCS)) do $(GCC_FOR_TARGET) -DLbounds_%n -fno-builtin -Wall $(OPTIMIZE) -c $(GCC_CFLAGS) $(SUBDIR_INCLUDES) string.c -o func/b%n.o
	for %n in ($(LIBFUNCS)) do $(GCC_FOR_TARGET) -DL%n -fno-builtin -Wall $(OPTIMIZE) -c $(GCC_CFLAGS) $(SUBDIR_INCLUDES) string.c -o func/n%n.o
	touch stmp_func

$(OBJS): $(HEADERS)
