#
# /+\
# +\	Copyright 1993, 1995 Christopher Seiwald.
# \+/
#
# This file is part of Jam - see jam.c for Copyright information.
#

#
# JAMBASE - jam 2.0 ruleset providing make(1)-like functionality
#
# Supports UNIX, NT, and VMS.
#
# 12/27/93 (seiwald) - purturb library sources with SOURCE_GRIST
# 04/18/94 (seiwald) - use 'default =' when setting OS specific vars
# 04/21/94 (seiwald) - do RmTemps together
# 05/05/94 (seiwald) - all supported C compilers support -o: relegate
#		       RELOCATE as an option; set Ranlib to "" to disable it
# 06/01/94 (seiwald) - new 'actions existing' to do existing sources
# 08/25/94 (seiwald) - new ObjectCcFlags rule to append to per-target CCFLAGS
# 08/29/94 (seiwald) - new ObjectHdrs rule to append to per-target HDRS
# 09/19/94 (seiwald) - LinkLibraries and Undefs now append
#		     - Rule names downshifted.
# 10/06/94 (seiwald) - Dumb yyacc stuff moved into Jamfile.
# 10/14/94 (seiwald) - (Crude) support for .s, .C, .cc, .cpp, and .f files.
# 01/08/95 (seiwald) - Shell now handled with awk, not sed
# 01/09/95 (seiwald) - Install* now take dest directory as target
# 01/10/95 (seiwald) - All entries sorted.
# 01/10/95 (seiwald) - NT support moved in, with LauraW's help.  
# 01/10/95 (seiwald) - VMS support moved in.
# 02/06/95 (seiwald) - ObjectC++Flags and SubDirC++Flags added.
# 02/07/95 (seiwald) - Iron out when HDRSEARCH uses "" or SEARCH_SOURCE.
# 02/08/95 (seiwald) - SubDir works on VMS.
# 02/14/95 (seiwald) - MkDir and entourage.

# Special targets defined in this file:
#
# all		- parent of first, shell, files, lib, exe
# first		- first dependent of 'all', for potential initialization
# shell		- parent of all Shell targets 
# files		- parent of all File targets
# lib		- parent of all Library targets
# exe		- parent of all Main targets
# dirs		- parent of all MkDir targets
# clean		- removes all Shell, File, Library, and Main targets
# uninstall	- removes all Install targets
#	

# Rules defined by this file:
#
# as obj.o : source.s ;			.s -> .o
# Bulk dir : files ;			populate directory with many files
# Cc obj.o : source.c ;			.c -> .o
# C++ obj.o : source.cc ;		.cc -> .o
# Clean clean : sources ;		remove sources with 'jam clean'
# File dest : source ;			copy file
# Fortran obj.o : source.f ;		.f -> .o
# Hardlink target : source ;		make link from source to target
# HdrRule source : headers ;		handle #includes
# Install target : source ;		install any single file
# InstallBin dir : sources ;		install binaries
# InstallLib dir : sources ;		install files
# InstallMan dir : sources ;		install man pages
# InstallShell dir : sources ;		install shell scripts
# Lex source.c : source.l ;		.l -> .c
# Library lib : source ;		archive library from compiled sources
# LibraryFromObjects lib : objects ;	archive library from objects
# LinkLibraries images : libraries ;	bag libraries onto Mains
# Main image : source ;			link executable from compiled sources
# MainFromObjects image : objects ;	link executable from objects
# MkDir dir ;				make a directory, if not there
# Object object : source ;		compile object from source
# ObjectCcFlags source : flags ;	add compiler flags for object
# ObjectC++Flags source : flags ;	add compiler flags for object
# ObjectHdrs source : dirs ;		add include directories for object
# Objects sources ;			compile sources
# RmTemps target : sources ;		remove temp sources after target made
# Setuid images ;			mark executables Setuid
# SubDir TOP d1 d2 ... ;		start a subdirectory Jamfile
# SubDirCcFlags flags ;			add compiler flags until next SubDir
# SubDirC++Flags flags ;		add compiler flags until next SubDir
# SubDirHdrs dirs ;			add include dirs until next SubDir
# SubInclude TOP d1 d2 ... ;		include a subdirectory Jamfile
# Shell exe : source ;			make a shell executable
# Undefines images : symbols ;		save undef's for linking
# UserObject object : source ;		handle unknown suffixes for Object
# Yacc source.c : source.y ;		.y -> .c
#

