#
# tk/unix/Makefile.in --
#
# Makefile to build a wish with Extended Tcl (wishx).
# 
#------------------------------------------------------------------------------
# Copyright 1992-1999 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 1.1.1.1 1999/06/28 20:35:36 damon Exp $
#------------------------------------------------------------------------------
#

#------------------------------------------------------------------------------
# Common and user-editable defines.
#
srcdir = @srcdir@
@MAKEINCLUDE@ @MAKEQUOTE@@bldbasedir@/unix/Common.mk@MAKEQUOTE@
@MAKEINCLUDE@ @MAKEQUOTE@@bldbasedir@/unix/Config.mk@MAKEQUOTE@

#------------------------------------------------------------------------------
# Local macros.
#
CC_SWITCHES = ${CPPFLAGS} ${XCFLAGS} ${CFLAGS} ${SYSCFLAGS} \
              ${TK_DEFS} ${TCL_DEFS}\
	      -I${TKX_GENERIC_DIR} -I${TKX_UNIX_SRC_DIR} \
	      -I${TK_GENERIC_DIR} -I${TK_UNIX_DIR} \
	      -I${TCLX_GENERIC_DIR} -I${TCLX_UNIX_SRC_DIR} \
	      -I${TKX_UNIX_BLD_DIR} -I${TCLX_UNIX_BLD_DIR} \
	      -I${TCL_GENERIC_DIR} -I${TCL_UNIX_DIR} ${TK_XINCLUDES}

LD_SWITCHES = ${LDFLAGS} ${TCL_LD_FLAGS} ${XLDFLAGS} ${XCFLAGS} ${CFLAGS}

# The ordering of the libraries is important.  Some X libs on SysV include
# "random" in a BSD module.  This would conflict with the one in the TclX
# library if it was brought in.

LIBS = ${TKX_LD_SEARCH_FLAGS} ${TK_LIB} ${TK_XLIBSW} @SHARED_LIBS@

ALL_LIBS = ${TKX_BUILD_LIB_SPEC} ${TK_LIB} ${TK_XLIBSW} \
	   ${TCLX_BUILD_LIB_SPEC} ${TCL_LIB} ${TK_LIBS} ${TCLX_LIBS} \
	   ${XLDLIBS} ${TKX_CC_SEARCH_FLAGS}

STATIC_LIBS = ${TKX_STLIB_NAME} ${TK_UNIX_DIR}/${TK_STLIB_NAME} ${TK_XLIBSW} \
              ${TCLX_UNIX_SRC_DIR}/${TCLX_STLIB_NAME} \
	      ${TCL_UNIX_DIR}/${TCL_STLIB_NAME} \
              ${TK_LIBS} ${TCLX_LIBS} ${XLDLIBS} ${TKX_CC_SEARCH_FLAGS}

#------------------------------------------------------------------------------
# Source and target macros.
#
GENERIC_OBJS = @TKX_BUILD_STANDALONE@ tkXinit.o tkXshell.o

GENERIC_SOBJS = @TKX_BUILD_DUMMY_STANDALONE@ tkXinit@SHOBJ_SUFFIX@ \
		tkXshell@SHOBJ_SUFFIX@

OBJS = ${GENERIC_OBJS}

SOBJS = ${GENERIC_SOBJS}

RUNTIME_DIR = ${srcbasedir}/tk/runtime

# C source optionally built by tcl2c
STANDALONE_SRCS = tkx.c

STANDALONE_OBJS = tkx.o

LIBTCLX = ../../tcl/unix/${TCLX_STLIB_NAME}
TCL = ../../tcl/unix/tcl

HELP_DIR = ${srcbasedir}/tk/help

TEST_OBJS = tkTest.o tkSquare.o @TCL_THREAD_TEST@ tkXunixTest.o

TEST_SAOBJS = tkTest.o tkSquare.o @TCL_THREAD_TEST@ tkXunixTestSA.o

#------------------------------------------------------------------------------
# Dependencies for generating the libraries and linking the executable.
# If a link fails, purge the executable, as some systems leave invalid
# executables around.
#
all: binaries libraries doc

binaries: wishx tktest

libraries: RUNTIME

doc:

static: wishx.static tktest.static

standalone: wishx.standalone tktest.standalone hello

