# 
# 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-subdirs,v $
# Revision 2.5  92/04/08  23:27:02  rpd
# 	Cross-compilation support.
# 	[92/03/19            jvh]
# 
# Revision 2.4  92/03/05  22:47:28  rpd
# 	New'n'improved RECURSE_TEMPLATE.
# 	Added TARGET_MACHINE to MVARIABLES.
# 	[92/02/29            rpd]
# 
# Revision 2.3  92/02/16  15:43:58  rpd
# 	Changed RECURSE_TEMPLATE to print informative messages.
# 	[92/02/16            rpd]
# 
# Revision 2.2  92/02/15  19:34:19  rpd
# 	Created from Makefile-common.
# 	[92/02/10            rpd]
# 

# Makefile-subdirs has definitions and rules useful to Makefiles
# which must to recurse into subdirectories.  For example,
#
#	include ${MAKETOP}Makefile-common
#	include ${MAKETOP}Makefile-subdirs
#
#	DIRS = foo bar baz
#	RECURSE = ${RECURSE_TEMPLATE/RECURSE_DIRS/${DIRS}}
#
#	install ::
#		+${RECURSE/RECURSE_TARGET/install}
#
#	clean ::
#		+${RECURSE/RECURSE_TARGET/clean}
#
# The '+' is optional, but its presence will cause make
# to execute the rule & recurse when doing a 'make -n'.


# Recursive makes should pass down the following variables:

MVARIABLES = INSTALLDIR FRELEASEDIR TRELEASEDIR \
	HOST_INSTALLDIR HOST_FRELEASEDIR HOST_TRELEASEDIR \
	PATH CPATH LPATH HOST_CPATH HOST_LPATH HOST_X_CPATH \
	TARGET_MACHINE HOST_MACHINE cpu host_cpu atsys host_atsys

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

# For each subdirectory, we want to
#	1) echo "cd dir && make target"
#	2) cd dir && ${MAKE} ${MFLAGS} target
#
# However, we only want to visit subdirectories that contain a
# Makefile.  Some directories might not be checked-out, or might not
# have Makefiles at all.
#
# We use wh to check for a Makefile somewhere on MAKEPSD,
# in case there is shadowing.
#
# Making this work is tricky.  We want make to stop
# if there is an error, which means no "-", which means make
# gives the shell "-e", which means "if" doesn't work, which
# means we need "sh -c" to lose the "-e", which means we
# need to check for failed submakes explicitly.
#
# We also create subdirectories in the object directory.
# (So that they exist before we try to cd into them.)
# We do this whether or not they contain a Makefile.
# For example, if RECURSE_DIRS is "bin bin/foo bin/bar",
# we must "mkdir bin" before "mkdir bin/foo" even if
# bin/Makefile doesn't exist.

RECURSE_TEMPLATE = @sh -c 'for dir in RECURSE_DIRS; do \
	[ -d $$dir ] || mkdir $$dir; \
	if ${WH} -p MAKEPSD $$dir/Makefile > /dev/null 2>&1; then \
	  echo "cd $$dir && make RECURSE_TARGET"; \
	  if (cd $$dir && ${MAKE} ${MFLAGS} RECURSE_TARGET); then continue; \
	  else exit; fi \
	fi; done; true'
