# 
# 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.12  92/04/08  23:26:41  rpd
# 	Added LIBENV, DEPENV.
# 	[92/03/28            jvh]
# 
# 	Cross-compilation support.
# 	[92/03/19            jvh]
# 
# Revision 2.11  92/03/05  22:47:18  rpd
# 	Changed to use double-colon rules for top-level targets.
# 	Added luna88k support.
# 	[92/02/28            rpd]
# 
# Revision 2.10  92/02/16  15:43:54  rpd
# 	Added EXIT_RULE, LIBCMUCS, DEPCMUCS.
# 	[92/02/16            rpd]
# 
# Revision 2.9  92/02/15  19:34:13  rpd
# 	Added LIBREADLINE, DEPREADLINE.
# 	Synced with mk/Makefile-common.
# 	[92/02/14            rpd]
# 
# Revision 2.8  92/01/27  16:42:05  rpd
# 	Changed RECURSE_TEMPLATE for '+'.
# 	[92/01/27            rpd]
# 
# Revision 2.7  92/01/24  18:11:25  rpd
# 	Added -DASSEMBLER to .s and .cs rules.
# 	[92/01/24            rpd]
# 
# Revision 2.6  92/01/24  01:30:13  rpd
# 	Changed default target to 'install'.
# 	Added luna88k to mkilinks and mkrlinks.
# 	Added RECURSE_TEMPLATE.
# 	[92/01/23            rpd]
# 
# Revision 2.5  92/01/22  22:51:21  rpd
# 	Revised for new Makefile organization.
# 	[92/01/17            rpd]
# 

# Makefile-common defines common, useful definitions.
# Makefiles should be structured as follows:
#
#	include ${MAKETOP}Makefile-common
#	... rules
#	-include Makedep
#
# Makefile-common is included first and Makedep last.
# Makefile-common is included first so that the Makefile
# can override definitions it doesn't like and so that
# definitions in Makefile-common can be used in dependency lines
# in the Makefile.
# 
# The Makefile can define several variables which are used
# but not defined by Makefile-common.  DEFS (-D, -U flags)
# and INCS (-I flags) are passed to cpp and cpp-using programs.
# CCOPTS augments CFLAGS.  MIGOPTS augments MIGFLAGS.

# ensure that 'install' is the default target

install ::

# to print the environment

printenv ::
	printenv

# 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.
#
# For xstrip, see Makefile-xstrip.
# For INSTALLDIR, use STRIP.  For HOST_INSTALLDIR, use HOST_STRIP.

RM = rm -f
CP = cp -p
MV = mv -f

CHMOD_INC = chmod 444
CHMOD_BIN = chmod 755
CHMOD_LIB = chmod 644
CHMOD_O = chmod 644
CHMOD_MAN = chmod 444

# 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.

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}

LIBCMUCS = -lcmucs
DEPCMUCS = ${DEP_TEMPLATE/NAME/libcmucs.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}

LIBREADLINE = -lreadline
DEPREADLINE = ${DEP_TEMPLATE/NAME/libreadline.a}

LIBENV = -lenv
DEPENV = ${DEP_TEMPLATE/NAME/libenv.a}

# 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).

# The following variables define the "compiler suite":
#	CC	- C compiler for the target machine
#	CFLAGS	- flags for the C compiler
#	HOST_CC	- C compiler for the host machine
#	HOST_CFLAGS - flags for HOST_CC
#	CPP	- C preprocessor for the target machine
#	HOST_CPP_FLAGS - Flags for target machine CPP for code
#		  to be compiled for and run on host machine
#	AS	- Assembler for the target machine
#	HOST_AS - Assembler for the host machine
#	ASCPP	- Preprocessor for assembly language
#	HOST_ASCPP - Preprocessor for host assembly language
#	ASFLAGS	- flags for the assembler
#	HOST_ASFLAGS - flags for the host assembler
#	LD	- Loader for the target machine
#	LDFLAGS	- flags for the loader
#	LDLIBS	- libraries needed when using the loader
#	AR	- Library archiver for the target machine
#	HOST_AR	- Library archiver for the host machine
#	RANLIB	- Postprocessor of library archives for the target machine
#	HOST_RANLIB - Postprocessor of library archives for the host machine
#	MIG	- mig for the target machine
#	MIGFLAGS - options for mig for the target machine
#	STRIP	- strip for the target machine
#	HOST_STRIP - strip for the host machine
#	SIZE	- size for the target machine
#	HOST_SIZE - size of the host machine

LUNA88K_CC = gcc2b -Vcmu
VAX_CC = gcc
SUN3_CC = gcc -m68020
I386_CC = gcc
I860_CC = cc860
PC532_CC = gcc2
CC = ${${TARGET_MACHINE}_CC?${${TARGET_MACHINE}_CC}:cc}

