#
# Copyright (c) 2000,2003 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston MA 02111-1307, USA.
# 
# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
# Mountain View, CA  94043, or:
# 
# http://www.sgi.com 
# 
# For further information regarding this notice, see: 
# 
# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
# Common build rules for gmake
#
ifndef _BUILDRULES_INCLUDED_
_BUILDRULES_INCLUDED_ = 1

include $(TOPDIR)/src/include/builddefs

#
# Standard targets
#
ifdef CMDTARGET
$(CMDTARGET) : $(SUBDIRS) $(OBJECTS)
	$(CCF) -o $(CMDTARGET) $(LDFLAGS) $(OBJECTS) $(LDLIBS)
endif

ifdef LIBTARGET
ifeq ($(TARGET_OS), irix)
_SHAREDOPTS	= -Wl,-soname,$(LIBTARGET)
endif
ifeq ($(TARGET_OS), linux)
_SHAREDOPTS	= -Wl,-soname,$(LIBTARGET)
endif
ifeq ($(TARGET_OS), solaris)
_SHAREDOPTS	= -fpic
endif
$(LIBTARGET) : $(SUBDIRS) $(OBJECTS)
	$(CC) $(LDFLAGS) -shared $(_SHAREDOPTS) -o $(LIBTARGET) \
		$(OBJECTS) $(LDLIBS) $(LIB_FOR_DLOPEN) $(LIB_FOR_BASENAME)
endif

ifdef STATICLIBTARGET
$(STATICLIBTARGET) : $(SUBDIRS) $(OBJECTS)
	$(AR) cr $(STATICLIBTARGET) $?
endif

# Suffix rule to support transition for YFILES to OBJECTS
%.tab.h : %.y
	$(YACC) -d -b `basename $< .y` $<

%.tab.c : %.y
	$(YACC) -d -b `basename $< .y` $<

#
# Rule for C++ files
#
%.o: %.cxx
	$(CXX) $(CXXFLAGS) -c $<

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $<

%.o: %.c++
	$(CXX) $(CXXFLAGS) -c $<

clean clobber :: $(SUBDIRS) $(PRO_SUBDIRS) $(SNIA_SUBDIRS)
	rm -rf $(DIRT)
	$(SUBDIRS_MAKERULE)

clean-lbs realclean: clean
	rm -f $(TOPDIR)/src/include/builddefs $(TOPDIR)/src/include/pcp.conf

# Rules for post-processing README, help and other files with pathnames in
# them. We attempt to keep line length under control but it's not always
# possible, so instead of potential screw-ups, we just punt on any indented
# line.
%.post: %
	sed -e 's;/var/pcp/pmdas;$(PCP_PMDAS_DIR);g' \
	    -e 's;/var/adm/pcplog;$(PCP_LOG_DIR)/pmcd;g' \
	    -e 's;/etc/pmcd.conf;$(PCP_PMCDCONF_PATH);g' \
	    -e 's;/etc/init.d/pcp;$(PCP_RC_DIR)/pcp;g' $< | \
	$(PCP_AWK_PROG) -v eatlines=0 '\
	eatlines	{ \
			    if ( (length == 0) || match ($$0, "^[ \t#@]") ) { \
				print longone | "fmt"; close ("fmt"); \
				longone = ""; eatlines = 0; \
				print;  \
			    } else { \
				longone=sprintf ("%s %s", longone, $$0); \
			    } \
			    next; \
			} \
	length > 75	{ \
			    if ( match ($$0, "^[ \t@#]") ) { \
				print; \
			    } else { \
				longone = $$0; eatlines = 1; \
			    } \
			    next; \
			} \
			{ print }' > $@

#
# Never blow away subdirs
#
ifdef SUBDIRS
.PRECIOUS: $(SUBDIRS)
endif

src-pcp : $(SRCFILES) $(SUBDIRS)
	@test ! -z "$$SRC_ROOT" || ( echo '$$SRC_ROOT not set ... bozo!' ; echo "... generally unsafe to run make src-pcp outside the Makepkgs script"; exit 1 )
	@test -z "$$DIR" && DIR="."; \
	for f in `echo $^`; do \
	    if test -d $$f ; then \
		$(MAKEF) -j 1 DIR=$$DIR/$$f -C $$f $@ || exit $$?; \
	    else \
		if [ -x $(TOPDIR)/tools/mangle-src ] ; then \
		    $(TOPDIR)/tools/mangle-src $(TOPDIR)/$$SRC_ROOT $$DIR/$$f \
			|| exit $$?; \
		fi; \
		$(ECHO) $$DIR/$$f; \
	    fi; \
	done

endif

$(_FORCE):

# The depend target does not depend on any other targets (even though it
# actually depends on CFILES and HFILES). This is because there are several
# places where we generate header files (e.g. domain.h is generated for each
# subdir below src/pmdas, and domain.h in turn depends on src/pmns/stdpmid,
# which itself a generated file ...). As a result, you can't run make
# depend after make clobber, because the generated files will be missing.
# 
# So running makedepend is for development use when you change a header
# somewhere and you need to be sure everything that depends on that header
# will be remade properly.

.PHONY : depend $(_FORCE)

depend : $(SUBDIRS)
	$(SUBDIRS_MAKERULE)
	touch dep
	$(MAKEDEPEND) -fdep -- $(CFLAGS) -- $(CFILES)

#
# include dep, but only if it exists
-include dep
