## -*- 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
###############################################################################

# Glasgow Haskell compiler: available from http://haskell.org/ghc/

EXTRA_HC_OPTS	+= -fasm-x86
HC_OPTS		+= -i$(SRCDIR) -i$(SRCDIR)/..
HC_OPTS		+= -fglasgow-exts 
HC_OPTS		+= -syslib data
HC_OPTS		+= -syslib text

# Profiling flags
#HC_OPTS		+= -prof
#HC_OPTS		+= -auto-all
#HC_OPTS		+= -auto

# Optimization flags (make compilation big and slow)
#HC_OPTS		+= -O2 -H90M

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

BASE_HS_FILES = \
  ListUtils.hs \
  Parser.hs ParserUtils.hs \
  PrettyUtils.hs \
  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) GenBundleType.hs Show.hs ListTypes.hs

BASE_HS_OBJS = $(addsuffix .o,  $(basename $(BASE_HS_FILES)))
KNIT_HS_OBJS = $(addsuffix .o,  $(basename $(KNIT_HS_FILES)))

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

$(BUILD_DIR)/knit: $(KNIT_HS_OBJS) Main.o graph.o 
	$(HC) $^ $(HC_OPTS) -o $@

$(BUILD_DIR)/knitdoc: $(BASE_HS_OBJS) Show.o
	$(HC) $^ $(HC_OPTS) -o $@

$(BUILD_DIR)/mk_bundletype: $(BASE_HS_OBJS) GenBundleType.o
	$(HC) $^ $(HC_OPTS) -o $@

$(BUILD_DIR)/knitGenBundles: $(BASE_HS_OBJS) ListTypes.o
	$(HC) $^ $(HC_OPTS) -o $@

veryclean ::
	$(RM) $(BUILD_DIR)/knit
	$(RM) $(BUILD_DIR)/knitdoc
	$(RM) $(BUILD_DIR)/mk_bundletype
	$(RM) $(BUILD_DIR)/knitGenBundles

clean ::
	$(RM) $(KNIT_HS_OBJS)
	$(RM) $(BASE_HS_OBJS)

# Override default use of -fasm-x86
# With ghc-4.08, we have to explicitly say -fvia-C
Graph.o:	Graph.hs
	$(HC) $(HC_OPTS) -I$(SRCDIR) -I$(SRCDIR)/.. -fvia-C \
	      -c $< -o $@
StdDIS.o:	StdDIS.hs
	$(HC) $(HC_OPTS) -I$(SRCDIR) -I$(SRCDIR)/.. -fvia-C \
	      -c $< -o $@

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

GREENCARD_FLAGS  	= --target GHC
# GREENCARD_FLAGS  	= --target ffi

.SUFFIXES	: .gc .hs

%.hs : %.gc
	$(GREENCARD) $(GREENCARD_FLAGS) -i$(SRCDIR)/.. $< -o $@

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

###############################################################################
# Haskell suffix rules
###############################################################################

.SUFFIXES	: .lhs .hs .o

%.o	        : %.hs
		$(HC) $(HC_OPTS) $(EXTRA_HC_OPTS) \
		      -c $< -o $@ -ohi $(addsuffix .hi,$(basename $@))
%.o	        : %.lhs
		$(HC) $(HC_OPTS) $(EXTRA_HC_OPTS) \
		      -c $< -o $@ -ohi $(addsuffix .hi,$(basename $@))
%.hi		: %.o
		@:

###############################################################################
# Dependencies
###############################################################################

depends.mk	: $(HS_FILES)
	-$(HC) -M -optdep-fdepends.mk \
	       -optdep--exclude-module=FiniteMap \
	       -optdep--exclude-module=Pretty \
	       $(HC_OPTS) $^
	-mv depends.mk depends.mk.raw
	-sed -e $(subst DIR,$(SRCDIR),'s@DIR/../@@g') \
	    -e $(subst DIR,$(SRCDIR),'s@DIR/@@g') \
	    < depends.mk.raw > depends.mk
	$(RM) depends.mk.raw

-include depends.mk

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