################################################################
# Makefile for SWI-Prolog 5.x
# $Id: Makefile.in,v 1.119 2004/09/30 10:00:50 jan Exp $
#
# Author:	Jan Wielemaker
#		jan@swi.psy.uva.nl
#		SWI
#		University of Amsterdam
#    		Roetersstraat 15
#		1018 WB  Amsterdam
#		The Netherlands
#
# This  Makefile  requires  GNU-Make,  often  installed  as  `gmake'  on
# Non-Linux systems.
#
# 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-lman	Install man files in Prolog lib hierarchy
#	* make check		Run some tests
#
# Copyright (C) University of Amsterdam, all rights reserved
# 
# Copyright policy:
#	
#	* LGPL (see file COPYING or http://www.swi-prolog.org/)
################################################################

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@
mandir=@mandir@
mansec=1
lmansec=3
smandir=$(mandir)/man$(mansec)
lmandir=$(mandir)/man$(lmansec)
plmandir=$(PLBASE)/man

CC=@CC@
LDEXE=@LDEXE@
LIBS=@LIBS@
AWK=@AWK@
SED=sed
ETAGS=@ETAGS@
AR=@AR@
RANLIB=@RANLIB@
ARCH=@ARCH@
INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_SCRIPT=@INSTALL_SCRIPT@
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
DEF=$(PL).def
CINCLUDE=$(INCLUDEDIR)/SWI-Prolog.h
STREAMH=$(INCLUDEDIR)/SWI-Stream.h
STUBC=$(INCLUDEDIR)/stub.c
STARTUPPATH=$(PL).prc
RUNTIMEDIR=../lib/$(ARCH)
PLLIB=@PLLIB@
LNLIBS=@LNLIBS@
USEPLLIB=@USEPLLIB@
RUNTIME=$(RUNTIMEDIR)/$(PLLIB)
SHARED=@PLSO@
SHAREDV=$(SHARED).$(PLVERSION)
SONAMEOPT=-Wl,-soname=@PLSO@.$(PLVERSION)
LIBRARYDIR=${PLBASE}/library
MKLIBS=@MKLIBS@
DEVPL=./pl.sh

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 \
	pl-beos.o pl-attvar.o pl-gvar.o pl-btree.o pl-main.o
XOBJ=	@ALLOCA@ @EXTRAOBJ@
RCOBJ=	rc/access.o rc/build.o rc/html.o rc/util.o

PLOBJ=	pl-extend.o
ALLOBJ= $(OBJ) $(PLOBJ) $(XOBJ)

PLINIT=	$(PB)/init.pl

