ROOT_DIR      = @ROOT_DIR@
TOP_LEVEL     = @TOP_LEVEL@
LIB_ENGINE_PL = @LIB_ENGINE_PL@
GPLC          = @GPLC@
CC            = @CC@
CFLAGS        = @CFLAGS@
AR_RC         = @AR_RC@
RANLIB        = @RANLIB@


LIBNAME = $(LIB_ENGINE_PL)
OBJLIB  = machine@OBJ_SUFFIX@ machine1@OBJ_SUFFIX@ mem_alloc@OBJ_SUFFIX@ \
          misc@OBJ_SUFFIX@ hash@OBJ_SUFFIX@ obj_chain@OBJ_SUFFIX@ \
          engine@OBJ_SUFFIX@ engine1@OBJ_SUFFIX@ wam_inst@OBJ_SUFFIX@ \
          atom@OBJ_SUFFIX@ pred@OBJ_SUFFIX@ oper@OBJ_SUFFIX@ \
          if_no_fd@OBJ_SUFFIX@ main@OBJ_SUFFIX@

all: wam_regs.h $(TOP_LEVEL).h obj_begin@OBJ_SUFFIX@ obj_end@OBJ_SUFFIX@ $(LIBNAME)


$(LIBNAME): $(OBJLIB)
	rm -f $(LIBNAME) 
	$(AR_RC)@AR_SEP@$(LIBNAME) $(OBJLIB)
	$(RANLIB) $(LIBNAME)


.SUFFIXES:
.SUFFIXES: @OBJ_SUFFIX@ .c $(SUFFIXES)

.c@OBJ_SUFFIX@:
	$(GPLC) -c -C '$(CFLAGS)' $*.c



# Configuration

config: pl_config@EXE_SUFFIX@ wam_regs.h

pl_config@EXE_SUFFIX@: pl_config.c gp_config.h machine.h wam_archi.def
	$(CC) $(CFLAGS) $(LFLAGS) @CC_EXE_NAME_OPT@pl_config@EXE_SUFFIX@ pl_config.c


wam_regs.h: pl_config@EXE_SUFFIX@ machine.h wam_archi.def 
	./pl_config@EXE_SUFFIX@
	(cd ../TopComp; $(MAKE) $(GPLC)@EXE_SUFFIX@)


gp_config.h:
	echo '*** run ./configure before make please ***'
	exit 1


cpp_headers@EXE_SUFFIX@: cpp_headers.c
	$(CC) $(CFLAGS) $(LFLAGS) @CC_EXE_NAME_OPT@cpp_headers@EXE_SUFFIX@ cpp_headers.c


$(TOP_LEVEL).h: headers.h cpp_headers
	D=`(cd ..;for i in *;do test -d $$i && X="$$X $(ROOT_DIR)/src/$$i";done;echo $$X)`; ./cpp_headers headers.h $(TOP_LEVEL).h $$D


# General core


machine@OBJ_SUFFIX@: machine.h machine.c gp_config.h wam_regs.h 

mem_alloc@OBJ_SUFFIX@: mem_alloc.c

misc@OBJ_SUFFIX@: misc.h misc.c machine.h 

hash@OBJ_SUFFIX@: hash.h hash.c

main@OBJ_SUFFIX@: main.c

obj_chain@OBJ_SUFFIX@: obj_chain.h obj_chain.c

obj_begin@OBJ_SUFFIX@: obj_chain.h obj_begin.c

obj_end@OBJ_SUFFIX@: obj_chain.h obj_end.c



# Prolog engine


engine@OBJ_SUFFIX@: engine.h engine.c 

                                          # warning: no C optim for engine1.c
engine1@OBJ_SUFFIX@: engine1.c 
	$(GPLC) -c engine1.c

wam_inst@OBJ_SUFFIX@: wam_archi.h wam_inst.h wam_inst.c unify.c

atom@OBJ_SUFFIX@: atom.h atom.c gp_config.h

pred@OBJ_SUFFIX@: pred.h pred.c

oper@OBJ_SUFFIX@: oper.h oper.c

if_no_fd@OBJ_SUFFIX@: if_no_fd.h if_no_fd.c


clean:
	rm -f pl_config@EXE_SUFFIX@ cpp_headers@EXE_SUFFIX@ *@OBJ_SUFFIX@ $(LIBNAME)

distclean: clean
	rm -f wam_archi.h $(TOP_LEVEL).h gp_config.h




# test obj chain utility

TEST_OC_OBJS=test_oc_m1@OBJ_SUFFIX@ test_oc_m2@OBJ_SUFFIX@

obj_chain1@OBJ_SUFFIX@: obj_chain.h obj_chain.c
	$(CC) $(CFLAGS) @CC_OBJ_NAME_OPT@obj_chain1@OBJ_SUFFIX@ -c obj_chain.c -DDEBUG

obj_begin1@OBJ_SUFFIX@: obj_chain.h obj_begin.c
	$(CC) $(CFLAGS) @CC_OBJ_NAME_OPT@obj_begin1@OBJ_SUFFIX@ -c obj_begin.c

obj_end1@OBJ_SUFFIX@: obj_chain.h obj_end.c
	$(CC) $(CFLAGS) @CC_OBJ_NAME_OPT@obj_end1@OBJ_SUFFIX@ -c obj_end.c

test_oc_m1@OBJ_SUFFIX@: test_oc_m1.c
	$(CC) $(CFLAGS) -c test_oc_m1.c

test_oc_m2@OBJ_SUFFIX@: test_oc_m2.c
	$(CC) $(CFLAGS) -c test_oc_m2.c

test_oc@OBJ_SUFFIX@: test_oc.c
	$(CC) $(CFLAGS) -c test_oc.c

test_oc@EXE_SUFFIX@: obj_begin1@OBJ_SUFFIX@ $(TEST_OC_OBJS) obj_chain1@OBJ_SUFFIX@ test_oc@OBJ_SUFFIX@ obj_end1@OBJ_SUFFIX@
	$(CC) @CC_EXE_NAME_OPT@test_oc@EXE_SUFFIX@ obj_begin1@OBJ_SUFFIX@ \
              $(TEST_OC_OBJS) obj_chain1@OBJ_SUFFIX@ test_oc@OBJ_SUFFIX@ \
              obj_end1@OBJ_SUFFIX@

clean-test_oc: 
	rm -f obj_begin1@OBJ_SUFFIX@ obj_chain1@OBJ_SUFFIX@ obj_end1@OBJ_SUFFIX@ \
              test_oc*@OBJ_SUFFIX@ test_oc@EXE_SUFFIX@

