# The purpose of this Makefile is to create a fresh Lisp image, 
# $(OUT)/lisp, augmented with a useful routines defined in
# fricas-lisp file.  This effectively creates our own dialect
# of Lisp, which is used during the rest of build process

fricas_quiet_flags = @fricas_quiet_flags@
lisp_flavor=@fricas_lisp_flavor@
axiom_gcl_rsym_hack = @axiom_gcl_rsym_hack@
axiom_c_runtime_extra = @axiom_c_runtime_extra@
fricas_algebra_optimization = @fricas_algebra_optimization@
GMP_WRAP_SO_TARGET = @GMP_WRAP_SO_TARGET@
LISP_CMD = $(FRICAS_LISP) $(fricas_quiet_flags)

## Create a fresh image for building interpsys and AXIOMsys
OUT = $(axiom_build_bindir)

subdir = src/lisp/

pamphlets = 

build_libdir = $(abs_top_builddir)/src/lib

lisp_DEPENDENCIES = $(build_libdir)/cfuns-c.o \
		    $(build_libdir)/sockio-c.o \
		    $(build_libdir)/libspad.a

ifeq ($(lisp_flavor),sbcl)
NUM_SB = num_sb.lisp
else
NUM_SB =
endif

.PHONY: all all-lisp
all: all-ax all-lisp

all-ax all-lisp: $(OUT)/lisp$(EXEEXT)

## Create a fresh image for building interpsys and AXIOMsys
## These objects files are the C runtime support
## and must be compiled into the Lisp image,
## as they must be present in the final interpreter
## and image.
## Use $(BASE) because Lisp (gcl) is native on Windows
lisp_c_objects = \
		$(BASE)$(build_libdir)/bsdsignal.o \
		$(BASE)$(build_libdir)/cfuns-c.o \
		$(BASE)$(build_libdir)/sockio-c.o

$(OUT)/lisp$(EXEEXT): do_it.$(lisp_flavor)

do_it.gcl: fricas-lisp.lisp fricas-package.lisp fricas-config.lisp
	$(axiom_gcl_rsym_hack)
	echo '(load "fricas-package.lisp") (load "fricas-config.lisp")' \
	     '(load "fricas-lisp.lisp")' \
	     '(setq compiler::*default-system-p* t)' \
	     '(compile-file "fricas-lisp.lisp")' | $(FRICAS_LISP)
	echo '(compiler::link nil "prelisp" ' \
              ' (format nil "(progn (let ((*load-path* (cons ~S *load-path*))'\
                                        ' (si::*load-types* ~S))' \
                                       ' (compiler::emit-fn t))' \
                                  ' (when (fboundp (quote si::sgc-on))' \
                                        ' (si::sgc-on nil))' \
                                  ' (setq compiler::*default-system-p* t))"' \
                      ' si::*system-directory* (quote (list ".lsp")))' \
               '  "$(lisp_c_objects) $(axiom_c_runtime_extra)")' \
            | $(FRICAS_LISP)
	echo '(load "fricas-package.lisp") (load "fricas-config.lisp")' \
	     '(load "fricas-lisp.$(OBJEXT)")' \
	     '(in-package "FRICAS-LISP") (save-core "$(OUT)/lisp$(EXEEXT)")' \
	    | ./prelisp$(EXEEXT)
	$(STAMP) $@

fricas-lisp.lisp: $(srcdir)/fricas-lisp.lisp
	cp $< $@

fricas-package.lisp: $(srcdir)/fricas-package.lisp
	cp $< $@

num_sb.lisp: $(srcdir)/num_sb.lisp
	if [ x$(GMP_WRAP_SO_TARGET) != x ] ; then \
	    cp $< $@ ; \
	else \
	    (echo '(in-package "FRICAS-LISP")' ; \
	     echo '(defun init-gmp(x) nil)' ; \
	     echo '(defun unistall-gmp-multiplication() nil)') > $@ ; \
	fi

fricas-config.lisp:
	echo '(in-package "FRICAS-LISP")' > $@
	echo '(defparameter algebra-optimization' \
	     '(quote $(fricas_algebra_optimization)))' >> $@

do_it.ecl: fricas-lisp.lisp fricas-package.lisp fricas-config.lisp
	echo "(in-package \"FRICAS-LISP\")" > fricas-ecl.lisp
	echo "(defvar *fricas-extra-c-files* (quote (" \
	     $(patsubst %, "\"%\"", $(lisp_c_objects)) \
	     ")))" >> fricas-ecl.lisp
	echo "(defvar *fricas-initial-lisp-objects* (quote (" \
	      $(patsubst %, "\"$(BASE)$(abs_builddir)/%\"", \
	          fricas-package.o fricas-config.o fricas-ecl.o \
		  fricas-lisp.o) ")))" \
	            >> fricas-ecl.lisp
	echo "(defvar *fricas-initial-lisp-forms* nil)" >> fricas-ecl.lisp
	echo '(load "fricas-package.lisp")' \
	     '(load "fricas-config.lisp")' \
	     '(load "fricas-ecl.lisp")' \
	     '(load "fricas-lisp.lisp")' \
	     '(in-package "FRICAS-LISP")' \
	     '(fricas-compile-file "fricas-package.lisp")' \
	     '(fricas-compile-file "fricas-config.lisp")' \
	     '(fricas-compile-file "fricas-ecl.lisp")' \
	     '(fricas-compile-file "fricas-lisp.lisp")' \
	     '(make-program "${OUT}/lisp$(EXEEXT)" nil)' | $(FRICAS_LISP)
	$(STAMP) $@

do_it.sbcl do_it.clisp do_it.openmcl do_it.lispworks: \
   fricas-lisp.lisp fricas-package.lisp fricas-config.lisp $(NUM_SB)
	echo '(load "fricas-package.lisp")' \
	    '(load "fricas-config.lisp")' \
            '#+:sbcl (load (compile-file "num_sb.lisp"))' \
	    '(load (compile-file "fricas-lisp.lisp"))' \
            '(in-package "FRICAS-LISP") (save-core "${OUT}/lisp$(EXEEXT)")' \
         | $(FRICAS_LISP)
	 $(STAMP) $@

do_it.cmucl: fricas-lisp.lisp fricas-package.lisp \
           fricas-config.lisp
	echo '(load "fricas-package.lisp")' \
	    '(load "fricas-config.lisp")' \
	    '(setf (ext:search-list "libspad:")' \
	    '(quote ("${axiom_target_libdir}/")))' \
	    '(ext:load-foreign "libspad:libspad.so")' \
	    '(load (compile-file "fricas-lisp.lisp"))' \
	    '(defswitch "-")' \
            '(in-package "FRICAS-LISP") (save-core "${OUT}/lisp$(EXEEXT)")' \
         | $(FRICAS_LISP)
	 $(STAMP) $@

# Build GCL takes quite a while, so we don't clean the
# directory in mostlyclean.  Rather, we do that in clean.
mostlyclean-local:

clean-local: mostlyclean
	-rm -f $(OUT)/lisp$(EXEEXT) prelisp$(EXEEXT) do_it.$(lisp_flavor)
	-rm -f fricas-lisp.$(LISPOBJEXT) num_sb.$(LISPOBJEXT) fricas-config.lisp

distclean-local: clean-local
	-rm -f Makefile
