#######################################################################
#
# adjust paths here
# and in frantk.conf
#
#######################################################################

PREFIX = /local/www.people/personal/meurig/haskell/built/frantk

# where to install libHSFranTk.a library and friends
LIBDIR = $(PREFIX)

# where to install the FranTk interface files
IMPORTS = $(PREFIX)/imports


# the library files to use - change for different version
TCLLIBS = -ltk8.3 -ltcl8.3

# Ununcomment this line to support concurrency. 
# This support does NOT work with ghci. It does not currently export 
# the special run_head_queue variable. Will allegedly 
# be fixed in a future version of ghc.

# this version supports concurrency
SUPPORTCONCURRENCY = TclCompatibilityGhcSupportsConcurrency.hs

# this version does not
#SUPPORTCONCURRENCY = TclCompatibilityGhc.hs


#######################################################################
#
# the C compiler must find the tcl/tk include files
#
# ( and the linker must find the tcl/tk libs
#   these paths are in frantk.conf )
#
#######################################################################

CC = gcc
CC_OPTS = -O
CC_INC =  -I/usr/X11/include -I/usr/local/include

GHC = ghc-5.00.2
GHC-PKG=ghc-pkg-5.00.2

#######################################################################
#
# do not modify
#
#######################################################################

SPECIAL_OBJS = TclHaskellSrc/tclhaskell.o \
    TclHaskellSrc/TclPrim.o TclHaskellSrc/TclCompatibility.o \
    FRPSrc/StaticTypes/Compatibility.o \
    FranTkSrc/FM.o

