
subdir = src/interp/

DOC=$(axiom_target_docdir)/src/interp

# Command to translate Boot to Common Lisp
BOOT_TO_LISP = \
	$(axiom_build_document) --tag=boot --mode=translate \
		--use=$(BOOTSYS) --output=$@ $<

# Command to translate Common Lisp to native object code
COMPILE_LISP = \
	$(axiom_build_document) --tag=lisp --mode=compile \
		--output=$@ --use=$(DEPSYS) $<
AUTO=$(axiom_targetdir)/autoload

BOOTSYS= $(axiom_build_bindir)/bootsys

DEPSYS=	$(axiom_build_bindir)/depsys

DEP=    vmlisp     hash       bootfuns \
        union      nlib       macros     comp \
        spaderror  debug      spad       bits \
        setq       property   foam_l     axext_l

LOADSYS= $(axiom_build_bindir)/lisp$(EXEEXT)
SAVESYS= $(axiom_build_bindir)/interpsys$(EXEEXT)
AXIOMSYS= $(axiom_target_bindir)/AXIOMsys$(EXEEXT)

OBJ_files= vmlisp hash bootfuns macros setq astr bits \
	alql buildom cattable cformat clam clammed \
	comp compat compress cparse cstream database \
	debug dq fname format g-boot g-cndata g-error g-opt \
	g-timer	g-util hypertex i-analy \
	i-code i-coerce	i-coerfn i-eval i-funsel i-intern \
	i-map i-output i-resolv	i-spec1 i-spec2 i-syscmd \
	i-toplev i-util incl int-top lisplib macex match \
	monitor msg msgdb newfort nlib nrunfast \
	nrungo nrunopt pathname pf2sex pile \
	posit property ptrees record rulesets scan \
	serror server setvars sfsfun-l sfsfun simpbool slam \
	sockio spad spaderror template termrw trace \
	union daase fortcall ncomp primitives parsing \
	bootlex def parse postpar preparse s-parser

OBJS= $(patsubst %, %.$(LISPOBJEXT), $(OBJ_files))

INOBJ_files= setvart interop patches

INOBJS= $(patsubst %, %.$(LISPOBJEXT), $(INOBJ_files))

OCOBJ_files= apply c-doc c-util profile category compiler \
	define functor info iterator modemap nruncomp \
	package	htcheck

OCOBJS= $(patsubst %, ${AUTO}/%.$(LISPOBJEXT), $(OCOBJ_files))

BROBJ_files= bc-matrix bc-misc bc-solve bc-util ht-util htsetvar \
	ht-root br-con br-data showimp br-op1 br-op2 \
	br-search br-util topics br-prof br-saturn

BROBJS= $(patsubst %, ${AUTO}/%.$(LISPOBJEXT), $(BROBJ_files))

ASCOMP_files= hashcode as foam_l axext_l

ASCOMP= $(patsubst %, %.$(LISPOBJEXT), $(ASCOMP_files))

ASAUTO= ${AUTO}/ax.$(LISPOBJEXT)

TIMESTAMP=$(axiom_targetdir)/timestamp
YEARWEEK=(progn (setq timestamp "${TIMESTAMP}") (setq boot::*build-version* "${VERSION}") (yearweek))


.PRECIOUS:	${DEPSYS}
.PRECIOUS:	${SAVESYS}
.PRECIOUS:	${AXIOMSYS}

PROCLAIMS=(progn (load "$(srcdir)/sys-pkg.lisp") \
                 (load "$(srcdir)/interp-proclaims.lisp"))

.SUFFIXES:
.SUFFIXES: .boot .clisp .lisp .pamphlet

.PHONY: all all-ax all-depsys all-interpsys all-axiomsys

all: all-ax

all-ax: stamp
	@echo finished $(srcdir)

stamp:  remove-stamp build-images
	$(STAMP) stamp

.PHONY: remove-stamp
remove-stamp:
	-rm -f stamp

.PHONY: build-images
build-images: remove-stamp all-interpsys

all-interpsys: all-depsys
	$(MAKE) $(SAVESYS)

all-axiomsys: all-depsys
	$(MAKE) $(AXIOMSYS)

all-depsys: $(DEPSYS)

clean:
	@echo 619 cleaning $(builddir)

.PRECIOUS: %.clisp
%.clisp: $(srcdir)/%.boot
	$(BOOT_TO_LISP)

.PRECIOUS: %.$(LISPOBJEXT)
%.$(LISPOBJEXT): %.clisp
	$(COMPILE_LISP)

# Compile the part of the interpreter written in Common Lisp
.PRECIOUS: %.lisp
%.$(LISPOBJEXT): $(srcdir)/%.lisp
	$(COMPILE_LISP)

mostlyclean-local:
	rm -f *.fn *.data *.lib *.$(LISPOBJEXT)

clean-local: mostlyclean-local
	rm -f *.clisp \
	  makedep.lisp makedep2.lisp makedep2.lst makeint.lisp \
	  exposed.lsp database.date

