##############################################################################
# Commondefs
#
# This file contains definitions and rules that are independent of the
# system configuration.

# Assume that whoever is including us defined $(TOP) to be a relative path
# from that directory to the top of the build tree

# Top of the source tree
TOPSRC = $(srcdir)/$(TOP)

##############################################################################
# Common definitions for Makefiles
#

RM = rm -f
ECHO = echo
TOUCH = touch
DEPFILE = .dependencies

##############################################################################
# Variables passed down via makes

CUR = .

##############################################################################
# Cleaning

DIRT = *.o core *.bak $(DEPFILE) $(DEPFILE).bak .depdir/* *.i *.[ib]lg \
       doc.dvi doc.ps $(GENERATED) $(LDIRT)
LDIRT =

CLOBBER = Makefile $(LCLOBBER)
LCLOBBER =

##############################################################################
# The default rule checks for reconfiguration and then runs builds "all", then
# assimilates any local dependency information.

reconfig: Makefile $(TOP)/config/Makedefs
	@if test -r $(DEPFILE); then					      \
	        $(ECHO) Using dependencies in $(CUR)/$(DEPFILE);	      \
		$(MAKE) CUR=$(CUR) -f Makefile -f $(DEPFILE) subdirs all;     \
	else								      \
		$(MAKE) CUR=$(CUR) -f Makefile subdirs all;		      \
	fi
	@$(MAKE) localdepend

##############################################################################
# Rules

subdirs:
	@sub="$(SUBDIRS)";				\
	for d in $$sub; do if test -d $$d; then		\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done

cvs:
	@if [ "$$CVSCHECK" = "-n" -a "$$TOPCVS" -eq 1 ]; then \
	    echo Checking directories \(NOT updating\); fi
	@sub="$(SUBDIRS)"; \
	for d in $$sub; do if test -d $$d; then			\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d TOPCVS=0 \
		CVSCHECK=$(CVSCHECK) cvs);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) cvs updating in $(CUR)
	@cvs $(CVSCHECK) update -l $(srcdir)

cvscheck:
	@make cvs CVSCHECK=-n TOPCVS=1

cvs_commit:
	@sub="$(SUBDIRS)";					\
	for d in $$sub; do if test -d $$d; then			\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d cvs);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) cvs committing in $(CUR)
	@cvs commit -l $(srcdir)

clobber:
	@sub="$(SUBDIRS)";					\
	for d in $$sub; do if test -d $$d; then			\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d clobber);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) clobbering in $(CUR)
	if test -d .depdir; then $(RM) -r .depdir; fi
	$(RM) $(TARGETS) $(DIRT) $(CLOBBER)

clean:
	@sub="$(SUBDIRS)";				\
	for d in $$sub; do if test -d $$d; then		\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d clean);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) cleaning in $(CUR)
	$(RM) $(DIRT)

doc:
	grind -o doc.dvi -style ttquot -two-column -para $(DOCFILES)
	dvips doc.dvi -o doc.ps

preview:
	grind -o tmp.dvi -style ttquot -one-column -para $(DOCFILES)
	xdvi tmp.dvi
	$(RM) tmp.dvi

##############################################################################
# Dependency rules
#
# The model of dependency generation is that dependencies are computed
# right before the actual file is produced. A source file foo generates a
# dependency file .depdir/foo that contains its dependencies. Once a
# directory has been compiled, the .depdir/* files are accumulated to
# construct a new $(DEPFILE) file. Running "make depend" additionally tries
# to build the system without actually running the compiler, so that
# out-of-date dependencies can be updated.
#
# As with most automatic dependency systems, dependencies on generated files
# are tricky to handle. There are two approaches: either state these
# dependencies explicitly in the Makefile.in, or arrange that the generated
# files will always already be up-to-date.
#
# One action that does not work conveniently in the current dependency
# system is moving an include file. Since "make depend" only collects
# information from the previous build, it does not learn about the change.
# Also, builds will no longer work because they pull in dependencies that
# mention a non-existent file. Running a build that does not pull in the
# automatic dependencies won't work either, because some files that need
# to be rebuilt may depend on the moved file only indirectly, through an
# include file that is mentioned in the dependencies. The best thing to do
# in this case is do a clean build to ensure that dependencies are computed
# properly.

depend:
	@sub="$(SUBDIRS)";				                   \
	if test -n "$$sub"; then                                           \
	for d in $$sub; do if test -d $$d;				   \
	     then (cd $$d; $(MAKE) CUR=$(CUR)/$$d depend);                 \
	     else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done; fi
	@$(ECHO) depending in $(CUR)
	@DEPENDING=__force__ $(MAKE) CUR=$(CUR) all
	@$(MAKE) CUR=$(CUR) localdepend

localdepend:
	@deps=`echo .depdir/*`; if [ "x$$deps" != x'.depdir/*' ]; \
	    then rm -f .depdir/*.bak; cat .depdir/* |egrep -v '^#' >$(DEPFILE);\
	    else touch $(DEPFILE); fi


##############################################################################
# Rules for automatic reconfiguration

Makefile: $(srcdir)/Makefile.in $(TOP)/config.status
	$(TOPSRC)/bin/vorp Makefile

$(TOP)/config/config.h: $(TOPSRC)/config/config.h.in
	$(RM) $(TOP)/config/config.h
	cd $(TOP); $(MAKE) config.status
	cd $(TOP); config.status

$(TOP)/config/Makedefs: $(TOPSRC)/config/Makedefs.in $(TOP)/config.status
	cd $(TOP); config.status

$(TOP)/config.status: $(TOPSRC)/configure
	cd $(TOP); $(MAKE) config.status