# compile everything with optimization
PMAX_CDEBUGFLAGS = -O2
CDEBUGFLAGS = ${${TARGET_MACHINE}_CDEBUGFLAGS?${${TARGET_MACHINE}_CDEBUGFLAGS}:-O}

# increase compiler string space on mips, so the compiler
# doesn't barf on long file names
PMAX_CFLAGS = -Wf,-XNk150
I860_CFLAGS = -DiPSC860

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

# C compiler for compiling for the host machine
HOST_CC = ${${HOST_MACHINE}_HOST_CC?${${HOST_MACHINE}_HOST_CC}:${CC}}

# C flags for compiling for the host machine
HOST_CFLAGS = ${${HOST_MACHINE}_HOST_CFLAGS?${${HOST_MACHINE}_HOST_CFLAGS}:${CFLAGS}}

# Flags for target CPP for code to be compiled for host (empty by default).
# Only used for code that must be preprocessed as for the target machine
# (predefines, includes) but run on the host machine
PMAX_HOST_CPP_FLAGS = -Dconst=
HOST_CPP_FLAGS = ${${HOST_MACHINE}_HOST_CPP_FLAGS?${${HOST_MACHINE}_HOST_CPP_FLAGS}:}

I860_CPP = /lib/cpp
PC532_CPP = ${CC} -x c -E
CPP = ${${TARGET_MACHINE}_CPP?${${TARGET_MACHINE}_CPP}:${CC} -E}

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

HOST_AS = ${${HOST_MACHINE}_HOST_AS?${${HOST_MACHINE}_HOST_AS}:${AS}}

I860_ASCPP = /lib/cpp
LUNA88K_ASCPP = ascpp ${CPP}
PC532_ASCPP = ${CC} -x c -E
ASCPP = ${${TARGET_MACHINE}_ASCPP?${${TARGET_MACHINE}_ASCPP}:${CC} -ES}

HOST_ASCPP = ${${HOST_MACHINE}_HOST_ASCPP?${${HOST_MACHINE}_HOST_ASCPP}:${ASCPP}}

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

HOST_ASFLAGS = ${${HOST_MACHINE}_HOST_ASFLAGS?${${HOST_MACHINE}_HOST_ASFLAGS}:${ASFLAGS}}

LD = ${${TARGET_MACHINE}_LD?${${TARGET_MACHINE}_LD}:ld}

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

# Some architectures need additional libraries defined when linking with
# ${LD} (e.g, compiler support libraries).

LUNA88K_LDLIBS = /usr/local/lib/gcc/m88k-luna/cmu/libgcc.a
PC532_LDLIBS = -lgcc
LDLIBS = ${${TARGET_MACHINE}_LDLIBS}

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

HOST_AR = ${${HOST_MACHINE}_HOST_AR?${${HOST_MACHINE}_HOST_AR}:${AR}}

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

HOST_RANLIB = ${${HOST_MACHINE}_HOST_RANLIB?${${HOST_MACHINE}_HOST_RANLIB}:${RANLIB}}

# 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.
# When running mig, we want to use HOST_LPATH instead of LPATH
# so that we find the appropriate migcom.  We do NOT want HOST_CPATH.

PC532_MIG = ${USE_HOST_LPATH} mig -cc "${CPP}"
MIG = ${${TARGET_MACHINE}_MIG?${${TARGET_MACHINE}_MIG}:${USE_HOST_LPATH} mig}

# the Makefile can define MIGOPTS to add something here

MIGFLAGS = ${${TARGET_MACHINE}_MIGFLAGS} -MD ${DEFS} ${INCS} ${MIGOPTS}

# strip/size commands, which may differ between target and host

STRIP = strip

HOST_STRIP = ${${HOST_MACHINE}_HOST_STRIP?${${HOST_MACHINE}_HOST_STRIP}:${STRIP}}

SIZE = size

HOST_SIZE = ${${HOST_MACHINE}_HOST_SIZE?${${HOST_MACHINE}_HOST_SIZE}:${SIZE}}

# Default suffix-based rules for creating objects:

.SUFFIXES: .cs .ss .b

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

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

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

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

DECODE = uudecode

.b.o:
	${DECODE} $*.b

MD = md

EXIT_RULE = -${MD} -u Makedep -f -d *.d

.EXIT ::
	${EXIT_RULE}

# Default definitions for Makefile-bin and Makefile-lib.
# These definitions are placed here so that a Makefile
# can override them before including Makefile-bin and/or Makefile-lib.
# (Because they are used in dependencies, they can't be overridden
# after including Makefile-bin and/or Makefile-lib.)

# default location for installing a library

LIBDIR = lib/

# default location for installing a binary

BINDIR = bin/

# Makefile-site may override the above definitions or may add
# additional site-specific definitions.  For example, Makefile-site
# can override the definition of CFLAGS, or add a definition
# of ${TARGET_MACHINE}_CC, or can even override the default
# suffix-based rules for creating objects.

-include Makefile-site
