#
# Makefile --
#
# Top-level makefile for Extended Tcl.
# 
#------------------------------------------------------------------------------
# Copyright 1992-1994 Karl Lehenbauer and Mark Diekhans.
#
# 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.  Karl Lehenbauer and
# Mark Diekhans make no representations about the suitability of this
# software for any purpose.  It is provided "as is" without express or
# implied warranty.
#------------------------------------------------------------------------------
# $Id: Makefile.in,v 4.0 1994/07/16 05:31:16 markd Rel $
#------------------------------------------------------------------------------
#
SHELL=/bin/sh

#------------------------------------------------------------------------------
# Autoconfig defines that can be overridden in Config.mk

CC                = @CC@
MCS               = @MCS_CMD@
RANLIB            = @RANLIB@
srcdir            = @srcdir@
srcbasedir        = @srcbasedir@
bldbasedir        = @bldbasedir@
VPATH             = @srcdir@
prefix            = /usr/local
exec_prefix       = /usr/local
ARCH              = @TCL_ARCH@
MAN_DIR_SEPARATOR = @MAN_DIR_SEPARATOR@
TCL_INST_MASTER   = ${TCL_MASTERDIR}/`tools/tclxversion`
TK_INST_MASTER    = ${TK_MASTERDIR}/`tools/tkxversion`

#------------------------------------------------------------------------------
# Include user-editable defines.

@MAKEINCLUDE@ @MAKEQUOTE@${bldbasedir}/Config.mk@MAKEQUOTE@

#------------------------------------------------------------------------------
# Other macros.

LIBTCL_A   = tclmaster/lib${ARCH}/libtcl.a
TCL_H      = tclmaster/include/tcl.h
LIBTK_A    = tkmaster/lib${ARCH}/libtk.a
TK_H       = tkmaster/include/tk.h
INSTCOPY   = ./runtcl tools/instcopy
CPMANPAGES = ./runtcl ${bldbasedir}/tools/cpmanpages
SYMLINKEXT = ${bldbasedir}/tools/symlinkext

#------------------------------------------------------------------------------
# Flags that were passed on the command line that are to be passed on to
# second level makes.

PASS_FLAGS = "CC=$(CC)" "CFLAGS=$(CFLAGS)"

#------------------------------------------------------------------------------
# The made.tmp files are used to indicate a makefile has successfully added
# it's .o files to a library.  We need to purge the right ones one a new
# library is copied.

TCLMADE.TMP = osSupport/made.tmp src/made.tmp
TKMADE.TMP  = tksrc/made.tmp

#------------------------------------------------------------------------------

all: TCLX runtcl ${TK_BUILD}

#------------------------------------------------------------------------------
# Compile the Extended Tcl library and link the Tcl shell.
#

TCLX: TCLCOPY runtcl
	cd tools;     ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all
	cd osSupport; ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all
	cd src;       ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all
	cd tclsrc;    ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all

#------------------------------------------------------------------------------
# Copy include and library files from the UCB Tcl distribution to the
# tclmaster directory.
#
TCLCOPY: MKTCLDIRS ${LIBTCL_A} ${TCL_H}

MKTCLDIRS:
	-mkdir tclmaster                2>/dev/null; exit 0
	-mkdir tclmaster/lib${ARCH}     2>/dev/null; exit 0
	-mkdir tclmaster/include        2>/dev/null; exit 0
	-mkdir tclmaster/bin${ARCH}     2>/dev/null; exit 0
	-mkdir tclmaster/src            2>/dev/null; exit 0

${LIBTCL_A}: ${TCL_UCB_LIB}/libtcl.a
	rm -f ${LIBTCL_A}
	cp ${TCL_UCB_LIB}/libtcl.a ${LIBTCL_A}
	${RANLIB} ${LIBTCL_A}

${TCL_H}: ${TCL_UCB_SRC}/tcl.h
	rm -f ${TCL_H}
	cp ${TCL_UCB_SRC}/tcl.h ${TCL_H}

#------------------------------------------------------------------------------
# Generate a wish shell {wishx} with Extended Tcl commands.
#

WISHX: TKCOPY runwishx
	cd tksrc;    ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all
	cd tktclsrc; ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} all

#------------------------------------------------------------------------------
# Copy include and library files from the UCB Tk distribution to the
# tkmaster directory.
#

