## -*- mode: Makefile -*-
##
## Copyright (c) 2000, 2001 University of Utah and the Flux Group.
## All rights reserved.
## 
## Permission to use, copy, modify, and distribute this file
## for any purpose with or without restriction is hereby granted.
##

# The `PROGRAM*' variables, which describe what we're building, are defined in
# the `GNUmakefile' that includes this file.

# Look for the Knit tools in the build tree and in the source tree.
# (In binary distributions, the programs are in the ``source'' tree.)
ifneq "$(strip $(wildcard $(TOP_OBJDIR)/bin/knit))" ""
  KNIT_BINDIR		= $(TOP_OBJDIR)/bin
else
ifneq "$(strip $(wildcard $(TOP_SRCDIR)/bin/knit))" ""
  KNIT_BINDIR		= $(TOP_SRCDIR)/bin
else
  # GNU Make 3.78 provides $(error ...), but the following works, too.
  error :=$(shell echo >&2 I cannot find the knit compiler.)
  error
endif
endif

KNIT			= $(KNIT_BINDIR)/knit
KNIT_FLAGS		=
KNIT_UNIT_PATH		= $(SRCDIR)
KNIT_TOOLS		= $(KNIT_BINDIR)

# Used in `knit_generated.mk'.
KNIT_QUIET_MAKE_INFORM	= @echo

# For Hugs-based Knit.  We set this because we invoke a copy of Knit that isn't
# (yet) installed.  If Knit is installed, you shouldn't need to set this.
HUGSFLAGS		= -P"$(KNIT_BINDIR):"
export HUGSFLAGS

###############################################################################

vpath % $(SRCDIR)

.PHONY: all
all: $(PROGRAM)

# Try to do the right thing when the user changes `PROGRAM_UNIT_NAME' without
# first doing a ``make veryclean''.
#
# If the Makefiles are tweaked, then force the `knit_generated.mk' file to be
# regenerated.  Further, remove all libraries, so as to avoid situations in
# which the $(KNIT_LIBS) appear to be up to date with respect to $(KNIT_OBJS)
# (the set of object files determined by Knit, according to the newly specified
# `PROGRAM_UNIT_NAME'), but in fact are not because the libraries were made
# from a different set of objects (i.e., the object files determined by a
# previous Knit run, for a different value of `PROGRAM_UNIT_NAME').
knit_generated.mk: GNUmakefile GNUmakerules

knit_generated.mk: $(PROGRAM_UNIT_FILE)
	$(RM) *.a
	$(KNIT) $(KNIT_FLAGS) \
	  UNIT_PATH=$(KNIT_UNIT_PATH) \
	  KNIT_TOOLS=$(KNIT_TOOLS) \
	  SRCDIR=$(SRCDIR) \
	  $(notdir $<) $(PROGRAM_UNIT_NAME)

include knit_generated.mk

###############################################################################

$(PROGRAM): knit_inits.o $(PROGRAM_EXTRA_OBJS) $(KNIT_LIBS)
	$(CC) -o $@ --begin-group $^ --end-group

knit_inits.o $(PROGRAM_EXTRA_OBJS): %.o: %.c
	$(CC) -c $< $(CFLAGS)

###############################################################################

.PHONY: clean
clean:
	$(RM) $(PROGRAM)
	$(RM) knit_inits.o $(PROGRAM_EXTRA_OBJS) $(KNIT_OBJS) $(KNIT_LIBS)
	$(RM) TMP
	find . -name '*.[do]' | xargs $(RM)
	find . -name '*xxx' | xargs $(RM)
	find . -name '*yyy' | xargs $(RM)

.PHONY: veryclean
veryclean: clean
	$(RM) knit_generated.mk
	$(RM) knit_inits.c
	$(RM) *_anon_*.c
	find . -name 'rename*' | xargs $(RM)

###############################################################################

## End of file.

