#############################################################################
# Makefile for creating Bousi-Prolog stand-alone application (Unix version) #
#############################################################################

# File where SWI-Prolog executable & linker names and home directory is stored
# (this file is automatically created by "bplInfo.sh" script)
PLINFO = .bplInfo

# Default size for Prolog stacks in KBs (0 means maximum stack size)
#LOCALSTACK = 16394
#GLOBALSTACK = 32768
#TRAILSTACK = 32768
LOCALSTACK = 0
GLOBALSTACK = 0
TRAILSTACK = 0
# ARGUMENTSTACK = 1024  # SWI-Prolog 7.6.4: option not available

# Source directories for Bousi-Prolog system, extern foreign library,
# documentation generator and test launcher
BOUSIDIR = source
EXTERNDIR = extern
DOCDIR = doc
TESTDIR = test

# Name of the Bousi-Prolog system's main module
MAINMODULE = bousi

# Filenames of the Bousi-Prolog executable and the test launcher
EXEC = bousi
TESTEXEC = bousitest

# Foreign library filename
LIBRARY = extern.so

# Documentation filename
DOCUMENTATION = doc.pdf


all: $(LIBRARY) $(EXEC)

library: $(LIBRARY)

clean:
	rm -f $(EXEC) $(TESTEXEC) $(LIBRARY) $(PLINFO) $(DOCUMENTATION)
	cd $(EXTERNDIR) && make clean
	cd $(TESTDIR) && make clean
	cd $(DOCDIR) && make clean

rebuild: clean all

$(EXEC): $(PLINFO) $(LIBRARY) $(BOUSIDIR)/*.pl
	@# Gets SWI-Prolog executable name from .bplInfo file and saves it
	@# into PROLOG variable
	read PROLOG LINKER PLDIR < $(PLINFO) && \
	$$PROLOG -g "[$(BOUSIDIR)/$(MAINMODULE)], qsave_program('$(EXEC)', [local=$(LOCALSTACK), global=$(GLOBALSTACK), trail=$(TRAILSTACK), goal=main, stand_alone=true, foreign=save]), halt."


$(TESTEXEC): $(PLINFO) $(LIBRARY) $(BOUSIDIR)/*.pl $(TESTDIR)/test*.pl
	@# Gets SWI-Prolog executable name from .bplInfo file and saves it
	@# into PROLOG variable
	read PROLOG LINKER PLDIR < $(PLINFO) && \
	cd $(TESTDIR) && \
	make PROLOG=$$PROLOG && \
	cp $(TESTEXEC) ..

$(LIBRARY): $(PLINFO) $(EXTERNDIR)/*.c $(EXTERNDIR)/*.h $(EXTERNDIR)/*.lex
	@# Gets SWI-Prolog executable & linker names and home directory from
	@# .bplInfo file and saves them into PROLOG, LINKER and PLDIR variables
	read PROLOG LINKER PLDIR < $(PLINFO) && \
	cd $(EXTERNDIR) && \
	make PROLOG=$$PROLOG LD=$$LINKER PLDIR="$$PLDIR" && \
	cp $(LIBRARY) ..

$(DOCUMENTATION): $(PLINFO) $(BOUSIDIR)/*.pl $(DOCDIR)/*.pl
	@# Gets SWI-Prolog executable name from .bplInfo file and saves it
	@# into PROLOG variable
	read PROLOG LINKER PLDIR < $(PLINFO) && \
	cd $(DOCDIR) && \
	make PROLOG=$$PROLOG && \
	cp $(DOCUMENTATION) ..

$(PLINFO):
	. ./bplInfo.sh

$(EXTERNDIR)/lexical.c:
	@# This file will be automatically generated by Flex