# Brief review of the jam language:
#
# Statements:
#	rule RULE - statements to process a rule
#	actions RULE - system commands to carry out target update
#
# Modifiers on actions:
#	together - multiple instances of same rule on target get executed
#		   once with their sources ($(>)) concatenated
#	updated - refers to updated sources ($(>)) only
#	ignore - ignore return status of command
#	quietly - don't trace its execution unless verbose
#	piecemeal - iterate command each time with a small subset of $(>)
#	existing - refers to currently existing sources ($(>)) only
#
# Special rules:
#	ALWAYS - always build a target
#	DEPENDS - builds the dependency graph
#	ECHO - blurt out targets on stdout
#	EXIT - blurt out targets and exit
#	INCLUDES - marks sources as headers for target (a codependency)
#	NOCARE - don't panic if the target can't be built
#	NOUPDATE - create the target if needed but never update it 
#	NOTFILE - ignore the timestamp of the target (it's not a file)
#	TEMPORARY - target need not be present if sources haven't changed
#
# Special variables set by jam:
#	$(<) - targets of a rule (to the left of the :)
#	$(>) - sources of a rule (to the right of the :)
#	$(UNIX) - true on UNIX
#	$(VMS) - true on VMS
#	$(NT) - true on NT
#	$(OS) - name of OS - varies wildly
#
# Special variables used by jam:
#	SEARCH - where to find something (used during binding and actions)
#	LOCATE - where to plop something not found with SEARCH
#	HDRRULE - rule to call to handle include files
#	HDRSCAN - egrep regex to extract include files
#
# Special targets:
#	all - default if none given on command line
#

# Initialize variables
#
# "default =" - set only if unset

#
# OS specific variable settings
#

switch $(OS)
{
case AIX :	LINKLIBS default = -lbsd ;
case DGUX :	RANLIB default = "" ;
case IRIX :	RANLIB default = "" ;
case HPUX :	RANLIB default = "" ;
		INSTALL default = "" ;
case PTX :	RANLIB default = "" ;
case SOLARIS :	RANLIB default = "" ;
}

