# Programmer: Leonidas Fegaras
# The database driver must be mysql, sqlite, or nodb
#   for using MySQL through ODBC, Sqlite, or no database at all

driver = mysql

ifeq (${driver},mysql)
args = -isrc -isrc/hxml-0.2 -isrc/withDB -isrc/mysql
else ifeq (${driver},sqlite)
args = -isrc -isrc/hxml-0.2 -isrc/withDB -isrc/sqlite
else
args = -isrc -isrc/hxml-0.2 -isrc/noDB
endif

parser = src/Text/XML/HXQ/Parser.hs
ghc = ghc-6.8.3 -O2 -funfolding-use-threshold=16 ${args}
src = * src/hxml-0.2/* src/noDB/Text/XML/HXQ/* src/withDB/Text/XML/HXQ/* \
      src/* src/Text/XML/HXQ/* src/mysql/* src/sqlite/*

# build the xquery interpreter
all:    $(parser) Main.hs
	$(ghc) --make Main.hs -o xquery

# generate the XQuery parser using happy
$(parser): XQueryParser.y
	happy -g -a -c -o $(parser) XQueryParser.y

test1:  $(parser) Test1.hs
	$(ghc) --make Test1.hs -o a.out
	./a.out

# testing on a large file (dblp.xml)
test2:  $(parser) Test2.hs
	$(ghc) --make Test2.hs -o a.out
	time ./a.out +RTS -H2m

# like test2 but uses quasi-quotes (for ghc >= 6.9 only)
test2a: $(parser) Test2a.hs
	$(ghc) --make Test2a.hs -o a.out
	./a.out +RTS -H2m -s

test3:  $(parser) TestDB.hs
	$(ghc) --make TestDB.hs -o a.out
	./a.out

test4:  $(parser) TestDB2.hs
	$(ghc) --make TestDB2.hs -o a.out
	./a.out

xmark:  $(parser) TestXMark.hs
	$(ghc) --make TestXMark.hs -o a.out
	time ./a.out +RTS -H2m -s

# populate the DBLP database using the dblp.xml file
dblp:   $(parser) TestDBLP.hs
	$(ghc) --make TestDBLP.hs -o a.out
	time ./a.out +RTS -K20m

# run in the ghci interpreter and load HXQ
ghci:   $(parser)
	ghci -XTemplateHaskell ${args} Main.hs

# run multiple tests
test:   $(parser) data/test.xq
	./xquery data/test.xq | diff - data/test-results.txt

# heap profiling of the compiler
profile: $(parser) Test2.hs
	ghc -O2 -funfolding-use-threshold=16 -isrc -isrc/hxml-0.2 -isrc/noDB \
                --make Test2.hs -o a.out
	ghc -O2 -funfolding-use-threshold=16 -isrc -isrc/hxml-0.2 -isrc/noDB \
                -prof -auto-all -osuf p_o --make Test2.hs -o a.out
	time ./a.out +RTS -hc
	hp2ps -color a.out.hp

# create the cabal distribution
cabal:	$(parser)
	runhaskell Setup.lhs configure
	runhaskell Setup.lhs build
	runhaskell Setup.lhs haddock
	runhaskell Setup.lhs sdist

clean:
	/bin/rm -f xquery Temp.hs a.out $(addsuffix .hi,$(src)) \
                   $(addsuffix .o,$(src)) $(addsuffix .p_o,$(src))

distclean: clean
	runhaskell Setup.lhs clean
