+DERIVED_SRCS = $(DERIVED_GC_SRCS) \
+ $(DERIVED_HSC_SRCS) \
+ $(DERIVED_HAPPY_SRCS) \
+ $(DERIVED_ALEX_SRCS) \
+ $(DERIVED_HC_SRCS)
+
+# EXCLUDED_SRCS can be set in the Makefile, otherwise it defaults to empty.
+EXCLUDED_GC_SRCS = $(filter %.gc, $(EXCLUDED_SRCS))
+EXCLUDED_HSC_SRCS = $(filter %.hsc, $(EXCLUDED_SRCS))
+EXCLUDED_HAPPY_Y_SRCS = $(filter %.y, $(EXCLUDED_SRCS))
+EXCLUDED_HAPPY_LY_SRCS = $(filter %.ly, $(EXCLUDED_SRCS))
+EXCLUDED_HAPPY_SRCS = $(EXCLUDED_HAPPY_Y_SRCS) $(EXCLUDED_HAPPY_LY_SRCS)
+EXCLUDED_ALEX_SRCS = $(filter %.x, $(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 %.gc, %_stub_ffi.c, $(EXCLUDED_GC_SRCS)) \
+ $(patsubst %.gc, %_stub_ffi.h, $(EXCLUDED_GC_SRCS)) \
+ $(patsubst %.y, %.hs, $(EXCLUDED_HAPPY_Y_SRCS)) \
+ $(patsubst %.ly, %.hs, $(EXCLUDED_HAPPY_LY_SRCS)) \
+ $(patsubst %.x, %.hs, $(EXCLUDED_ALEX_SRCS)) \
+ $(patsubst %.hs, %.hc, $(EXCLUDED_HS_SRCS)) \
+ $(patsubst %.lhs, %.hc, $(EXCLUDED_LHS_SRCS)) \
+ $(patsubst %.hs, %_stub.c, $(EXCLUDED_HS_SRCS)) \
+ $(patsubst %.lhs, %_stub.c, $(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)))
+HS_OBJS = $(addsuffix .$(way_)o,$(basename $(HS_SRCS)))
+HS_IFACES = $(addsuffix .$(way_)hi,$(basename $(HS_SRCS)))
+
+HI_BOOTS = $(patsubst %.hs-boot, %.$(way_)hi-boot, \
+ $(patsubst %.lhs-boot, %.$(way_)hi-boot, $(PRE_HS_BOOT_SRCS)))
+
+O_BOOTS = $(patsubst %.hs-boot, %.$(way_)o-boot, \
+ $(patsubst %.lhs-boot, %.$(way_)o-boot, $(PRE_HS_BOOT_SRCS)))
+
+GC_C_OBJS = $(addsuffix _stub_ffi.$(way_)o,$(basename $(filter %.gc,$(SRCS))))
+HSC_C_OBJS = $(addsuffix _hsc.$(way_)o,$(basename $(filter %.hsc,$(SRCS))))
+
+ifeq "$(BootingFromHc)" "NO"
+# We don't want to build the _stub.c files ourselves, unless we're
+# bootstrapping from .hc files.
+EXCLUDED_C_SRCS = $(patsubst %.lhs, %_stub.c, $(HS_SRCS)) \
+ $(patsubst %.hs, %_stub.c, $(HS_SRCS)) \
+ $(patsubst %.gc, %_stub_ffi.c, $(GC_SRCS)) \
+ $(patsubst %.gc, %_stub_ffi.h, $(GC_SRCS))
+endif
+
+# These are droppings from hsc2hs - ignore them if we see them.
+EXCLUDED_C_SRCS += $(patsubst %.hsc, %_hsc_make.c, $(HSC_SRCS))
+
+C_SRCS = $(filter-out $(EXCLUDED_C_SRCS),$(filter %.c %.S,$(SRCS)))
+C_OBJS = $(addsuffix .$(way_)o,$(basename $(C_SRCS)))