wishx: tkXAppInit.o @TKX_SHLIB_BUILD_REF@ @TKX_STLIB_BUILD_REF@ ${TCL} Makefile
	${CC} ${LD_SWITCHES} tkXAppInit.o ${ALL_LIBS} \
	    -o $@ || (rm -f $@; exit 1)

wishx.static: tkXAppInit.o ${TKX_STLIB_NAME} ${LIBTCLX} Makefile
	${CC} ${LD_SWITCHES} tkXAppInit.o ${STATIC_LIBS} -o $@ || \
	    (rm -f $@; exit 1)

wishx.standalone: tkXAppInitSA.o ${TKX_STLIB_NAME} ${LIBTCLX} Makefile
	${CC} ${LD_SWITCHES} tkXAppInitSA.o ${STATIC_LIBS} -o $@ || \
	    (rm -f $@; exit 1)

${TKX_STLIB_NAME}: ${OBJS}
	${AR} cr ${TKX_STLIB_NAME} ${OBJS}
	${RANLIB} ${TKX_STLIB_NAME}

@TKX_SHLIB_BUILD_TARGET@: ${SOBJS}
	rm -f ${TKX_SHLIB_NAME}
	${TCL_SHLIB_LD} -o ${TKX_SHLIB_NAME} ${SOBJS} \
	    ${TK_BUILD_LIB_SPEC} ${TCLX_BUILD_LIB_SPEC} \
	    ${TCL_BUILD_LIB_SPEC} ${TCL_SHLIB_LD_LIBS}

hello:	hello.c
	${CC} ${LD_SWITCHES} -o $@ hello.c ${STATIC_LIBS}

#------------------------------------------------------------------------------
# We need the "help" directory here so that the help command will work before
# installation. Normally just symlink, unless we don't have them.
#
RUNTIME: tkx.tcl tclhelp help.tmp

tkx.tcl: ${RUNTIME_DIR}/tkx.tcl
	rm -f tkx.tcl
	cp ${RUNTIME_DIR}/tkx.tcl tkx.tcl

tclhelp: ${RUNTIME_DIR}/tclhelp.tcl
	rm -f tclhelp
	echo Creating tclhelp script
	@echo "#!/bin/sh"       			 >tclhelp
	@echo "# The next line restarts using wishx \\"	>>tclhelp
	@echo 'exec wishx $$0 $${1+"$$@"}'		>>tclhelp
	@echo ""					>>tclhelp
	@echo "package require Tk ${TK_VERSION}"	>>tclhelp
	@echo "package require Tclx ${TCLX_VERSION}"	>>tclhelp
	@echo ""					>>tclhelp
	@cat ${RUNTIME_DIR}/tclhelp.tcl 		>>tclhelp
	@chmod a+rx,a-w tclhelp

help.tmp:
	if [ "@HAVE_SYML@" = "YES" ]; then \
	    rm -rf help ;\
	    ln -s ${HELP_DIR} help ;\
	else \
	    rm -rf help ;\
	    (cd ${HELP_DIR}; tar -cf - help) | tar -xf - ;\
	fi
	touch help.tmp

#------------------------------------------------------------------------------
# Generate the test program.
#

tktest: ${TEST_OBJS} @TKX_SHLIB_BUILD_REF@ @TKX_STLIB_BUILD_REF@ ${TCL}
	${CC} ${LD_SWITCHES} ${TEST_OBJS} ${ALL_LIBS} \
	    -o $@ || (rm -f $@; exit 1)

tktest.static: ${TEST_OBJS} @TKX_STLIB_BUILD_REF@ ${TCL}
	${CC} ${LD_SWITCHES} ${TEST_OBJS} ${STATIC_LIBS} \
	    -o $@ || (rm -f $@; exit 1)

tktest.standalone: ${TEST_SAOBJS} @TKX_STLIB_BUILD_REF@ ${TCL}
	${CC} ${LD_SWITCHES} ${TEST_SAOBJS} ${STATIC_LIBS} \
	    -o $@ || (rm -f $@; exit 1)

tkTest.o: ${TK_SRC}/generic/tkTest.c
	${CC} -c ${CC_SWITCHES} ${TK_SRC}/generic/tkTest.c

tkSquare.o: ${TK_SRC}/generic/tkSquare.c
	${CC} -c ${CC_SWITCHES} ${TK_SRC}/generic/tkSquare.c

tkXtest.o: ${TKX_GENERIC_DIR}/tkXtest.c
	${CC} -c ${CC_SWITCHES} ${TKX_GENERIC_DIR}/tkXtest.c

