#
#  Main makefile for Tcl/Objective-C interface library
#  Copyright (C) 1993,1994 R. Andrew McCallum
#
#  Written by:	R. Andrew McCallum <mccallum@cs.rochester.edu>
#  Dept. of Computer Science, U. of Rochester, Rochester, NY  14627
#
#  This file is part of the Tcl/Objective-C interface 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; if not, write to the Free
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


SHELL = /bin/sh

#### Start of system configuration section. ####

srcdir = @srcdir@
VPATH = @srcdir@

CC = @CC@
RANLIB = @RANLIB@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
AR = ar
ARFLAGS = rc
MAKEINFO = makeinfo

# directories in which to find the required include files
TCL_INCLUDE_FLAGS = @TCLINCLUDES@
TK_INCLUDE_FLAGS= @TKINCLUDES@
X_INCLUDE_FLAGS = @XINCLUDES@
READLINE_INCLUDE_FLAGS = @READLINEINCLUDES@

TCL_LIBRARY_FLAGS = @TCLLIBS@
TK_LIBRARY_FLAGS = @TKLIBS@
X_LIBRARY_FLAGS = @XLIBSW@
READLINE_LIBRARY_FLAGS = @READLINELIBS@

TCLLIBS = $(TCL_LIBRARY_FLAGS) \
	$(READLINE_LIBRARY_FLAGS) -lobjc -lm @LIBS@
LIBS = $(TK_LIBRARY_FLAGS) $(TCL_LIBRARY_FLAGS) \
	$(X_LIBRARY_FLAGS) $(READLINE_LIBRARY_FLAGS) -lobjc -lm @LIBS@

DEFS = @DEFS@

# All these are optional.  You can redefine CFLAGS, CPPFLAGS and
# INCLUDEFLAGS on the command line however you like.
CFLAGS = -Wall -Wno-implicit -g -O2
CPPFLAGS = 
INCLUDEFLAGS =

prefix = @prefix@
exec_prefix = $(prefix)

# Installation locations
libdir = $(exec_prefix)/lib
includedir = $(prefix)/include
infodir = $(prefix)/info

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

TCLOBJC_VERSION = 1.3

ALL_INCLUDE_FLAGS = -I. -I$(srcdir) $(INCLUDEFLAGS) \
	$(TCL_INCLUDE_FLAGS) $(TK_INCLUDE_FLAGS) \
        $(X_INCLUDE_FLAGS) $(READLINE_INCLUDE_FLAGS)
ALL_CPPFLAGS = $(ALL_INCLUDE_FLAGS) $(CPPFLAGS)
ALL_CFLAGS = $(CFLAGS)
ALL_OBJCFLAGS = $(CFLAGS) @NON_NeXT_ALL_OBJCFLAGS@

# definitions to be passed to subdir Makefile's
MAKEDEFINES = CC='$(CC)' CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' \
DEFS='$(DEFS)'

%.o : %.m
	$(CC) -c $(ALL_CPPFLAGS) $(DEFS) $(ALL_OBJCFLAGS) $<
%.o : %.c
	$(CC) -c $(ALL_CPPFLAGS) $(DEFS) $(ALL_CFLAGS) $<
%.pic_o : %.m
	$(CC) -c -o $@ -fpic -DPIC $(ALL_CPPFLAGS) $(DEFS) $(ALL_OBJCFLAGS) $<
%.pic_o : %.c
	$(CC) -c -o $@ -fpic -DPIC $(ALL_CPPFLAGS) $(DEFS) $(ALL_CFLAGS) $<

ifeq ($(findstring -DHAVE_OBJC_MALLOC,$(DEFS)),)
OBJC_MALLOC_SRC=objc-malloc.m
OBJC_MALLOC_OBJ=objc-malloc.o
endif

SRCS = \
@NON_NeXT_EXTRA_SRCS@ \
TclInterp.m \
TkInterp.m \
tclObjc.m \
$(OBJC_MALLOC_SRC)

OBJS = \
@NON_NeXT_EXTRA_OBJS@ \
TclInterp.o \
@TkInterp_o@ \
tclObjc.o \
$(OBJC_MALLOC_OBJ)

SHARED_OBJS = $(OBJS:%.o=%.pic_o)

HDRS = \
TclInterp.h \
@TkInterp_h@

