#  Makefile for testing dynamic loading of C functions from Icon
#
#  "make" builds everything.
#  "make kr" builds the programs that don't require ANSI C.
#
#  If building with the compiler (instead of the interpreter)
#  use the "-fs" option to avoid problems.

FUNCLIB = libfunc.so
KRLIB = libkr.so

ICONT = icont
IFLAGS = -us
CC = cc
CFLAGS = -O -w

AFUNCS = argdump.o cspgen.o fpoll.o tconnect.o
KRFUNCS = bitcount.o ddump.o
FUNCS = $(AFUNCS) $(KRFUNCS)

APROGS = dldemo cspace tnet
KRPROGS = btest ddtest
PROGS = $(APROGS) $(KRPROGS)


default: all
all:	 $(PROGS)
kr:	 $(KRPROGS)


# header file
$(PROGS):  libnames.icn
libnames.icn: Makefile
	@echo 'Creating libnames.icn:'
	echo '# library names from Makefile'	 >libnames.icn
	echo '$$define FUNCLIB "./$(FUNCLIB)"'	>>libnames.icn
	echo '$$define KRLIB "./$(KRLIB)"'	>>libnames.icn


# full library
$(APROGS): $(FUNCLIB)
$(FUNCLIB): $(FUNCS) mklib.sh
	CC="$(CC)" CFLAGS="$(CFLAGS)" sh mklib.sh $(FUNCLIB) $(FUNCS)
$(FUNCS):  icall.h

# library of K&R functions
$(KRPROGS): $(KRLIB)
$(KRLIB): $(KRFUNCS) mklib.sh
	CC="$(CC)" CFLAGS="$(CFLAGS)" sh mklib.sh $(KRLIB) $(KRFUNCS)


# ANSI C programs
btest:	btest.icn	; $(ICONT) $(IFLAGS) btest
ddtest:	ddtest.icn	; $(ICONT) $(IFLAGS) ddtest

# K&R programs
cspace:	cspace.icn	; $(ICONT) $(IFLAGS) cspace
dldemo:	dldemo.icn	; $(ICONT) $(IFLAGS) dldemo
tnet:	tnet.icn	; $(ICONT) $(IFLAGS) tnet


# cleanup
clean Clean:
	-rm -f $(PROGS) $(FUNCLIB) $(KRLIB) *.o *.so libnames.icn
	-rm -f `grep -l '^/\*ICONC\*/$$' *.[ch] /dev/null` core so_locations
