################################################################
# Makefile for SWI-Prolog 4.x
# $Id: Makefile.in,v 1.90 2001/04/09 15:03:48 jan Exp $
#
# Author:	Jan Wielemaker
#		jan@swi.psy.uva.nl
#		SWI
#		University of Amsterdam
#    		Roetersstraat 15
#		1018 WB  Amsterdam
#		The Netherlands
#
# Public targets:
# 
#	* make			Simply makes all programs in the current tree
#	* make install		Installs the libraries and public executables
#	* make install-version  Installs pl as pl-x.y.z in public directory
#	* make install-bins	Link executables from $(bindir)
#	* make install-arch	Install machine dependent files
#	* make install-libs	Install machine independent files
#	* make install-mt	Install multi-threaded version next to normal
#	* make check		Run some tests
#
# Copyright (C) University of Amsterdam, all rights reserved
# 
# Copyright policy:
#	
#	* GPL-2 (see file COPYING or http://www.gnu.org/)
#
#	* Licenses for use with proprietary software are available.
#	  See http://www.swi.pay.uva.nl/pub/SWI-Prolog/
#
################################################################

PLVERSION=@PLVERSION@

PL=@PL@@RT@
PLV=@PL@@RT@-@PLVERSION@
prefix=@prefix@
exec_prefix=@exec_prefix@
srcdir=@srcdir@
@VPATH@ @srcdir@
bindir=@bindir@
PLBASE=$(prefix)/lib/@PL@-$(PLVERSION)
# NOTE: Also defined in config.h!
BOOTFILE=@BOOTFILE@
man_prefix=@mandir@
mansec=1
lmansec=3
mandir=$(man_prefix)/man$(mansec)
lmandir=$(man_prefix)/man$(lmansec)
plmandir=$(PLBASE)/man

CC=@CC@
LIBS=@LIBS@
AWK=@AWK@
SED=sed
ETAGS=@ETAGS@
AR=@AR@
RANLIB=@RANLIB@
ARCH=@ARCH@
INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_DATA=@INSTALL_DATA@
MKDIR=@MKDIR@
REL_LN=$(srcdir)/rel-ln

COFLAGS=@COFLAGS@
CWFLAGS=@CWFLAGS@
CIFLAGS=@CIFLAGS@
CMFLAGS=@CMFLAGS@
CFLAGS=	$(CWFLAGS) $(COFLAGS) $(CIFLAGS) $(CMFLAGS)
LDFLAGS=@LDFLAGS@

PLFOREIGN=@PLFOREIGN@

DEFSTARTUP=.plrc

PB=../boot
INCLUDEDIR=../include
EXPORTS=$(INCLUDEDIR)/SWI-Exports
CINCLUDE=$(INCLUDEDIR)/SWI-Prolog.h
STREAMH=$(INCLUDEDIR)/SWI-Stream.h
STUBC=$(INCLUDEDIR)/stub.c
STARTUPPATH=$(PL).prc
RUNTIMEDIR=../runtime/$(ARCH)
PLLIB=libpl.a
RUNTIME=$(RUNTIMEDIR)/$(PLLIB)
LIBRARYDIR=${PLBASE}/library

OBJ=	pl-atom.o pl-wam.o pl-stream.o pl-error.o pl-arith.o pl-bag.o \
	pl-comp.o pl-rc.o pl-dwim.o pl-ext.o pl-file.o pl-flag.o \
	pl-fmt.o pl-funct.o pl-gc.o pl-glob.o pl-itf.o pl-list.o \
	pl-load.o pl-modul.o pl-op.o pl-os.o pl-prims.o pl-pro.o \
	pl-proc.o pl-prof.o pl-read.o pl-rec.o pl-rl.o pl-setup.o \
	pl-sys.o pl-table.o pl-trace.o pl-util.o pl-wic.o pl-write.o \
	pl-term.o pl-buffer.o pl-thread.o pl-xterm.o \
	pl-feature.o pl-ctype.o
XOBJ=	@ALLOCA@ @EXTRAOBJ@
RCOBJ=	rc/access.o rc/build.o rc/html.o rc/rc.o rc/util.o

DEPOBJ=	pl-main.o
EXT=	pl-extend.o
ALLOBJ= $(OBJ) $(DEPOBJ) $(EXT) $(XOBJ)

PLINIT=	$(PB)/init.pl

