#####################################################################
# Makefile for creating Bousi-Prolog foreign library (Unix version) #
#####################################################################

# Note that PROLOG, PLDIR and (optionally) LD variables must be
# passed to "make" in order to build the foreign library
# VALUES ​​THAT ARE CURRENTLY PASSED
# PROLOG = swipl
# PLDIR = /usr/local/Cellar/swi-prolog/7.6.4/libexec/lib/swipl-7.6.4

# Compiler and linker flags
NOWARN = -Wno-incompatible-pointer-types -Wno-implicit-function-declaration -Wno-unused-result -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast
CFLAGS =  -c -fPIC -I"$(PLDIR)/include" -I/usr/include $(NOWARN) 
LDFLAGS = -shared -fPIC -ledit -L/usr/lib -L/opt/local/lib $(NOWARN) -pl  $(PROLOG)

# Compiler and linker default names
CC = gcc
LD = swipl-ld

# Object files used by foreign library
OBJS = closure.o fuzzysets.o array.o lexical.o tokenize.o shell.o blocks.o install.o

# Foreign library name
LIBRARYNAME = extern
LIBRARY = $(LIBRARYNAME).so
DLLLIBRARY = $(LIBRARYNAME).dll


.PHONY: clean all

all: $(LIBRARY)

clean:
	rm -f $(LIBRARY) $(DLLLIBRARY) $(OBJS) lexical.c

$(LIBRARY): $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^
	if [ -f $(DLLLIBRARY) ]; then mv $(DLLLIBRARY) $(LIBRARY); fi

closure.o: closure.c closure.h array.h bool.h
blocks.o: blocks.c array.h bool.h closure.h blocks.h
fuzzysets.o: fuzzysets.c fuzzysets.h array.h bool.h
array.o: array.c array.h
lexical.o: lexical.c lexical.h bool.h
tokenize.o: tokenize.c tokenize.h array.h lexical.h
shell.o: shell.c shell.h bool.h
install.o: install.c
	$(CC) $(CFLAGS) -o$@ $<

lexical.c: scanner.lex
	@# This source code file is automatically generated by Flex
	flex -o$@ $<

