#
# Mach Operating System
# Copyright (c) 1989 Carnegie-Mellon University
# All rights reserved.  The CMU software License Agreement specifies
# the terms and conditions for use and redistribution.
#
#
# HISTORY
# $Log:	Makefile,v $
# Revision 1.2  92/03/04  17:54:09  pha
# *** empty log message ***
# 
# Revision 1.1.1.1  92/03/02  17:56:22  pha
# osf/1 server V3.5
# 
# Revision 1.1  92/03/02  17:56:20  pha
# Initial revision
# 
# Revision 1.1.1.1  92/03/02  17:52:17  pha
# osf/1 server V3.5
# 
# Revision 1.1  92/03/02  17:52:16  pha
# Initial revision
# 
# Revision 3.11  91/12/20  17:55:51  barbou
# Generate new include file "server_build.h" shared between the server
# and the emulator (for consistency checking).
# 
# Revision 1.2  92/02/27  11:38:25  pha
# 	numerous changes so it compiles on osf/1
# 
# Revision 1.1.1.1  92/01/14  11:34:30  pha
# OSF/1 V3.1 Unix Server
# 
# Revision 1.1  92/01/14  11:34:29  pha
# Initial revision
# 
# Revision 3.10  91/11/15  15:31:25  condict
# Copy the machine-dependent header files from the server/${target_cpu}
# src dir to the server/machine object dir.
# 
# Revision 3.9  91/11/07  15:15:03  condict
# Substantially changed to support shadow source trees (Paul Roy).  Also,
# making of server include files is now done with simple recursive call to
# make, instead of bizarre shenanigans.
# 
# Revision 3.8  91/10/02  10:50:18  condict
# Change unconditional reference to src/latest to use BCSBBASE instead.
# 
# Revision 3.7  91/09/06  13:48:22  condict
# Trivial changes to allow building under OSF/1.
# 
# Revision 3.6  91/08/30  10:28:27  condict
# Change make built-in var TARGET_MACHINE to TARGET_CPU, allowing cross-compiles.
# 
# Revision 3.5  91/08/28  10:53:32  barbou
# Store $CONFIG in vers.config (for upgrade to OSF/1.0.2).
# 
# Revision 3.4  91/08/27  15:26:00  barbou
# Upgrade to UX26.
# 
# Revision 3.3  91/07/30  15:11:04  jose
# added CONFIG as an argument for lower level Makefile
# 
# Revision 3.2  91/04/12  13:44:52  condict
# Add target and actions to automatically generate syscalls.c, init_sysent.c,
# and syscall.h from server/conf/syscalls.master, as in OSF/1
# 
# Revision 3.1  91/03/13  09:16:04  condict
# Modified to work with the OSF/1 header files
# 
# Revision 3.0  91/01/17  12:07:40  condict
# Unchanged copy from Mach 3.0 BSD UNIX server
# 
# Revision 2.6  91/01/08  17:44:07  rpd
# 	Allow "server" as a target to make the "kernel"
# 	[91/01/04  11:45:47  rvb]
# 
# Revision 2.5  90/11/05  15:32:47  rpd
# 	Change MACHINE to TARGET_MACHINE.
# 	[90/10/30            rwd]
# 
# Revision 2.4  90/10/25  15:06:50  rwd
# 	Add references for i386 the same as at386.
# 	[90/10/24            rwd]
# 
# Revision 2.3  90/05/21  13:49:02  dbg
# 	Added AT386.
# 	[90/03/14            dbg]
# 
# Revision 2.2  89/11/29  15:27:07  af
# 	Added pmax.
# 	[89/11/28  14:07:01  af]
# 
# Revision 2.1  89/08/04  15:22:44  rwd
# Created.
# 
# Revision 2.27  89/04/22  15:12:18  gm0w
# 	Changed commands to create the "make" script for the build area
# 	into a sed of "conf/make.template".  Moved commands to create
# 	configuration for build into "conf/mkconfig.csh".
# 	[89/04/14            gm0w]
# 
# Revision 2.26  89/04/18  16:41:50  mwyoung
# 	Allow additional flags (e.g., "-p") to be passed to config.
# 	[89/01/05            mwyoung]
# 
# Revision 2.18.3.1  89/03/27  22:55:09  mwyoung
# 	Allow additional flags (e.g., "-p") to be passed to config.
# 	[89/01/05            mwyoung]
# 
# Revision 2.25  89/03/07  00:48:53  rpd
# 	Removed ../sys from the CPATH used in the build directory.
# 
# Revision 2.24  89/02/25  14:12:36  gm0w
# 	Changes for cleanup.
# 
# Revision 2.23  89/02/25  14:07:41  gm0w
# 	Changes for cleanup.
# 
# Revision 2.22  89/01/31  01:19:58  rpd
# 	Support multimax.
# 	[89/01/24            dlb]
# 
# Revision 2.21  89/01/23  22:01:13  af
# 	Changes for MIPS and I386: <foo>_cpu, <FOO>_OUTPUT, and FEATURES_<FOO>
# 	[89/01/09            rvb]
# 
# Revision 2.20  88/12/22  11:52:53  mja
# 	Remove Makefile.top-level before copying to it in case it may
# 	already exist non-writeable.
# 
# Revision 2.19  88/12/20  13:38:27  rpd
# 	Fixed the log (??).
# 	[88/11/26  21:13:27  rpd]
# 
# Revision 2.18  88/10/18  03:13:20  mwyoung
# 	Make it possible to invoke this Makefile using the SOURCE_DIR
# 	technology -- getting Makefile from ".." only worked because
# 	VPATH was being used.
# 	[88/10/10            mwyoung]
# 
# Revision 2.17  88/09/27  17:29:50  rvb
# 	Fixed Log
# 
# Revision 2.16  88/09/27  16:05:59  rvb
# 	Add new pmake target and PMAKE variable and link pmake to make
# 	in the build directory.
# 	[88/09/25            rvb]
# 
# Revision 2.15  88/09/07  15:44:57  rpd
# 	Moved CONFIG macros to Makeconf, so that the user
# 	can easily customize them by modifying Makeconf.
# 	[88/09/07  01:53:58  rpd]
# 	
# 	Removed redundant definition of CONFIG_SUFFIX as empty string.
# 	This allows a definition to go into Makeconf if desired.
# 	[88/09/05  23:46:20  rpd]
# 	
# 	Eliminated a dependency on ${CONFIG}.
# 	Use dependencies to create ${CONFIG}/Makeconf.
# 	[88/09/05  21:41:22  rpd]
# 	
# 	Fixed typo.
# 	[88/09/05  20:51:19  rpd]
# 	
# 	Removed dependencies on directories; they cause to many recompilations.
# 	Put VPATH and OBJECTDIR values into the make script when it is generated.
# 	Moved from NO_SOURCE_DIR to USE_SOURCE_DIR.
# 	[88/09/05  20:30:48  rpd]
# 	
# 	Fixed up some more.  Now VPATH's value isn't wired-in to the script when
# 	it is created.
# 	[88/09/02  18:15:05  rpd]
# 	
# 	Several changes with the following goals:
# 	 1) The object directory can be a subdirectory of the source directory.
# 	 Solution: create an empty Makeconf file in the object directory, so
# 	 that makes in the object directory don't get confused by a Makeconf
# 	 file that might be in the source directory.
# 	
# 	 2) The generated make script in the object directory now properly handles
# 	 passed-in VPATHs with zero, one, or more components.  When possible, it
# 	 tries to use SOURCE_DIR instead of VPATH.
# 	[88/09/02  14:04:08  rpd]
# 
# Revision 2.14  88/09/01  15:52:59  mwyoung
# 	Don't use the '-N' switch, as it turns off OBJECTDIR processing
# 	as well as Makeconf searching.  Sigh.
# 	[88/08/30            mwyoung]
# 
# Revision 2.13  88/08/30  17:22:42  mwyoung
# 	Remove occurances of string "make" from the "./make" script.
# 	[88/08/30            mwyoung]
# 
# Revision 2.12  88/08/30  00:12:25  mwyoung
# 	When building the "make" script in the build directory, don't
# 	precompute the location of the appropriate "make" program.
# 	Computing it at runtime allows for other "make" programs (e.g.,
# 	"pmake") and allows compilation to be done on more than one
# 	different machine.
# 	
# 	Move the machine-specific top-level Makefile into the
# 	machine-specific directory.
# 	[88/08/28            mwyoung]
# 	
# 	Eliminate make dependencies on ${CONFIG}... create the directory
# 	if required in the rules for a particular object.  [Otherwise, the
# 	directory modification time changes, causing things to rebuild.]
# 	[88/08/27            mwyoung]
# 
# Revision 2.11  88/08/25  19:17:44  mwyoung
# 	If the incoming VPATH is simple, pass a value in for SOURCE_DIR
# 	to reduce the number of wasted name lookups.
# 	[88/08/21            mwyoung]
# 	
# 	If the incoming VPATH is simple, pass a value in for SOURCE_DIR
# 	to reduce the number of wasted name lookups.
# 	[88/08/25  19:28:56  mwyoung]
# 
# Revision 2.10  88/08/25  18:39:56  mwyoung
# 	Added some dependencies, so that "make CONFIG=foo config" works
# 	when configuration foo doesn't have a build directory.
# 	[88/08/15  23:14:58  rpd]
# 
# Revision 2.9  88/08/06  17:57:50  rpd
# Changed the generated make script: MAKECONF should be MAKEDIR;
# do more with CPATH now.
# 
# Revision 2.8  88/07/29  03:17:03  rpd
# Removed dependencies upon Makeconf, so that the build directories
# can be subdirectories of the source directory.
# 
# Revision 2.7  88/07/21  16:25:33  rpd
# Include machine-dependent Makefile.  Slight change to config rule,
# to allow the machine-dependent Makefile to specify further actions.
# 
# Revision 2.6  88/07/19  20:40:08  mwyoung
# Always rebuild the config program... it would only get rebuilt if
# the binary didn't exist.
# Attempt to build <sys/features.h> after configuring.  If
# the set of files used to build the features file changes, then
# it will have to be rerun manually because I have no interest
# in figuring out how to get make to do it for me.
# 
# Revision 2.5  88/07/18  16:50:00  mwyoung
# Fix configuration_file-building script.
# 
# Allow "doconf" or "config" targets to be made before
# a full "make" has taken place.
# 
# Revision 2.4  88/07/16  01:04:08  mwyoung
# Resurrect MASTER.local files.
# 
# Revision 2.3  88/07/15  18:19:26  mja
# Created.
# 

