-$1/$2/build/%.$$($3_way_)o : $1/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
- "$$($1_$2_HC)" $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
-
-$1/$2/build/%.$$($3_way_)o : $1/$2/build/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
- "$$($1_$2_HC)" $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
-
-$1/$2/build/%.$$($3_way_)hc : $1/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
- "$$($1_$2_HC)" $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
-
-$1/$2/build/%.$$($3_way_)hc : $1/$2/build/%.cmm $$(rts_H_FILES) $$($1_$2_HC)
- "$$($1_$2_HC)" $$($1_$2_$3_MOST_HC_OPTS) -c $$< -o $$@
+# ----------------------------------------------------------------------------
+# On Windows, as the RTS and base libraries have recursive imports,
+# we have to break the loop with "import libraries".
+# These are made from rts/win32/libHS*.def which contain lists of
+# all the symbols in those libraries used by the RTS.
+#
+ifneq "$$(findstring dyn, $1)" ""
+ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
+
+ALL_RTS_DEF_LIBNAMES = base ghc-prim
+ALL_RTS_DEF_LIBS = \
+ rts/dist/build/win32/libHSbase.dll.a \
+ rts/dist/build/win32/libHSghc-prim.dll.a \
+ rts/dist/build/win32/libHSffi.dll.a
+
+# -- import libs for the regular Haskell libraries
+define make-importlib-def # args $1 = lib name
+rts/dist/build/win32/libHS$1.def : rts/win32/libHS$1.def
+ cat rts/win32/libHS$1.def \
+ | sed "s/@LibVersion@/$$(libraries/$1_dist-install_VERSION)/" \
+ | sed "s/@ProjectVersion@/$(ProjectVersion)/" \
+ > rts/dist/build/win32/libHS$1.def
+
+rts/dist/build/win32/libHS$1.dll.a : rts/dist/build/win32/libHS$1.def
+ "$$(DLLTOOL)" -d rts/dist/build/win32/libHS$1.def \
+ -l rts/dist/build/win32/libHS$1.dll.a
+endef
+$(foreach lib,$(ALL_RTS_DEF_LIBNAMES),$(eval $(call make-importlib-def,$(lib))))