#
# 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 8.43 2000/08/10 02:21:48 wart Exp $
#------------------------------------------------------------------------------
#

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

# LD_LIBRARY_FLAGS may embed the LIB_RUNTIME_DIR macro
libdir			= @libdir@
LIB_RUNTIME_DIR		= $(libdir)

#------------------------------------------------------------------------------
# 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} \
	      ${TK_CC_SEARCH_FLAGS} ${TCL_CC_SEARCH_FLAGS} \
	      ${XLDFLAGS} ${XCFLAGS} ${CFLAGS}

ALL_LIBS = ${TKX_BUILD_LIB_SPEC} ${TK_BUILD_LIB_SPEC} \
	   ${TCLX_BUILD_LIB_SPEC} ${TCL_BUILD_LIB_SPEC} \
	   ${XLDLIBS} ${TK_LIBS} ${TCLX_LIBS}

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

#------------------------------------------------------------------------------
# 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 = @HELP_DIR@/tk

TEST_OBJS = tkTest.o tkSquare.o tclThreadTest.o tkXunixTest.o

TEST_SAOBJS = tkTest.o tkSquare.o tclThreadTest.o 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_STUB_LIB_SPEC} ${TCLX_BUILD_LIB_SPEC} \
	    ${TCL_BUILD_STUB_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_DIR}/generic/tkTest.c
	${CC} -c ${CC_SWITCHES} ${TK_SRC_DIR}/generic/tkTest.c

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

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

tclThreadTest.o: ${TCL_SRC_DIR}/generic/tclThreadTest.c
	${CC} -c ${CC_SWITCHES} ${TCL_SRC_DIR}/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
	dir=`pwd` && cd ${TK_SRC_DIR}/tests && $${dir}/runtest -geometry +0+0 all.tcl
test.static: all
	dir=`pwd` && cd ${TK_SRC_DIR}/tests && $${dir}/runtest.static -geometry +0+0 all.tcl

#------------------------------------------------------------------------------
# Build help for Tk.  These files are normally part of the distribution and
# not rebuilt by users.
#
BUILDHELP=@BUILDHELP@
buildhelp:
	rm -rf ${HELP_DIR} help help.tmp
	if test ! -d @HELP_DIR@ ; then \
	    mkdir -p @HELP_DIR@ ;\
	fi
	mkdir -p ${HELP_DIR}
	${BLDMANHELP} ${TK_SRC_DIR}/doc ${TCLX_TOOLS_SRC_DIR}/tkmanpages \
	    ${HELP_DIR} Tk.brf
	${INSTCOPY} ${HELP_DIR} ${INSTALL_ROOT}${TKX_INST_RUNTIME}/help

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

install: install-binaries install-libraries install-doc

install-binaries: install-exec

install-libraries:  install-runtime install-misc

install-doc:

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

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} $<
