find_modules: automatic detection of used modules.
authorMarko Kreen <markokr@gmail.com>
Wed, 2 Sep 2009 09:14:55 +0000 (12:14 +0300)
committerMarko Kreen <markokr@gmail.com>
Wed, 2 Sep 2009 09:14:55 +0000 (12:14 +0300)
It's a shell script to parse out used libusual modules
from user source files.  So instead:

  USUAL_DIR =
  USUAL_MODULES =

user can do

  USUAL_DIR =
  USUAL_LOCAL_SRCS =

where USUAL_LOCAL_SRCS contains source files, both .c and .h,
that include libusual files.  Then Setup.mk will launch find_modules
to get actually used list of modules.

Setup.mk
find_modules.sh [new file with mode: 0755]

index 860ee9fb34a8036aa328c504011633449a46f62d..cb7bbd47adf9c9bd5b38f72e511ccaecc74be9a5 100644 (file)
--- a/Setup.mk
+++ b/Setup.mk
@@ -1,20 +1,34 @@
 
-#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)
 
diff --git a/find_modules.sh b/find_modules.sh
new file mode 100755 (executable)
index 0000000..7a08755
--- /dev/null
@@ -0,0 +1,48 @@
+#! /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
+