DISTFILES = \
List.m \
HashTable.m \
TclInterp.m \
TkInterp.m \
tclObjc.m \
objc-malloc.m \
xmalloc.c \
checkTcl.m \
checkTk.m \
coll/List.h \
coll/HashTable.h \
TclInterp.h \
TkInterp.h \
tclObjc.h.in \
objc-malloc.h \
objc-gnu2next.h \
check.tcl check.tk \
config.tcllib \
COPYING.LIB ChangeLog \
Makefile.in configure configure.in mkinstalldirs \
gcc.patch \
version.texi \
README readme.texi \
INSTALL install.texi \
NEWS news.texi \
TODO 

all: libtclobjc.a libtclobjc.so

libtclobjc.a: $(OBJS)
	$(AR) $(ARFLAGS) libtclobjc.a $(OBJS)
	$(RANLIB) libtclobjc.a

libtclobjc.so: $(SHARED_OBJS)
	gcc -shared -Wl,-soname,libtclobjc.so.$(TCLOBJC_VERSION) \
	  -o libtclobjc.so.$(TCLOBJC_VERSION) $(SHARED_OBJS) \
	  $(READLINE_LIBRARY_FLAGS) -lc

install: installdirs all
	$(INSTALL_DATA) libtclobjc.a $(libdir)/libtclobjc.a
	$(RANLIB) $(libdir)/libtclobjc.a
	cd $(srcdir) &&	$(INSTALL_DATA) $(HDRS) $(includedir)
	$(INSTALL_DATA) tclObjc.h $(includedir)

installdirs:
	$(srcdir)/mkinstalldirs $(includedir) $(libdir)

uninstall:
	rm -f $(libdir)/libtclobjc.a
	for file in $(HDRS) tclObjc.h; do \
	  rm -rf $(includedir)/$$file ; \
	done

check: doCheckTcl @doCheckTk@

doCheckTcl: checkTcl
	./checkTcl < $(srcdir)/check.tcl

# Without readline, redirecting into Tk's promptAndEval isn't working
# I haven't looked into why.
doCheckTk: checkTk
	./checkTk $(srcdir)/check.tk

checkTcl: checkTcl.o libtclobjc.a @READLINE_O_EXTRAS@
	$(CC) -g -o checkTcl checkTcl.o libtclobjc.a @READLINE_O_EXTRAS@ $(TCLLIBS)

checkTk: checkTk.o libtclobjc.a @READLINE_O_EXTRAS@
	$(CC) -o checkTk checkTk.o libtclobjc.a @READLINE_O_EXTRAS@ $(LIBS)

depend:
	rm -f $(srcdir)/Makefile.depend
	$(CC) $(ALL_INCLUDE_FLAGS) -M $(SRCS) > $(srcdir)/Makefile.depend

tclObjc.h: Makefile.in tclObjc.h.in
	sed -e \
	's/#define TCLOBJC_VERSION .*/#define TCLOBJC_VERSION "$(TCLOBJC_VERSION)"/'\
		< $(srcdir)/tclObjc.h.in > tclObjc.h

tclObjc.o TclInterp.o: tclObjc.h

version.texi: Makefile
	rm -f $(srcdir)/version.texi
	echo '@set TCLOBJC_VERSION' $(TCLOBJC_VERSION) > $(srcdir)/version.texi
README: readme.texi version.texi news.texi
	$(MAKEINFO) -o README -D README_ONLY \
	--no-header --no-split $(srcdir)/readme.texi
INSTALL: install.texi version.texi
	$(MAKEINFO) -o INSTALL -D INSTALL_ONLY \
	--no-header --no-split $(srcdir)/install.texi
NEWS: news.texi version.texi
	$(MAKEINFO) -o NEWS -D NEWS_ONLY \
	--no-header --no-split $(srcdir)/news.texi

Makefile: Makefile.in config.status
	$(SHELL) config.status
config.status: configure
	$(SHELL) $(srcdir)/configure --no-create
configure: configure.in
	cd $(srcdir); autoconf

TAGS: $(SRCS)
	etags $(SRCS)

mostlyclean:
	rm -f *~ coll/*~ core

clean: mostlyclean
	rm -f libtclobjc.* *.o *.pic_o Makefile.depend checkTk checkTcl

realclean: clean
	rm -f Makefile config.status config.cache config.log

superclean: realclean
	rm -f TAGS configure

dist: $(DISTFILES)
	echo libtclobjc-$(TCLOBJC_VERSION) > .fname
	rm -rf `cat .fname`
	mkdir `cat .fname`
	mkdir `cat .fname`/coll
	for file in $(DISTFILES); do \
	  ln $$file `cat .fname`/$$file ; \
	done
	tar -chvf `cat .fname`.tar `cat .fname`
	gzip `cat .fname`.tar
	rm -rf `cat .fname` .fname

