+# NB. use := rather than = here, otherwise the wildcard will get re-computed
+# every time PRE_SRCS is expanded (this happens a lot).
+ALL_SRCS = $(wildcard $(patsubst ./%, %, \
+ $(patsubst %,%/*.hs, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.lhs, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.y, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.ly, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.c, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.hc, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.S, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.prl, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.lprl, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.lit, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.verb, . $(ALL_DIRS)) \
+ $(patsubst %,%/*.hsc, . $(ALL_DIRS)) \
+ ))
+
+# ALL_SRCS is computed once and for all into PRE_SRCS at the top of target.mk.
+
+PRE_HS_SRCS = $(filter %.hs, $(PRE_SRCS))
+PRE_LHS_SRCS = $(filter %.lhs, $(PRE_SRCS))
+
+HSC_SRCS = $(filter %.hsc, $(PRE_SRCS))
+HAPPY_SRCS = $(filter %.y, $(PRE_SRCS)) $(filter %.ly, $(PRE_SRCS))
+
+DERIVED_SRCS = $(patsubst %.hsc, %.hs, $(HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.c, $(HSC_SRCS)) \
+ $(patsubst %.hsc, %_hsc.h, $(HSC_SRCS)) \
+ $(patsubst %.hsc, %.hc, $(HSC_SRCS)) \
+ $(patsubst %.y, %.hs, $(HAPPY_SRCS)) \
+ $(patsubst %.ly, %.hs, $(HAPPY_SRCS)) \
+ $(patsubst %.hs, %.hc, $(PRE_HS_SRCS)) \
+ $(patsubst %.lhs, %.hc, $(PRE_LHS_SRCS))
+
+# EXCLUDED_SRCS can be set in the Makefile, otherwise it defaults to empty.
+EXCLUDED_HSC_SRCS = $(filter %.hsc, $(EXCLUDED_SRCS))
+EXCLUDED_HAPPY_SRCS = $(filter %.y, $(EXCLUDED_SRCS)) $(filter %.ly, $(EXCLUDED_SRCS))
+EXCLUDED_HS_SRCS = $(filter %.hs, $(EXCLUDED_SRCS))
+EXCLUDED_LHS_SRCS = $(filter %.lhs, $(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)) \
+ $(patsubst %.hsc, %.hc, $(EXCLUDED_HSC_SRCS)) \
+ $(patsubst %.y, %.hs, $(EXCLUDED_HAPPY_SRCS)) \
+ $(patsubst %.ly, %.hs, $(EXCLUDED_HAPPY_SRCS)) \
+ $(patsubst %.hs, %.hc, $(EXCLUDED_HS_SRCS)) \
+ $(patsubst %.lhs, %.hc, $(EXCLUDED_LHS_SRCS))
+# Exclude _hsc.c files; they get built as part of the cbits library,
+# not part of the main library
+
+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, $(sort $(SRCS) $(BOOT_SRCS)))