Here's a crack at the solution:
Take 3 (this could probably be a its own project at this stage):
(defgeneric ->key (thing))
(defmethod ->key ((thing string))
(intern (string-upcase thing) :keyword))
(defmethod ->key ((thing symbol))
(if (keywordp thing)
thing
(intern (symbol-name thing) :keyword)))
(defgeneric dependencies-of (system))
(defmethod dependencies-of ((system symbol))
(mapcar #'->key (slot-value (asdf/system:find-system system) 'asdf/component:sideway-dependencies)))
(defun ordered-dep-tree (dep-tree)
(let ((res))
(labels ((in-res? (dep-name) (member dep-name res))
(insert-pass (remaining)
(loop for (dep . sub-deps) in remaining
for unmet-sub-deps = (remove-if #'in-res? sub-deps)
if (null unmet-sub-deps) do (push dep res)
else collect (cons dep unmet-sub-deps) into next-rems
finally (return next-rems))))
(loop for (dep . callers) in dep-tree for deps-of = (dependencies-of dep)
if (null deps-of) do (push dep res)
else collect (cons dep deps-of) into non-zeros
finally (loop while non-zeros
do (setf non-zeros (insert-pass non-zeros)))))
(reverse res)))
(defgeneric dependency-tree (system))
(defmethod dependency-tree ((system symbol))
(let ((res (make-hash-table)))
(labels ((rec (sys)
(loop with deps = (dependencies-of sys)
for dep in deps for dep-k = (->key dep)
unless (gethash dep-k res) do (rec dep)
do (pushnew (->key sys) (gethash dep-k res)))))
(rec system))
(ordered-dep-tree (alexandria:hash-table-alist res))))
That still doesn't filter for sb-*
-style packages, but I figure I can do that in a separate pass. It seems to work though...
CL-USER> (dependency-tree :hunchentoot)
(:SB-BSD-SOCKETS :TRIVIAL-BACKTRACE :RFC2388 :SB-ROTATE-BYTE
:TRIVIAL-GARBAGE :TRIVIAL-FEATURES :CL-PPCRE :ALEXANDRIA :SB-POSIX
:CL-BASE64 :TRIVIAL-GRAY-STREAMS :USOCKET :MD5 :BABEL :FLEXI-STREAMS
:BORDEAUX-THREADS :CHUNGA :CFFI :CL-FAD :CL+SSL)
I think that's a list of all packages that need to be loaded before :hunchentoot
, presented in an order they can be loaded (no package appears before all of its dependencies appear). It doesn't handle circular dependencies, but I don't think asdf does either, so...