#
#	Targets of interest:
#		doconf		Rebuild the configuration file
#		config		Reconfigure
#		kernel		Rebuild just the kernel
#		server		Rebuild just the kernel
#		pmake		Rebuild just the kernel using pmake
#		buildconf	Build all of the "standard" configurations
#

VAX_cpu=vax
IBMRT_cpu=ca
SUN3_cpu=sun3
MMAX_cpu=mmax
PMAX_cpu=mips
AT386_cpu=i386
I386_cpu=i386
DELTA88K_cpu=delta88k
cpu=$(${TARGET_MACHINE}_cpu)

VAX_OUTPUT=Makefile
SUN3_OUTPUT=Makefile
IBMRT_OUTPUT=Makefile
MMAX_OUTPUT=Makefile
PMAX_OUTPUT=Makefile
AT386_OUTPUT=Makefile
I386_OUTPUT=Makefile
DELTA88K_OUTPUT=Makefile

CONFIG = DEFAULT

CONFIG_INPUT=conf/${CONFIG}
CONFIG_OUTPUT=${CONFIG}/$(${TARGET_MACHINE}_OUTPUT)

MASTER_DIR=conf

SYSCALLS=${CONFIG}/init_sysent.c ${CONFIG}/syscalls.c include/sys/syscall.h

