## -*- mode: Makefile -*-
##
## Copyright (c) 2000 University of Utah and the Flux Group.
## All rights reserved.
## 
## This file is part of the Knit component composition software.
## 
## Permission to use, copy, modify, distribute, and sell this software and
## its documentation is hereby granted without fee, provided that the above
## copyright notice and this permission/disclaimer notice is retained in all
## copies or modified versions, and that both notices appear in supporting
## documentation.  THE COPYRIGHT HOLDERS PROVIDE THIS SOFTWARE "AS IS" AND
## WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE.  THE COPYRIGHT HOLDERS DISCLAIM ANY LIABILITY OF ANY KIND FOR
## ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
## 
## Users are requested, but not required, to send to csl-dist@cs.utah.edu any
## improvements that they make and grant Univ. of Utah redistribution rights.
##

BUILD_DIR=$(TOP_OBJDIR)/bin

vpath % $(SRCDIR) $(SRCDIR)/..

# This goes first to make it the default
.PHONY: all
all		::

.PHONY: install
install		::

.PHONY: clean
clean		::

.PHONY: veryclean
veryclean	:: clean

Graph.so : graph.o

###############################################################################
# Parent
###############################################################################

## ``Recurse'' into our parent directory in order to build the files that don't
## depend on the Haskell system we use.  (Blech.)
##
all install clean veryclean::
	$(MAKE) -C .. $@

###############################################################################
# Configuration
###############################################################################

###############################################################################
# Knit and documentation generator
###############################################################################

BASE_HS_FILES = \
  ListUtils.hs \
  Parser.hs ParserUtils.hs \
  PrettyUtils.hs FiniteMap.lhs Pretty.lhs \
  Id.hs FastString.hs \
  AbstractUnits.hs ParseUnits.hs PPUnits.hs \
  AbstractC.hs SExpr.hs ParseC.hs PP1.hs \
  Sequence.hs MonadUtils.hs Monads.hs \
  ZEncode.hs

KNIT_HS_FILES = \
  $(BASE_HS_FILES) \
  StdDIS.hs Graph.hs Schedule.hs \
  TopSort.hs \
  MakeMakefile.hs EvalUnits.hs Constraints.hs \
  Flatten.hs CostC.hs DepC.hs EscC.hs RN.hs DepAsm.hs

HS_FILES = $(KNIT_HS_FILES) Main.hs GenBundleType.hs Show.hs ListTypes.hs

all     :: $(BUILD_DIR)/knit
all	:: $(BUILD_DIR)/knitdoc
all	:: $(BUILD_DIR)/mk_bundletype
all	:: $(BUILD_DIR)/knitGenBundles

$(BUILD_DIR)/knit:		knit \
				$(addprefix $(BUILD_DIR)/,\
					$(KNIT_HS_FILES) \
					Main.hs Graph.so StdDIS.so)
	$(CP) $< $@

$(BUILD_DIR)/knitdoc:		knitdoc \
				$(addprefix $(BUILD_DIR)/,\
					$(BASE_HS_FILES) \
					Show.hs)
	$(CP) $< $@

$(BUILD_DIR)/mk_bundletype:		mk_bundletype \
				$(addprefix $(BUILD_DIR)/,\
					$(BASE_HS_FILES) \
					Show.hs)
	$(CP) $< $@

$(BUILD_DIR)/knitGenBundles:	knitGenBundles \
				$(addprefix $(BUILD_DIR)/,\
					$(BASE_HS_FILES) \
					ListTypes.hs)
	$(CP) $< $@

$(BUILD_DIR)/%.hs: %.hs
	$(CP) $< $@
$(BUILD_DIR)/%.lhs: %.lhs
	$(CP) $< $@
$(BUILD_DIR)/%.so: %.so
	$(CP) $< $@

veryclean ::
	$(RM) $(addprefix $(BUILD_DIR)/,$(HS_FILES))
	$(RM) $(BUILD_DIR)/Graph.so
	$(RM) $(BUILD_DIR)/StdDIS.so
	$(RM) $(BUILD_DIR)/knit
	$(RM) $(BUILD_DIR)/knitdoc
	$(RM) $(BUILD_DIR)/mk_bundletype
	$(RM) $(BUILD_DIR)/knitGenBundles

###############################################################################
# GreenCard
###############################################################################

ifneq "$(GREENCARD_DIR)" ""

GREENCARD_FLAGS		= --target Hugs 
# GREENCARD_FLAGS	+= --include-dir $(GREENCARD_DIR)/lib/hugs

# Use GreenCard interpreted by Hugs.
GREENCARD    	= $(RUNHUGS) -c100 -h2m $(GREENCARD_DIR)/src/GreenCard.lhs

.SUFFIXES	: .gc .hs

%.c %.hs	: %.gc
		$(GREENCARD) $(GREENCARD_FLAGS) -i.. $< -opre $*

#clean		::
#		$(RM) $(GC_HS) $(GC_OBJS) 

endif

###############################################################################
# GreenCard suffix rules
###############################################################################

.SUFFIXES	: .c .o .so

DLL_FLAGS	= -shared 
PIC_FLAGS	= 

%.o		: %.c
	$(CC) -c -I. -I$(SRCDIR)/.. $(CPPFLAGS) $(DEFS) \
	      $(CFLAGS) $(PIC_FLAGS) $<
%.so		: %.o
	$(CC) -o $@ $(DLL_FLAGS) $(LDFLAGS) $^

###############################################################################
# End of Makefile
###############################################################################

