find-component.lisp 6.86 KB
Newer Older
1
2
3
;;;; -------------------------------------------------------------------------
;;;; Finding components

4
(uiop/package:define-package :asdf/find-component
5
  (:recycle :asdf/find-component :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/cache
7
   :asdf/component :asdf/system :asdf/find-system)
8
9
10
11
12
13
14
15
  (:export
   #:find-component
   #:resolve-dependency-name #:resolve-dependency-spec
   #:resolve-dependency-combination
   ;; Conditions
   #:missing-component #:missing-component-of-version #:retry
   #:missing-dependency #:missing-dependency-of-version
   #:missing-requires #:missing-parent
16
   #:missing-required-by #:missing-version))
17
18
19
20
(in-package :asdf/find-component)

;;;; Missing component conditions

21
22
23
(with-upgradability ()
  (define-condition missing-component-of-version (missing-component)
    ((version :initform nil :reader missing-version :initarg :version)))
24

25
26
  (define-condition missing-dependency (missing-component)
    ((required-by :initarg :required-by :reader missing-required-by)))
27

28
29
30
  (defmethod print-object ((c missing-dependency) s)
    (format s (compatfmt "~@<~A, required by ~A~@:>")
            (call-next-method c nil) (missing-required-by c)))
31

32
33
34
  (define-condition missing-dependency-of-version (missing-dependency
                                                   missing-component-of-version)
    ())
35

36
37
38
39
40
  (defmethod print-object ((c missing-component) s)
    (format s (compatfmt "~@<Component ~S not found~@[ in ~A~]~@:>")
            (missing-requires c)
            (when (missing-parent c)
              (coerce-name (missing-parent c)))))
41

42
43
44
45
46
47
  (defmethod print-object ((c missing-component-of-version) s)
    (format s (compatfmt "~@<Component ~S does not match version ~A~@[ in ~A~]~@:>")
            (missing-requires c)
            (missing-version c)
            (when (missing-parent c)
              (coerce-name (missing-parent c))))))
48
49
50
51


;;;; Finding components

52
(with-upgradability ()
53
  (defgeneric find-component (base path &key registered)
54
55
56
    (:documentation "Find a component by resolving the PATH starting from BASE parent.
If REGISTERED is true, only search currently registered systems."))
  (defgeneric resolve-dependency-combination (component combinator arguments)
57
58
    (:documentation "Return a component satisfying the dependency specification (COMBINATOR . ARGUMENTS)
in the context of COMPONENT"))
59

60
61
62
  ;; Methods for find-component

  ;; If the base component is a string, resolve it as a system, then if not nil follow the path.
63
64
65
66
67
  (defmethod find-component ((base string) path &key registered)
    (if-let ((s (if registered
                    (registered-system base)
                    (find-system base nil))))
      (find-component s path :registered registered)))
68

69
70
  ;; If the base component is a symbol, coerce it to a name if not nil, and resolve that.
  ;; If nil, use the path as base if not nil, or else return nil.
71
  (defmethod find-component ((base symbol) path &key registered)
72
    (cond
73
74
      (base (find-component (coerce-name base) path :registered registered))
      (path (find-component path nil :registered registered))
75
76
      (t    nil)))

77
  ;; If the base component is a cons cell, resolve its car, and add its cdr to the path.
78
79
  (defmethod find-component ((base cons) path &key registered)
    (find-component (car base) (cons (cdr base) path) :registered registered))
80

81
  ;; If the base component is a parent-component and the path a string, find the named child.
82
83
84
  (defmethod find-component ((parent parent-component) (name string) &key registered)
    (declare (ignorable registered))
    (compute-children-by-name parent :only-if-needed-p t)
85
86
    (values (gethash name (component-children-by-name parent))))

87
  ;; If the path is a symbol, coerce it to a name if non-nil, or else just return the base.
88
  (defmethod find-component (base (name symbol) &key registered)
89
    (if name
90
        (find-component base (coerce-name name) :registered registered)
91
92
        base))

93
  ;; If the path is a cons, first resolve its car as path, then its cdr.
94
95
96
  (defmethod find-component ((c component) (name cons) &key registered)
    (find-component (find-component c (car name) :registered registered)
                    (cdr name) :registered registered))
97

98
  ;; If the path is a component, return it, disregarding the base.
99
100
  (defmethod find-component ((base t) (actual component) &key registered)
    (declare (ignorable registered))
101
102
    actual)

103
104
  ;; Resolve dependency NAME in the context of a COMPONENT, with given optional VERSION constraint.
  ;; This (private) function is used below by RESOLVE-DEPENDENCY-SPEC and by the :VERSION spec.
105
106
107
108
109
110
111
  (defun resolve-dependency-name (component name &optional version)
    (loop
      (restart-case
          (return
            (let ((comp (find-component (component-parent component) name)))
              (unless comp
                (error 'missing-dependency
112
                       :required-by component
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
                       :requires name))
              (when version
                (unless (version-satisfies comp version)
                  (error 'missing-dependency-of-version
                         :required-by component
                         :version version
                         :requires name)))
              comp))
        (retry ()
          :report (lambda (s)
                    (format s (compatfmt "~@<Retry loading ~3i~_~A.~@:>") name))
          :test
          (lambda (c)
            (or (null c)
                (and (typep c 'missing-dependency)
                     (eq (missing-required-by c) component)
129
                     (equal (missing-requires c) name))))
130
131
          (unless (component-parent component)
            (let ((name (coerce-name name)))
132
              (unset-asdf-cache-entry `(find-system ,name))))))))
133

134
135
136
  ;; Resolve dependency specification DEP-SPEC in the context of COMPONENT.
  ;; This is notably used by MAP-DIRECT-DEPENDENCIES to process the results of COMPONENT-DEPENDS-ON
  ;; and by PARSE-DEFSYSTEM to process DEFSYSTEM-DEPENDS-ON.
137
138
139
140
141
142
  (defun resolve-dependency-spec (component dep-spec)
    (let ((component (find-component () component)))
      (if (atom dep-spec)
          (resolve-dependency-name component dep-spec)
          (resolve-dependency-combination component (car dep-spec) (cdr dep-spec)))))

143
  ;; Methods for RESOLVE-DEPENDENCY-COMBINATION to parse lists as dependency specifications.
144
  (defmethod resolve-dependency-combination (component combinator arguments)
145
146
    (parameter-error (compatfmt "~@<In ~S, bad dependency ~S for ~S~@:>")
                     'resolve-dependency-combination (cons combinator arguments) component))
147
148
149
150
151
152

  (defmethod resolve-dependency-combination (component (combinator (eql :feature)) arguments)
    (when (featurep (first arguments))
      (resolve-dependency-spec component (second arguments))))

  (defmethod resolve-dependency-combination (component (combinator (eql :version)) arguments)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
153
    (resolve-dependency-name component (first arguments) (second arguments)))) ;; See lp#527788
154