-# 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
+MODS_WITH_SRC = $(patsubst ccan/%/, %, $(sort $(foreach m, $(MODS), $(dir $(wildcard ccan/$m/*.c)))))
+
+default: libccan.a
+
+# Automatic dependency generation: makes ccan/*/*.d files.
+DEPGEN=-MMD
+-include $(foreach m, $(MODS), ccan/$(m)/*.d)
+
+DIRS=$(patsubst %, ccan/%, $(filter-out $(MODS_EXCLUDE), $(MODS_WITH_SRC)))
+
+# 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)) >> $@