INCSRC=	pl-index.c pl-alloc.c pl-fli.c
SRC=	$(OBJ:.o=.c) $(PLOBJ:.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/license.pl \
	../boot/sort.pl ../boot/bags.pl ../boot/apply.pl \
	../boot/writef.pl ../boot/history.pl ../boot/attvar.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 debug.pl \
	bim.pl quintus.pl edinburgh.pl ctypes.pl files.pl \
	edit.pl emacs_interface.pl shell.pl check.pl \
	tty.pl readln.pl readutil.pl make.pl option.pl \
	am_match.pl oset.pl ordsets.pl occurs.pl lists.pl \
	netscape.pl url.pl main.pl assoc.pl \
	threadutil.pl qpforeign.pl dif.pl when.pl \
	checklast.pl checkselect.pl operators.pl \
	prolog_stack.pl
CLP=	bounds.pl
PLWIN=	wise.pl dde.pl progman.pl registry.pl
builddirs = ../lib ../lib/$(ARCH)

all:	banner dirs \
	headers rc-lib $(MKLIBS) system startup index plld

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

system:		$(PL)
startup:	$(STARTUPPATH)
headers:	parms.h $(CINCLUDE) $(STREAMH) $(STUBC) @EXTRAHDR@
staticlib:	$(RUNTIME)
sharedlib:	$(RUNTIMEDIR)/$(SHARED)
index:		../library/INDEX.pl

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


$(PL):		$(RUNTIME) $(PLOBJ)
		$(LDEXE) $(LDFLAGS) -o $(PL) $(PLOBJ) -L../lib/$(ARCH) -lpl $(LNLIBS)

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


dirs::
		@for d in $(builddirs); do \
		  if [ ! -d $$d ]; then mkdir $$d; fi; \
		done

$(RUNTIME):	$(OBJ) $(RCOBJ) $(XOBJ)
		rm -f $@
		$(AR) r $@ $(OBJ) $(RCOBJ) $(XOBJ)
		$(RANLIB) $@

$(RUNTIMEDIR)/$(SHAREDV):	$(RUNTIMEDIR) $(OBJ) $(RCOBJ) $(XOBJ)
		$(CC) -shared $(COFLAGS) -o $@ $(SONAMEOPT) \
			$(OBJ) $(RCOBJ) $(XOBJ) $(LIBS)

$(RUNTIMEDIR)/$(SHARED):	$(RUNTIMEDIR)/$(SHAREDV)
		( cd ../lib/$(ARCH) && rm -f $(SHARED) && ln -s $(SHAREDV) $(SHARED) )

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

../library/INDEX.pl:	../library ../library/[a-z]*.pl
		$(DEVPL) -x $(STARTUPPATH) \
			-f none -F none \
			-g make_library_index\(\'../library\'\) \
			-t halt

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

# Create SWI-Exports, used for importing and exporting symbols on behalf of
# the AIX version.

exports:	$(EXPORTS)
def:		$(DEF)

$(EXPORTS):	Makefile $(srcdir)/pl-itf.h $(INCLUDEDIR)
		echo \#\!$(bindir)/$(PL) > $@
		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 >> $@

# Create pl.def, required for exporting symbols from pl.exe for the CygWin
# platform

$(DEF):		$(srcdir)/pl-itf.h
		echo 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 >> $@

$(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	    \"$(LNLIBS)\""     >> $@%
		@echo "#define C_PLLIB	    \"$(USEPLLIB)\""   >> $@%
		@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))

check:
		$(DEVPL) -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-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 idot
		$(INSTALL_DATA) $(STARTUPPATH) $(PLBASE)/$(BOOTFILE)
		echo . > $(PLBASE)/swipl
		echo .. > $(PLBASE)/bin/swipl
		cd $(PLBASE)/library; \
		   ../bin/$(ARCH)/$(PL) \
			-f none \
			-g make_library_index\('.'\) \
			-t halt

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

iruntime::
		$(INSTALL_DATA) $(RUNTIME) $(PLBASE)/lib/$(ARCH)
		$(RANLIB) $(PLBASE)/lib/$(ARCH)/$(PLLIB)
		if [ -f $(RUNTIMEDIR)/$(SHAREDV) ]; then \
		  $(INSTALL_DATA) $(RUNTIMEDIR)/$(SHAREDV) $(PLBASE)/lib/$(ARCH) ; \
		( cd $(PLBASE)/lib/$(ARCH) && rm -f $(SHARED) && ln -s $(SHAREDV) $(SHARED) ) ; \
		fi

install-implib::
		$(INSTALL_DATA) libplimp.a $(PLBASE)/lib/$(ARCH)

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:		iclp
		cd ../library; \
		for f in $(PLLIBS); do \
			$(INSTALL_DATA) $$f $(PLBASE)/library; \
		done

iclp::
		cd ../library/clp; \
		for f in $(CLP); do \
			$(INSTALL_DATA) $$f $(PLBASE)/library/clp; \
		done

idot::
		$(INSTALL_DATA) ../dotfiles/dotplrc $(PLBASE)/dotfiles
		$(INSTALL_DATA) ../dotfiles/dotxpcerc $(PLBASE)/dotfiles
		$(INSTALL_DATA) ../dotfiles/README $(PLBASE)/dotfiles
		$(INSTALL_SCRIPT) ../dotfiles/edit $(PLBASE)/dotfiles

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::
		-if [ ! -d $(mandir) ]; then $(MKDIR) $(mandir); fi
		-if [ ! -d $(smandir)  ];    then $(MKDIR) $(smandir);     fi
		-$(INSTALL_DATA) pl.1 $(smandir)/$(PL).$(mansec)
		-$(INSTALL_DATA) $(srcdir)/plrc.1 $(smandir)/plrc.$(mansec)
		-$(INSTALL_DATA) $(srcdir)/plld.1 $(smandir)/plld.$(mansec)

install-lman::
		$(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)

#
# runtime-only environment installation
#

RTHOME=		$(prefix)/lib/$(PLV)
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
		echo . > $(RTHOME)/swipl
		echo .. > $(RTHOME)/bin/swipl
		$(INSTALL_DATA) ../README.RT $(RTHOME)
		$(INSTALL_DATA) ../include/SWI-Prolog.h $(RTHOME)/include
		$(INSTALL_DATA) ../include/SWI-Stream.h $(RTHOME)/include

#
# Cleanup
#

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

distclean:	clean
		@(cd rc && $(MAKE) distclean)
		rm -rf $(INCLUDEDIR) ../lib
		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 $(PL).exe plld.exe libplimp.a $(DEF)
		rm -f config.log config.cache

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

