##############################################################################
#
# Makefile
#
# Make program to build the Foam library.
#
# Copyright (c) 1990-2007 Aldor Software Organization Ltd (Aldor.org).
#
##############################################################################
#
# Targets: all, clean, quick
#
#    make all	-- make the Foam library for the AXIOM-XL compiler
#    make junk	-- list files which are not part of the AXIOM-XL compiler
#    make clean -- remove generated files  (e.g. foo.c~)
#
# Parameters:  ALDORROOT
#
#    To override defaults do, e.g.,
#	 make -e ALDORROOT=/spad/local/axiomxl/rs all
#    or
#	 setenv ALDORROOT /spad/local/axiomxl/rs; setenv MAKEFLAGS e; make all
#
##############################################################################

PWD=$(MAKEDIR)
MAKEFLAGS=-nologo
#
# Environment
#
# $(ALDORROOT) is retrieved from the environment
IncDir	= $(ALDORROOT)\include
LibDir	= $(ALDORROOT)\lib
LispDir = $(ALDORROOT)\lib
LinkDir = links

LALDLIB	     = $(LibDir)\libfoamlib.al
LIBFOAMLIB   = $(LibDir)\libfoamlib.lib
LALDFOAM     = $(LibDir)\libfoam.al
LIBFOAM	     = $(LibDir)\libfoam.lib
LIBFOAMBASE = $(LibDir)\libfoam-base.lib

AXLCDB	  = -W check -Csmax=0 -Qno-cc
#AXLCDB	  = -W check -Csmax=0 -Fc -Zdb -Qno-cc
AXLFLAGS  = -Fc -Q8 -Qinline-all -M no-ALDOR_W_OverRideLibraryFile $(AXLCDB)

AR	= lib -nologo
UNIAR   = $(ALDORROOT)\bin\uniar.exe
RM	= delt
CP	= copy

#
# Object lists
#
AxlObjs	= runtime.obj

#
# Target lists
#
LispTargs= $(LispDir)\foam_l.lsp $(LispDir)\runtime.lsp
LibTargs = $(LALDFOAM) $(LIBFOAM)

# Don't delete the libraries if a compilation fails.
.PRECIOUS: $(LibTargs)

#
# generic targets
#
OKFILES	= Makefile NMakefile descrip.mms links scheme \
	  hashax.boot foam_l.lsp foam_s.scm runtime.as


all: $(LispTargs) $(LibTargs)

libclean:
	@ del /s $(LALDFOAM) $(LIBFOAM)

library: $(LIBFOAM)

# The doranlib dependency ensures that doranlib gets called.
$(LALDFOAM): $(AxlObjs)
	$(UNIAR) r $(LALDFOAM) runtime.ao
	@del runtime.ao

$(LIBFOAM): $(LIBFOAMBASE) $(LIBFOAMLIB) $(AxlObjs)
	lib -nologo -out:$@ $(LIBFOAMBASE) $(LIBFOAMLIB) $(AxlObjs)
	@del $(AxlObjs)
	@del $(LIBFOAMBASE)

$(LIBFOAMBASE): 
	@cd links 
	@$(MAKE) /$(MAKEFLAGS) -f NMakefile all LibDir=$(LibDir) SYS=$(SYS)
	@cd ..

#
# Actually make something
#
$(LispDir)\foam_l.lsp: foam_l.lsp
	copy foam_l.lsp $(LibDir)\foam_l.lsp

#@ if [ -f $$ALDORROOT/srclib/any_as.o ] ; then ar rcv $(LIBFOAM) $$ALDORROOT/srclib/any_as.o ; fi
#$(DOALDOR) -l $(LibDir) runtime foam -Flsp -W runtime $(AXLCDB) $(SYSARG)
runtime.obj runtime.ao $(LispDir)\runtime.lsp: \
 runtime.as $(LALDLIB)
	aldor -fc -fo -flsp -fao -W runtime $(AXLCDB) -q3 -qinline-all $(SYSARG) runtime.as
	@move runtime.lsp $(LispDir)\runtime.lsp

#
# There must be no gets in runtime (gets to rtexns are allowed).
#
#getcheck:
#	@ ar x $(LALDFOAM) runtime.ao && aldor.sh -Fc -Csmax=0 runtime.ao && /bin/rm -f runtime.ao
#	-@ grep 'fiFileInitializer(' runtime.c | sed -e 's/^[^"]*"\([^"]*\).*$$/*** Error from Makefile: gets from \1 are not allowed./' | grep -v rtexns | cat

