##############################################################################
#
# Makefile
#
# Make program to compile the Foam runtime files
#
# Copyright (c) 1990-2007 Aldor Software Organization Ltd (Aldor.org).
#
##############################################################################
#
# Targets: all, junk, clean, quick
#
#    make all	-- make the runtime files 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
#
##############################################################################
#
# We do not use the compiler archive since these files may be compiled
# with different options, or different compilers.
#
##############################################################################
#
#  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
AR	= ar rv
CP	= cp
LN	= ln
RM	= rm -f
ifeq "$(findstring sun4os57g,$(MACHINE))" "sun4os57g"
  CCPROG=docc
else
  CCPROG=unicl -Wstdc -g
endif 

#CCPROG	= unicl $(UNICLARGS)
#CCPROG  = docc # use if unicl whinges about ansi constructs

# C flags for building the FOAM run time system.
ifeq "$(SYS)" ""
 SYSARG=
else
 SYSARG=-Wsys=$(SYS)
endif 

#CFLAGS	= -g -DFOAM_RTS $(SYSARG) -DSTO_CAN_BLACKLIST -DSTO_DEBUG_DISPLAY
#CFLAGS	= -O -DNDEBUG -DFOAM_RTS $(SYSARG) -DSTO_DEBUG_DISPLAY
#OFLAGS	= -g # -O -DNDEBUG -Wopts=-pg # -g -Wopts=-pg
OFLAGS	= -O -DNDEBUG # -g -Wopts=-pg
#DFLAGS	= -DSTO_DEBUG_DISPLAY -DSTO_CENSUS
DFLAGS =
CFLAGS	= -DFOAM_RTS $(SYSARG) $(DFLAGS) $(OFLAGS)

#
# Environment
#
IncDir	= $(ALDORROOT)/include
LibDir	= $(ALDORROOT)/lib
SRC     =../../../src
LIBFOAM	= $(LibDir)/libfoam.a
BUILDARG = $(ALDORROOT)/toolbin/buildarg
MACH	  = `echo $(MACHINE) | awk -F- '{print $$1}'`

#Portability
ifeq ($(MACHINE), win32gcc)
	CCPROG=unicl.sh -Wstdc 
endif

#
# File Lists
#
PORTH0        = axlport.h  platform.h cconfig.h optcfg.h foamopt.h compopt.h
STDCH0        = assert.h0 ctype.h0 errno.h0  float.h0  limits.h0 \
		locale.h0 math.h0  setjmp.h0 signal.h0 stdarg.h0 \
		stddef.h0 stdio.h0 stdlib.h0 string.h0 time.h0   \
	        unistd.h0
OPSYSH0       = os_cms.c os_dos.c os_os2.c os_unix.c os_vms.c \
		os_macs7.c os_win32.c
FOAMHX        =	axlgen.h axlgen0.h editlevels.h debug.h fluid.h format.h test.h

FOAMC	      = stdc.c cport.c opsys.c btree.c store.c memclim.c bigint.c util.c \
		foam_c.c foam_i.c output.c table.c xfloat.c foamopt.c compopt.c \
		dword.c foam_cfp.c timer.c
#FOAMAS	      = any_as.s

FOAMH         = $(PORTH0) $(STDCH0) $(OPSYSH0) $(FOAMC:.c=.h) $(FOAMHX)
COMMONFILES   = $(FOAMH) $(FOAMC) $(FOAMAS)
OKFILES       = Makefile NMakefile descrip.mms $(COMMONFILES)

OBJECTS       = $(LIBFOAM)(stdc.o)  $(LIBFOAM)(cport.o) $(LIBFOAM)(opsys.o)  \
		$(LIBFOAM)(btree.o) $(LIBFOAM)(store.o) $(LIBFOAM)(bigint.o) \
		$(LIBFOAM)(util.o)  $(LIBFOAM)(foam_c.o) $(LIBFOAM)(foam_i.o) \
		$(LIBFOAM)(foam_cfp.o)  $(LIBFOAM)(output.o) \
		$(LIBFOAM)(table.o) $(LIBFOAM)(xfloat.o) $(LIBFOAM)(foamopt.o) \
		$(LIBFOAM)(dword.o) $(LIBFOAM)(compopt.o) $(LIBFOAM)(timer.o)

IncTargs = $(IncDir)/foam_c.h $(IncDir)/optcfg.h $(IncDir)/foamopt.h

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

#
# Generic rules
#
all: .cvsignore library

reallyclean:
	@ ( \
		for file in $(COMMONFILES) ; \
		do \
			echo $$file ; \
		done | sort | uniq | xargs rm \
	)

library: $(IncTargs) $(COMMONFILES) $(LIBFOAM)

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

clean:
	@ $(RM) *~

include: $(IncTargs) $(COMMONFILES)

links:	$(COMMONFILES)