MAKE_ARGS=
PMAKE_ARGS= -k -P6 "P=&" all

SOURCE_DIR=..

${CONFIG}.all:	${CONFIG}/make \
		${CONFIG}/Makeconf \
		${CONFIG}.doconf \
		usr_include_files \
		machine_include_files \
		${CONFIG}.config \
		${CONFIG}.make

${CONFIG}.make kernel server:	${CONFIG}/make ${CONFIG}/Makeconf
	@echo "[ Making ${CONFIG} ]"
	@echo "[ MKINC = ${MKINC} ]"
	@cd ${CONFIG} && USE_SOURCE_DIR=1 ./make ${MAKE_ARGS} CONFIG=${CONFIG}

usr_include_files: ./include ./include/sys
	@echo "[ Making server/include ]"
	@cd include; ${MAKE}

machine_include_files: ./machine
	@echo "[ Making server/machine ]"
	@cd machine; ${MAKE}

pmake:	${CONFIG}/make ${CONFIG}/Makeconf
	@echo "[ Pmaking ${CONFIG} ]"
	@cd ${CONFIG} && USE_SOURCE_DIR=1 ./pmake ${PMAKE_ARGS}

clean:	${CONFIG}/make ${CONFIG}/Makeconf
	@echo "[ Cleaning ${CONFIG} ]"
	@cd ${CONFIG} && USE_SOURCE_DIR=1 ./make ${MAKE_ARGS} clean

${CONFIG}.doconf doconf: ${CONFIG_INPUT}