distclean-local: clean-local

${SAVESYS} ${AXIOMSYS}: makeint.lisp \
    ${DEPSYS} ${OBJS}  util.$(LISPOBJEXT) \
    $(srcdir)/sys-pkg.lisp \
    ${OCOBJS} ${BROBJS} \
    database.date ${INOBJS} ${ASCOMP} ${ASAUTO} \
    ${NAGBROBJS} \
    ${LOADSYS} \
    $(axiom_targetdir)/algebra/exposed.$(FASLEXT) \
    $(axiom_src_datadir)/doc/msgs/s2-us.msgs \
    $(srcdir)/../algebra/warm.data

makeint.lisp:
	@ echo 5 invoking make in `pwd` with parms:
	@ echo SRC= $(axiom_src_srcdir)
	@ echo LISP=${LISP} BYE=${BYE}
	cp $(axiom_src_datadir)/doc/msgs/s2-us.msgs \
		 $(axiom_target_datadir)/msgs/s2-us.msgs
	@ echo '${PROCLAIMS}' > makeint.lisp
	echo '(load #-:ecl "util" #+:ecl "$(srcdir)/util.lisp")' >> makeint.lisp
	@ echo '(in-package "BOOT")' >> makeint.lisp
	@ touch ${TIMESTAMP}
	@ echo '${YEARWEEK}' >> makeint.lisp
	echo '(build-interpsys' \
	  '(quote ($(patsubst %, "%", ${OBJS} ${ASCOMP} ${INOBJS} )))' \
	  '(quote ())' \
	  '(quote ($(patsubst %, "$(BASE)%", ${OCOBJS})))' \
	  '(quote ($(patsubst %, "$(BASE)%", ${BROBJS})))' \
	  '(quote ())' \
	  '(quote ())' \
	  '(quote ($(patsubst %, "$(BASE)%", ${ASAUTO})))' \
	  '"${AXIOM}")' >> makeint.lisp
	@ echo '(in-package "BOOT")' >> makeint.lisp
	echo '#-:ecl(load "$(srcdir)/../algebra/warm.data")' >> makeint.lisp
	echo '#-:ecl(|clearClams|)' >> makeint.lisp
	@ echo '#+:GCL (setq compiler::*suppress-compiler-notes* t)' >> makeint.lisp
	@ echo '#+:GCL (si::gbc-time 0)' >> makeint.lisp

${SAVESYS}:
	echo '(load "makeint.lisp") #-:ecl(VMLISP::reclaim)' \
             '#+:ecl(FRICAS-LISP::make-program "$(BASE)$@" nil)' \
             '#-:ecl(BOOT::spad-save "$(BASE)$@" t)' | ${LOADSYS}

	@ echo 6 ${SAVESYS} created

depsys_boot_sources = clam g-boot g-util parse postpar s-parser slam

# Lisp files beyond DEP needed for DEPSYS
depsys_lisp_files = parsing bootlex preparse def

