#
# postgres.prog.mk - originally based on
#	@(#)bsd.prog.mk 5.35 (Berkeley) 8/26/92
#
# /usr/local/devel/postgres-v4r2/src/tools/bmake/mk-proto/RCS/postgres.prog.mk,v 1.13 1993/08/09 10:23:19 aoki Exp
#

# No man pages for now
NOMAN= true

.SUFFIXES: .out .o .c .y .l .s .8 .7 .6 .5 .4 .3 .2 .1 .0

.8.0 .7.0 .6.0 .5.0 .4.0 .3.0 .2.0 .1.0:
	nroff -man ${.IMPSRC} > ${.TARGET}

CFLAGS+=${COPTS}

STRIP?=	-s

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

LIBC?=		/usr/lib/libc.a
LIBCOMPAT?=	/usr/lib/libcompat.a
LIBCURSES?=	/usr/lib/libcurses.a
LIBDBM?=	/usr/lib/libdbm.a
LIBDES?=	/usr/lib/libdes.a
LIBL?=		/usr/lib/libl.a
LIBKDB?=	/usr/lib/libkdb.a
LIBKRB?=	/usr/lib/libkrb.a
LIBKVM?=	/usr/lib/libkvm.a
LIBM?=		/usr/lib/libm.a
LIBMP?=		/usr/lib/libmp.a
LIBPC?=		/usr/lib/libpc.a
LIBPLOT?=	/usr/lib/libplot.a
LIBRESOLV?=	/usr/lib/libresolv.a
LIBRPC?=	/usr/lib/sunrpc.a
LIBTERM?=	/usr/lib/libterm.a
LIBUTIL?=	/usr/lib/libutil.a

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

MAKEOBJDIR?=	obj

INSTALL?=	install

.if defined(SHAREDSTRINGS)
CLEANFILES+=strings
.c.o:
	${CC} -E ${CFLAGS} ${.IMPSRC} | xstr -c -
	@${CC} ${CFLAGS} -c x.c -o ${.TARGET}
	@rm -f x.c
.endif

.if defined(PROG)
.if defined(SRCS)

OBJS+=  ${SRCS:R:S/$/.o/g}
DEPENDS+=  ${SRCS:R:S/$/.d/g}

${PROG}: ${OBJS} ${LIBC} ${DPADD}
	${CC} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}

.else defined(SRCS)

SRCS= ${PROG}.c

${PROG}: ${SRCS} ${LIBC} ${DPADD}
	${CC} ${CFLAGS} -o ${.TARGET} ${.CURDIR}/${SRCS} ${LDADD}

MKDEP=	-p

.endif

.if	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
	!defined(MAN7) && !defined(MAN8) && !defined(NOMAN)
MAN1=	${PROG}.0
.endif
.endif
.if !defined(NOMAN)
MANALL=	${MAN1} ${MAN2} ${MAN3} ${MAN4} ${MAN5} ${MAN6} ${MAN7} ${MAN8}
.else
MANALL=
.endif
manpages: ${MANALL}

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

.if !target(all)
.MAIN: all
all: ${PROG} ${MANALL} _PROGSUBDIR
.endif

.if !target(clean)
clean: _PROGSUBDIR
	rm -f a.out [Ee]rrs mklog core.${PROG} ${PROG} ${OBJS} ${CLEANFILES}
.endif

.if !target(cleandir)
cleandir: _PROGSUBDIR
	rm -f a.out [Ee]rrs mklog core core.${PROG} ${PROG} ${OBJS} ${CLEANFILES}
	rm -f .depend ${DEPENDS} ${MANALL}
.endif

# some of the rules involve .h sources, so remove them from mkdep line
.if !target(depend)
depend: .depend _PROGSUBDIR
.depend: ${SRCS}
.if defined(PROG)
	mkdep ${MKDEP} ${CFLAGS:M-[UID]*} ${.ALLSRC:M*.c}
.endif
.endif

.if !target(install)
.if !target(beforeinstall)
beforeinstall:
.endif
.if !target(afterinstall)
afterinstall:
.endif

