# $Id: Imakefile,v 1.50 1998/08/23 08:54:15 queinnec Exp $
# ///////////////////////////////////////////////////////////////////////
#                    //          //
#                   // DMeroon  //  & Icslas
#                  //          //
# A continuation-based causal distributed class-based object system.
# Christian.Queinnec@polytechnique.fr
# ///////////////////////////////////////////////////////////////////////

# This is the Imakefile/Makefile to build DMeroon and Icslas.

# The simplest way to regenerate DMeroon is to run:
#                      make
# Two variables may be specified additionally. If they are absent then
# make will ask you for them. These variables are TARGET (specifying 
# the directory where to build DMeroon), SUPPORT which imposes the 
# support language to use. 

# After choosing your support language, you will have to look or edit
# the config/SUPPORT.mkf configuration file to mention the correct paths.
# SUPPORT is the name of the binding language.

# NOTE: This Imakefile can be used as a Makefile either. The Makefile
# of the distribution is a copy of this Imakefile. But don't, Don't, DON'T
# run xmkmf or similar things on it, just run `make' or `make -f
# Imakefile' if you inadvertently altered the distributed Makefile.

# You may build for more than one support language provided they are
# built in different TARGET subdirectories. Once built, you may change
# the name of the generated TARGET directory. Normally this is what is
# silently done.

###################################################################
# Normally, you should not edit what follows.
##################################################################
# Regenerate a specialized Makefile in TARGET directory then run it.
# It is now sufficient to run make in the TARGET directory with the
# TOP variable telling where are the source files (and the current
# Imakefile of course).

# This first entry checks that the SUPPORT and TARGET variables are
# correctly set. SUPPORT tells what support language to use, TARGET
# specified where to compile files [TARGET must be an absolute path].

first : 
	@Commands/propose.sh "${SUPPORT}" "${TARGET}"

# When SUPPORT and TARGET are correctly specified, you can directly 
# invoke the work entry of that Imakefile (which in turn invokes the
# world [By default, world means regenerate DMeroon] entry of the
# generated TARGET/Makefile).

WORLD=world

work : mkdir
work : create-target-config-files
work : create-target-makefile 
	@echo "-------Rebuilding in ${TARGET} ..."
	cd ${TARGET} ; . ./config.sh ; exec make ${WORLD}

# Create, if not already done, a TARGET directory. It is usually a 
# subdirectory of o/ with a name based on the architecture of the machine.
# This directory is where all compilation products will go.
# mkdirhier is adapted from the X11 distribution.

mkdir :
	-Commands/mkdirhier ${TARGET}

# Determine the type of the machine. Use a variation of Bigloo's
# tools/dmarch program. Copy the dmarch.c file not to have a dmarch.o 
# left in the config subdirectory. 
# NOTE: Here CC and CFLAGS are the default ones so dmarch.c should be
# compilable by gcc as well as cc. This implies traditional C style.
# If you do not have a cc compiler then try to use gcc, if still
# a problem then report the error.

TMP	=	/tmp

${TMP}/dmarch.c : config/dmarch.c
	-rm -f ${TMP}/dmarch.c
	cp config/dmarch.c ${TMP}/dmarch.c
${TMP}/dmarch : ${TMP}/dmarch.c
	-cd ${TMP} ; ${CC} ${CFLAGS} -o dmarch dmarch.c
	if [ -f ${TMP}/dmarch ] ; then ${TMP}/dmarch ; else \
		${MAKE} -f Imakefile CC=gcc ${TMP}/dmarch ; fi
	if [ -f ${TMP}/dmarch ] ; then ${TMP}/dmarch ; else \
		echo "Cannot find a C compiler" ; exit 1 ; fi

# Record the setting of some (shell,make) variables. So they can be used
# consistently throughout various makefiles or Scheme files. There is a 
# hack for config.mkf which is enriched with config/$USER.mkf if it exists.
# This allows for the user to customize config.mkf.

create-target-config-files : ${TMP}/dmarch
create-target-config-files : ${TARGET}/config.mkf
create-target-config-files : ${TARGET}/config.sh
create-target-config-files : ${TARGET}/config.scm

${TARGET}/config.mkf : 
	( ARCH=`${TMP}/dmarch` ;\
	  SITE=`hostname | awk -F. '{ print $$1 }' ` ; \
	  export ARCH SITE ; \
	  echo TOP=`pwd` ; \
	  echo "SITE=$$SITE" ; \
	  echo "ARCH=$$ARCH" ; \
	  echo TARGET=`cd ${TARGET}; pwd` ; \
	  echo HOSTTYPE="${HOSTTYPE}" ; \
	  echo SUPPORT="${SUPPORT}" ; \
	  echo ;\
	  echo "#define SITE_is_$$SITE   1" ; \
	  echo "#define ARCH_is_$$ARCH   1" ; \
	  echo "#define SUPPORT_is_${SUPPORT}    1" ;\
	  echo ;\
	  ) > ${TARGET}/config.mkf

${TARGET}/config.sh : 
	( echo TOP=`pwd` ; \
	  echo ARCH=`${TMP}/dmarch` ; \
	  echo SITE=`hostname | awk -F. '{ print $$1 }' ` ; \
	  echo TARGET=`cd ${TARGET}; pwd` ; \
	  echo HOSTTYPE="${HOSTTYPE}" ; \
	  echo SUPPORT="${SUPPORT}" ; \
	  echo "export TOP ARCH SITE TARGET HOSTTYPE" ; \
	  ) > ${TARGET}/config.sh

${TARGET}/config.scm :
	( echo "(define TOP \"`pwd`\")" ;\
	  echo "(define ARCH \"`${TMP}/dmarch`\")" ; \
	  echo "(define SITE \"`hostname | awk -F. '{ print $$1 }' `\")" ; \
	  echo "(define TARGET \"`cd ${TARGET}; pwd`\")" ; \
	  echo "(define HOSTTYPE \"${HOSTTYPE}\")" ; \
	  echo "(define SUPPORT \"${SUPPORT}\")" ; \
	  ) > ${TARGET}/config.scm

# Regenerate a Makefile, use imake for that. Don't run xmkmf or
# equivalent on this Imakefile. While generating this Makefile, some
# extra variables can be used for customization: one indicates the
# SITE, the other indicates the type of the cpu as discovered by dmarch.
# They are all specified in the config.mkf file. 

create-target-makefile : ${TARGET}/Makefile

${TARGET}/Makefile : ${TMP}/dmarch 
${TARGET}/Makefile : ${TARGET}/config.mkf
${TARGET}/Makefile : Imakefile config/Site.def 
${TARGET}/Makefile : config/Imake.tmpl config/Imake.rules
${TARGET}/Makefile : config/all.mkf config/test.mkf
${TARGET}/Makefile : config/${SUPPORT}.mkf
	if [ -r config/$$USER.mkf ] ;\
	then FLAG="-DUSER_MAKEFILE=<config/$$USER.mkf>" ;\
	else true; fi ;\
	imake -v -f config/all.mkf -Iconfig -s ${TARGET}/Makefile \
		-DCONFIG="<${TARGET}/config.mkf>" \
		-DUSER_is_$$USER=1 \
		-DSUPPORT_MAKEFILE="<${SUPPORT}.mkf>" \
		$$FLAG

#end of Imakefile.