tclThreadTest.o: ${TCL_SRC}/generic/tclThreadTest.c
	${CC} -c ${CC_SWITCHES} ${TCL_SRC}/generic/tclThreadTest.c

#------------------------------------------------------------------------------
# If test directory in not in build tree, copy it from the source tree.
#
${bldbasedir}/tk/tests:
	mkdir ${bldbasedir}/tk/tests
	cp ${srcbasedir}/tk/tests/*.test ${bldbasedir}/tk/tests

#------------------------------------------------------------------------------
# Run the UCB tests with wishx.

test: all
	echo "cd ${TK_SRC}/tests; source all.tcl; exit" | \
	    ./runtest -geometry +0+0

test.static: all
	echo "cd ${TK_SRC}/tests; source all.tcl; exit" | \
	    ./runtest.static -geometry +0+0

test.verbose: all
	echo "cd ${TK_SRC}/tests; set VERBOSE 1; source all.tcl; exit" | \
	    ./runtest -geometry +0+0

#------------------------------------------------------------------------------
# Build help for Tk.  These files are normally part of the distribution and
# not rebuilt by users.
#
buildhelp:
	rm -rf ${HELP_DIR} help help.tmp
	mkdir ${HELP_DIR}
	${BLDMANHELP} ${TK_SRC}/doc ${srcbasedir}/unix/tools/tkmanpages \
	    ${HELP_DIR} Tk.brf

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

install: install-binaries install-libraries install-doc

install-binaries: install-exec

install-libraries:  install-runtime install-misc

install-doc:

install-misc:
	${INSTCOPY} ${TKX_UNIX_SRC_DIR}/tkXAppInit.c \
	    ${INSTALL_ROOT}${TKX_INST_RUNTIME}
	${INSTCOPY} ${HELP_DIR} ${INSTALL_ROOT}${TKX_INST_RUNTIME}/help

install-runtime:
	${INSTCOPY} tkx.tcl ${INSTALL_ROOT}${TKX_INST_RUNTIME}

install-exec:
	${INSTCOPY} wishx ${INSTALL_ROOT}${TKX_INST_BIN}
	${STRIP} ${INSTALL_ROOT}${TKX_INST_BIN}/wishx
	if test "X@TKX_STLIB_BUILD_REF@" != "X"; then \
	    ${INSTCOPY} ${TKX_STLIB_NAME} ${INSTALL_ROOT}${TKX_INST_LIB}; \
	    ${RANLIB} ${INSTALL_ROOT}${TKX_INST_LIB}/${TKX_STLIB_NAME}; \
	fi
	${INSTCOPY} tkxConfig.sh ${INSTALL_ROOT}${TKX_INST_LIB}
	if test "@SHARED_BUILD@" = "1"; then \
	    ${INSTCOPY} ${TKX_SHLIB_NAME} ${INSTALL_ROOT}${TKX_INST_LIB} ;\
	    ${INSTCOPY} pkgIndex.tcl ${INSTALL_ROOT}${TKX_EXEC_RUNTIME} ;\
	fi
	${INSTCOPY} tclhelp ${INSTALL_ROOT}${TKX_INST_BIN}

#------------------------------------------------------------------------------
config:
	(cd ../../unix && configure)

#------------------------------------------------------------------------------
clean:
	-rm -f tkXAppInit.o ${OBJS} ${SOBJS} ${TEST_OBJS} ${TEST_SAOBJS}
	-rm -f wishx wishx.static wishx.standalone
	-rm -f tktest tktest.static tktest.standalone tkXAppInitSA.o
	-rm -f ${TKX_STLIB_NAME} ${TKX_SHLIB_NAME} 
	-rm -f ${STANDALONE_SRCS} ${STANDALONE_OBJS} tkXAppInitSA.c
	-rm -rf tkx.tcl tclhelp help help.tmp

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

distclean: clean
	-rm -f Makefile tkxConfig.sh pkgIndex.tcl
	-rm -f runtest runtest.static runtest.standalone

# Disable Sun's parallel make, it doesn't get the dependencies right.
.NO_PARALLEL:


#==============================================================================
# Repetitive rules below:

#------------------------------------------------------------------------------
# Dependencies for generating objects.
#
tkXinit.o: ${TKX_GENERIC_DIR}/tkXinit.c Makefile
	${CC} -c ${CC_SWITCHES} -DTKX_LIBRARY=\"${TKX_LIBRARY_PATH}\" \
	  ${TKX_GENERIC_DIR}/tkXinit.c

tkXshell.o: ${TKX_GENERIC_DIR}/tkXshell.c
	${CC} -c ${CC_SWITCHES} ${TKX_GENERIC_DIR}/tkXshell.c

tkXAppInit.o: ${TKX_UNIX_SRC_DIR}/tkXAppInit.c
	${CC} -c ${CC_SWITCHES} ${TKX_UNIX_SRC_DIR}/tkXAppInit.c

tkXinitSA.o: ${TKX_GENERIC_DIR}/tkXinitSA.c ${STANDALONE_SRCS}
	${CC} -c ${CC_SWITCHES} ${TKX_GENERIC_DIR}/tkXinitSA.c

tkXinitDSA.o: ${TKX_GENERIC_DIR}/tkXinitDSA.c Makefile
	${CC} -c ${CC_SWITCHES} ${TKX_GENERIC_DIR}/tkXinitDSA.c

tkXAppInitSA.c: ${TKX_UNIX_SRC_DIR}/tkXAppInit.c Makefile
	rm -f tkXinitSA.c
	sed -e s/Tclx_Init/Tclx_InitStandAlone/ \
	    -e s/Tcl_Init/Tcl_InitStandAlone/ \
	    -e s/Tkx_Init/Tkx_InitStandAlone/ \
	    -e s/Tk_Init/Tk_InitStandAlone/ \
	    ${TKX_UNIX_SRC_DIR}/tkXAppInit.c >tkXAppInitSA.c

tkXunixTest.o: ${TKX_UNIX_SRC_DIR}/tkXunixTest.c
	${CC} -c ${CC_SWITCHES} ${TKX_UNIX_SRC_DIR}/tkXunixTest.c

tkXunixTestDA.o: ${TKX_UNIX_SRC_DIR}/tkXunixTestSA.c
	${CC} -c ${CC_SWITCHES} ${TKX_UNIX_SRC_DIR}/tkXunixTestSA.c

tkXunixTestSA.c: ${TKX_UNIX_SRC_DIR}/tkXunixTest.c Makefile
	rm -f tkXinitSA.c
	sed -e s/Tclx_Init/Tclx_InitStandAlone/ \
	    -e s/Tcl_Init/Tcl_InitStandAlone/ \
	    -e s/Tkx_Init/Tkx_InitStandAlone/ \
	    -e s/Tk_Init/Tk_InitStandAlone/ \
	    ${TKX_UNIX_SRC_DIR}/tkXunixTest.c >tkXunixTestSA.c


#------------------------------------------------------------------------------
# Dependencies for generating objects (position independant versions).
#
tkXinit..o: ${TKX_GENERIC_DIR}/tkXinit.c Makefile
	@OBJ_SAVE@
	${CC} -c ${CC_SHOBJ_FLAG} ${CC_SWITCHES} ${TCL_SHLIB_CFLAGS} \
	    -DTKX_LIBRARY=\"${TKX_LIBRARY_PATH}\" ${TKX_GENERIC_DIR}/tkXinit.c
	@OBJ_RESTORE@

tkXinitDSA..o: ${TKX_GENERIC_DIR}/tkXinitDSA.c Makefile
	@OBJ_SAVE@
	${CC} -c ${CC_SHOBJ_FLAG} ${CC_SWITCHES} ${TCL_SHLIB_CFLAGS} \
	    ${TKX_GENERIC_DIR}/tkXinitDSA.c
	@OBJ_RESTORE@

tkXshell..o: ${TKX_GENERIC_DIR}/tkXshell.c
	@OBJ_SAVE@
	${CC} -c ${CC_SHOBJ_FLAG} ${CC_SWITCHES} ${TCL_SHLIB_CFLAGS} \
	    ${TKX_GENERIC_DIR}/tkXshell.c
	@OBJ_RESTORE@

#------------------------------------------------------------------------------
# Rules to build standalone .c files using tcl2c.  Explicit rules are required,
# as not all makes can handle cross-directory rules.

tkx.c: ${RUNTIME_DIR}/tkx.tcl
	${TCL2C} -o $@ -a -n $@ $<
hello.c: ${TKX_GENERIC_DIR}/hello.tcl
	${TCL2C} -o $@ -tkx $<
.c.o:
	${CC} -c ${CC_SWITCHES} $<