if $(UNIX)
{
	AR		default = ar ru ;
	AS		default = as ;
	AWK		default = awk ;
	ASFLAGS		default = ;
	BINDIR		default = /usr/local/bin ;
	C++		default = gcc ;
	C++FLAGS	default = ;
	CC		default = cc ;
	CCFLAGS		default = ;
	CP		default = cp ;
	CHMOD		default = chmod ;
	EXEMODE		default = 711 ;
	FILEMODE	default = 644 ;
	FORTRAN		default = f77 ;
	FORTRANFLAGS	default = ;
	HDRS		default = ;
	INSTALL		default = install ;
	LEX		default = lex ;
	LIBDIR		default = /usr/local/lib ;
	LINK		default = $(CC) ;
	LINKFLAGS	default = $(CCFLAGS) ;
	LINKLIBS	default = ;
	LN		default = ln ;
	MANDIR		default = /usr/local/man ;
	MKDIR		default = mkdir ;
	MV		default = mv -f ;
	OPTIM		default = -O ;
	RANLIB		default = ranlib ;
	RM		default = rm -f ;
	SHELLHEADER	default = "#!/bin/sh" ;
	SHELLMODE	default = 755 ;
	SLASH		default = / ;
	STDHDRS		default = /usr/include ;
	SUFLIB		default = .a ;
	SUFOBJ		default = .o ;
	SUFEXE		default = "" ;
	UNDEFFLAG	default = "-u _" ;
	YACC		default = yacc -d ;
}
else if $(NT)
{
    if $(BCCROOT)
    {
	ECHO "Compiler is Borland C++" ;

	AR		default = tlib ;
	ARFLAGS		default = /C /P64 ;
	CC		default = bcc32 ;
	CCFLAGS		default = -v -w-  ;
	MV		default = rename ;
	RM		default = del ;
	RW		default = $(BCCROOT)\\lib\\rw ;
	RWLIBPATH	default = $(RW)\\lib ;
	LINK		default = $(CC) ;
	LINKFLAGS	default = $(CCFLAGS) ;
	SLASH		default = \\ ;
	STDLIBPATH	default = $(BCCROOT)\\lib ;
	STDHDRS		default = $(BCCROOT)\\include ;
	SUFLIB		default = .lib ;
	SUFOBJ		default = .obj ;
	SUFEXE		default = .exe ;
    }
    else if $(MSVCNT)
    {
	ECHO "Compiler is Microsoft Visual C++" ;

	AR		default = lib ;
	CC		default = cl ;
	CCFLAGS		default = /D \"NT\" ;
	MV		default = rename ;
	RM		default = del ;
	RW		default = $(MSVCNT)\\lib\\rw ;
	RWLIBPATH	default = $(RW)\\lib ;
	LINK		default = $(CC) ;
	LINKFLAGS	default = $(CCFLAGS) ;
	LINKLIBS	default = $(MSVCNT)\\lib\\advapi32.lib
				$(MSVCNT)\\lib\\libcmt.lib
				$(MSVCNT)\\lib\\libc.lib
				$(MSVCNT)\\lib\\oldnames.lib
				$(MSVCNT)\\lib\\kernel32.lib ;
	OPTIM		default =  ;
	SLASH		default = \\ ;
	STDHDRS		default = $(MSVCNT)\\include ;
	SUFLIB		default = .lib ;
	SUFOBJ		default = .obj ;
	SUFEXE		default = .exe ;
	UNDEFFLAG	default = "/u _" ;
    }
}
else if $(VMS)
{
	AS		default = as ;
	CC		default = cc ;
	CCFLAGS		default = ;
	CRELIB		default = true ;
	EXEMODE		default = (w:e) ;
	FILEMODE	default = (w:r) ;
	HDRS		default = ;
	LEX		default = lex ;
	LINK		default = link ;
	LINKFLAGS	default = ;
	LINKLIBS	default = ;
	MV		default = rename ;
	OPTIM		default = ;
	RM		default = delete ;
	SHELLMODE	default = (w:er) ;
	SLASH		default = . ;
	STDHDRS		default = decc$library_include ;
	SUFLIB		default = .olb ;
	SUFOBJ		default = .obj ;
	SUFEXE		default = .exe ;

	switch $(OS) 
	{
	case OPENVMS : CCFLAGS default = /stand=vaxc ;
	case VMS     : LINKLIBS default = sys$library:vaxcrtl.olb/lib ;
	}
}

JAMFILE		default = Jamfile ;
JAMRULES	default = Jamrules ;

HDRPATTERN = "^#[	 ]*include[	 ]*[<\"](.*)[\">].*$" ;

#
# Base dependencies - first for "bootstrap" kinds of rules
#

DEPENDS all : first shell files lib exe ;
NOTFILE all first shell files lib exe dirs clean uninstall ;
ALWAYS clean uninstall ;

#
# Rules
#

rule As
{
	DEPENDS $(<) : $(>) ;
}

rule Bulk
{
	for i in $(>)
	{
	    File $(i:D=$(<)) : $(i) ;
	}
}

rule Cc
{
	DEPENDS $(<) : $(>) ;

	# Just to clarify here: this sets the per-target CCFLAGS to
	# be the current value of (global) CCFLAGS and SUBDIRCCFLAGS.

	CCFLAGS on $(<) += $(CCFLAGS) $(SUBDIRCCFLAGS) ;

	# If the compiler's -o flag doesn't work, relocate the .o

	if $(RELOCATE)
	{
	    CcMv $(<) : $(>) ;
	}

	if $(VMS) && $(HDRS[1])
	{
	    SLASHINC on $(<) = "/inc=(" $(HDRS[1]) ,$(HDRS[2-]) ")" ;
	}
}

rule C++
{
	DEPENDS $(<) : $(>) ;
	C++FLAGS on $(<) += $(C++FLAGS) $(SUBDIRC++FLAGS) ;

	if $(VMS) && $(HDRS[1])
	{
	    SLASHINC on $(<) = "/inc=(" $(HDRS[1]) ,$(HDRS[2-]) ")" ;
	}
}

