# Programmer: Leonidas Fegaras
# The database driver must be mysql, sqlite, or nodb
#   for using MySQL through ODBC, Sqlite, or no database at all
# set base3 to true for base-3 ghc (old ghc)
# set extras to -rtsopts for GHC > 7.0

driver = mysql
base3 = false
extras = -rtsopts


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

ifeq (${base3},true)
args=${argss} -package base-3.0.3.1 -cpp -D_BASE_3_
else
args=${argss} -fspec-constr-count=100 ${extras}
endif

parser = src/Text/XML/HXQ/Parser.hs
ghc = ghc -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) xquery.hs
	$(ghc) --make xquery.hs -o xquery

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

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

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

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

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

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

# validate dblp.xml against dblp.xsd
validate: $(parser) tests/Validate.hs
	$(ghc) --make tests/Validate.hs -o a.out
	time ./a.out +RTS -H2m -s

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

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

xsym:   $(parser) xsym10.hs
	$(ghc) --make xsym10.hs -o a.out
	time ./a.out +RTS -K20m

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

# heap profiling of the compiler
profile: $(parser) tests/Test2.hs
	$(ghc) -isrc -isrc/hxml-0.2 -isrc/noDB --make tests/Test2.hs -o a.out
	$(ghc) -isrc -isrc/hxml-0.2 -isrc/noDB -prof -auto-all -osuf p_o --make tests/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 --user
	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
