3

How do I go about finding the complete dependency tree for a given project in Common Lisp?

I've tried using (ql-dist:dependency-tree "my-project") which errors ((ql-dist:find-system "my-project") returns nil whether my system is loaded or not), and (slot-value (asdf/system:find-system "my-project") 'asdf/component:sideway-dependencies) seems to return only direct dependencies where I'm looking for the full tree (it also seems to return conditional/implementation-specific dependencies, such as sb-posix and sb-bsd-sockets, which I'd prefer to do without).

Is there a standard one-step way of doing this, or will I need to recursively walk the output of that sideway-dependencies slot and filter idiosyncratically?

1 Answer 1

1

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.