${CONFIG}.config config::	${CONFIG}/make \
				${CONFIG}/Makeconf \
				${CONFIG}/Makefile.top-level \
				./bin/config \
				${CONFIG_INPUT} \
				./include/sys/server_build.h \
				${SYSCALLS} \
				conf/files \
				conf/files.${cpu} \
				conf/Makefile.template \
				conf/Makefile.${cpu}
	cd ${CONFIG} && \
         ./make CONFIG=${CONFIG} \
		CONFIG_SUFFIX=${CONFIG_SUFFIX} \
		CONFIG_FLAGS=${CONFIG_FLAGS} \
		-f Makefile.top-level \
		make.config

./bin ../bin ./src ./src/config ./include ./include/sys ./machine ./conf: 
	mkdir $@

./bin/config: ./bin ./src ./src/config ALWAYS
	@echo "[ Generating $@ ]"
	@S=src/config; cd $$S && SRCSUFFIX=$$S ${MAKE} ../../bin/config 

./bin/buildconf: ./bin ./src
	@echo "[ Generating $@ ]"
	@S=src; cd $$S && SRCSUFFIX=$$S ${MAKE} ../bin/buildconf

${CONFIG}/Makeconf:
	@echo "[ Generating $@ ]"
	@echo "TARGET_MACHINE=${TARGET_MACHINE}" > $@
	@echo "MKINC=${MKINC}" >> $@
	@echo "REG_EXP=.*" >> $@

${CONFIG}/Makefile.top-level:	Makefile
	@echo "[ Generating $@ ]"
	@rm -f $@; cp -p Makefile $@

${CONFIG}/make:	Makefile conf/make.template
	@echo "[ Generating $@ ]"
	@echo "[ VPATH = ${VPATH} ]"
	@-if [ -d ${CONFIG} ]; then true; else mkdir ${CONFIG}; fi
	@-if [ -d ${CONFIG}/sys ]; then true; else mkdir ${CONFIG}/sys; fi
	@sed -e 's;@OBJECTDIR@;${OBJECTDIR};g' -e 's;@VPATH@;${VPATH};g' \
	 < conf/make.template > $@.tmp; \
	 chmod +x $@.tmp; \
         mv $@.tmp $@; \
	 rm -f ${CONFIG}/pmake; \
	 ln $@ ${CONFIG}/pmake

${CONFIG_INPUT}: ./conf ALWAYS
	@echo "[ generating $@ from ${CONFIG}.${TARGET_MACHINE} ]"
	cd conf && ${MAKE} ${MFLAGS} CONFIG=${CONFIG} \
	    TARGET_MACHINE=${TARGET_MACHINE}

./include/sys/server_build.h: conf/version.build
	@echo "[ Generating $@ from ${MASTER_DIR}/version.build ]"
	@echo "#define SERVER_BUILD \"`cat conf/version.build`\"" > ./include/sys/server_build.h

# Generate the files that depend on the list of system calls and their
# attributes.  After doing so, in case ./include/sys got updated by the cp
# command, we touch the files again, so they will be newer than the dir:
#
${SYSCALLS}:	./include ./include/sys \
			conf/syscalls.master conf/makesyscalls.sh
	@echo "[ Generating ${SYSCALLS} from conf/syscalls.master ]"
	@sh conf/makesyscalls.sh -hk conf/syscalls.master ${CONFIG}
	@cp ${CONFIG}/sys/syscall.h include/sys/syscall.h
	@touch ${SYSCALLS}

make.config:	../${CONFIG_OUTPUT}${CONFIG_SUFFIX}

../${CONFIG_OUTPUT}${CONFIG_SUFFIX}:	\
		../${CONFIG_INPUT} \
		$(SOURCE_DIR)/conf/files \
		$(SOURCE_DIR)/conf/files.${cpu} \
		$(SOURCE_DIR)/conf/Makefile.template \
		$(SOURCE_DIR)/conf/Makefile.${cpu} \
		./Makefile.top-level
	@echo "[ Configuring in ${CONFIG} ]"
	../bin/config -c $(SOURCE_DIR)/conf ${CONFIG_FLAGS} ${CONFIG}
	@echo ${CONFIG} >../${CONFIG}/vers.config
	@-if [ -d sys ] ; then true; else mkdir sys; fi

buildconf: ./bin/buildconf conf/buildconf.${cpu}
	@-if [ "_${BUILDCONF}_" = "__" ]; then \
	      argv=`sed <conf/buildconf.${cpu} -e "/^#/d"`; \
	  else \
	      argv=${BUILDCONF}; \
	  fi; \
	exec ./bin/buildconf ${OPTIONS} $$argv

ALWAYS:

-include ${cpu}/Makefile.top-level