TKCOPY: MKTKDIRS ${LIBTK_A} ${TK_H}

MKTKDIRS:
	-mkdir tkmaster                2>/dev/null; exit 0
	-mkdir tkmaster/lib${ARCH}     2>/dev/null; exit 0
	-mkdir tkmaster/include        2>/dev/null; exit 0
	-mkdir tkmaster/bin${ARCH}     2>/dev/null; exit 0
	-mkdir tkmaster/src            2>/dev/null; exit 0

${LIBTK_A}: ${TK_UCB_LIB}/libtk.a
	rm -f ${LIBTK_A}
	cp ${TK_UCB_LIB}/libtk.a ${LIBTK_A}
	${RANLIB} ${LIBTK_A}

${TK_H}: ${TK_UCB_SRC}/tk.h
	rm -f ${TK_H}
	cp ${TK_UCB_SRC}/tk.h ${TK_H}

#------------------------------------------------------------------------------
# Generate scripts to point the TCL_LIBRARY/TK_LIBRARY environment variable at
# the local master directories so tcl & wishx can be run before installing.

runtcl:
	@echo ':'                                                  >runtcl
	@echo '# script for testing Tcl before installation'      >>runtcl
	@echo "TCL_LIBRARY=`pwd`/tclmaster"                       >>runtcl
	@echo "TCL_PROGRAM=`pwd`/tclmaster/bin${ARCH}/tcl"        >>runtcl
	@echo "export TCL_LIBRARY TCL_PROGRAM"                    >>runtcl
	@echo "if [ \$$# = 0 ]"                                   >>runtcl
	@echo "then"                                              >>runtcl
	@echo "    exec \$$TCL_PROGRAM"                           >>runtcl
	@echo "else"                                              >>runtcl
	@echo "    exec \$$TCL_PROGRAM \"\$$@\""                  >>runtcl
	@echo "fi"                                                >>runtcl
	chmod a+rx runtcl

runwishx:
	@echo ':'                                                  >runwishx
	@echo '# script for testing wishx before installation'    >>runwishx
	@echo "TCL_LIBRARY=`pwd`/tclmaster"                       >>runwishx
	@echo "TK_LIBRARY=`pwd`/tkmaster"                         >>runwishx
	@echo "TCL_PROGRAM=`pwd`/tclmaster/bin${ARCH}/tcl"        >>runwishx
	@echo "WISHX=`pwd`/tkmaster/bin${ARCH}/wishx"             >>runwishx
	@echo "export TCL_LIBRARY TK_LIBRARY TCL_PROGRAM"         >>runwishx
	@echo "if [ \$$# = 0 ]"                                   >>runwishx
	@echo "then"                                              >>runwishx
	@echo "    exec \$$WISHX"                                 >>runwishx
	@echo "else"                                              >>runwishx
	@echo "    exec \$$WISHX \"\$$@\""                        >>runwishx
	@echo "fi"                                                >>runwishx
	chmod a+rx runwishx

#------------------------------------------------------------------------------
# Test to see if the C++ include file compiles and links.

tcl++:
	cd src;${MAKE} -${MAKEFLAGS} tcl++

#------------------------------------------------------------------------------
# Run the UCB and Extended Tcl tests.

test: ucbtests extdtests

ucbtests: all
	@echo "***************************************************************"
	@echo "*** Expect warnings about not having the following commands:"
	@echo "***     testasync"
	@echo "***     testcmdinfo"
	@echo "***     testdcall"
	@echo "***     testdstring"
	@echo "***     testlink"
	@echo "***"
	@echo "*** Expect warnings about not having the following math functions:"
	@echo "***     T1"
	@echo "***     T2"
	@echo "***************************************************************"
	@echo ""
	./runtcl -c "cd ${TCL_UCB_SRC}/tests;source ${srcbasedir}/tests/all"

extdtests: all
	./runtcl -c "cd tests;source all"

tktest: all
	@echo "***************************************************************"
	@echo "*** Expect warnings about not having the following commands:"
	@echo "***     testmakexists"
	@echo "***************************************************************"
	@echo ""
	cd ${srcdir}/tktests ;\
	    ${bldbasedir}/runwishx -f tktests.tcl -n wish ${TK_UCB_SRC}

#------------------------------------------------------------------------------
# Rebuild help files.  The are shipped with TclX, but can be rebuilt if Tcl or
# Tk versions have changed.