INCSRC=	pl-index.c pl-alloc.c pl-fli.c
SRC=	$(OBJ:.o=.c) $(DEPOBJ:.o=.c) $(EXT:.o=.c) $(INCSRC)
HDR=	config.h parms.h pl-buffer.h pl-ctype.h pl-incl.h pl-itf.h pl-main.h \
	pl-os.h pl-data.h

PLSRC=	../boot/syspred.pl ../boot/toplevel.pl \
	../boot/make.pl ../boot/sort.pl ../boot/bags.pl ../boot/apply.pl \
	../boot/list.pl ../boot/writef.pl ../boot/history.pl \
	../boot/profile.pl ../boot/dwim.pl ../boot/rc.pl \
	../boot/parms.pl ../boot/autoload.pl ../boot/qlf.pl \
	../boot/topvars.pl ../boot/messages.pl ../boot/load.pl
PLLIBS= MANUAL helpidx.pl help.pl explain.pl \
	qsave.pl shlib.pl statistics.pl system.pl \
	backcomp.pl gensym.pl listing.pl \
	bim.pl quintus.pl edinburgh.pl ctypes.pl files.pl \
	edit.pl emacs_interface.pl shell.pl check.pl \
	tty.pl readln.pl \
	am_match.pl oset.pl \
	netscape.pl url.pl \
	threadutil.pl
PLWIN=	wise.pl dde.pl progman.pl registry.pl

all:	banner \
	headers rc-lib system startup index runtime \
	plld \
	swipl swiplbin

.c.o:
	$(CC) -c -I. -I$(srcdir) -I$(srcdir)/rc $(CFLAGS) $< @COUTOPT@

system:		$(PL)
startup:	$(STARTUPPATH)
headers:	parms.h $(EXPORTS) $(CINCLUDE) $(STREAMH) $(STUBC)
runtime:	$(RUNTIME)

banner:
		@echo "****************"
		@echo "Making SWI-Prolog $(PLVERSION) for $(ARCH)"
		@echo "To be installed in $(bindir)/$(PL)"
		@echo "Home (libraries) in $(PLBASE)"
		@echo "****************"


$(PL):		$(ALLOBJ) rc/librc.a
		$(CC) $(LDFLAGS) -o $(PL) -Lrc $(ALLOBJ) -lrc $(LIBS)


$(STARTUPPATH):	$(PLINIT) $(PLSRC) $(PL)
		./$(PL) -O -o $(STARTUPPATH) -b $(PLINIT)


$(RUNTIME):	$(OBJ) $(RCOBJ) $(DEPOBJ) $(XOBJ)
		if [ ! -d ../runtime ]; then $(MKDIR) ../runtime; fi
		if [ ! -d $(RUNTIMEDIR) ]; then $(MKDIR) $(RUNTIMEDIR); fi
		rm -f $@
		$(AR) r $@ $(OBJ) $(RCOBJ) $(DEPOBJ) $(XOBJ)
		$(RANLIB) $@

rc-lib:
		@(cd rc && $(MAKE) COFLAGS="$(COFLAGS)")

