#  Compiler Toolkit: root makefile 
#
#  Author : Manuel M T Chakravarty
#  Created: 25 July 1998
#
#  Version $Revision: 1.18 $ from $Date: 2005/05/18 03:04:02 $
#
#  Copyright (c) [1998..2002] Manuel M T Chakravarty
#
#  This file is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This file is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  = DOCU =====================================================================
#
#  Essentially, instantiates the generic definitions from `common.mk' and 
#  includes the Makefiles of all parts, such that cross-part dependencies still
#  allow to use the correct flags when calling the compiler.
#  The dependency information for this package is read from `.depend' if 
#  existent. 
#

#  ***************************************
#  !!! This makefile requires GNU make !!!
#  ***************************************

# package name (must be before the include due to conditionals in `common.mk')
#
PACKAGE=base
PCKNAME=ctk
PCKVERSION=$(BASEVERSION)

include ../mk/common.mk

# compute object targets
#
# * don't include the files from sysdep/ as some are not used and those used 
#   will be demanded anyway
#
OBJS:=$(patsubst %.hs,%.o,\
        $(wildcard $(addsuffix /*.hs,$(filter-out sysdep,$(BASEPARTS)))))

# NB: nhc98 seems to have problems with the import of boundsMA into DynArrays.
#     So, exclude it for the meantime.
#
ifeq ($(SYS),nhc1)
  OBJS:=$(shell echo $(filter-out state/DynArrays.o,$(OBJS)))
endif

# directories containing interface files that might be used here
#
HIDIRS=$(BASEPARTS)

# Options for $(MKDEPENDHS)
#
# * MKDEPENDFILES are set here, because some of the .hs files in sysdep/ will
#   not be used at all
#
MKDEPENDOPTS   =
MKDEPENDFILES += $(addprefix sysdep/,SysDep.hs SysDepPosix.hs)

# build targets
#
.PHONY: default config objs all libctk test

default: all
config : base.build.conf
objs   : $(OBJS)
all    : objs libctk
test   : rectest

# the library achieve containing the whole CTK
#
# NB: Don't put the addsuffix expression to the to dependencies, as the 
#     wildcard won't expand to anything sensible in a clean tree.
#
libctk: libctk.a ctk.o
libctk.a: objs
	-$(RM) $@
	$(AR) -crs $@ $(addsuffix /*.o,$(PARTS))

ctk.o: libctk.a
	ld -r -x -o $@ $(WHOLE_ARCHIVE_FLAG) $<

# in-place ghc package configuration file
#
#base.build.conf: libctk.a
ifeq ("$(CABAL)","yes")
# GHC 6.4 uses the new Cabal package.conf format, so generate base.build.conf
# from base.build.conf.cabal
# Evil Hack: `ghc-pkg' barfs if `libctk.a' does not exist, but we must build 
#   the package conf *before* we build the library (constraint of the toplevel
#   Makefile).  So, we `touch libctk.a' to fake it... *sigh*
base.build.conf: base.build.conf.cabal
	touch libctk.a
	echo '[]' > $@
	ghc-pkg update -f $@ $<
	rm -f libctk.a
else
base.build.conf: base.build.conf.ghc-pre-6.4
	cp $< $@
endif

# auxilliary targets
#
.PHONY: depend clean cleanhi

depend : gendepend
clean  : recclean
	-$(RM) libctk.a base.build.conf
cleanhi: reccleanhi