realinstall: _PROGSUBDIR
.if defined(PROG)
	${INSTALL} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
	    ${PROG} ${DESTDIR}${BINDIR}/${PROG}
.endif
.if defined(LINKS) && !empty(LINKS)
	@set ${LINKS}; \
	while test $$# -ge 2; do \
		l=${DESTDIR}$$1; \
		shift; \
		t=${DESTDIR}$$1; \
		shift; \
		echo $$t -\> $$l; \
		rm -f $$t; \
		ln $$l $$t; \
	done; true
.endif

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

.if !target(lint)
lint: ${SRCS} _PROGSUBDIR
.if defined(PROG)
	cd ${.CURDIR}; \
	${LINT} ${LINTFLAGS} ${CFLAGS:M-[UID]*} \
	${.ALLSRC:M*.c:M/*:S,${.CURDIR}/,,g}
.endif
.endif

#
# "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: _PROGSUBDIR
.else
obj: _PROGSUBDIR
	@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: _PROGSUBDIR
.else
objdir: _PROGSUBDIR
	@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:
.else
localobj:
	@-cd ${.CURDIR}; \
	[ ! -d obj ] && mkdir obj 2>/dev/null; \
	true
.endif
.endif

#
# Marc( && Paul)'s disgusting tagsfile hack follows.
#
# The reason for the extensive processing of the paths is that
# ${.ALLSRC} is gigantic due to the cross product of the number of
# sources times the length of ${.CURDIR} if we are using symbolic
# linked obj directories (that is, if we are not using a local tree),
# so the whole mess is just way too big for the (/bin/sh) argument
# vector.  So we move to ${.CURDIR} and strip it off the sources, then
# anything that is relative to the (old) current directory (doesn't
# have a slash in it) gets a duplicate with an obj/ prepended to it
# (which breaks if we aren't using "obj" directories), and the mess is
# fed to {c,e}tags.  Then we massage the ctags output to have the
# absolute pathname in the file reference so we can refer to this tags
# file from any source subdirectory below.
#
# If we are using local obj dirs (${.CURDIR} == "..") it becomes
# marginally easier.  However, we have turned this off in bmake since
# it causes entirely too many other problems.
#

#
# for emacs TAGS (etags) files.
#
# emacs has the notion of a single TAGS file that it is visiting;
# hence local TAGS files (links) need not exist in each directory.
#
.if !target(TAGS)
TAGS: ${HEADERS} ${SRCS} _PROGSUBDIR
.if defined(PROG)
	-cd ${.CURDIR}; \
	rm -f ${.TARGET}; \
	etags -t \
	${.ALLSRC:N/*:S,^,${MAKEOBJDIR}/&,g} ${.ALLSRC:M/*:S,${.CURDIR}/,,g}
.endif
.endif

#
# for vi tags (ctags) files.
#
# "vi -t" wants a tags file in the current directory, so in addition
# to the other stuff, we need to create (hard) links to the resulting
# tags file in every subdirectory.  Also, for linked tags files to
# work, the tags files MUST contain absolute pathnames.
#
# This is a hack, too - we should explicitly mark what directories are
# to have tags links.
#
.if !target(tags)
tags: _linktags _maketags

_maketags: ${HEADERS} ${SRCS} _PROGSUBDIR
.if defined(PROG)
	-cd ${.CURDIR}; \
	rm -f tags; \
	ctags -t \
	${.ALLSRC:N/*:S,^,${MAKEOBJDIR}/&,g} ${.ALLSRC:M/*:S,${.CURDIR}/,,g}; \
	mv tags tags.tmp; \
	TOPDIR=`pwd`/; \
	sed "s,	,	$$TOPDIR," < tags.tmp > tags; \
	rm -f tags.tmp
.endif

_linktags::
.if defined(PROG)
	-cd ${.CURDIR}; \
	TAGFILE=`pwd`/tags; \
	find * -name tags -exec rm -f {} \; ; \
	find * ! -name RCS -type d -exec ln -s $$TAGFILE {} \; ;
.endif
.endif

.if !defined(NOMAN)
.include <postgres.man.mk>
.else
maninstall:
.endif