rule File
{
	DEPENDS files : $(<) ;
	DEPENDS $(<) : $(>) ;
	SEARCH on $(>) = $(SEARCH_SOURCE) ;
	MODE on $(<) = $(FILEMODE) ;
	Chmod $(<) ;
}

rule Fortran
{
	DEPENDS $(<) : $(>) ;
}

rule HardLink
{
	DEPENDS files : $(<) ;
	DEPENDS $(<) : $(>) ;
	SEARCH on $(>) = $(SEARCH_SOURCE) ;
}

rule HdrRule
{
	# HdrRule source : headers ;

	# N.B.	This rule is called during binding, potentially after
	# the fate of many targets has been determined, and must be
	# used with caution: don't add dependencies to unrelated
	# targets, and don't set variables on $(<).

	# Tell Jam that anything depending on $(<) also depends on $(>),
	# set SEARCH so Jam can find the headers, but then say we don't
	# care if we can't actually find the headers (they may have been
	# within ifdefs),

	INCLUDES $(<) : $(>) ;
	SEARCH on $(>) = $(HDRSEARCH) ;
	NOCARE $(>) ;

	# Propagate on $(<) to $(>)

	HDRSEARCH on $(>) = $(HDRSEARCH) ;
	HDRSCAN on $(>) = $(HDRSCAN) ;
	HDRRULE on $(>) = $(HDRRULE) ;
}

rule Install
{
	DEPENDS install : $(<) ;
	DEPENDS $(<) : $(>) ;
	SEARCH on $(>) = $(SEARCH_SOURCE) ;

	# depend upon and create the directory

	DEPENDS $(<) : $(<:D) ;
	MkDir $(<:D) ;

	# Arrange for jam uninstall

	Clean uninstall : $(<) ;

	if ! $(INSTALL)
	{
	    Chmod $(<) ;

	    if $(OWNER) { Chown $(<) ; OWNER on $(<) = $(OWNER) ; }
	    if $(GROUP) { Chgrp $(<) ; GROUP on $(<) = $(GROUP) ; }
	}
}

rule InstallBin
{
	for i in $(>)
	{
	    Install $(i:D=$(<)) : $(i) ;
	    MODE on $(i:D=$(<)) = $(EXEMODE) ;
	}
}

rule InstallLib
{
	for i in $(>)
	{
	    Install $(i:D=$(<)) : $(i) ;
	    MODE on $(i:D=$(<)) = $(FILEMODE) ;
	}
}

rule InstallMan
{
	# Really this just strips the . from the suffix

	for i in $(>)
	{
	    switch $(i:S)
	    {
	    case .1 : s = 1 ; case .2 : s = 2 ; case .3 : s = 3 ;
	    case .4 : s = 4 ; case .5 : s = 5 ; case .6 : s = 6 ;
	    case .7 : s = 7 ; case .8 : s = 8 ; case .l : s = l ;
	    case .n : s = n ; case .man : s = 1 ;
	    }

	    d = $(i:D=man$(s):S=.$(s)) ;

	    Install $(d:R=$(<)) : $(i) ;
	    MODE on $(d:R=$(<)) = $(FILEMODE) ;
	}
}

rule InstallShell
{
	for i in $(>)
	{
	    Install $(i:D=$(<)) : $(i) ;
	    MODE on $(i:D=$(<)) = $(SHELLMODE) ;
	}
}

rule Lex
{
	DEPENDS $(<) : $(>) ;
	LOCATE on $(<) = $(LOCATE_TARGET) ;
	Clean clean : $(<) ;
}

rule Library
{
	LibraryFromObjects $(<) : $(>:S=$(SUFOBJ)) ;
	Objects $(>) ;
}

rule LibraryFromObjects
{
	# library depends on its member objects

	l = $(<:S=$(SUFLIB)) ;
	s = $(>) ;

	if $(SOURCE_GRIST)
	{
	    s = $(>:G=$(SOURCE_GRIST)) ;
	}

	DEPENDS lib : $(l) ;
	DEPENDS $(l) : $(l)($(s:BS)) ;

	Clean clean : $(l) ;

	# We wish we could locate the library and it's contents,
	# but the reference to $(NEEDLIBS) in Main's actions
	# get the unbound names.  Only $(<) and $(>) refer to
	# bound file name in rule actions.  Sigh.
	#
	# LOCATE on $(<) $(<)($(s:BS)) = $(LOCATE_TARGET) ;

	# each archive member object depends on real object
	# each real object gets compiled from sources

	for i in $(s)
	{
	    DEPENDS $(l)($(i:BS)) : $(i) ;
	}

	# must call separate Archive rule so that 'updated' modifier
	# on 'actions' refers to updated .o's.
	# delete objects after archive is made

	if $(CRELIB) { CreLib $(l) ; }

	Archive $(l) : $(s) ;
	RmTemps $(l) : $(s) ;

	if $(RANLIB) { Ranlib $(l) ; }
}

