#
# PCR top-level makefile
#
# Demers, March 12, 1990 3:29:14 pm PST
# AIshigo, December 14, 1988 12:48:51 pm PST
# Weiser, December 1, 1989
# CHauser, December 14, 1989 10:11:44 am PST
#


#
# The following should be set to reflect local installation site:
# For testing purposes, INSTALL_MAN could be set to ${XR_HOME}/testman.  
#
# XR_HOME should almost always be set to the full pathname expansion
# of "..", i.e. the parent of the directory containing this makefile.
#
# (beware of using paths containing .. directly in XR_HOME,
# or INSTALL_MAN.  These are expanded in various contexts where .. will vary.
#

# XR_HOME = /pseudo/xrhome/
XR_HOME = /net/lane/everett/xrhome/
INSTALL_MAN = ${XR_HOME}/testmanl

#
# If the following changes, fix CPDIFF in makefile.common to track it
#
UTILITIES_DIR = ${XR_HOME}/UTILITIES
CPDIFF = ${UTILITIES_DIR}/copyifdifferent
SHAR = ${UTILITIES_DIR}/makescript

#
# The file 'makefile.version' is created automatically, but you can edit it
#   if you wish.  It must define
#
#   VERSION_MAJOR = xx
#   VERSION_MINOR = yy
#   VERSION_LOCAL = zz (distributed version) or "X" (at Xerox) 
#   VERSION_DEV = ".dev" (development version) or empty
#

include makefile.version

VERSION = ${VERSION_MAJOR}_${VERSION_MINOR}.${VERSION_LOCAL}${VERSION_DEV}

#
# Kludge to determine what the next major and minor release names are
#
NEWMINORVERSION = ${VERSION_MAJOR}_`expr ${VERSION_MINOR} + 1`.${VERSION_LOCAL}

NEWMAJORVERSION = `expr ${VERSION_MAJOR} + 1`_${VERSION_MINOR}.${VERSION_LOCAL}


#
# To add a component, change these and change CONFIG_COMPONENTS
#   in the configuration targets below, where appropriate.
#
# To add a configuration, change these and add the appropriate target below.
#

ALL_CONFIGS = OptThreads Threads
ALL_COMPONENTS = boot debugnub dylibload gc interp loading package pcr threads
SUBDIRS = BIN INCLUDE LIB MAN SRC
CLEAN_SUBDIRS = BIN INCLUDE LIB SRC

TARFILE = pcr.tar
SHARFILE = pcr.shar


############################################################
#
#
# Public targets:
#
#
############################################################

#
# Usage: (no target) do nothing.
#

notarget:
	echo "Please read instructions on how to use this stuff."


#
# Usage: make install
#
# (that's a mistake)
#
install:
	echo "You must use install1 and install2"

#
# Usage: make install1
#
# renames the distribution directory to the INSTALLED directory and 
#	builds all the configs
#	
install1:
	name1=`pwd`; name2=`basename $$name1`; if [ $$name2 != DIST ]; then \
		echo "Must install from ${XR_HOME}/DIST only" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} all
	mv ../DIST ../${VERSION}
	( cd .. ; rm -f INSTALLED ; ln -s ${VERSION} INSTALLED )

#
# Usage: make install2
#
# installs the man pages ...
#

install2:
	for dir in ${ALL_COMPONENTS}; do \
		(cd $$dir; $(MAKE) -${MAKEFLAGS} \
			MAN_DIR=${INSTALL_MAN} MAKEPUBLIC=TRUE \
			man); \
		done


#
# Usage:
#   make bounce
#   make bounce_local
#
# (these are equivalent)
#
# Bounce, incrementing local version.
#   For use in making local changes to distributed versions.
#   Do this only from ${XR_HOME}/DEVELOPMENT
#

