# OBJS - object files (possibly prefixed).
#
# PROG - name of final executable
-#
-#
+
+# We attempt to automatically devine the list of sources $(SRCS) to
+# compile by looking in the current directory. This is complicated by
+# the fact that a .hsc file gives rise to a .hs file (which needs to
+# be automatically included in $(SRCS)), but the .hs file might
+# already be present in the current directory and we don't want to
+# compile it twice.
+
+# So we figure out the sources in three stages: first figure out
+# what's in the current directory (this is $(PRE_SRCS)). Then figure
+# out all the "derived" sources (eg. A.hsc generates A.hs and
+# A_hsc.c), and finally put all these together and remove duplicates
+# (GNU make's handy sort function does the duplicate removing).
#
# BOOT_SRCS: list of machine generated Haskell modules.
# HS_SRCS: list of Haskell modules you want to compile.
# (caveat: assuming no funny use of -hisuf and that
# file name and module name match)
-SRCS=$(wildcard *.lhs *.hs *.c *.lc *.prl *.lprl *.lit *.verb)
+PRE_SRCS = $(wildcard *.lhs *.hs *.c *.prl *.lprl *.lit *.verb *.hsc)
+
+HSC_SRCS = $(filter %.hsc, $(PRE_SRCS))
+DERIVED_SRCS = $(patsubst %.hsc, %.hs, $(HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.c, $(HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.h, $(HSC_SRCS))
+
+# EXCLUDED_SRCS can be set in the Makefile, otherwise it defaults to empty.
+EXCLUDED_HSC_SRCS = $(filter %.hsc, $(EXCLUDED_SRCS))
+EXCLUDED_DERIVED_SRCS = $(patsubst %.hsc, %.hs, $(EXCLUDED_HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.h, $(EXCLUDED_HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.c, $(EXCLUDED_HSC_SRCS))
+
+CLOSED_EXCLUDED_SRCS = $(sort $(EXCLUDED_SRCS) $(EXCLUDED_DERIVED_SRCS))
+
+SRCS = $(filter-out $(CLOSED_EXCLUDED_SRCS), \
+ $(sort $(PRE_SRCS) $(DERIVED_SRCS)))
+
+HS_SRCS = $(filter %.lhs %.hs %.hc,$(sort $(SRCS) $(BOOT_SRCS)))
+HS_OBJS = $(addsuffix .$(way_)o,$(basename $(HS_SRCS)))
+HS_HCS = $(addsuffix .$(way_)hc,$(basename $(HS_SRCS)))
+HS_SS = $(addsuffix .$(way_)s,$(basename $(HS_SRCS)))
+HS_IFACES = $(addsuffix .$(way_)hi,$(basename $(HS_SRCS)))
-HS_SRCS=$(filter %.lhs %.hs %.hc,$(SRCS) $(BOOT_SRCS))
-HS_OBJS=$(addsuffix .$(way_)o,$(basename $(HS_SRCS)))
-HS_HCS=$(addsuffix .$(way_)hc,$(basename $(HS_SRCS)))
-HS_IFACES=$(addsuffix .$(way_)hi,$(basename $(HS_SRCS)))
+HSC_C_OBJS = $(patsubst %.hsc, %_hsc.o, $(filter %.hsc, $(SRCS)))
-C_SRCS=$(filter %.lc %.c,$(SRCS))
-C_OBJS=$(addsuffix .$(way_)o,$(basename $(C_SRCS)))
+C_SRCS = $(filter %.c,$(SRCS))
+C_OBJS = $(addsuffix .$(way_)o,$(basename $(C_SRCS)))
# SCRIPT_SRCS: list of raw script files (in literate form)
# SCRIPT_OBJS: de-litted scripts
# The default set of files for the dependency generators to work on
# is just their source equivalents.
#
+
+ifneq "$(BootingFromHc)" "YES"
MKDEPENDHS_SRCS=$(HS_SRCS)
-MKDEPENDC_SRCS=$(C_SRCS)
+else
+MKDEPENDHS_SRCS=
+endif
+
+MKDEPENDC_SRCS=$(C_SRCS)
#------------------------------------------------------------------
#
# that may require extra tools to create.
#
#
-MOSTLY_CLEAN_FILES += $(HS_OBJS) $(C_OBJS)
-CLEAN_FILES += $(HS_PROG) $(C_PROG) $(SCRIPT_PROG) $(SCRIPT_LINK) \
- $(PROG) $(LIBRARY) $(HS_IFACES) $(HS_HCS) \
- a.out
+MOSTLY_CLEAN_FILES += $(HS_OBJS) $(C_OBJS)
+CLEAN_FILES += $(HS_PROG) $(C_PROG) $(SCRIPT_PROG) $(SCRIPT_LINK) \
+ $(PROG) $(LIBRARY) $(HS_IFACES) $(HS_SS) a.out \
+ $(DERIVED_SRCS)
-MAINTAINER_CLEAN_FILES += .depend $(BOOT_SRCS)
+# Don't clean the .hc files if we're bootstrapping
+ifneq "$(BootingFromHc)" "YES"
+CLEAN_FILES += $(HS_HCS)
+endif
+
+DIST_CLEAN_FILES += .depend
+MAINTAINER_CLEAN_FILES += $(BOOT_SRCS)
#
# `Standard' set of files to clean out.
MOSTLY_CLEAN_FILES += \
*.CKP *.ln *.BAK *.bak .*.bak *.o *core a.out errs ,* *.a .emacs_* \
tags TAGS *.ind *.ilg *.idx *.idx-prev *.aux *.aux-prev *.dvi *.log \
- *.toc *.lot *.lof *.blg *.cb
+ *.toc *.lot *.lof *.blg *.cb *_stub.c *_stub.h *.raw_s
#------------------------------------------------------------------
#
#
+# Directory in which DLLs are dumped so as not to get picked up by running
+# programs (e.g. ghc or hsc) that run in the build tree
+
+DLL_PEN = $(FPTOOLS_TOP)/dll
+