-#USUAL_DIR = .
-#USUAL_MODULES = fileutil logging lookup3 bitswap misc
-#USUAL_OBJDIR
-
-USUAL_HDRS = $(addprefix $(USUAL_DIR)/usual/, $(addsuffix .h, $(USUAL_MODULES)))
+# toplevel dir of libusual - always required
+#req: USUAL_DIR = .
USUAL_CPPFLAGS = -I$(USUAL_DIR)
+#
# target: local objs
-USUAL_OBJDIR ?= .
-USUAL_SRCS = $(wildcard $(addprefix $(USUAL_DIR)/usual/, $(addsuffix .c, $(USUAL_MODULES))))
-USUAL_OBJS = $(addprefix $(USUAL_OBJDIR)/, $(notdir $(USUAL_SRCS:.c=.o)))
+#
+
+# req: USUAL_LOCAL_SRCS or USUAL_MODULES
+# opt: USUAL_OBJDIR
+
# needs following rule:
#$(USUAL_OBJDIR)/%.o: $(USUAL_DIR)/usual/%.c $(USUAL_HDRS)
# $(CC) -c -o $@ $< $(DEFS) $(CPPFLAGS) $(CFLAGS)
+USUAL_OBJDIR ?= .
+USUAL_HDRS = $(addprefix $(USUAL_DIR)/usual/, $(addsuffix .h, $(USUAL_MODULES)))
+USUAL_SRCS = $(wildcard $(addprefix $(USUAL_DIR)/usual/, $(addsuffix .c, $(USUAL_MODULES))))
+USUAL_OBJS = $(addprefix $(USUAL_OBJDIR)/, $(notdir $(USUAL_SRCS:.c=.o)))
+
+# find_modules is slow, so do immediate assignment
+#USUAL_MODULES ?= $(shell $(USUAL_DIR)/find_modules.sh $(USUAL_DIR) $(USUAL_LOCAL_SRCS))
+ifeq ($(origin USUAL_MODULES),undefined)
+USUAL_MODULES := $(shell $(USUAL_DIR)/find_modules.sh $(USUAL_DIR) $(wildcard $(USUAL_LOCAL_SRCS)))
+endif
+
+#
# target: libusual.a
+#
+
USUAL_LIBS = -lusual
USUAL_LDFLAGS = -L$(USUAL_DIR)
--- /dev/null
+#! /bin/sh
+
+top="$1"
+shift
+
+set -e
+
+# sanity check
+test -n "$top" || {
+ echo "usage: $0 USUAL_DIR SRC ..." >&2
+ exit 1
+}
+test -f "$top/usual/base.h" || {
+ echo "usage: $0 USUAL_DIR SRC ..." >&2
+ exit 1
+}
+test -n "$1" || exit 0
+
+# return uniq module names, exclude already found ones
+grep_usual() {
+ excl="nonex"
+ for m in $m_done; do
+ excl="$excl\\|$m"
+ done
+ sed -n \
+ -e "/^#include[ \t]*[<\"]usual[/]\\($excl\\)[.]h/d" \
+ -e '/^#include[ \t]*[<"]usual[/]/ s,.*[<"]usual/\(.*\)[.]h[>"].*,\1,p' "$@" \
+ | sort -u
+}
+
+# return module filename globs
+make_pats() {
+ for m in "$@"; do
+ echo "$top/usual/$m.[ch]"
+ done
+}
+
+# loop over grep until all mods are found
+m_done=""
+m_tocheck=$(grep_usual "$@")
+while test -n "$m_tocheck"; do
+ m_done="$m_done $m_tocheck"
+ m_tocheck=$(grep_usual $(make_pats $m_tocheck))
+done
+
+# done
+echo $m_done
+