#
# postgres.subdir.mk - originally based on
#	@(#)bsd.subdir.mk	5.10 (Berkeley) 8/7/91
#
# /usr/local/devel/postgres-v4r2/src/tools/bmake/mk-proto/RCS/postgres.subdir.mk,v 1.5 1993/07/29 06:30:55 aoki Exp
#

.MAIN: all

STRIP?=	-s

BINGRP?=	bin
BINOWN?=	bin
BINMODE?=	555

OBJDEST?=       /private/obj/
OBJSTRIPPREFIX?=        /usr/

MAKEOBJDIR?=	obj

_SUBDIRUSE: .USE
	@for entry in ${SUBDIR}; do \
		(if test -d ${.CURDIR}/$${entry}.${MACHINE}; then \
			echo "===> $${entry}.${MACHINE}"; \
			cd ${.CURDIR}/$${entry}.${MACHINE}; \
		else \
			echo "===> $$entry"; \
			cd ${.CURDIR}/$${entry}; \
		fi; \
		${MAKE} ${.TARGET:realinstall=install}); \
	done

${SUBDIR}::
	@if test -d ${.TARGET}.${MACHINE}; then \
		cd ${.CURDIR}/${.TARGET}.${MACHINE}; \
	else \
		cd ${.CURDIR}/${.TARGET}; \
	fi; \
	${MAKE} all

.if !target(all)
all: _SUBDIRUSE
.endif

.if !target(clean)
clean: _SUBDIRUSE
.endif

.if !target(cleandir)
cleandir: _SUBDIRUSE
.endif

.if !target(depend)
depend: _SUBDIRUSE
.endif

.if !target(manpages)
manpages: _SUBDIRUSE
.endif

.if !target(install)
.if !target(beforeinstall)
beforeinstall:
.endif
.if !target(afterinstall)
afterinstall:
.endif
install: afterinstall
afterinstall: realinstall
realinstall: beforeinstall _SUBDIRUSE
.if defined(ALLINSTALL)
beforeinstall: all
.endif
.endif
.if !target(maninstall)
maninstall: _SUBDIRUSE
.endif

.if !target(lint)
lint: _SUBDIRUSE
.endif

#
# The following "*obj*" targets differ from the
# postgres.{prog,shell}.mk targets in that we must build objs for
# subdirs, too (so users can put local Makefiles in them).
# 

#
# "obj" places links pointing into the object tree (rooted at OBJDEST)
# inside of the source tree.
#
# The object directories themselves may not exist yet and must be
# explicitly created by doing "objdir". 
#
# The convention is that the name of the object tree for /foo/bar/...
# is ${OBJDEST}/foo/bar/...  though /usr/foo/bar/... is special-cased
# to become ${OBJDEST}/foo/bar/... as well.
#
.if !target(obj)
.if defined(NOOBJ)
obj: _SUBDIRUSE
.else
obj: _SUBDIRUSE
	@cd ${.CURDIR}; rm -rf ${MAKEOBJDIR}; \
	here=`pwd`; dest=${OBJDEST}`echo $$here | sed 's,${OBJSTRIPPREFIX},,'`; \
	echo "$$here -> $$dest"; ln -s $$dest ${MAKEOBJDIR}; \
	if test ! -d $$dest; then \
		bmkdir -p $$dest; \
	else \
		true; \
	fi;
.endif
.endif

#
# "objdir" actually creates the object tree directories.
#
# It assumes that the links generated by the "obj" target already exist.
#
.if !target(objdir)
.if defined(NOOBJ)
objdir: _SUBDIRUSE
.else
objdir: _SUBDIRUSE
	@cd ${.CURDIR}; \
	dest=`ls -ld ${MAKEOBJDIR} | awk '{print $$NF}'`; \
	if test ! -d $$dest; then \
		bmkdir -p $$dest; \
	else \
		true; \
	fi;
.endif
.endif

#
# "localobj" creates actual "obj" directories inside of the source tree.
#
# This is used in source trees that have been copied from a master tree.
#
.if !target(localobj)
.if defined(NOOBJ)
localobj: _SUBDIRUSE
.else
localobj: _SUBDIRUSE
	@-cd ${.CURDIR}; \
	[ ! -d obj ] && mkdir obj 2>/dev/null; \
	true
.endif
.endif

#
# We do not have to duplicate Marc's disgusting tagsfile hack here
# (c.f. postgres.prog.mk) since non-program directoriess obviously
# don't have tags.  However, this directory may have subdirectories
# that do have tags.
#
.if !target(tags)
tags: _SUBDIRUSE
.endif
