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

PREFIX = /ExtraUtils/built/frantk

LIBDIR = $(PREFIX)
IMPORTS = $(PREFIX)/imports

TCLROOT = /ExtraUtils/Tcl

TCLLIBDIR= $(TCLROOT)/bin

# the library files to use - change for different version
TCLLIBS = -ltk80 -ltcl80


# 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


# Location of ghc package configuration file. This
# will be in the directory in which ghc was installed
PKGFILE = c:/Program\ Files/Glasgow\ Haskell\ Compiler/ghc-5.02/package.conf

#######################################################################
#
# 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.nt )
#
#######################################################################

CC = ghc
CC_OPTS = -O 
CC_INC = -I$(TCLROOT)/include -I$(TCLROOT)/include/X11

GHC = ghc
GHC-PKG = ghc-pkg

#######################################################################
#
# 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)

HSFRANTKOBJS = 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 

HSFRPOBJS=FRPSrc/FranCore.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

HSTCLOBJS=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 

HSOBJS = $(HSFRANTKOBJS) $(HSFRPOBJS) $(HSTCLOBJS)

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
#
#######################################################################

all : frp tcl frantk cbits


# build the frp library for ghc & ghci
frp: libHSFRP.a HSFRP.o

libHSFRP.a: $(SPECIAL_OBJS) FranTkSrc/FranTk.o
	ar -r libHSFRP.a $(HSFRPOBJS)
	ranlib libHSFRP.a

HSFRP.o: libHSFRP.a
	ld -r --whole-archive -o HSFRP.o libHSFRP.a


# build the tclhaskell library for ghc & ghci
tcl: libHSTcl.a HSTcl.o

libHSTcl.a: $(SPECIAL_OBJS) FranTkSrc/FranTk.o
	ar -r libHSTcl.a $(HSTCLOBJS)
	ranlib libHSTcl.a

HSTcl.o:
	ld -r --whole-archive -o HSTcl.o libHSTcl.a

# build the frantk library for ghc & ghci
frantk: libHSFranTk.a HSFranTk.o

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

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

# build the cbits library containing tclhaskell.o in a dll
# ghci on windows does not appear to be able to deal with 
# connecting statically to this c library
cbits: FranTk_cbits.dll

FranTk_cbits.dll: TclHaskellSrc/tclhaskell.o
	dllwrap -mno-cygwin --target=i386-mingw32 --output-lib libFranTk_cbits.a --export-all --output-def TclHaskellSrc/FranTk_cbits.def -o FranTk_cbits.dll TclHaskellSrc/tclhaskell.o -L$(TCLLIBDIR) $(TCLLIBS)


LIBS = libHSFRP.a libHSTcl.a libHSFranTk.a HSFRP.o HSTcl.o HSFranTk.o FranTk_cbits.dll libFranTK_cbits.a 

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

pkg : 
	$(RM) $(PKGFILE).old
	$(GHC-PKG) -a < frantk.conf.nt

unpkg:
	$(RM) $(PKGFILE).old
	$(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) 




#######################################################################
#
# 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
#
#######################################################################

hugs : TclHaskellSrc/TclPrim.dll 

TclHaskellSrc/TclPrim.dll: TclHaskellSrc/TclPrim.c TclHaskellSrc/tclhaskell.o TclHaskellSrc/tclhaskell.h
	$(CC) -c $(CC_OPTS) $(CC_INC) TclHaskellSrc/TclPrim.c -o TclHaskellSrc/TclPrim.o 
	dllwrap -mno-cygwin --target=i386-mingw32 --output-lib TclHaskellSrc/libtclprim_imp.a --export-all --output-def TclHaskellSrc/tclprim.def -o TclHaskellSrc/TclPrim.dll TclHaskellSrc/TclPrim.o TclHaskellSrc/tclhaskell.o -L$(TCLLIBDIR) $(TCLLIBS)
	$(RM) TclHaskellSrc/TclPrim.o







