# 
# Mach Operating System
# Copyright (c) 1992 Carnegie Mellon University
# All Rights Reserved.
# 
# Permission to use, copy, modify and distribute this software and its
# documentation is hereby granted, provided that both the copyright
# notice and this permission notice appear in all copies of the
# software, derivative works or modified versions, and any portions
# thereof, and that both notices appear in supporting documentation.
# 
# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
# CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
# 
# Carnegie Mellon requests users of this software to return to
# 
#  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
#  School of Computer Science
#  Carnegie Mellon University
#  Pittsburgh PA 15213-3890
# 
# any improvements or extensions that they make and grant Carnegie Mellon 
# the rights to redistribute these changes.
#  
# HISTORY
# $Log:	Makefile-common,v $
# Revision 2.2  92/02/02  13:08:43  rpd
# 	Created for the new Makefile organization.
# 	[92/01/30            rpd]
# 

# common definitions that Makefiles can include
# Makefiles should be structured as follows:
#
#	include ${MAKETOP}Makefile-common
#	... rules
#	-include Makedep
#
# Makefile-common is included first and Makedep last

# ensure that 'install' is the default target

install :

# Recursive makes should pass down the following variables:

MVARIABLES = INSTALLDIR FRELEASEDIR TRELEASEDIR PATH CPATH LPATH cpu atsys

MFLAGS = ${MVARIABLES/.*/&="${&}"}

# The mkodirs target can be used to create subdirectories
# in the object directory.  It is normally used with
#
#	MKODIRS = ...
#
#	.INIT : mkodirs
#
# NB: Using direct dependencies on directories is unwise
# because when the modtime of the directory changes things
# will be unnecessarily remade.

mkodirs :
	+@-for dir in ${MKODIRS}; do \
	  [ -d $$dir ] || \
	  { echo "mkdir $$dir"; mkdir $$dir; } \
	done

# The mkidirs target can be used to create subdirectories
# in the install directory.  It is normally used with
#
#	MKIDIRS = ...
#
#	install : mkidirs ...

mkidirs :
	@-for dir in ${MKIDIRS;.*;${INSTALLDIR}/&}; do \
	  [ -d $$dir ] || \
	  { echo "mkdir $$dir"; mkdir $$dir; } \
	done

# The mkrdirs target can be used to create subdirectories
# in the release directory.  It is normally used with
#
#	MKRDIRS = ${MKIDIRS}
#
#	release : mkrdirs ...

mkrdirs :
	@-for dir in ${MKRDIRS;.*;${TRELEASEDIR}/&}; do \
	  [ -d $$dir ] || \
	  { echo "mkdir $$dir"; mkdir $$dir; } \
	done

# The mkilinks target can be used to create 'machine' links
# in the install directory.  It is normally used with
#
#	MKILINKS = ...
#
#	install : mkilinks ...

mkilinks :
	@-for dir in ${MKILINKS;.*;${INSTALLDIR}/&}; do \
	  [ -d $${dir}/.pmax_mach -o ! -d $${dir}/mips ] || \
	  { echo "linking $${dir}/.pmax_mach"; \
	    ln -s mips $${dir}/.pmax_mach; }; \
	  [ -d $${dir}/.i386_mach -o ! -d $${dir}/i386 ] || \
	  { echo "linking $${dir}/.i386_mach"; \
	    ln -s i386 $${dir}/.i386_mach; }; \
	  [ -d $${dir}/.vax_mach -o ! -d $${dir}/i386 ] || \
	  { echo "linking $${dir}/.vax_mach"; \
	    ln -s vax $${dir}/.vax_mach; }; \
	  [ -d $${dir}/.sun3_mach -o ! -d $${dir}/sun3 ] || \
	  { echo "linking $${dir}/.sun3_mach"; \
	    ln -s sun3 $${dir}/.sun3_mach; }; \
	  [ -d $${dir}/.i860_mach -o ! -d $${dir}/i860 ] || \
	  { echo "linking $${dir}/.i860_mach"; \
	    ln -s i860 $${dir}/.i860_mach; }; \
	  [ -d $${dir}/.mac2_mach -o ! -d $${dir}/mac2 ] || \
	  { echo "linking $${dir}/.mac2_mach"; \
	    ln -s mac2 $${dir}/.mac2_mach; }; \
	  [ -d $${dir}/.luna88k_mach -o ! -d $${dir}/luna88k ] || \
	  { echo "linking $${dir}/.luna88k_mach"; \
	    ln -s luna88k $${dir}/.luna88k_mach; }; \
	  [ -d $${dir}/machine ] || \
	  { echo "linking $${dir}/machine"; \
	    ln -s .@sys $${dir}/machine; } \
	done

# The mkrlinks target can be used to create 'machine' links
# in the release directory.  It is normally used with
#
#	MKRLINKS = ${MKILINKS}
#
#	release : mkrlinks ...

