#
#  Copyright (C) 1996 
#	Ovidiu Predescu <ovidiu@bx.logicnet.ro>
#	Mircea Oancea <mircea@jupiter.elcom.pub.ro>
#
#  Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
#	   Mircea Oancea <mircea@jupiter.elcom.pub.ro>
#  Date: 1996
#
#  This file is part of the FoundationExtensions library.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Library General Public License for more details.
#
#  You should have received a copy of the GNU Library General Public
#  License along with this library; see the file COPYING.LIB.
#  If not, write to the Free Software Foundation,
#  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

TOP	= ..
INCLUDE	= $(TOP)/include
LIB	= $(TOP)/lib

include $(TOP)/config.mak

TARGET	= $(LIB)/$(LIB_FOUNDATION_EXT_NAME)

.SUFFIXES:	.m .h $(objext)

OBJDIR	= obj

MFILES	= objc-runtime.m NSException.m NSConcreteNumber.m \
	GarbageCollector.m GCObject.m GCArray.m GCDictionary.m \
	FormatScanner.m PrintfFormatScanner.m \
	DefaultScannerHandler.m PrintfScannerHandler.m

EXCEPTION_MFILES = FoundationException.m GeneralExceptions.m \
	NSCoderExceptions.m

HFILES	= $(filter-out behavior.h, $(MFILES:.m=.h)) support.h
EXCEPTION_HFILES = $(EXCEPTION_MFILES:.m=.h)


ifeq ($(FOUNDATION_LIBRARY), foundation)
ADDITIONAL_FOUNDATION_HFILES = NSHashTable.h NSMapTable.h NSSet.h \
	NSMethodSignature.h NSInvocation.h
HFILES	:= $(HFILES) HiddenClasses.h MissingMethods.h
MFILES	:= behavior.m $(MFILES) encoding.m misc.m MissingMethods.m
endif


ifneq (, $(findstring $(FOUNDATION_LIBRARY), gnustep-base))
HFILES	:= $(HFILES) MissingMethods.h
MFILES	:= $(MFILES) MissingMethods.m
endif


ifeq ($(WIN32), yes)
INIT_FILE = FoundationExt_init_runtime
INIT_FILE_OBJ = $(INIT_FILE)$(objext) 
endif

OFILES	= $(MFILES:.m=$(objext))
EXCEPTION_OFILES = $(EXCEPTION_MFILES:.m=$(objext))

mfile_compile_cmd = \
	$(CC) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $(OBJDIR)/$(@F)

$(addprefix $(OBJDIR)/, %$(objext)) : %.m
	$(mfile_compile_cmd)

$(addprefix $(OBJDIR)/, %$(objext)) : $(addprefix exceptions/, %.m)
	$(mfile_compile_cmd)

all: additional_hfiles NSException.h $(OBJDIR) $(TARGET)

install: all headers $(libdir)/$(LIB_FOUNDATION_EXT_NAME)

ifeq ($(FOUNDATION_LIBRARY), foundation)
additional_hfiles: $(addprefix ../Foundation/, $(ADDITIONAL_FOUNDATION_HFILES))

../Foundation/%.h: %.h
	cp $< $@
else
additional_hfiles:
endif

ifeq ($(WIN32), yes)
#
# Initialization routine
#
$(OBJDIR)/$(INIT_FILE_OBJ): \
		$(addprefix $(OBJDIR)/, $(OFILES) $(EXCEPTION_OFILES))
	cd $(OBJDIR) & \
	nm $(OFILES) $(EXCEPTION_OFILES) | grep "  __GLOBAL_" > tmpinit.c & \
	collect tmpinit.c $(INIT_FILE) & \
	$(CC) -c $(CFLAGS) $(INIT_FILE).c & \
	rm tmpinit.c
endif

$(TARGET): $(addprefix $(OBJDIR)/, $(OFILES) $(EXCEPTION_OFILES) \
				   $(INIT_FILE_OBJ))
	rm -f $@
ifneq ($(LIBTOOL),)
	$(LIBTOOL) -o $@ $^ 
else
	$(AR) $(ARFLAGS) $@ $^
	$(RANLIB) $@
endif

ifeq ($(FOUNDATION_LIBRARY), foundation)
NSException.h: foundation/NSException.h
	cp $< $@
endif

ifeq ($(FOUNDATION_LIBRARY), Sun)
NSException.h: SunFoundation/NSException.h
	cp $< $@
endif

ifeq ($(FOUNDATION_LIBRARY), gnustep-base)
NSException.h: gnustep-base/NSException.h
	cp $< $@
endif


headers: $(includedir)/extensions/exceptions foundation_headers \
	$(addprefix $(includedir)/extensions/, $(HFILES)) \
	$(addprefix $(includedir)/extensions/exceptions/, $(EXCEPTION_HFILES))

uninstall::
	rm -rf $(includedir)/extensions
	rm -f $(libdir)/$(LIB_FOUNDATION_EXT_NAME)

ifeq ($(FOUNDATION_LIBRARY), foundation)
foundation_headers: $(includedir)/Foundation

$(includedir)/Foundation:
	../mkinstalldirs $@
	(cd ../Foundation; for f in *; do \
	    $(INSTALL_DATA) $$f $@; \
	done)

uninstall::
	rm -rf $(includedir)/Foundation
else
foundation_headers:
endif


$(addprefix $(includedir)/extensions/, %.h): %.h
	$(INSTALL_DATA) $< $@

$(includedir)/extensions/objc-runtime.h:
	sed "s^#include <config.h>^#define $(OBJC_RUNTIME)_RUNTIME 1^" objc-runtime.h >temp.h
	$(INSTALL_DATA) temp.h $@
	rm temp.h

$(addprefix $(INCLUDE)/extensions/exceptions/, %.h): \
		$(addprefix exceptions/, %.h)
	$(INSTALL_DATA) $< $@


ifeq ($(WIN32), yes)
$(includedir)/extensions/exceptions:
	if not exist $(includedir) mkdir $(includedir)
	if not exist $(includedir)\extensions mkdir $(includedir)\extensions
	if not exist $(includedir)\extensions\exceptions \
	    mkdir $(includedir)\extensions\exceptions

$(libdir):
	if not exist $(libdir) mkdir $(libdir)
else
$(includedir)/extensions/exceptions $(libdir):
	../mkinstalldirs $@
endif

$(libdir)/$(LIB_FOUNDATION_EXT_NAME): $(libdir) $(TARGET)
	$(INSTALL_DATA) $(TARGET) $@
	$(RANLIB) $@


$(OBJDIR):
	mkdir $@

clean:
	rm -rf *~ $(OBJDIR)

distclean: clean
	rm -rf Makefile support.h NSException.h Foundation