rule Link
{
	MODE on $(<) = $(EXEMODE) ;
	Chmod $(<) ;
}

rule LinkLibraries
{
	# make library dependencies of target
	# set NEEDLIBS variable used by 'actions Main'

	if $(<:S)
	{
	    t = $(<) ;
	} else {
	    t = $(<:S=$(SUFEXE)) ;
	}

	DEPENDS $(t) : $(>:S=$(SUFLIB)) ;
	NEEDLIBS on $(t) += $(>:S=$(SUFLIB)) ;
}

rule Main
{
	MainFromObjects $(<) : $(>:S=$(SUFOBJ)) ;
	Objects $(>) ;
}

rule MainFromObjects
{
	# make compiled sources a dependency of target

	s = $(>) ;

	if $(SOURCE_GRIST)
	{
	    s = $(>:G=$(SOURCE_GRIST)) ;
	}

	if $(<:S)
	{
	    t = $(<) ;
	} else {
	    t = $(<:S=$(SUFEXE)) ;
	}

	DEPENDS exe : $(t) ;
	DEPENDS $(t) : $(s) ;
	LOCATE on $(t) = $(LOCATE_TARGET) ;

	Clean clean : $(t) ;

	Link $(t) : $(s) ;
}

rule MkDir
{
	if ! $($(<)-mkdir) 
	{
	    # Cheesy gate to prevent multiple invocations on same dir
	    # MkDir1 has the actions 
	    # If dir exists, don't update it
	    # Arrange for jam dirs

	    $(<)-mkdir = true ;
	    MkDir1 $(<) ;
	    NOUPDATE $(<) ;
	    Depends dirs : $(<) ;

	    # Recursively make parent directories.
	    # On UNIX, $(<:D) = $(<)'s parent, & we recurse until root
	    # On VMS, $(<:D) = $(<), no recursing, but you don't need to 
	    # create parent directories explicitly there.

	    s = $(<:D) ;

	    if $(s) && $(s) != $(<)
	    {
		Depends $(<) : $(s) ;
		MkDir $(s) ;
	    }
	}
}

rule Object
{
	# locate object and search for source, if wanted

	Clean clean : $(<) ;

	LOCATE on $(<) = $(LOCATE_TARGET) ;
	SEARCH on $(>) = $(SEARCH_SOURCE) ;

	# Save HDRS for -I$(HDRS) on compile.
	# We shouldn't need -I$(SEARCH_SOURCE) as cc can find headers
	# in the .c file's directory, but generated .c files (from
	# yacc, lex, etc) are located in $(LOCATE_TARGET), possibly
	# different from $(SEARCH_SOURCE).

	HDRS on $(<) = $(HDRS) $(SUBDIRHDRS) $(SEARCH_SOURCE) ;

	# handle #includes for source: Jam scans for headers with
	# the regexp pattern $(HDRSCAN) and then invokes $(HDRRULE)
	# with the scanned file as the target and the found headers
	# as the sources.  HDRSEARCH is just grist for HdrRule.

	# $(h) is where cc first looks for #include "foo.h" files.
	# If the source file is in a distant directory, look there.
	# Else, look in "" (the current directory).

	if $(SEARCH_SOURCE)
	{
	    h = $(SEARCH_SOURCE) ;
	}
	else
	{
	    h = "" ;
	}

	HDRRULE on $(>) = HdrRule ;
	HDRSCAN on $(>) = $(HDRPATTERN) ;
	HDRSEARCH on $(>) = $(HDRS) $(SUBDIRHDRS) $(h) $(STDHDRS) ;

	# if source is not .c, generate .c with specific rule

	switch $(>:S)
	{
	    case .c :	Cc $(<) : $(>) ;
	    case .C :	C++ $(<) : $(>) ;
	    case .cc :	C++ $(<) : $(>) ;
	    case .cpp : C++ $(<) : $(>) ;
	    case .f :	Fortran $(<) : $(>) ;
	    case .l :	Cc $(<) : $(<:S=.c) ;
			Lex $(<:S=.c) : $(>) ;
	    case .s :	As $(<) : $(>) ;
	    case .y :	Cc $(<) : $(<:S=.c) ;
			Yacc $(<:S=.c) : $(>) ;
	    case * :	UserObject $(<) : $(>) ;
	}
}