mkrlinks :
	@-for dir in ${MKRLINKS;.*;${TRELEASEDIR}/&}; do \
	  [ -d $${dir}/.pmax_mach -o ! -d $${dir}/mips ] || \
	  { echo "linking $${dir}/.pmax_mach"; \
	    ln -s mips $${dir}/.pmax_mach; }; \
	  [ -d $${dir}/.i386_mach -o ! -d $${dir}/i386 ] || \
	  { echo "linking $${dir}/.i386_mach"; \
	    ln -s i386 $${dir}/.i386_mach; }; \
	  [ -d $${dir}/.vax_mach -o ! -d $${dir}/i386 ] || \
	  { echo "linking $${dir}/.vax_mach"; \
	    ln -s vax $${dir}/.vax_mach; }; \
	  [ -d $${dir}/.sun3_mach -o ! -d $${dir}/sun3 ] || \
	  { echo "linking $${dir}/.sun3_mach"; \
	    ln -s sun3 $${dir}/.sun3_mach; }; \
	  [ -d $${dir}/.i860_mach -o ! -d $${dir}/i860 ] || \
	  { echo "linking $${dir}/.i860_mach"; \
	    ln -s i860 $${dir}/.i860_mach; }; \
	  [ -d $${dir}/.mac2_mach -o ! -d $${dir}/mac2 ] || \
	  { echo "linking $${dir}/.mac2_mach"; \
	    ln -s mac2 $${dir}/.mac2_mach; }; \
	  [ -d $${dir}/.luna88k_mach -o ! -d $${dir}/luna88k ] || \
	  { echo "linking $${dir}/.luna88k_mach"; \
	    ln -s luna88k $${dir}/.luna88k_mach; }; \
	  [ -d $${dir}/machine ] || \
	  { echo "linking $${dir}/machine"; \
	    ln -s .@sys $${dir}/machine; } \
	done

# The following template can be used to recursively make
# in subdirectories.  For example,
#
#	DIRS = foo bar ...
#	RECURSE = ${RECURSE_TEMPLATE/DIRS/${DIRS}}
#
#	install :
#		+${RECURSE/TARGET/install}
#
#	clean :
#		+${RECURSE/TARGET/clean}
#
# We only visit subdirectories that contain a Makefile.
# We use wh to check for a Makefile somewhere on MAKEPSD,
# in case there is shadowing.  (Some directories might
# not be checked-out, or might not have Makefiles at all.)
# The '+' is optional, but its presence will cause make
# to execute the rule & recurse when doing a 'make -n'.

RECURSE_TEMPLATE = @-for dir in DIRS; do \
	if ${WH} -p MAKEPSD $$dir/Makefile > /dev/null 2>&1; then \
	(cd $$dir && ${MAKE} ${MFLAGS} TARGET); \
	fi; done

# If a source directory needs to have machine-dependent files,
# they should go into subdirectories named mips, sun3, vax, etc.
# The source Makefile should use
#	VPATH = .:${cpu}
# to instruct make to search the machine-dependent subdirectory.
#
# A cross-compiling build can override the definition of cpu.

VAX_cpu = vax
IBMRT_cpu = ca
SUN3_cpu = sun3
MMAX_cpu = mmax
PMAX_cpu = mips
I386_cpu = i386
SUN4_cpu = sun4
I860_cpu = i860
LUNA88K_cpu = luna88k
MAC2_cpu = mac2
cpu = ${${TARGET_MACHINE}_cpu}

# DEFS and INCS are passed to cpp in various contexts (cc, mig, etc.)
# They can be defined by the source Makefile.
#
# DEFS =
# INCS =

# Definitions used to install files in the release directory:
# Installation rules should look like
#
# ${INSTALLDIR}/foo : foo
#	${RM} $@
#	${CP} $? $@
#	strip/whatever
#	${CHMOD_foo} $@
#
# NB: For $? to work properly, the installed file can only depend on one file.

RM = rm -f
CP = cp -p
MV = mv -f
STRIP = strip
XSTRIP = xstrip
CHMOD_INC = chmod 444
CHMOD_BIN = chmod 755
CHMOD_LIB = chmod 644
CHMOD_O = chmod 644
CHMOD_MAN = chmod 444

# Definitions used to run mig:
# A mig rule should look like
#
# foo.h foo_server.c fooUser.c : foo.defs
#	${MIG} ${MIGFLAGS} -server foo_server.c foo.defs
#
# All files produced by mig should be listed left of the colon.
# When given the -MD flag, mig produces dependencies in the file
# foo.defs.d.  It includes a dependency on migcom.

MIG = mig
# the Makefile can define MIGOPTS to add something here
MIGFLAGS = -MD ${DEFS} ${INCS} ${MIGOPTS}

# Definitions used to build programs:
# A rule for a binary should look like
#
# LIBS = ${LIBFOO} ${LIBBAR}
# DEPS = ${DEPFOO} ${DEPBAR} ${DEPCRT0}
#
# foo : ${OBJS}
#	${RM} $@ $@.out
#	${DEPS/%/$@} ${CC} -o $@.out ${CFLAGS} ${OBJS} ${LIBS} && \
#	${MV} $@.out $@
#
# The binary is made as foo.out and renamed to foo to prevent foo
# from existing if there are undefined symbols.  Hence a subsequent
# make will again try to make foo.
#
# The DEPS stuff creates dependencies on crt0.o and libraries.
# It would be nice if ld created a dependency file just like cpp.
# Unfortunately, it doesn't.  We use wh to do it ourself.
# This can be disabled by redefining DEP_TEMPLATE.

