# This is the makefile for the BDB binding example.
# You may need to edit some of its parts for better results.

GCC = gcc
GHC = ghc
HSFFIG = ../hsffig
SPLITTER = ../splitter

# Path to the Berkeley DB installation:

BDB = /usr/local/BerkeleyDB.4.2

# The command line to invoke hsc2hs:

HSC2HS = hsc2hs -t ../template-hsffig.h

all: util bftest bdbtest

util:
	cd .. ; make all

db.h:
	echo "#include" \"$(BDB)/include/db.h\" > db.h
	echo "#include" "<sys/stat.h>" >> db.h

DB_H.hs: DB_H.hsc
	$(HSC2HS) DB_H.hsc -o DB_H.hs_unsplit
	$(SPLITTER) DB_H.hs_unsplit

DB_H.hsc: db.h 
	$(GCC) -E -dD db.h | $(HSFFIG) > DB_H.hsc

BF_H.hsc: bf.h
	$(GCC) -E -dD bf.h | $(HSFFIG) > BF_H.hsc

BF_H.hs: BF_H.hsc
	$(HSC2HS) BF_H.hsc -o BF_H.hs

UNISTD_H.hsc: /usr/include/unistd.h
	$(GCC) -E -dD /usr/include/unistd.h | $(HSFFIG) > UNISTD_H.hsc

UNISTD_H.hs: UNISTD_H.hsc
	$(HSC2HS) UNISTD_H.hsc -o UNISTD_H.hs_unsplit
	$(SPLITTER) UNISTD_H.hs_unsplit

bdbtest: bdbtest.hs DB_H.hs
	$(GHC) -ffi --make bdbtest.hs $(BDB)/lib/libdb.a -o bdbtest

bftest: bftest.hs BF_H.hs
	$(GHC) -c BF_H_hsc.c
	$(GHC) -ffi -fglasgow-exts --make bftest.hs BF_H_hsc.o -o bftest

syscall: syscall.hs UNISTD_H.hs
	$(GHC) -ffi -fglasgow-exts --make syscall.hs -o syscall

clean:
	rm -f bdbtest syscall bftest db.h DB_H* BF_H* *.o *.hi access.db