rule ObjectCcFlags
{
	s = $(<:S=$(SUFOBJ)) ;

	if $(SOURCE_GRIST)
	{
	    s = $(s:G=$(SOURCE_GRIST)) ;
	}

	CCFLAGS on $(s) += $(>) ;
}

rule ObjectC++Flags
{
	s = $(<:S=$(SUFOBJ)) ;

	if $(SOURCE_GRIST)
	{
	    s = $(s:G=$(SOURCE_GRIST)) ;
	}

	C++FLAGS on $(s) += $(>) ;
}

rule ObjectHdrs
{
	s = $(<:S=$(SUFOBJ)) ;

	if $(SOURCE_GRIST)
	{
	    s = $(s:G=$(SOURCE_GRIST)) ;
	}

	HDRS on $(s) += $(>) ;
}

rule Objects
{
	s = $(<) ;

	if $(SOURCE_GRIST)
	{
	    s = $(<:G=$(SOURCE_GRIST)) ;
	}

	for i in $(s)
	{
		Object $(i:S=$(SUFOBJ)) : $(i) ;
	}
}

rule RmTemps
{
	TEMPORARY $(>) ;
}

rule Setuid
{
	if $(<:S)
	{
	    t = $(<) ;
	} else {
	    t = $(<:S=$(SUFEXE)) ;
	}

	MODE on $(t) = 4711 ;
}

rule Shell
{
	DEPENDS shell : $(<) ;
	DEPENDS $(<) : $(>) ;
	SEARCH on $(>) = $(SEARCH_SOURCE) ;
	MODE on $(<) = $(SHELLMODE) ;
	Chmod $(<) ;
}

rule SubDir
{
	#
	# SubDir TOP d1 [ ... ]
	#
	# This introduces a Jamfile that is part of a project tree 
	# rooted at $(TOP).  It (only once) includes the project-specific
	# rules file $(TOP)/Jamrules and then sets search & locate stuff.
	#
	# If the variable $(TOPRULES) is set (where TOP is the first arg 
	# to SubDir), that file is included instead of $(TOP)/Jamrules.
	#
	# d1 ... are the directory elements that lead to this directory 
	# from $(TOP).  We construct the system dependent path from these
	# directory elements in order to set search&locate stuff.
	# 

	if ! $($(<[1]))
	{
	    EXIT Top level of source tree has not been set with $(<[1]) ;
	}

	#
	# If $(TOP)/Jamrules hasn't been included, do so.
	#

	if ! $($(<[1])-included)
	{
	    # Gated entry.

	    $(<[1])-included = TRUE ;

	    # File is $(TOPRULES) or $(TOP)/Jamrules.

	    r = $($(<[1])RULES) ;

	    if ! $(r)
	    {
		r = $(JAMRULES:R=$($(<[1]))) ;
	    }

	    # Include it.

	    include $(r) ;
	}

	# Get the grist $(g), search $(s), by concatenating the
	# directory elements using the OS specific path separator.

	g = $(<[2]) ;
	s = $(<[2]) ;

	for i in $(<[3-])
	{
	    g = $(g)!$(i) ;
	    s = $(s)$(SLASH)$(i) ;
	}

	if $(VMS)
	{
	    s = [$(s)] ;
	}

	# Now set up SEARCH_SOURCE, LOCATE_TARGET, SOURCE_GRIST
	# These can be reset if needed.	 For example, if the source
	# directory should not hold object files, LOCATE_TARGET can
	# subsequently be redefined.

	SUBDIR = $(s:R=$($(<[1]))) ;
	SEARCH_SOURCE = $(SUBDIR) ;
	LOCATE_TARGET = $(SUBDIR) ;
	SOURCE_GRIST = $(g) ;

	# Reset per-directory ccflags, hdrs

	SUBDIRCCFLAGS = ;
	SUBDIRC++FLAGS = ;
	SUBDIRHDRS = ;
}