I860_CDEBUGFLAGS = -O -DiPSC860
PMAX_CDEBUGFLAGS = -O2 -g3
CDEBUGFLAGS = ${${TARGET_MACHINE}_CDEBUGFLAGS?${${TARGET_MACHINE}_CDEBUGFLAGS}:-O}

# increase compiler string space on mips
PMAX_CFLAGS = -Wf,-XNk150

# the Makefile can define CCOPTS to add something here
CFLAGS = -MD ${${TARGET_MACHINE}_CFLAGS} ${CDEBUGFLAGS} ${DEFS} ${INCS} ${CCOPTS}

I860_CPP = /lib/cpp
LUNA88K_CPP = ${CC} -E
CPP = ${${TARGET_MACHINE}_CPP?${${TARGET_MACHINE}_CPP}:${CC} -ES}

VAX_CC = gcc
SUN3_CC = gcc
I386_CC = gcc
I860_CC = cc860
CC = ${${TARGET_MACHINE}_CC?${${TARGET_MACHINE}_CC}:cc}

PMAX_ASFLAGS = -nocpp
ASFLAGS = ${${TARGET_MACHINE}_ASFLAGS}

I860_AS = as860
AS = ${${TARGET_MACHINE}_AS?${${TARGET_MACHINE}_AS}:as}

LD = ld

PMAX_LDFLAGS = -e __start
I386_LDFLAGS = -e __start
LDFLAGS	= ${${TARGET_MACHINE}_LDFLAGS?${${TARGET_MACHINE}_LDFLAGS}:-e start}

WH = wh
DEP_TEMPLATE = echo "%: `${WH} -Lq NAME`" >> %.out.d;

CRT0 = `wh -Lq crt0.o`
DEPCRT0 = ${DEP_TEMPLATE/NAME/crt0.o}

LIBMACH = -lmach
DEPMACH = ${DEP_TEMPLATE/NAME/libmach.a}

LIBMACH_SA = -lmach_sa
DEPMACH_SA = ${DEP_TEMPLATE/NAME/libmach_sa.a}

LIBTHREADS = -lthreads
DEPTHREADS = ${DEP_TEMPLATE/NAME/libthreads.a}

LIBMACHID = -lmachid
DEPMACHID = ${DEP_TEMPLATE/NAME/libmachid.a}

LIBNETNAME = -lnetname
DEPNETNAME = ${DEP_TEMPLATE/NAME/libnetname.a}

LIBNETMEMORY = -lnetmemory
DEPNETMEMORY = ${DEP_TEMPLATE/NAME/libnetmemory.a}

LIBSERVICE = -lservice
DEPSERVICE = ${DEP_TEMPLATE/NAME/libservice.a}

LIBXMM = -lxmm
DEPXMM = ${DEP_TEMPLATE/NAME/libxmm.a}

LIBL = -ll
DEPL = ${DEP_TEMPLATE/NAME/libl.a}

LIBCS = -lcs
DEPCS = ${DEP_TEMPLATE/NAME/libcs.a}

LIBM = -lm
DEPM = ${DEP_TEMPLATE/NAME/libm.a}

LIBTERMCAP = -ltermcap
DEPTERMCAP = ${DEP_TEMPLATE/NAME/libtermcap.a}

LIBCURSES = -lcurses
DEPCURSES = ${DEP_TEMPLATE/NAME/libcurses.a}

# Definitions used to build libraries:
# A rule for a library should look like
#
# libfoo.a : ${OBJS}
#	${RM} $@
#	${AR} cq $@ ${OBJS}
#	${RANLIB} $@
#
# This ensures that the library is updated properly
# even if file names are truncated (so that 'ar r' fails).

I860_AR = ar860
AR = ${${TARGET_MACHINE}_AR?${${TARGET_MACHINE}_AR}:ar}

I860_RANLIB = ranlib860
RANLIB = ${${TARGET_MACHINE}_RANLIB?${${TARGET_MACHINE}_RANLIB}:ranlib}

# Default suffix-based rules for creating objects:

.SUFFIXES: .cs .ss

.c.o:
	${CC} -c ${CFLAGS} $*.c

.s.o:
	${CPP} -MD -DASSEMBLER ${DEFS} ${INCS} $*.s > $*.as
	${AS} ${ASFLAGS} -o $*.o $*.as
	${RM} $*.as

.cs.o:
	${CPP} -MD -DASSEMBLER ${DEFS} ${INCS} $*.cs > $*.as
	${AS} ${ASFLAGS} -o $*.o $*.as
	${RM} $*.as

.ss.o:
	${AS} ${ASFLAGS} -o $*.o $*.ss


MD = md

.EXIT:
	-${MD} -u Makedep -f -d *.d
