-# Every directory with .c files is included.
-DIRS=$(filter-out $(EXCLUDE), $(patsubst %/, %, $(sort $(dir $(wildcard ccan/*/*.c)))))
+# Anything with C files needs building; dir leaves / on, sort uniquifies
+DIRS=$(patsubst %/, %, $(sort $(foreach m, $(filter-out $(MODS_EXCLUDE), $(MODS_WITH_SRC)), $(dir $(wildcard ccan/$m/*.c)))))
+
+# Generate everyone's separate Makefiles.
+-include $(foreach dir, $(DIRS), $(dir)-Makefile)
+
+ccan/%-Makefile:
+ @echo $@: $(wildcard ccan/$*/*.[ch]) ccan/$*/_info > $@
+ @echo ccan/$*.o: $(patsubst %.c, %.o, $(wildcard ccan/$*/*.c)) >> $@