rule SubDirCcFlags
{
	SUBDIRCCFLAGS += $(<) ;
}

rule SubDirC++Flags
{
	SUBDIRC++FLAGS += $(<) ;
}

rule SubDirHdrs
{
	SUBDIRHDRS += $(<) ;
}

rule SubInclude
{
	# That's
	#	SubInclude TOP d1 [ d2 [ d3 [ d4 ] ] ]
	#
	# to include a subdirectory's Jamfile.

	if ! $($(<[1]))
	{
	    EXIT Top level of source tree has not been set with $(<[1]) ;
	}

	s = $(<[2]) ;

	for i in $(<[3-])
	{
	    s = $(s)$(SLASH)$(i) ;
	}

	if $(VMS)
	{
	    s = [$(s)] ;
	}

	include $(JAMFILE:D=$(s):R=$($(<[1]))) ;
}

rule Undefines
{
	if $(<:S)
	{
	    t = $(<) ;
	} else {
	    t = $(<:S=$(SUFEXE)) ;
	}

	UNDEFS on $(t) += $(UNDEFFLAG)$(>) ;
}

rule UserObject
{
	EXIT "Unknown suffix on" $(>) "- see UserObject rule in Jamfile(5)." ;
}

rule Yacc
{
	h = $(<:BS=.h) ;

	# Some places don't have a yacc.

	if $(YACC)
	{
	    DEPENDS $(<) $(h) : $(>) ;
	    Yacc1 $(<) $(h) : $(>) ;
	    Clean clean : $(<) $(h) ;
	}

	# make sure someone includes $(h) else it will be
	# a deadly independent target

	INCLUDES $(<) : $(h) ;
	LOCATE on $(<) $(h) = $(LOCATE_TARGET) ;
}

#
# Actions
#

