# Makefile
# ex.  > make INST_PREFIX=/usr
#

NAME = clp
DATE_TAG:=`date +%Y%m%d`

INST_PREFIX = /usr/local

CCOPT = -I.  -I$(INST_PREFIX)/include
LDOPT =  -L$(INST_PREFIX)/lib
CFLAGS = -Wall -O2 $(CCOPT)

AZ_LIB_EXT = $(INST_PREFIX)/lib/azprolog/ext
AZ_LIB_OBJ = $(INST_PREFIX)/lib/azprolog/obj

AZPCFLAGS = /message /s_verbos /cc $(CC) /ccopt "$(CCOPT)" /link_opt "$(LDOPT)"

ifeq ($(AZ_DEBUG_MODE),true)
AZPC = azpc_d
else
AZPC = azpc
endif

CC=gcc

EXEC_TARGETS = clp.so lib_clp.a clp azclp

# for execute azpc
export LD_LIBRARY_PATH=$(INST_PREFIX)/lib


default: clp.so lib_clp.a

clp: clp_userfile.c
	$(CC) $(CFLAGS) -c $<
	gcc -o $@ clp_userfile.o $(AZ_LIB_OBJ)/azpi.o $(AZ_LIB_OBJ)/curses.o -L$(AZ_LIB_EXT) -l_clp -lazp -lcurses -lm -ldl

azclp: clp.c
	$(AZPC) -p $< /i /e $@ /curses  /ccopt "$(CFLAGS)"
	rm -f *.o

clp.so: clp.c
	$(CC) $(CFLAGS) -fPIC -o clp.o -c $<
	$(CC) -shared -dynamiclib -o $@ clp.o
	chmod 644 $@

lib_clp.a: clp.c
	$(CC) $(CFLAGS) -o clp.o -c $<
	ar rv $@ clp.o
	ranlib $@

clp.c: clp.pl
	AZProlog=$(INST_PREFIX) $(INST_PREFIX)/bin/$(AZPC) -p $< /ncc $(AZPCFLAGS)


clean:
	rm -f clp.c *~ .*~ *.o mkaz userfile.c $(EXEC_TARGETS)

tar:
	cd ..; tar -cvf $(NAME)-$(DATE_TAG).tar $(NAME)
	gzip ../$(NAME)-$(DATE_TAG).tar