index:
		./$(PL) -x $(STARTUPPATH) \
			-f none -F none \
			-g make_library_index\(\'../library\'\) \
			-t halt

$(INCLUDEDIR):
		if [ ! -d $@ ]; then $(MKDIR) $@; fi

$(EXPORTS):	Makefile $(srcdir)/pl-itf.h $(INCLUDEDIR)
		echo \#\!$(bindir)/$(PL) > $(EXPORTS)
		grep '^__pl_export' $(srcdir)/pl-itf.h $(srcdir)/pl-stream.h | \
		    $(SED) -e 's/.*[ 	*]\(_*PL_[a-zA-Z0-9_]*\).*/\1/' \
			   -e 's/.*[ 	*]\(S[a-zA-Z_]*\).*/\1/' | \
		    grep -v '_t$$' | grep -v '^PL_extension$$' | \
		    sort -u >> $(EXPORTS)

$(CINCLUDE):	$(srcdir)/pl-itf.h $(INCLUDEDIR)
		cp $(srcdir)/pl-itf.h $(CINCLUDE)

$(STREAMH):	$(srcdir)/pl-stream.h $(INCLUDEDIR)
		cp $(srcdir)/pl-stream.h $@

$(STUBC):	$(srcdir)/pl-extend.c $(INCLUDEDIR)
		$(SED) 's/"pl-itf.h"/<SWI-Prolog.h>/' $(srcdir)/pl-extend.c > $@

parms.h:	Makefile
		@echo "#define PLHOME       \"$(PLBASE)\""      > $@%
		@echo "#define DEFSTARTUP   \"$(DEFSTARTUP)\"" >> $@%
		@echo $(PLVERSION) | \
		$(AWK) -F. '{ printf("#define PLVERSION %d\n", $$1 * 10000 + $$2 * 100 + $$3); }' >> $@%
		@echo "#define ARCH	    \"$(ARCH)\""       >> $@%
		@echo "#define C_LIBS	    \"$(LIBS)\""       >> $@%
		@echo "#define C_CC	    \"$(CC)\""	       >> $@%
		@echo "#define C_LDFLAGS    \"$(LDFLAGS)\""    >> $@%
		@if cmp -s $@% $@; then rm $@%; else mv $@% $@; fi

pl-main.o:	parms.h
pl-setup.o:	parms.h
pl-wic.o:	parms.h
pl-funct.o:	$(srcdir)/pl-funct.ih
pl-atom.o:	$(srcdir)/pl-funct.ih
pl-wam.o:	pl-alloc.c pl-index.c pl-fli.c

$(srcdir)/pl-funct.ih:	$(srcdir)/ATOMS
		cd $(srcdir) ; $(AWK) -f defatom.awk < ATOMS

$(srcdir)/pl-atom.ih:	$(srcdir)/ATOMS
		cd $(srcdir) ; $(AWK) -f defatom.awk < ATOMS

# Hack to deal with __register_frame problems in different glibc 2 versions
# (Linux, compiled on SuSE 6.0, glibc 2.0.7, egcs 1.1.1)

frame.o:
		ar x `$(CC) -print-libgcc-file-name` $@

plld:		$(srcdir)/plld.c
		$(CC) -I. -I$(srcdir) $(CFLAGS) -o $@ $(srcdir)/plld.c

tags:		$(srcdir)/TAGS

$(srcdir)/TAGS:	$(SRC)
		(cd $(srcdir) && $(ETAGS) $(SRC) $(HDR))

swipl:
		echo "." > $@
swiplbin:	
		echo ".." > $@

check:
		./$(PL) -f $(srcdir)/test.pl -F none -g test,halt -t 'halt(1)'

install:	@INSTALLTARGET@
install-version: install-arch install-libs install-vbins

dv-install:	install-arch install-libs install-bins

install-mt:	
		$(INSTALL_PROGRAM) $(PL) $(PLBASE)/bin/$(ARCH)/pl-mt
		$(REL_LN) $(PLBASE)/bin/$(ARCH)/pl-mt $(bindir)

install-bins:	install-arch
		-if [ ! -d $(bindir)  ]; then $(MKDIR) $(bindir); fi
		for f in $(PL) plld plrc; do \
		    $(REL_LN) $(PLBASE)/bin/$(ARCH)/$$f $(bindir); \
		done

install-vbins:	install-arch
		-if [ ! -d $(bindir)  ]; then $(MKDIR) $(bindir); fi
		$(REL_LN) $(PLBASE)/bin/$(ARCH)/$(PL) $(bindir)/$(PLV)
		for f in plld plrc; do \
		    $(REL_LN) $(PLBASE)/bin/$(ARCH)/$$f $(bindir); \
		done

install-arch:	idirs iruntime
		for f in $(PL) plld rc/plrc; do \
		    $(INSTALL_PROGRAM) $$f $(PLBASE)/bin/$(ARCH); \
		done

install-libs:	idirs iboot ilib iruntime iinclude iman
		$(INSTALL_DATA) $(STARTUPPATH) $(PLBASE)/$(BOOTFILE)
		$(INSTALL_DATA) swipl $(PLBASE)
		$(INSTALL_DATA) swiplbin $(PLBASE)/bin/swipl
		# $(INSTALL_DATA) ../COPYING $(PLBASE)
		cd $(PLBASE)/library; \
		   ../bin/$(ARCH)/$(PL) \
			-f none \
			-g make_library_index\('.'\) \
			-t halt

installdirs=	bin bin/$(ARCH) lib lib/$(ARCH) man boot library \
		include runtime runtime/$(ARCH)

iruntime:
		if [ ! -d $(PLBASE)/runtime/$(ARCH) ]; then \
			$(MKDIR) $(PLBASE)/runtime/$(ARCH); \
		fi
		if [ -f $(RUNTIME) ]; then \
			$(INSTALL_DATA) $(RUNTIME) $(PLBASE)/runtime/$(ARCH); \
		fi

idirs:
		-if [ ! -d $(prefix)/lib  ];    then $(MKDIR) $(prefix)/lib;     fi
		-if [ ! -d $(PLBASE)  ];    then $(MKDIR) $(PLBASE); 	fi
		for d in $(installdirs); do \
			if [ ! -d $(PLBASE)/$$d ]; then \
				$(MKDIR) $(PLBASE)/$$d; \
			fi; \
		done


iboot:		
		cd ../boot; \
		for f in *.pl; do $(INSTALL_DATA) $$f $(PLBASE)/boot; done
ilib:		
		cd ../library; \
		for f in $(PLLIBS); do \
			$(INSTALL_DATA) $$f $(PLBASE)/library; \
		done

iinclude:       
		cd ../include; \
		for f in SWI-Prolog.h SWI-Exports SWI-Stream.h stub.c; do \
		    if [ -f $$f ]; then \
			$(INSTALL_DATA) $$f $(PLBASE)/include; \
		    fi; \
		done
		if [ -d $(prefix)/include -a -w $(prefix)/include ]; then \
		    $(INSTALL_DATA) ../include/SWI-Prolog.h $(prefix)/include; \
		    $(INSTALL_DATA) ../include/SWI-Stream.h $(prefix)/include; \
		fi

iman:		@INSTALLRLMAN@
		$(INSTALL_DATA) pl.1 $(plmandir)/$(PL).$(mansec)
		$(INSTALL_DATA) $(srcdir)/plrc.1 $(plmandir)/plrc.$(mansec)
		$(INSTALL_DATA) $(srcdir)/plld.1 $(plmandir)/plld.$(mansec)
		$(INSTALL_DATA) $(srcdir)/readline.3 $(plmandir)/readline.$(lmansec)
		-if [ ! -d $(man_prefix) ]; then $(MKDIR) $(man_prefix); fi
		-if [ ! -d $(mandir)  ];    then $(MKDIR) $(mandir);     fi
		-$(INSTALL_DATA) $(plmandir)/$(PL).$(mansec) $(mandir)
		-$(INSTALL_DATA) $(plmandir)/plrc.$(mansec) $(mandir)
		-$(INSTALL_DATA) $(plmandir)/plld.$(mansec) $(mandir)

install_rlman:
		-if [ ! -d $(lmandir) ]; then $(MKDIR) $(lmandir); fi
		-if [ ! -r $(lmandir)/readline.$(lmansec) ]; then \
		  $(INSTALL_DATA) $(srcdir)/readline.3 $(lmandir)/readline.$(lmansec); \
		fi

#
# runtime-only environment installation
#

RTHOME=		$(prefix)/lib/rt-pl-$(PLVERSION)
RTDIRS=		$(RTHOME) $(RTHOME)/bin $(RTHOME)/lib $(RTHOME)/lib/$(ARCH) \
		$(RTHOME)/man $(RTHOME)/bin/$(ARCH) $(RTHOME)/include

rt-install:
		@echo "****************"
		@echo "Creating runtime environment in $(RTHOME)"
		@echo "****************"
		@for d in $(RTDIRS); do \
		    if [ ! -d $$d ]; then \
			echo $(MKDIR) $$d; \
			$(MKDIR) $$d; \
		    fi; \
		done
		$(INSTALL_PROGRAM) $(PL) $(RTHOME)/bin/$(ARCH)/pl
		$(INSTALL_DATA) pl.1 $(RTHOME)/man/pl.1
		$(INSTALL_DATA) swipl $(RTHOME)/swipl
		$(INSTALL_DATA) swiplbin $(RTHOME)/bin/swipl
		$(INSTALL_DATA) ../README.RT $(RTHOME)
		$(INSTALL_DATA) ../include/SWI-Prolog.h $(RTHOME)/include
		$(INSTALL_DATA) ../include/SWI-Stream.h $(RTHOME)/include
#		-cd $(bindir); rm -f plrt; ln -s $(RTHOME)/bin/$(ARCH)/pl plrt

#
# Cleanup
#

clean:
		@(cd rc && $(MAKE) clean)
		rm -f *.o a.out core *~ #* parms.h

distclean:	clean
		@(cd rc && $(MAKE) distclean)
		rm -rf $(INCLUDEDIR) $(RUNTIMEDIR)
		rm -f ../library/INDEX.pl
		rm -f Makefile config.h config.status parms.h
		rm -f swipl swiplbin pl.prc
		rm -f pl plld pl.1
		rm -f config.log config.cache

realclean:	clean
		rm -f $(STARTUPPATH)
		rm -f config.log config.cache
		rm -rf $(PL) ../startup ../include ../runtime