if $(UNIX)
{

    actions updated together piecemeal Archive
    {
	$(AR) $(<) $(>)
    }

    actions As
    {
	$(AS) $(ASFLAGS) -o $(<) $(>) ;
    }

    actions C++
    {
	$(C++) -c $(C++FLAGS) $(OPTIM) -I$(HDRS) -o $(<) $(>)
    }

    actions Cc
    {
	$(CC) -c $(CCFLAGS) $(OPTIM) -I$(HDRS) -o $(<) $(>)
    }

    actions CcMv
    {
	[ $(<) != $(>:BS=$(SUFOBJ)) ] && $(MV) $(>:BS=$(SUFOBJ)) $(<)
    }

    actions Chgrp
    {
	chgrp $(GROUP) $(<)
    }

    actions Chmod
    {
	chmod $(MODE) $(<)
    }

    actions Chown
    {
	chown $(OWNER) $(<)
    }

    actions piecemeal together existing Clean
    {
	$(RM) $(>)
    }

    actions File
    {
	$(RM) $(<)
	$(CP) $(>) $(<) &&
    }

    actions Fortran
    {
	$(FORTRAN) $(FORTRANFLAGS) -o $(<) $(>)
    }

    actions HardLink
    {
	$(RM) $(<) && $(LN) $(>) $(<)
    }

    if $(INSTALL)
    {
	actions Install
	{
	$(INSTALL) -m$(MODE) -o$(OWNER) -g$(GROUP) $(>) $(<)
	}
    }
    else
    {
	actions Install
	{
	$(CP) $(>) $(<) 
	}
    }

    actions Lex
    {
	$(LEX) $(>) && $(MV) lex.yy.c $(<)
    }

    actions Link
    {
	if $(LINK) $(LINKFLAGS) -o $(<) $(UNDEFS) $(>) $(NEEDLIBS) $(LINKLIBS) 
	then :
	else $(RM) $(<) && exit 1
	fi
    }

    actions MkDir1
    {
	$(MKDIR) $(<)
    }

    actions together Ranlib
    {
	$(RANLIB) $(<)
    }

    actions quietly updated piecemeal together RmTemps
    {
	$(RM) $(>)
    }

    actions Shell
    {
	$(AWK) '
		NR == 1 { print "$(SHELLHEADER)" }
		NR == 1 && /^[#:]/ { next }
		/^##/ { next }
		{ print }
	' < $(>) > $(<)
    }

    actions Yacc1
    {
	$(YACC) $(>) &&
	{
	    $(MV) y.tab.c $(<[1])
	    $(MV) y.tab.h $(<[2])
	}
    }
}
else if $(NT)
{
    if $(BCCROOT)
    {
	actions Link
	{
	$(LINK) -e$(<) $(LINKFLAGS) $(UNDEFS) -L$(LINKLIBS) $(NEEDLIBS) $(>) || del $(<) /f
	}

	actions together piecemeal Archive
	{
	$(AR) $(ARFLAGS) $(<) -+$(>)
	}
    }
    else if $(MSVCNT)
    {
	actions Link
	{
	$(LINK) $(LINKFLAGS) /o$(<) $(UNDEFS) $(>) $(NEEDLIBS) $(LINKLIBS)
	}

	actions together piecemeal Archive
	{
	$(AR) /out:$(<) $(>)
	}
    }

    actions Cc
    {
	$(CC) /c $(CCFLAGS) $(OPTIM) /out:$(<) /I$(HDRS) $(>)
    }

    actions Chmod
    {
    }

    actions piecemeal together existing Clean
    {
	$(RM) $(>) /f /q
    }

    actions File
    {
	copy $(>) $(<)
    }

    actions quietly updated piecemeal together RmTemps
    {
	$(RM) $(>)
    }
}
else if $(VMS)
{

    actions updated together piecemeal Archive 
    {
	lib/replace $(<) $(>[1]) ,$(>[2-])
    }

    actions Cc
    { 
	cc/obj=$(<) $(CCFLAGS) $(OPTIM) $(SLASHINC) $(>) 
    }

    actions Chmod
    {
	set file/prot=$(MODE) $(<)
    }

    actions piecemeal together existing Clean
    {
	$(RM) $(>[1]);* ,$(>[2-]);*
    }

    actions together quietly CreLib
    {
       if f$search("$(<)") .eqs. "" then lib/create $(<)
    }

    actions File
    {
	copy $(>) $(<)
    }

    actions Install
    {
	copy $(>) $(<)
    }

    actions Lex
    {
	$(LEX) $(>) 
	$(MV) lex.yy.c $(<)
    }

    actions Link 
    {
	$(LINK)/exe=$(<) $(LINKFLAGS) $(>[1]) ,$(>[2-]) ,$(NEEDLIBS)/lib ,$(LINKLIBS)
    }

    actions MkDir1
    {
	create/dir $(<)
    }

    actions quietly updated piecemeal together RmTemps
    {
	$(RM) $(>[1]);* ,$(>[2-]);*
    }

    actions Shell
    {
	copy $(>) $(<)
    }

    actions Yacc1
    {
	$(YACC) $(>)
	$(MV) y.tab.c $(<[1])
	$(MV) y.tab.h $(<[2])
    }
}


#
# Backwards compatibility with jam 1, where rules were uppercased.
#

rule BULK { Bulk $(<) : $(>) ; }
rule FILE { File $(<) : $(>) ; }
rule HDRRULE { HdrRule $(<) : $(>) ; }
rule INSTALL { Install $(<) : $(>) ; }
rule LIBRARY { Library $(<) : $(>) ; }
rule LIBS { LinkLibraries $(<) : $(>) ; }
rule LINK { Link $(<) : $(>) ; }
rule MAIN { Main $(<) : $(>) ; }
rule SETUID { Setuid $(<) ; }
rule SHELL { Shell $(<) : $(>) ; }
rule UNDEFINES { Undefines $(<) : $(>) ; }

# Old INSTALL* didn't take dest directory.

rule INSTALLBIN { InstallBin $(BINDIR) : $(<) ; }
rule INSTALLLIB { InstallLib $(LIBDIR) : $(<) ; }
rule INSTALLMAN { InstallMan $(MANDIR) : $(<) ; }

#
# Now include the user's Jamfile.
#

include $(JAMFILE) ;