$(IncDir)/foam_c.h: foam_c.h
	$(CP) foam_c.h $(IncDir)/foam_c.h

$(IncDir)/optcfg.h: optcfg.h
	$(CP) optcfg.h $(IncDir)/optcfg.h

$(IncDir)/foamopt.h: foamopt.h
	$(CP) foamopt.h $(IncDir)/foamopt.h

.cvsignore:
	echo .cvsignore $(COMMONFILES) > .cvsignore
#
# Rules to get the common source files (as links or copies)
#
axlport.h:	$(SRC)/axlport.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
platform.h:	$(SRC)/platform.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
cconfig.h:	$(SRC)/cconfig.h ;	 ( $(RM) $@ ; $(LN) $? $@ )

assert.h0:	$(SRC)/assert.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
ctype.h0:	$(SRC)/ctype.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
errno.h0:	$(SRC)/errno.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
float.h0:	$(SRC)/float.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
limits.h0:	$(SRC)/limits.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
locale.h0:	$(SRC)/locale.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
math.h0:	$(SRC)/math.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
setjmp.h0:	$(SRC)/setjmp.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
signal.h0:	$(SRC)/signal.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
stdarg.h0:	$(SRC)/stdarg.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
stddef.h0:	$(SRC)/stddef.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
stdio.h0:	$(SRC)/stdio.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
stdlib.h0:	$(SRC)/stdlib.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
string.h0:	$(SRC)/string.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
time.h0:	$(SRC)/time.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )
unistd.h0:	$(SRC)/unistd.h0 ;	 ( $(RM) $@ ; $(LN) $? $@ )

os_cms.c:	$(SRC)/os_cms.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_dos.c:	$(SRC)/os_dos.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_os2.c:	$(SRC)/os_os2.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_unix.c:	$(SRC)/os_unix.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_vms.c:	$(SRC)/os_vms.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_macs7.c:	$(SRC)/os_macs7.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
os_win32.c:	$(SRC)/os_win32.c ;	 ( $(RM) $@ ; $(LN) $? $@ )

axlgen.h:	$(SRC)/axlgen.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
axlgen0.h:	$(SRC)/axlgen0.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
editlevels.h:	$(SRC)/editlevels.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
debug.h:	$(SRC)/debug.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
fluid.h:	$(SRC)/fluid.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
format.h:	$(SRC)/format.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
test.h: 	$(SRC)/test.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )

stdc.h: 	$(SRC)/stdc.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
cport.h:	$(SRC)/cport.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
opsys.h:	$(SRC)/opsys.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
btree.h:	$(SRC)/btree.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
store.h:	$(SRC)/store.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
memclim.h:	$(SRC)/memclim.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
bigint.h:	$(SRC)/bigint.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
dword.h:	$(SRC)/dword.h ;	 ( $(RM) $@ ; $(LN) $? $@ )
util.h: 	$(SRC)/util.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
table.h: 	$(SRC)/table.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
xfloat.h: 	$(SRC)/xfloat.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )

stdc.c: 	$(SRC)/stdc.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
cport.c:	$(SRC)/cport.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
opsys.c:	$(SRC)/opsys.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
btree.c:	$(SRC)/btree.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
memclim.c:	$(SRC)/memclim.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
store.c:	$(SRC)/store.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
bigint.c:	$(SRC)/bigint.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
dword.c:	$(SRC)/dword.c ;	 ( $(RM) $@ ; $(LN) $? $@ )
util.c: 	$(SRC)/util.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
table.c: 	$(SRC)/table.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
xfloat.c: 	$(SRC)/xfloat.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )

foam_c.h: 	$(SRC)/foam_c.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foam_i.h: 	$(SRC)/foam_i.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foam_cfp.h: 	$(SRC)/foam_cfp.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foamopt.h: 	$(SRC)/foamopt.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
compopt.h: 	$(SRC)/compopt.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
optcfg.h: 	$(SRC)/optcfg.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
output.h: 	$(SRC)/output.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )
timer.h: 	$(SRC)/timer.h ; 	 ( $(RM) $@ ; $(LN) $? $@ )

foam_c.c: 	$(SRC)/foam_c.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foam_i.c: 	$(SRC)/foam_i.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foam_cfp.c: 	$(SRC)/foam_cfp.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
any_as.s: 	$(SRC)/any_as.s ; 	 ( $(RM) $@ ; $(LN) $? $@ )
foamopt.c: 	$(SRC)/foamopt.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
compopt.c: 	$(SRC)/compopt.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
output.c: 	$(SRC)/output.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )
timer.c: 	$(SRC)/timer.c ; 	 ( $(RM) $@ ; $(LN) $? $@ )

#
# Rules to compile the common files
#

# The doranlib dependency ensures that doranlib gets called.
$(LIBFOAM): $(OBJECTS) doranlib ; doranlib $@
doranlib: 			; @echon


