##############################################################################
#
# Makefile
#
# Make program to build the Foam library.
#
# Copyright (c) 1990-2007 Aldor Software Organization Ltd (Aldor.org).
#
##############################################################################
#
# Targets: all, junk, 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
#
##############################################################################
#
#  2006/Dec/22
#
#  On Solaris, the value of PWD does not change when make is invoked
#  recursively.  Consequently, it is necessary to explicitly set it
#  at the start of the makefile.
#
PWD = $(shell pwd)

SHELL	= /bin/sh

#
# Environment
#
# $(ALDORROOT) is retrieved from the environment
IncDir	= $(ALDORROOT)/include
LibDir	= $(ALDORROOT)/lib
LispDir = $(ALDORROOT)/lib
LinkDir = links

LALDLIB	 = $(ALDORROOT)/lib/libfoamlib.al
LALDFOAM = $(LibDir)/libfoam.al
LIBFOAM	 = $(LibDir)/libfoam.a

SYS      =
SYSARG = `if [ "$(SYS)" != "" ] ; then echo "-Csys=$(SYS)" ; else echo "" ; fi`
AXLCDB	  = -W check -Csmax=0 -Qno-cc -Cargs=-g 
#AXLCDB	  = -W check -Csmax=0 -Fc -Zdb -Qno-cc
AXLFLAGS  = -Z db -Fc -Q8 -Qinline-all -M no-ALDOR_W_OverRideLibraryFile $(AXLCDB)

AR	= ar rv
RM	= rm -f
CP	= cp

ALDOR  	=aldor
DOALDOR =$(ALDORROOT)/toolbin/doaldor
OBJ	=o

#Portability
ifeq ($(MACHINE), win32gcc)
	ALDOR=aldor.sh
endif

ifeq ($(MACHINE), win32msvc)
	ALDOR	=aldor.sh
	OBJ	=obj
	LIBFOAM	= $(LibDir)/libfoam.lib
endif

#
# Object lists
#
AxlObjs	= $(LIBFOAM)(runtime.$(OBJ))
CObjs	= $(LIBFOAM)(foam_c.$(OBJ)) $(LIBFOAM)(output.$(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:	linksdir $(LispTargs) $(LibTargs) getcheck

junk::
	@ ls -d $(OKFILES) * | sort | grep -v CVS | uniq -u

clean::
	@ rm -f *~ core

libclean:
	@ rm -f $(LALDFOAM) $(LIBFOAM)

linksdir:
	(cd links ; $(MAKE) all LibDir=$(LibDir) SYS=$(SYS))

junk::
	-(cd links ; $(MAKE) junk)

clean::
	-(cd links ; $(MAKE) clean)

library: $(LIBFOAM) linksdir

# The doranlib dependency ensures that doranlib gets called.
$(LALDFOAM): $(AxlObjs) doranlib		; @ echo doranlib $@
$(LIBFOAM): $(AxlObjs) doranlib			; doranlib $@

doranlib:
	@echon

#
# Actually make something
#
$(LispDir)/foam_l.lsp: foam_l.lsp
	$(CP) foam_l.lsp $(LibDir)/foam_l.lsp

$(LIBFOAM)(runtime.$(OBJ)) \
$(LALDFOAM)(runtime.ao) \
$(LispDir)/runtime.lsp: runtime.as $(LALDLIB)
	@$(DOALDOR) -l $(LibDir) runtime foam -Flsp -W runtime $(AXLCDB) $(SYSARG)
	@if [ -f $$ALDORROOT/srclib/any_as.$(OBJ) ] ; then ar rcv $(LIBFOAM) $$ALDORROOT/srclib/any_as.$(OBJ) ; fi

#
# There must be no gets in runtime (gets to rtexns are allowed).
#
getcheck:
	@ ar x $(LALDFOAM) runtime.ao && $(ALDOR) -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