bounce bounce_local: bounceCheckInner
	@if [ "x${VERSION_LOCAL}" = xX ]; then \
		echo "Use \"make bounce_major\" or \"make bounce_minor\"" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		${MAKE} -${MAKEFLAGS} \
			"VERSION_MAJOR=${VERSION_MAJOR}" \
			"VERSION_MINOR=${VERSION_MINOR}" \
			"VERSION_LOCAL=`expr ${VERSION_LOCAL} + 1`" \
			"VERSION_DEV=" \
			versionInner ; \
		${MAKE} -${MAKEFLAGS} \
			all ; \
		${MAKE} -${MAKEFLAGS} \
			installSelfInner \
	)

#
# Usage: make development
#
# Make a development version from an installed one.
#   Generally used immediately after extracting a distribution.
#
development: developmentCheckInner
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		${MAKE} -${MAKEFLAGS} \
			"VERSION_MAJOR=${VERSION_MAJOR}" \
			"VERSION_MINOR=${VERSION_MINOR}" \
			"VERSION_LOCAL=${VERSION_LOCAL}" \
			"VERSION_DEV=.dev" \
			versionInner ; \
		${MAKE} -${MAKEFLAGS} \
			installSelfInner ; \
	)

#
# Usage: make <config-name>
#
# build named configuration
# store results in ../BIN/config-name, ../LIB/config-name
#

Threads:
	${MAKE} -${MAKEFLAGS} \
	"CONFIG=Threads" \
	"CONFIG_COMPONENTS=boot debugnub dylibload gc interp loading package pcr threads" \
	"CONFIG_CFLAGS=-g -DSUNOS4X -DTHREADS -DFINALIZE -DREALFREE" \
	"CONFIG_AFLAGS=-DSUNOS4X -DTHREADS -DFINALIZE -DREALFREE" \
	configInner

OptThreads:
	${MAKE} -${MAKEFLAGS} \
	"CONFIG=OptThreads" \
	"CONFIG_COMPONENTS=boot debugnub dylibload gc interp loading package pcr threads" \
	"CONFIG_CFLAGS=-O2 -DSUNOS4X -DTHREADS -DFINALIZE -DREALFREE" \
	"CONFIG_AFLAGS=-DSUNOS4X -DTHREADS -DFINALIZE -DREALFREE" \
	configInner

#
# Usage: make all
#
# make all configs
#
all: ${ALL_CONFIGS}