$(LIBFOAM)(stdc.o):   stdc.c   $(PORTH0) $(STDCH0)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/stdc.c" ; \
	  $(CCPROG) $(CFLAGS) -c  $$SRC/stdc.c ;  \
	  $(AR) $(LIBFOAM) stdc.o ; \
	  $(RM) stdc.o)

$(LIBFOAM)(cport.o):  cport.c  $(PORTH0) $(STDCH0)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/cport.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/cport.c ;  \
	  $(AR) $(LIBFOAM) cport.o ; \
	  $(RM) cport.o)

$(LIBFOAM)(opsys.o):  opsys.c  $(PORTH0) $(STDCH0) $(OPSYSH0)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/opsys.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/opsys.c ;  \
	  $(AR) $(LIBFOAM) opsys.o ; \
	  $(RM) opsys.o)

$(LIBFOAM)(btree.o):  btree.c  $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/btree.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/btree.c ;  \
	  $(AR) $(LIBFOAM) btree.o ; \
	  $(RM) btree.o)

store $(LIBFOAM)(store.o):  store.c  $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/store.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/store.c ;  \
	  $(AR) $(LIBFOAM) store.o ; \
	  $(RM) store.o)

memclim $(LIBFOAM)(memclim.o):  memclim.c  $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/memclim.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/memclim.c ;  \
	  $(AR) $(LIBFOAM) memclim.o ; \
	  $(RM) memclim.o)

$(LIBFOAM)(bigint.o): bigint.c $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/bigint.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/bigint.c ;  \
	  $(AR) $(LIBFOAM) bigint.o ; \
	  $(RM) bigint.o)

$(LIBFOAM)(dword.o): dword.c $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/dword.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/dword.c ;  \
	  $(AR) $(LIBFOAM) dword.o ; \
	  $(RM) dword.o)

$(LIBFOAM)(util.o):   util.c   $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/util.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/util.c ;  \
	  $(AR) $(LIBFOAM) util.o ; \
	  $(RM) util.o)

$(LIBFOAM)(table.o):   table.c   $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/table.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/table.c ;  \
	  $(AR) $(LIBFOAM) table.o ; \
	  $(RM) table.o)

$(LIBFOAM)(xfloat.o):   xfloat.c   $(FOAMH)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/xfloat.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/xfloat.c ;  \
	  $(AR) $(LIBFOAM) xfloat.o ; \
	  $(RM) xfloat.o)

foam_c $(LIBFOAM)(foam_c.o): foam_c.c $(IncTargs)
	@(SRC=`pwd` ; FINC=`pwd` ; \
	  cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/foam_c.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/foam_c.c ;  \
	  $(AR) $(LIBFOAM) foam_c.o ; \
	  $(RM) foam_c.o)


foam_cfp $(LIBFOAM)(foam_cfp.o): foam_cfp.c $(IncTargs)
	@(SRC=`pwd` ; FINC=`pwd` ; \
	  cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/foam_cfp.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/foam_cfp.c ;  \
	  $(AR) $(LIBFOAM) foam_cfp.o ; \
	  if [ -f $(ALDORROOT)/srclib/any_as.o ] ; then $(AR) $(LIBFOAM) $(ALDORROOT)/srclib/any_as.o ; fi ; \
	  $(RM) foam_cfp.o)

foam_i $(LIBFOAM)(foam_i.o): foam_i.c $(IncTargs)
	@(SRC=`pwd` ; FINC=`pwd` ; \
	  cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/foam_i.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/foam_i.c ;  \
	  $(AR) $(LIBFOAM) foam_i.o ; \
	  $(RM) foam_i.o)

output $(LIBFOAM)(output.o): output.c $(IncTargs)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/output.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/output.c ;  \
	  $(AR) $(LIBFOAM) output.o ; \
	  $(RM) output.o)

foamopt $(LIBFOAM)(foamopt.o): foamopt.c $(IncTargs)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/foamopt.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/foamopt.c ;  \
	  $(AR) $(LIBFOAM) foamopt.o ; \
	  $(RM) foamopt.o)

compopt $(LIBFOAM)(compopt.o): compopt.c $(IncTargs)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/compopt.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/compopt.c ;  \
	  $(AR) $(LIBFOAM) compopt.o ; \
	  $(RM) compopt.o)
 
timer $(LIBFOAM)(timer.o): timer.c $(IncTargs)
	@(SRC=`pwd` ; cd $(LibDir) ; \
	  echo "$(CCPROG) $(CFLAGS) -c $$SRC/timer.c" ; \
	  $(CCPROG) $(CFLAGS) -c $$SRC/timer.c ;  \
	  $(AR) $(LIBFOAM) timer.o ; \
	  $(RM) timer.o)
 
