# GNUmakefile
#
# Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
# All rights reserved.
#
# Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
#
# This file is part of libFoundation.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.
#
# We disclaim all warranties with regard to this software, including all
# implied warranties of merchantability and fitness, in no event shall
# we be liable for any special, indirect or consequential damages or any
# damages whatsoever resulting from loss of use, data or profits, whether in
# an action of contract, negligence or other tortious action, arising out of
# or in connection with the use or performance of this software.

TOP	= ../..
LIB	= $(TOP)/lib
RUNTEST	= runtest

include $(TOP)/config.mak

TESTTOOLS = objc NSException NSAutoreleasePool \
	NSString NSArray NSDictionary NSDate NSValue \
	NSRunLoop NSTimer NSNotification \
	NSArchiver NSScanner GarbageCollector StackZone \
	NSUserDefaults NSBundle NSProcessInfo \
	NSInvocation NSMethodSignature test

AUTOMATIC_TESTS = GCArray GCDictionary

.SUFFIXES: .m .h $(objext)

OBJDIR	= obj
PROG	= $(OBJDIR)/driver$(exeext)

ifeq ($(BUNDLE_AVAIL), yes)
BUNDLE	= $(OBJDIR)/bundle-binary
endif

MFILES	= driver.m linking.m functions.m loop.m \
	ArchiverClasses.m ExceptionClasses.m MyObject.m NotificationObserver.m\
	objc.m invocation.m exceptions.m string.m SignatureTest.m \
	MyRange.m IntValueObject.m signature.m \
	timer.m MyGCObject.m multithreading.m 

OFILES	= $(addprefix $(OBJDIR)/, $(MFILES:.m=$(objext)))

$(addprefix $(OBJDIR)/, %$(objext)) : %.m
	$(CC) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $(OBJDIR)/$(@F)

all: $(OBJDIR) $(PROG) $(BUNDLE) .gdbinit

$(PROG): $(OFILES)
	$(CC) -o $@ $(OFILES) $(LDFLAGS) $(LIBS)

$(OBJDIR):
	mkdir $@

SignatureTest.m: signature-test.pl
	$(PERL) $^

$(OBJDIR)/bundle$(objext): bundle.m
	$(CC) $(BUNDLE_CFLAGS) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $(OBJDIR)/$(@F)

$(BUNDLE): $(OBJDIR)/bundle$(objext)
	$(BUNDLE_LD) $(BUNDLE_LDFLAGS) -o $@ $^
# Make a copy of $(BUNDLE) in the bundle from the NSBundle test
# to make the test work for NeXT foundation
	FOUNDATION_LIBRARY=${FOUNDATION_LIBRARY}; \
	if [ "$${FOUNDATION_LIBRARY}" = foundation ]; then \
	  cp $(BUNDLE) testsuite/NSBundle/bundle/bundle; \
	fi

clean:
	rm -rf $(OBJDIR) SignatureTest.m testsuite/Foundation \
	    testsuite/NSMethodSignature/aut-mthsign.exp \
	    $(addprefix testsuite/, $(AUTOMATIC_TESTS)) testsuite/*.log \
	    testsuite/*.sum dbg.log site.bak site.exp .gdbinit t \
	    testsuite/NSBundle/bundle/bundle

distclean: clean
	rm -rf testsuite/config/config.exp

realclean: distclean

check:	all site.exp
	@(for f in $(TESTTOOLS); do \
	    echo $(RUNTEST) $(RUNTESTFLAGS) --tool $$f --srcdir . PROG=../$(PROG) BUNDLE_AVAIL=$(BUNDLE_AVAIL);\
	    (cd testsuite; $(RUNTEST) $(RUNTESTFLAGS) --tool $$f --srcdir . PROG=../$(PROG) BUNDLE_AVAIL=$(BUNDLE_AVAIL));\
	done)

$(TESTTOOLS):: all site.exp
	(cd testsuite; $(RUNTEST) $(RUNTESTFLAGS) --tool $@ --srcdir . PROG=../$(PROG) BUNDLE_AVAIL=$(BUNDLE_AVAIL))

GCArray: all site.exp
	@(if [ ! -d testsuite/$@ ]; then \
	    create-test.sh NSArray GCArray NSArray GCArray NSMutableArray GCMutableArray; \
	fi)
	(cd testsuite; $(RUNTEST) $(RUNTESTFLAGS) --tool $@ --srcdir . PROG=../$(PROG))

GCDictionary: all site.exp
	@(if [ ! -d testsuite/$@ ]; then \
	    create-test.sh NSDictionary GCDictionary NSDictionary GCDictionary NSMutableDictionary GCMutableDictionary; \
	fi)
	(cd testsuite; $(RUNTEST) $(RUNTESTFLAGS) --tool $@ --srcdir . PROG=../$(PROG))


check-all: all site.exp
	@(cd testsuite; \
	if test -d Foundation; then \
	    echo Removing the old content of Foundation directory...; \
	    rm -rf Foundation; \
	fi; \
	mkdir Foundation; \
	echo Linking all test files in the Foundation directory...; \
	for d in $(TESTTOOLS); do \
	    for f in $$d/*; do \
		if test -f $$f; then ln -s ../$$f Foundation; fi; \
	    done; \
	done; \
	cd ..; \
	echo $(RUNTEST) $(RUNTESTFLAGS) --tool Foundation --srcdir . PROG=../$(PROG) BUNDLE_AVAIL=$(BUNDLE_AVAIL);\
	(cd testsuite; $(RUNTEST) $(RUNTESTFLAGS) --tool Foundation --srcdir . PROG=../$(PROG) BUNDLE_AVAIL=$(BUNDLE_AVAIL)))

site.exp: GNUmakefile
	@echo "Making a new config file..."
	-@rm -f ./tmp?
	@touch site.exp

	-@mv site.exp site.bak
	@echo "## these variables are automatically generated by make ##" > ./tmp0
	@echo "# Do not edit here. If you wish to override these values" >> ./tmp0
	@echo "# add them to the last section" >> ./tmp0
	@echo "set objdir `pwd`/obj" >> ./tmp0
	@echo "## All variables above are generated by configure. Do Not Edit ##" >> ./tmp0
		@cat ./tmp0 > site.exp
	@cat site.bak | sed \
			-e '1,/^## All variables above are.*##/ d' >> site.exp
	-@rm -f ./tmp?

.gdbinit:
	@echo Making $@...
	@echo define make >$@
	@echo shell $(MAKE) >>$@
	@echo end >>$@
	@echo dir . $(TOP)/../libFoundation/Foundation >>$@
	@echo set view-program Edit >>$@
	@echo view >>$@