buildhelp: buildtclhelp buildtkhelp

buildtclhelp:
	cd tclsrc;   ${MAKE} -${MAKEFLAGS} buildtclhelp

buildtkhelp:
	cd tktclsrc; ${MAKE} -${MAKEFLAGS} buildtkhelp

#------------------------------------------------------------------------------
# Install Extended Tcl using the standard model

install: all TCLXINSTALL TCLXINSTALL-EXEC \
             ${TK_BUILD}MAYBE ${TK_BUILD}MAYBE-EXEC

install-exec: all TCLXINSTALL-EXEC ${TK_BUILD}MAYBE-EXEC

TCLXINSTALL:
	@echo ""
	@echo "   Install Extended Tcl using standard model"
	@echo ""
	rm -rf ${TCL_INST_MASTER}
	${INSTCOPY} tclmaster/*.tcl tclmaster/*.tlib tclmaster/*.tndx \
	   tclmaster/help tclmaster/src ${TCL_INST_MASTER}
	${INSTCOPY} tclmaster/include/tclExtend.h tclmaster/include/tcl++.h \
	   ${TCL_INCLUDEDIR}
	${CPMANPAGES} -rmcat @${MAN_DIR_SEPARATOR}@ \
	    ${TCL_MAN_CMD_SECTION} ${TCL_MAN_FUNC_SECTION} @@ \
	    ${srcbasedir}/man ${TCL_MAN_BASEDIR}

TCLXINSTALL-EXEC:
	${INSTCOPY} tclmaster/bin${ARCH}/tcl ${TCL_BINDIR}
	strip ${TCL_BINDIR}/tcl
	${MCS} ${TCL_BINDIR}/tcl
	${INSTCOPY} tclmaster/lib${ARCH}/libtclx.a ${TCL_LIBDIR}
	${RANLIB} ${TCL_LIBDIR}/libtclx.a

TKXINSTALL:
	rm -rf ${TK_INST_MASTER}
	${INSTCOPY} tkmaster/*.tcl tkmaster/*.tlib tkmaster/*.tndx \
	   tkmaster/help tkmaster/src tkmaster/*.ps tkmaster/demos \
	   ${TK_INST_MASTER}

TKXINSTALL-EXEC:
	${INSTCOPY} tkmaster/bin${ARCH}/wishx ${TCL_BINDIR}
	strip ${TCL_BINDIR}/wishx
	${MCS} ${TCL_BINDIR}/wishx
	${INSTCOPY} tkmaster/bin${ARCH}/tclhelp ${TCL_BINDIR}
	${INSTCOPY} tkmaster/lib${ARCH}/libtkx.a ${TCL_LIBDIR}
	${RANLIB} ${TCL_LIBDIR}/libtkx.a

# Fake targets to decide if we install wishx or not.

MAYBE:
MAYBE-EXEC:

WISHXMAYBE: TKXINSTALL
WISHXMAYBE-EXEC: TKXINSTALL-EXEC

#------------------------------------------------------------------------------
# Install Extended Tcl using the master directory model.

install-master: all MASTER-NOTE TCLXMINSTALL ${TK_BUILD}MMAYBE

install-master-exec: all MASTER-NOTE TCLXMINSTALL-EXEC ${TK_BUILD}MMAYBE-EXEC

MASTER-NOTE:
	@echo ""
	@echo "*************************************************************"
	@echo "*** TclX currently does not build symbolic links to the   ***"
	@echo "*** manual pages in the master directories.  If you need  ***"
	@echo "*** these man page links you must build them by hand.     ***"
	@echo "*************************************************************"
	@echo ""

TCLXMINSTALL:
	@echo ""
	@echo "   Install TclX master directory"
	@echo ""
	rm -rf ${TCL_INST_MASTER}
	${INSTCOPY} -dirname tclmaster ${TCL_INST_MASTER}
	strip ${TCL_INST_MASTER}/bin/tcl
	${MCS}  ${TCL_INST_MASTER}/bin/tcl
	${RANLIB} ${TCL_INST_MASTER}/lib${ARCH}/*.a
	${CPMANPAGES} @${MAN_DIR_SEPARATOR}@ \
	    ${TCL_MAN_CMD_SECTION} ${TCL_MAN_FUNC_SECTION} @@ \
	    ${TCL_UCB_SRC}/doc ${TCL_INST_MASTER}/man
	${CPMANPAGES} @${MAN_DIR_SEPARATOR}@ \
	    ${TCL_MAN_CMD_SECTION} ${TCL_MAN_FUNC_SECTION} @@ \
	    ${srcbasedir}/man ${TCL_INST_MASTER}/man
	${SYMLINKEXT} ${TCL_INST_MASTER}/bin${ARCH}/* ${TCL_BINDIR}
	${SYMLINKEXT} ${TCL_INST_MASTER}/lib${ARCH}/* ${TCL_LIBDIR}
	${SYMLINKEXT} ${TCL_INST_MASTER}/include/* ${TCL_INCLUDEDIR}

TCLXMINSTALL-EXEC:
	@echo ""
	@echo "   Install TclX executables only"
	@echo ""
	${INSTCOPY} tclmaster/bin${ARCH} ${TCL_INST_MASTER}
	strip ${TCL_INST_MASTER}/bin/tcl
	${MCS}  ${TCL_INST_MASTER}/bin/tcl
	${INSTCOPY} tclmaster/lib${ARCH} ${TCL_INST_MASTER}
	${RANLIB} ${TCL_INST_MASTER}/lib${ARCH}/*.a
	${SYMLINKEXT} ${TCL_INST_MASTER}/bin${ARCH}/* ${TCL_BINDIR}
	${SYMLINKEXT} ${TCL_INST_MASTER}/lib${ARCH}/* ${TCL_LIBDIR}

TKXMINSTALL:
	@echo ""
	@echo "   Install TkX master directory"
	@echo ""
	rm -rf ${TK_INST_MASTER}
	${INSTCOPY} -dirname tkmaster ${TK_INST_MASTER}
	strip ${TK_INST_MASTER}/bin/wishx
	${MCS}  ${TK_INST_MASTER}/bin/wishx
	${RANLIB} ${TK_INST_MASTER}/lib${ARCH}/*.a
	${CPMANPAGES} @${MAN_DIR_SEPARATOR}@ \
	    ${TK_MAN_CMD_SECTION} ${TK_MAN_FUNC_SECTION} \
	    ${TK_MAN_UNIXCMD_SECTION} \
	    ${TK_UCB_SRC}/doc ${TK_INST_MASTER}/man
	${SYMLINKEXT} ${TK_INST_MASTER}/bin${ARCH}/* ${TCL_BINDIR}
	${SYMLINKEXT} ${TK_INST_MASTER}/lib${ARCH}/* ${TCL_LIBDIR}
	${SYMLINKEXT} ${TK_INST_MASTER}/include/* ${TCL_INCLUDEDIR}

TKXMINSTALL-EXEC:
	@echo ""
	@echo "   Install TkX executables only"
	@echo ""
	${INSTCOPY} tkmaster/bin${ARCH} ${TK_INST_MASTER}
	strip ${TK_INST_MASTER}/bin/wishx
	${MCS}  ${TK_INST_MASTER}/bin/wishx
	${INSTCOPY} tkmaster/lib${ARCH} ${TK_INST_MASTER}
	${RANLIB} ${TK_INST_MASTER}/lib${ARCH}/*.a
	${SYMLINKEXT} ${TK_INST_MASTER}/bin${ARCH}/* ${TCL_BINDIR}
	${SYMLINKEXT} ${TK_INST_MASTER}/lib${ARCH}/* ${TCL_LIBDIR}

# Fake targets to decide if we install wishx or not.

MMAYBE:
MMAYBE-EXEC:

WISHXMMAYBE: TKXMINSTALL
WISHXMMAYBE-EXEC: TKXMINSTALL-EXEC

#------------------------------------------------------------------------------
# Clean up all files that were built by make.

clean:
	cd tools;     ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	cd osSupport; ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	cd src;       ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	cd tclsrc;    ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	cd tksrc;     ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	cd tktclsrc;  ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} clean
	-rm -f runtcl runwishx
	-rm -rf tclmaster tkmaster

#------------------------------------------------------------------------------
# Restore to the distributed state.

distclean: clean
	cd tools;     ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	cd osSupport; ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	cd src;       ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	cd tclsrc;    ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	cd tksrc;     ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	cd tktclsrc;  ${MAKE} -${MAKEFLAGS} ${PASS_FLAGS} distclean
	rm -f Makefile config.status