SRCS = $(wildcard */*.*hs */*/*.*hs)
HSOBJS = FranTkSrc/AbstractInterface.o FranTkSrc/BVar.o FranTkSrc/BVars.o FranTkSrc/CItem.o FranTkSrc/CollectionB.o FranTkSrc/CollectionBVar.o FranTkSrc/Component.o FranTkSrc/ComponentWidgets.o FranTkSrc/Conf.o FranTkSrc/DisplayCItem.o FranTkSrc/DisplayPItem.o FranTkSrc/DisplaySimple.o FranTkSrc/DisplayWidget.o FranTkSrc/FM.o FranTkSrc/FranTkConc.o FranTkSrc/FranTk.o FranTkSrc/FRPWrap.o FranTkSrc/GFRP.o FranTkSrc/Group.o FranTkSrc/GUIDef.o FranTkSrc/GUI.o FranTkSrc/HasInput.o FranTkSrc/Identify.o FranTkSrc/ListB.o FranTkSrc/MapG.o FranTkSrc/MItem.o FranTkSrc/PileArray.o FranTkSrc/PrimWidget.o FranTkSrc/RectInfo.o FranTkSrc/SetB.o FranTkSrc/TreeIO.o FranTkSrc/WH.o FranTkSrc/WidgetConf.o FranTkSrc/WidgetExports.o FranTkSrc/Widget.o FranTkSrc/WidgetSetImpl.o FranTkSrc/WidgetSet.o FranTkSrc/Widgets.o FRPSrc/FranCore.o TclHaskellSrc/ConcTcl.o TclHaskellSrc/ExtArray.o TclHaskellSrc/PlacePos.o TclHaskellSrc/Remover.o TclHaskellSrc/TclCompatibility.o TclHaskellSrc/TclGUI.o TclHaskellSrc/TclPrimCore.o TclHaskellSrc/TclPrimImpl.o TclHaskellSrc/TclPrim.o TclHaskellSrc/TclPrimWidgets.o TclHaskellSrc/TclTime.o TclHaskellSrc/TclTrie.o TclHaskellSrc/TrieIO.o TclHaskellSrc/Utils.o FRPSrc/BehaviorTypes/BehaviorTypes.o FRPSrc/BehaviorTypes/ColorCoreB.o FRPSrc/BehaviorTypes/Point2B.o FRPSrc/BehaviorTypes/Point3B.o FRPSrc/BehaviorTypes/RectB.o FRPSrc/BehaviorTypes/Transform2B.o FRPSrc/BehaviorTypes/Transform3B.o FRPSrc/BehaviorTypes/Vector2B.o FRPSrc/BehaviorTypes/Vector3B.o FRPSrc/BehaviorTypes/VectorSpaceB.o FRPSrc/FRPImpl/BehaviorIO.o FRPSrc/FRPImpl/Behavior.o FRPSrc/FRPImpl/BPrim.o FRPSrc/FRPImpl/Delayed.o FRPSrc/FRPImpl/Event.o FRPSrc/FRPImpl/EventPrim.o FRPSrc/FRPImpl/FranPrim.o FRPSrc/FRPImpl/FranTime.o FRPSrc/FRPImpl/FRPImpl.o FRPSrc/FRPImpl/Listener.o FRPSrc/FRPImpl/ListenerOps.o FRPSrc/FRPImpl/ListenerPrim.o FRPSrc/FRPImpl/MutSet.o FRPSrc/FRPImpl/TermE.o FRPSrc/FRPImpl/WirePrim.o FRPSrc/FRPImpl/WorkPool.o FRPSrc/StaticTypes/BaseTypes.o FRPSrc/StaticTypes/ColorCore.o FRPSrc/StaticTypes/Compatibility.o FRPSrc/StaticTypes/Force.o FRPSrc/StaticTypes/Point2.o FRPSrc/StaticTypes/Point3.o FRPSrc/StaticTypes/Rect.o FRPSrc/StaticTypes/StaticTypes.o FRPSrc/StaticTypes/Transform2.o FRPSrc/StaticTypes/Transform3.o FRPSrc/StaticTypes/Vector2.o FRPSrc/StaticTypes/Vector3.o FRPSrc/StaticTypes/VectorSpace.o
OBJS = $(HSOBJS) TclHaskellSrc/tclhaskell.o
HIS  = $(HSOBJS:.o=.hi)

GHC_OPTS = -fvia-C -fglasgow-exts \
     -fallow-overlapping-instances -fallow-undecidable-instances \
     -package concurrent -package data \
     -package-name FranTk 

GHC_INC = -iTclHaskellSrc \
   -iFRPSrc -iFRPSrc/StaticTypes -iFRPSrc/BehaviorTypes -iFRPSrc/FRPImpl \
   -iFranTkSrc

#######################################################################
#
# make targets
#
#######################################################################

LIBS = libHSFranTk.a HSFranTk.o


all : $(LIBS)


install : libHSFranTk.a
	mkdir -p $(LIBDIR)
	mkdir -p $(IMPORTS)
	cp -p $(LIBS) $(LIBDIR)/
	cp -p $(HIS) $(IMPORTS)/

pkg : 
	$(GHC-PKG) -a < frantk.conf

unpkg:
	$(GHC-PKG) -r FranTk

clean : 
	rm -f $(OBJS) $(HIS) *.a *.o TclHaskellSrc/*.o TclHaskellSrc/*.so



FranTkSrc/FranTk.o :  FranTkSrc/FranTk.lhs
	$(GHC) --make FranTkSrc/FranTk.lhs $(GHC_OPTS) $(GHC_INC) 


libHSFranTk.a : $(SPECIAL_OBJS) FranTkSrc/FranTk.o
	ar -r $@ $(OBJS)
	ranlib $@

HSFranTk.o : libHSFranTk.a
	ld -r --whole-archive -o HSFranTk.o libHSFranTk.a

#######################################################################
#
# rules for objects that must be built before  ghc --make  takes over
#
#######################################################################

TclHaskellSrc/tclhaskell.o: TclHaskellSrc/tclhaskell.c TclHaskellSrc/tclhaskell.h
	$(CC) -c $(CC_OPTS) $(CC_INC) TclHaskellSrc/tclhaskell.c -o TclHaskellSrc/tclhaskell.o


TclHaskellSrc/TclPrim.o:  TclHaskellSrc/TclPrimGhc.hs TclHaskellSrc/tclhaskell.h
	$(GHC) $(GHC_OPTS) $(GHC_INC) -c  TclHaskellSrc/TclPrimGhc.hs -o $@ 

TclHaskellSrc/TclCompatibility.o:  TclHaskellSrc/$(SUPPORTCONCURRENCY) TclHaskellSrc/Check.h
	$(GHC) $(GHC_OPTS) $(GHC_INC) '-#include "TclHaskellSrc/Check.h"' -c  TclHaskellSrc//$(SUPPORTCONCURRENCY) -o $@ 


FRPSrc/StaticTypes/Compatibility.o : FRPSrc/StaticTypes/Compatibility.ghc.hs
	$(GHC)  $(GHC_OPTS)  $(GHC_INC) -c $<  -o $@

FranTkSrc/FM.o : FranTkSrc/FMGhc.hs
	$(GHC)  $(GHC_OPTS)  $(GHC_INC) -c $<  -o $@



#######################################################################
#
# Hugs Stuff
#
#######################################################################


# ld options for generating shared libraries for Hugs:

# SLD_OPTS   = -r		# (Solaris)
SLD_OPTS = -shared	# (OSF/1+Linux)


# The directories containing the X11, Tcl and Tk (binary) libraries:

CCLIB = -L /usr/lib/X11 -L /usr/local/lib


# the libraries required by Tcl/Tk:

#TCLREQ =  -ltk8.3 -ltcl8.3 -lX11 -lm -lsocket -lnsl -lintl -lw # (Solaris)
TCL_REQ  = -ltk8.3 -ltcl8.3 -lm -lc                   # (OSF/1+Linux)


hugs: TclHaskellSrc/TclPrim.so

TclHaskellSrc/TclPrim.so: TclHaskellSrc/TclPrim.c TclHaskellSrc/tclhaskell.o TclHaskellSrc/tclhaskell.h
	$(CC) -c $(CC_OPTS) $(CC_INC) -o TclHaskellSrc/TclPrim.o -c TclHaskellSrc/TclPrim.c
	$(LD) $(SLD_OPTS) \
		$(CCLIB) \
		-o TclHaskellSrc/TclPrim.so TclHaskellSrc/TclPrim.o TclHaskellSrc/tclhaskell.o \
			$(TCL_REQ)
	$(RM) TclHaskellSrc/TclPrim.o