${DEPSYS}:	$(patsubst %, $(srcdir)/%.lisp, \
                    ${DEP} $(depsys_lisp_files) sys-pkg util) \
	        $(patsubst %, %.clisp, ${depsys_boot_sources})
	@ echo 3 making ${DEPSYS}
	$(mkinstalldirs) $(axiom_build_bindir)
	echo '${PROCLAIMS}' > makedep.lisp
	echo '(push :oldboot *features*)' >> makedep.lisp
	echo '(load "$(srcdir)/util.lisp")' >> makedep.lisp
	echo '(in-package "BOOT")' >> makedep.lisp
	echo '(build-depsys (quote (' \
	          '$(patsubst %, "$(srcdir)/%.lisp", ${DEP})' \
		  ')) "${AXIOM}" "$(builddir)")' >> makedep.lisp
	echo > makedep2.lst
	for A in postpar.clisp parse.clisp \
	         $(patsubst %, $(srcdir)/%.lisp, $(depsys_lisp_files)) \
	         clam.clisp s-parser.clisp slam.clisp \
	         g-boot.clisp g-util.clisp; do \
	   B=$${A##*/} ; \
	   C=$${B%.*} ; \
	   echo '(load-maybe-compiling "'$$A'" "'$$C'.$(LISPOBJEXT)")' \
	          >> makedep.lisp ; \
	   echo $$C >> makedep2.lst ; \
	done
	for A in ${DEP} util primitives ; do \
	    echo '(maybe-compile "$(srcdir)/'$$A'.lisp"' \
	         '"'$$A'.$(LISPOBJEXT)")' >> makedep.lisp ; \
	done
	echo '#-(OR :GCL :CMU)(maybe-compile "$(srcdir)/sys-pkg.lisp"' \
	             '"sys-pkg.$(LISPOBJEXT)")' >> makedep.lisp
	echo '(quit)' >> makedep.lisp
	echo '(load "makedep.lisp")' | ${LOADSYS}
	echo '(in-package "FRICAS-LISP")' > makedep2.lisp
	echo '(make-program "$(BASE)$@" (quote (' >> makedep2.lisp
	echo '#+(OR :GCL :CMU)"$(srcdir)/sys-pkg.lisp"' \
	     '#-(OR :GCL :CMU)"sys-pkg.$(LISPOBJEXT)"' >> makedep2.lisp
	echo '#+:GCL"$(srcdir)/interp-proclaims.lisp"' >> makedep2.lisp
	for A in util ${DEP} primitives \
                 `cat makedep2.lst` ; do \
              echo '"'$$A'.$(LISPOBJEXT)"' >> makedep2.lisp ; \
        done
	echo ')))' >>  makedep2.lisp
	cat makedep2.lisp | ${LOADSYS}
	@ echo 4 ${DEPSYS} created
.PHONY: all-axiomsys

all-axiomsys: ${AXIOMSYS}

${AXIOMSYS}:
	echo '(defparameter FRICAS-LISP::*building-axiomsys* t)' \
	   '(load "makeint.lisp") #-:ecl(VMLISP::reclaim)' \
	   '#+:cmu (setf (ext:search-list "libspad:")' \
	   '(list "${AXIOM}/lib/" "${libdir}/fricas/target/${target}/lib/"))' \
	   '#+:cmu (setq ext:*top-level-auto-declare* t)' \
	   '#+:cmu (setq *compile-verbose* nil)' \
	   '#+:cmu (setq *compile-print* nil)' \
	   '#+:cmu (declaim (optimize (ext:inhibit-warnings 3)))' \
	   '#+:ecl(FRICAS-LISP::make-program "$(BASE)$@" nil)' \
	   '#-:ecl(BOOT::spad-save "$(BASE)$@" t)' \
             | DAASE="$(BASE)$(axiom_targetdir)" ${LOADSYS}
	@ echo 6a ${AXIOMSYS} created
exposed.lsp: $(axiom_src_algdir)/exposed.lsp.pamphlet
	@ echo 615 making $@ from $<
	$(axiom_build_document) --tangle --output=$@ $<

$(axiom_targetdir)/algebra/exposed.$(FASLEXT) : exposed.lsp ${DEPSYS}
	@ echo 616 making $@ from exposed.lsp
	echo '(progn  (compile-file "exposed.lsp" :output-file' \
	      ' "$(BASE)$(axiom_targetdir)/algebra/exposed.$(FASLEXT)") (${BYE}))' \
		| ${DEPSYS}

database.date:
	@ echo 617 the database was updated...remaking interpsys
	touch database.date

AUTO_from_IN=\
 bootlex def parse parsing postpar preparse s-parser

$(addprefix $(AUTO)/, $(addsuffix .$(LISPOBJEXT), $(AUTO_from_IN))): \
   $(AUTO)/%.$(LISPOBJEXT) : %.$(LISPOBJEXT)
	cp $< $@

IN_from_MID=\
 as buildom cattable clammed compat compress \
 database format fortcall g-cndata g-error g-opt \
 g-timer hashcode hypertex \
 i-analy i-code i-coerce i-coerfn i-eval i-funsel \
 i-intern interop i-map i-output i-resolv i-spec1 i-spec2 \
 i-syscmd i-toplev i-util lisplib match msgdb \
 newfort nrunfast nrungo nrunopt pathname \
 record rulesets server setvars setvart simpbool \
 template termrw trace

$(addsuffix .$(LISPOBJEXT), $(IN_from_MID)): \
    %.$(LISPOBJEXT) : %.clisp
	@ echo 10 making $@ from $<
	echo '(in-package "FRICAS-LISP")' \
	     '(progn  (fricas-compile-file "$<" :output-file "$@") (${BYE}))' \
	     | ${DEPSYS}

AUTO_from_MID=\
 apply ax bc-matrix bc-misc bc-solve bc-util br-con br-data br-op1 \
 br-op2 br-prof br-saturn br-search br-util category c-doc \
 compiler c-util define functor htcheck ht-root htsetvar ht-util \
 info iterator modemap nruncomp package profile topics 

$(patsubst %, $(AUTO)/%.$(LISPOBJEXT), $(AUTO_from_MID) showimp): \
    $(AUTO)/%.$(LISPOBJEXT) : %.clisp
	@ echo 10 making $@ from $<
	echo '(in-package "FRICAS-LISP")' \
	     '(progn (fricas-compile-file "$<" :output-file "$(BASE)$@")' \
	     ' (${BYE}))' | ${DEPSYS}

MID_from_IN= $(IN_from_MID) $(AUTO_from_MID)

$(addsuffix .clisp, $(MID_from_IN)): \
    %.clisp : $(srcdir)/%.boot
	@ echo 11 making $@ from $<
	echo '(progn (boottran::boottocl "$<" "$@") (${BYE}))' | ${DEPSYS}

