#
# Makefile for Tcl threads
#

.KEEP_STATE:

# Top-level directories in which to install binaries and include files.
#
exec_prefix =	/usr/local
prefix =	/usr/local

# Location of MT-sturdy tcl.h and libtcl respectively
# The run-path for mttclsh and mtwish are set from TCL_RUN_PATH; this should
# be the install directory of libtcl.so.7  If static linking is used,
# TCL_RUN_PATH should _not_ be set.
#
TCL_DIR = ../tcl
TCL_BIN_DIR = ../tcl
TCL_RUN_PATH = -R$(exec_prefix)/lib

# Location of tk.h and libtk respectively
#
TK_DIR = ../../../tk3.6
TK_BIN_DIR = ../../../tk3.6

# Top of X11 install tree
#
X_DIR = /usr/openwin

# Uncomment the following two lines to link mttclsh and mtwish statically
# with libtcl.  The executable and its memory footprint will be larger,
# but you won't have to worry about RUNPATH or LD_LIBRARY_PATH.  If
# static linking is used, TCL_RUN_PATH should _not_ be set.
#B_STATIC = -Bstatic
#B_DYNAMIC = -Bdynamic

# --- Should not need to modify anything below this line

TCL_SO_VERSION=7
SO_VERSION=1

BIN_DIR = $(exec_prefix)/bin
LIB_DIR = $(exec_prefix)/lib
H_DIR = $(prefix)/include

CINCL = -I$(TCL_DIR) -I$(TK_DIR) -I$(X_DIR)/include
CDEBUGFLAGS = -g
MT_FLAGS = -D_REENTRANT
MT_LIBS = -lthread
CFLAGS = $(MTFLAGS) $(CDEBUGFLAGS) $(CINCL)
CLIBS.so = ../tcl/libtcl.so.$(TCL_SO_VERSION) -lthread

LDFLAGS = $(TCL_RUN_PATH)

TCLSH_LIBS = -L$(TCL_BIN_DIR) $(B_STATIC) -ltcl $(B_DYNAMIC) -lm $(MT_LIBS)
WISH_LIBS = -L$(TK_BIN_DIR) -L$(TCL_BIN_DIR) -L$(X_DIR) -ltk $(B_STATIC) -ltcl $(B_DYNAMIC) -lX11 -lsocket -lnsl -lm $(MT_LIBS)

all: libtcl_thread.a libtcl_thread.so.$(SO_VERSION) libtcl_thread.sa \
	libtk_thread.a libtk_thread.so.$(SO_VERSION) libtk_thread.sa \
	mttclsh mtwish

libtcl_thread.a: tcl_thread.o
	rm -f $@
	$(AR) cq $@ tcl_thread.o

libtcl_thread.so.$(SO_VERSION): tcl_thread-pic.o
	$(CC) -G -o $@ tcl_thread-pic.o $(CLIBS.so)

libtcl_thread.sa: tcl_thread-pic.o
	$(LD) -r -o $@ tcl_thread-pic.o

libtk_thread.a: tcl_thread.o tk_thread.o
	rm -f $@
	$(AR) cq $@ tcl_thread.o tk_thread.o

libtk_thread.so.$(SO_VERSION): tcl_thread-pic.o tk_thread-pic.o
	$(CC) -G -o $@ tcl_thread-pic.o tk_thread-pic.o $(CLIBS.so)

libtk_thread.sa: tcl_thread-pic.o tk_thread-pic.o
	$(LD) -r -o $@ tcl_thread-pic.o tk_thread-pic.o

mttclsh: mttclsh.o tcl_thread.o
	$(CC) $(LDFLAGS) mttclsh.o tcl_thread.o -o mttclsh $(TCLSH_LIBS)

mtwish: mtwish.o tcl_thread.o tk_thread.o
	$(CC) $(LDFLAGS) mtwish.o tk_thread.o tcl_thread.o -o mtwish $(WISH_LIBS)
 
clean:
	rm -f libt*.s* *.a mttclsh mtwish *.o core

install:
	@for i in $(LIB_DIR) $(BIN_DIR) ; \
	    do \
	    if [ ! -d $$i ] ; then \
		echo "Making directory $$i"; \
		mkdir $$i; \
		chmod 755 $$i; \
		else true; \
		fi; \
	    done;
	@rm -f $(BIN_DIR)/mttclsh $(BIN_DIR)/mtwish
	cp mttclsh $(BIN_DIR)
	cp mtwish $(BIN_DIR)
	@rm -f $(LIB_DIR)/libtcl_thread.so.$(SO_VERSION)
	cp libtcl_thread.so.$(SO_VERSION) $(LIB_DIR)
	@cd $(LIB_DIR) ; \
	rm -f libtcl_thread.so ; \
	ln -s libtcl_thread.so.$(SO_VERSION) libtcl_thread.so
	@rm -f $(LIB_DIR)/libtcl_thread.sa
	cp libtcl_thread.sa $(LIB_DIR)
	@rm -f $(LIB_DIR)/libtk_thread.so.$(SO_VERSION)
	cp libtk_thread.so.$(SO_VERSION) $(LIB_DIR)
	@cd $(LIB_DIR) ; \
	rm -f libtk_thread.so ; \
	ln -s libtk_thread.so.$(SO_VERSION) libtk_thread.so
	@rm -f $(LIB_DIR)/libtk_thread.sa
	cp libtk_thread.sa $(LIB_DIR)

tcl_thread-pic.o: tcl_thread.c
	$(CC) $(CFLAGS) -Kpic -o $@ -c tcl_thread.c

tk_thread-pic.o: tk_thread.c
	$(CC) $(CFLAGS) -Kpic -o $@ -c tk_thread.c