#
# Usage: make clean
#
clean:
	for dir in ${ALL_COMPONENTS}; do \
		(cd $$dir; make clean); \
		done
	rm -rf LIB/* BIN/* INCLUDE/* SRC/* MAN/* *.o *.a *.bak *~

##################################################################
#
#
# "private" targets -- for use before distribution
#
#
##################################################################

#
# Usage: make bounce_major
#
# Bounce, incrementing major version.
#
#   Do this only from ${XR_HOME}/DEVELOPMENT
#

bounce_major: bounceCheckInner
	@if [ "x${VERSION_LOCAL}" != xX ]; then \
		echo "Please don't do major bounce of distribution" ; \
		exit 1 ; \
	fi
	@if [ -h ${NEWMAJORVERSION} -o -d ${NEWMAJORVERSION} -o -f \
	${NEWMAJORVERSION} ] ; then \
		echo "${NEWMAJORVERSION} directory already exists" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		${MAKE} -${MAKEFLAGS} \
			"VERSION_MAJOR=`expr ${VERSION_MAJOR} + 1`" \
			"VERSION_MINOR=0" \
			"VERSION_LOCAL=X" \
			"VERSION_DEV=" \
			versionInner ; \
		${MAKE} -${MAKEFLAGS} \
			all ; \
		${MAKE} -${MAKEFLAGS} \
			installSelfInner \
	)
	${MAKE} -${MAKEFLAGS} \
		"VERSION_MAJOR=`expr ${VERSION_MAJOR} + 1`" \
		"VERSION_MINOR=0" \
		"VERSION_LOCAL=X" \
		versionInnerDev 

#
# Usage: make bounce_minor
#
# Bounce, incrementing minor version.
#
#   Do this only from ${XR_HOME}/DEVELOPMENT
#

bounce_minor: bounceCheckInner
	@if [ "x${VERSION_LOCAL}" != xX ]; then \
		echo "Please don't do minor bounce of distribution" ; \
		exit 1 ; \
	fi
	@if [ -h ${NEWMINORVERSION} -o -d ${NEWMINORVERSION} -o -f \
	${NEWMINORVERSION} ] ; then \
		echo "${NEWMINORVERSION} directory already exists" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		${MAKE} -${MAKEFLAGS} \
			"VERSION_MAJOR=${VERSION_MAJOR}" \
			"VERSION_MINOR=`expr ${VERSION_MINOR} + 1`" \
			"VERSION_LOCAL=X" \
			"VERSION_DEV=" \
			versionInner ; \
		${MAKE} -${MAKEFLAGS} \
			all ; \
		${MAKE} -${MAKEFLAGS} \
			installSelfInner \
	)
	${MAKE} -${MAKEFLAGS} \
		"VERSION_MAJOR=${VERSION_MAJOR}" \
		"VERSION_MINOR=`expr ${VERSION_MINOR} + 1`" \
		"VERSION_LOCAL=X" \
		versionInnerDev 
	

############################################################
#
#
# Distribution targets:
#
#
############################################################

#
# Usage: make tar
#
# Make a tar file of an INSTALLED tree
#
tar: 
	@if [ "x${VERSION_DEV}" != x ]; then \
		echo "Run \"make tar\" from installed directory" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} tarInner

testtar:
	${MAKE} -${MAKEFLAGS} tarInner

tarInner:
	@if [ -h ../DIST -o -d ../DIST -o -f ../DIST ] ; then \
		echo "DIST directory already exists" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		echo "VERSION_MAJOR" = ${VERSION_MAJOR} > makefile.version ; \
		echo "VERSION_MINOR" = ${VERSION_MINOR} >> makefile.version ; \
		echo "VERSION_LOCAL" = 0 >> makefile.version ; \
		echo "VERSION_DEV" = ${VERSION_DEV} >> makefile.version ; \
		${MAKE} -${MAKEFLAGS} clean )
	rm -rf ../DIST
	mv ../TEMP ../DIST
	( cd ../UTILITIES ; ${MAKE} -${MAKEFLAGS} clean )
	( fullname=`pwd`; name=`basename $$fullname`; cd .. ; tar cvf ${TARFILE}_$${name}_`date '+%m-%d-%y-%H:%M:%S'` README ./UTILITIES ./DIST ./EXTRAS)
	rm -rf ../DIST
	
doExcludeFile:
	@ls -d ./OLD_PCR_* ./SAV* ./*.co ./*/SAV*  ./*/*.Z > excludeFile
	@echo ./excludeFile >> excludeFile
	@echo ----- Excluding `cat excludeFile`

#
# Usage: make shar
#
# Make a shar file of an INSTALLED tree
#
shar: ${UTILITIES_DIR}/${SHAR}
	@if [ "x${VERSION_DEV}" != x ]; then \
		echo "Run \"make tar\" from installed directory" ; \
		exit 1 ; \
	fi
	@if [ -h ../DIST -o -d ../DIST -o -f ../DIST ] ; then \
		echo "DIST directory already exists" ; \
		exit 1 ; \
	fi
	${MAKE} -${MAKEFLAGS} copyInner
	( cd ../TEMP; \
		echo "VERSION_MAJOR" = ${VERSION_MAJOR} > makefile.version ; \
		echo "VERSION_MINOR" = ${VERSION_MINOR} >> makefile.version ; \
		echo "VERSION_LOCAL" = 0 >> makefile.version ; \
		echo "VERSION_DEV" = ${VERSION_DEV} >> makefile.version ;\
		${MAKE} -${MAKEFLAGS} clean )
	rm -rf ../DIST
	mv ../TEMP ../DIST
	( cd ../UTILITIES ; ${MAKE} -${MAKEFLAGS} cleancopy )
	( cd ..; \
		rm -f ${SHARFILE} ; \
		${SHAR} -k -v ${VERSION} -o ${SHARFILE}_`date '+%m-%d-%y-%H:%M:%S'` README ./UTILITIES ./DIST )
	rm -rf ../DIST


##################################################################
#
#
# "internal" targets -- for recursive use ONLY
#
#
##################################################################

#
# make ${UTILITIES_DIR}/${CPDIFF}
#

${UTILITIES_DIR}/${CPDIFF}:
	( cd ${UTILITIES_DIR}; make ${CPDIFF} )
	
${UTILITIES_DIR}/${SHAR}:
	( cd ${UTILITIES_DIR}; make ${SHAR} )


#
# make CONFIG=... CONFIG_COMPONENTS=... CONFIG_CFLAGS=... configInner
#

CONFIG_DIR = ${CONFIG}${TARGET_MACH}

configInner: ${UTILITIES_DIR}/${CPDIFF}
	@if [ "x${CONFIG}" = x -o "x${CONFIG_COMPONENTS}" = x \
		-o "x${CONFIG_COMPONENTS}" = x ]; then \
		echo "Use \"make <config_name>\" NOT \"make configInner\"" ; \
		exit 1 ; \
	fi
	echo CONFIG= ${CONFIG} > makefile.config.2
	echo MACHINE= ${TARGET_MACH} >> makefile.config.2
	echo STD_INCLUDES= -IINCLUDE -I../INCLUDE >> makefile.config.2
	echo CONFIG_CFLAGS= ${CONFIG_CFLAGS} >> makefile.config.2
	echo CONFIG_AFLAGS= ${CONFIG_AFLAGS} >> makefile.config.2
	echo VERSION= ${VERSION} >> makefile.config.2
	rm -rf LIB/${CONFIG_DIR}
	mkdir LIB/${CONFIG_DIR}
	rm -rf BIN/${CONFIG_DIR}
	mkdir BIN/${CONFIG_DIR}
	rm -rf OBJtemp; mkdir OBJtemp
	for dir in ${CONFIG_COMPONENTS}; do \
		(echo COMPONENT= $$dir > makefile.config); \
		(cat makefile.config.2 >> makefile.config); \
		(${CPDIFF} makefile.config $$dir/makefile.config); \
		done
	for dir in ${ALL_COMPONENTS}; do \
		(cd $$dir; $(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE \
			include); \
		done
	for dir in ${CONFIG_COMPONENTS}; do \
		(cd $$dir; $(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE depend; \
			$(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE \
			obj); \
		done
	for dir in ${CONFIG_COMPONENTS}; do \
		(cd $$dir; $(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE depend; \
			$(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE \
			MAN_DIR=../MAN \
			man); \
		done
	echo "extern char" > OBJtemp/XRRoot2.c
	cat OBJtemp/version_string_names >> OBJtemp/XRRoot2.c
	echo "*(XR_versions[]);" >> OBJtemp/XRRoot2.c
	echo "char *(XR_versions[]) = {" >> OBJtemp/XRRoot2.c
	cat OBJtemp/version_string_addresses >> OBJtemp/XRRoot2.c
	echo "    0 };" >> OBJtemp/XRRoot2.c
	( cd OBJtemp; \
		${CC} -c ${CONFIG_CFLAGS} XRRoot2.c; \
		/bin/ld -r -o XRRoot.o ThreadsRoot.o XRRoot2.o; \
		rm -f ThreadsRoot.o XRRoot2.o; \
		ar r xr.a *.o; \
		mv xr.a XRRoot.o ../LIB/${CONFIG_DIR} )
	( cd LIB/${CONFIG_DIR}; ranlib xr.a )
	rm -rf OBJtemp
	rm -rf LIBAtemp; mkdir LIBAtemp
	for dir in ${CONFIG_COMPONENTS}; do \
		(cd $$dir; $(MAKE) -${MAKEFLAGS} MAKEPUBLIC=TRUE \
			lib liba bin src); \
		done
	( cd LIBAtemp; ar r libxr.a `ls`; mv libxr.a ../LIB/${CONFIG_DIR} )
	( cd LIB/${CONFIG_DIR}; ranlib libxr.a )
	rm -rf LIBAtemp
	rm -f makefile.config makefile.config.2
	ln BIN/${CONFIG_DIR}/PortableCommonRuntime OLD_PCR_`date '+%m-%d-%y-%H:%M:%S'`

#
# make VERSION_MAJOR=... VERSION_MINOR=... VERSION_LOCAL=... 
#         VERSION_DEV=... versionInner
#
# Create a local makefile.version
#
versionInner:
	echo "VERSION_MAJOR" = ${VERSION_MAJOR} > makefile.version
	echo "VERSION_MINOR" = ${VERSION_MINOR} >> makefile.version
	echo "VERSION_LOCAL" = ${VERSION_LOCAL} >> makefile.version
	echo "VERSION_DEV" = ${VERSION_DEV} >> makefile.version
	${MAKE} -${MAKEFLAGS} clean
	
#
# make versionInnerDev
#
# Updates the makefile.version for the development directory
#
versionInnerDev:
	echo "VERSION_MAJOR" = ${VERSION_MAJOR} > makefile.version
	echo "VERSION_MINOR" = ${VERSION_MINOR} >> makefile.version
	echo "VERSION_LOCAL" = ${VERSION_LOCAL} >> makefile.version
	echo "VERSION_DEV" = .dev >> makefile.version

#
# make copyInner
#
# Copy this subtree to ../TEMP
#
copyInner: doExcludeFile
	rm -rf ../TEMP; mkdir ../TEMP
	tar cFFfX - excludeFile . | ( cd ../TEMP; tar xvf - )

# copyInner:
# 	rm -rf ../TEMP; cp -r -p . ../TEMP

#
# make installSelfInner
#
# rename self (../TEMP) to correct thing as determined from VERSION variables.
#
installSelfInner:
	@( dir=`pwd`; \
		if [ "`basename $$dir`" != TEMP ]; then \
			echo "BUG in makefile: installSelfInner botch" ; \
			exit 1 ; \
		fi \
	)
	@if [ -h ${VERSION} -o -d ${VERSION} -o -f ${VERSION} ] ; then \
		echo "${VERSION} directory already exists" ; \
		exit 1 ; \
	fi
	if [ "x${VERSION_DEV}" = x ] ; then \
		if [ -d ../${VERSION} ] ; then \
			echo "Aborting bounce, directory ${VERSION} already exists." ; \
			exit 1; \
		fi ; \
		mv ../TEMP ../${VERSION} ; \
		rm -f ../INSTALLED ; \
		( cd ..; ln -s ${VERSION} INSTALLED ) ; \
	else \
		rm -f ../DEVELOPMENT ; \
		mv ../TEMP ../DEVELOPMENT ; \
	fi

#
# Validity check for "make development"
#
developmentCheckInner:
	@if [ "x${VERSION_DEV}" != x ]; then \
		echo "Run \"make development\" from installed directory" ; \
		exit 1 ; \
	fi
	@if [ -h ../DEVELOPMENT -o -f ../DEVELOPMENT \
			-o -d ../DEVELOPMENT ] ; then \
		echo "DEVELOPMENT directory already exists" ; \
		exit 1 ; \
	fi

#
# Validity check for "make bounce_xxx"
#
bounceCheckInner:
	@if [ "x${VERSION_DEV}" = x ]; then \
		echo "Run \"make bounce_xxx\" from development directory" ; \
		exit 1 ; \
	fi














