action.lisp 13 KB
Newer Older
1 2 3
;;;; -------------------------------------------------------------------------
;;;; Actions

4
(asdf/package:define-package :asdf/action
5
  (:nicknames :asdf-action)
6
  (:recycle :asdf/action :asdf)
7
  (:use :asdf/common-lisp :asdf/driver :asdf/upgrade
8
   :asdf/component :asdf/system :asdf/find-system :asdf/find-component :asdf/operation)
9
  (:intern #:stamp #:done-p)
10
  (:export
11
   #:action #:define-convenience-action-methods
12
   #:explain #:action-description
13
   #:downward-operation #:upward-operation #:sibling-operation
14 15 16 17
   #:component-depends-on #:component-self-dependencies
   #:input-files #:output-files #:output-file #:operation-done-p
   #:action-status #:action-stamp #:action-done-p
   #:component-operation-time #:mark-operation-done #:compute-action-stamp
18
   #:perform #:perform-with-restarts #:retry #:accept #:feature
19
   #:traverse-actions #:traverse-sub-actions #:required-components ;; in plan
20
   #:action-path #:find-action
21
   ))
22 23 24
(in-package :asdf/action)

(deftype action () '(cons operation component)) ;; a step to be performed while building the system
25

26 27 28
(defgeneric* traverse-actions (actions &key &allow-other-keys))
(defgeneric* traverse-sub-actions (operation component &key &allow-other-keys))
(defgeneric* required-components (component &key &allow-other-keys))
29

30 31 32 33 34 35
;;;; Reified representation for storage or debugging. Note: dropping original-initags
(defun action-path (action)
  (destructuring-bind (o . c) action (cons (type-of o) (component-find-path c))))
(defun find-action (path)
  (destructuring-bind (o . c) path (cons (make-operation o) (find-component () c))))

36 37 38

;;;; Convenience methods
(defmacro define-convenience-action-methods
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  (function (operation component &optional keyp)
   &key if-no-operation if-no-component operation-initargs)
  (let* ((rest (gensym "REST"))
         (found (gensym "FOUND"))
         (more-args (when keyp `(&rest ,rest &key &allow-other-keys))))
    (flet ((next-method (o c)
             (if keyp
                 `(apply ',function ,o ,c ,rest)
                 `(,function ,o ,c))))
      `(progn
         (defmethod ,function ((,operation symbol) ,component ,@more-args)
           (if ,operation
               ,(next-method
                 (if operation-initargs ;backward-compatibility with ASDF1's operate. Yuck.
                     `(apply 'make-operation ,operation :original-initargs ,rest ,rest)
                     `(make-operation ,operation))
                 `(find-component () ,component))
               ,if-no-operation))
         (defmethod ,function ((,operation operation) ,component ,@more-args)
           (if (typep ,component 'component)
59 60
               (error "No defined method for ~S on ~/asdf-action:format-action/"
                      ',function (cons ,operation ,component))
61 62 63 64
               (let ((,found (find-component () ,component)))
                 (if ,found
                     ,(next-method operation found)
                     ,if-no-component))))))))
65

66 67 68

;;;; self-description

69
(defgeneric* action-description (operation component)
70 71 72
  (:documentation "returns a phrase that describes performing this operation
on this component, e.g. \"loading /a/b/c\".
You can put together sentences using this phrase."))
73
(defmethod action-description (operation component)
74
  (format nil (compatfmt "~@<~A on ~A~@:>")
75
          (type-of operation) component))
76
(defgeneric* (explain) (operation component))
77
(defmethod explain ((o operation) (c component))
78
  (asdf-message (compatfmt "~&~@<; ~@;~A~:>~%") (action-description o c)))
79
(define-convenience-action-methods explain (operation component))
80

81 82 83
(defun* format-action (stream action &optional colon-p at-sign-p)
  (assert (null colon-p)) (assert (null at-sign-p))
  (destructuring-bind (operation . component) action
84
    (princ (action-description operation component) stream)))
85 86 87 88


;;;; Dependencies

89
(defgeneric* component-depends-on (operation component) ;; ASDF4: rename to component-dependencies
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  (:documentation
   "Returns a list of dependencies needed by the component to perform
    the operation.  A dependency has one of the following forms:

      (<operation> <component>*), where <operation> is a class
        designator and each <component> is a component
        designator, which means that the component depends on
        <operation> having been performed on each <component>; or

      (FEATURE <feature>), which means that the component depends
        on <feature>'s presence in *FEATURES*.

    Methods specialized on subclasses of existing component types
    should usually append the results of CALL-NEXT-METHOD to the
    list."))
(defgeneric* component-self-dependencies (operation component))
106 107
(define-convenience-action-methods component-depends-on (operation component))
(define-convenience-action-methods component-self-dependencies (operation component))
108 109 110 111 112

(defmethod component-depends-on ((o operation) (c component))
  (cdr (assoc (type-of o) (component-in-order-to c)))) ; User-specified in-order dependencies

(defmethod component-self-dependencies ((o operation) (c component))
113
  ;; NB: result in the same format as component-depends-on
114 115 116 117
  (loop* :for (o-spec . c-spec) :in (component-depends-on o c)
         :unless (eq o-spec 'feature) ;; avoid the FEATURE "feature"
           :when (find c c-spec :key #'(lambda (dep) (resolve-dependency-spec c dep)))
             :collect (list o-spec c)))
118 119 120 121 122

;;;; upward-operation, downward-operation
;; These together handle actions that propagate along the component hierarchy.
;; Downward operations like load-op or compile-op propagate down the hierarchy:
;; operation on a parent depends-on operation on its children.
123 124 125 126 127 128
;; By default, an operation propagates itself, but it may propagate another one instead.
(defclass downward-operation (operation)
  ((downward-operation
    :initform nil :initarg :downward-operation :reader downward-operation)))
(defmethod component-depends-on ((o downward-operation) (c parent-component))
  `((,(or (downward-operation o) o) ,@(component-children c)) ,@(call-next-method)))
129 130
;; Upward operations like prepare-op propagate up the component hierarchy:
;; operation on a child depends-on operation on its parent.
131 132 133 134
;; By default, an operation propagates itself, but it may propagate another one instead.
(defclass upward-operation (operation)
  ((upward-operation
    :initform nil :initarg :downward-operation :reader upward-operation)))
135
;; For backward-compatibility reasons, a system inherits from module and is a child-component
136
;; so we must guard against this case. ASDF4: remove that.
137
(defmethod component-depends-on ((o upward-operation) (c child-component))
138 139 140 141 142 143 144 145 146 147 148 149 150
  `(,@(if-let (p (component-parent c))
        `((,(or (upward-operation o) o) ,p))) ,@(call-next-method)))
;; Sibling operations propagate to siblings in the component hierarchy:
;; operation on a child depends-on operation on its parent.
;; By default, an operation propagates itself, but it may propagate another one instead.
(defclass sibling-operation (operation)
  ((sibling-operation
    :initform nil :initarg :sibling-operation :reader sibling-operation)))
(defmethod component-depends-on ((o sibling-operation) (c component))
  `((,(or (sibling-operation o) o)
     ,@(loop :for dep :in (component-sibling-dependencies c)
             :collect (resolve-dependency-spec c dep)))
    ,@(call-next-method)))
151 152 153


;;;; Inputs, Outputs, and invisible dependencies
154 155
(defgeneric* (output-files) (operation component))
(defgeneric* (input-files) (operation component))
156
(defgeneric* (operation-done-p) (operation component)
157
  (:documentation "Returns a boolean, which is NIL if the action is forced to be performed again"))
158 159 160
(define-convenience-action-methods output-files (operation component))
(define-convenience-action-methods input-files (operation component))
(define-convenience-action-methods operation-done-p (operation component))
161 162 163 164 165

(defmethod operation-done-p ((o operation) (c component))
  (declare (ignorable o c))
  t)

166 167 168 169
(defmethod output-files :around (operation component)
  "Translate output files, unless asked not to"
  operation component ;; hush genera, not convinced by declare ignorable(!)
  (values
170 171
   (multiple-value-bind (pathnames fixedp) (call-next-method)
     ;; 1- Make sure we have absolute pathnames
172 173
     (let* ((directory (pathname-directory-pathname
                        (component-pathname (find-component () component))))
174
            (absolute-pathnames
175 176 177
              (loop
                :for pathname :in pathnames
                :collect (ensure-pathname-absolute pathname directory))))
178 179 180 181
       ;; 2- Translate those pathnames as required
       (if fixedp
           absolute-pathnames
           (mapcar *output-translation-function* absolute-pathnames))))
182
   t))
183 184 185 186 187 188 189 190 191 192 193 194 195
(defmethod output-files ((o operation) (c component))
  (declare (ignorable o c))
  nil)
(defun* output-file (operation component)
  "The unique output file of performing OPERATION on COMPONENT"
  (let ((files (output-files operation component)))
    (assert (length=n-p files 1))
    (first files)))

(defmethod input-files ((o operation) (c parent-component))
  (declare (ignorable o c))
  nil)

196
(defmethod input-files ((o operation) (c component))
197 198
  (or (loop* :for (dep-o) :in (component-self-dependencies o c)
             :append (or (output-files dep-o c) (input-files dep-o c)))
199 200
      ;; no non-trivial previous operations needed?
      ;; I guess we work with the original source file, then
201 202
      (if-let ((pathname (component-pathname c)))
        (and (file-pathname-p pathname) (list pathname)))))
203 204 205 206


;;;; Done performing

207
(defgeneric* component-operation-time (operation component)) ;; ASDF4: hide it behind plan-action-stamp
208 209
(define-convenience-action-methods component-operation-time (operation component))

210
(defgeneric* mark-operation-done (operation component)) ;; ASDF4: hide it behind (setf plan-action-stamp)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
(defgeneric* compute-action-stamp (plan operation component &key just-done)
  (:documentation "Has this action been successfully done already,
and at what known timestamp has it been done at or will it be done at?
Takes two keywords JUST-DONE and PLAN:
JUST-DONE is a boolean that is true if the action was just successfully performed,
at which point we want compute the actual stamp and warn if files are missing;
otherwise we are making plans, anticipating the effects of the action.
PLAN is a plan object modelling future effects of actions,
or NIL to denote what actually happened.
Returns two values:
* a STAMP saying when it was done or will be done,
  or T if the action has involves files that need to be recomputed.
* a boolean DONE-P that indicates whether the action has actually been done,
  and both its output-files and its in-image side-effects are up to date."))

(defclass action-status ()
  ((stamp
    :initarg :stamp :reader action-stamp
    :documentation "STAMP associated with the ACTION if it has been completed already
in some previous image, or T if it needs to be done.")
   (done-p
    :initarg :done-p :reader action-done-p
    :documentation "a boolean, true iff the action was already done (before any planned action)."))
  (:documentation "Status of an action"))

(defmethod print-object ((status action-status) stream)
  (print-unreadable-object (status stream :type t)
    (with-slots (stamp done-p) status
      (format stream "~@{~S~^ ~}" :stamp stamp :done-p done-p))))

(defmethod component-operation-time ((o operation) (c component))
  (gethash (type-of o) (component-operation-times c)))

(defmethod mark-operation-done ((o operation) (c component))
  (setf (gethash (type-of o) (component-operation-times c))
        (compute-action-stamp nil o c :just-done t)))


;;;; Perform

251 252
(defgeneric* (perform-with-restarts) (operation component))
(defgeneric* (perform) (operation component))
253
(define-convenience-action-methods perform (operation component))
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

(defmethod perform :before ((o operation) (c component))
  (ensure-all-directories-exist (output-files o c)))
(defmethod perform :after ((o operation) (c component))
  (mark-operation-done o c))
(defmethod perform ((o operation) (c parent-component))
  (declare (ignorable o c))
  nil)
(defmethod perform ((o operation) (c source-file))
  (sysdef-error
   (compatfmt "~@<Required method PERFORM not implemented for operation ~A, component ~A~@:>")
   (class-of o) (class-of c)))

(defmethod perform-with-restarts (operation component)
  ;; TOO verbose, especially as the default. Add your own :before method
  ;; to perform-with-restart or perform if you want that:
  #|(when *asdf-verbose* (explain operation component))|#
  (perform operation component))
(defmethod perform-with-restarts :around (operation component)
  (loop
    (restart-case
        (return (call-next-method))
      (retry ()
        :report
        (lambda (s)
          (format s (compatfmt "~@<Retry ~A.~@:>")
280
                  (action-description operation component))))
281 282 283 284
      (accept ()
        :report
        (lambda (s)
          (format s (compatfmt "~@<Continue, treating ~A as having been successful.~@:>")
285
                  (action-description operation component)))
286 287 288
        (mark-operation-done operation component)
        (return)))))

289 290 291 292
;;; Generic build operation
(defmethod component-depends-on ((o build-op) (c component))
  `((,(or (component-build-operation c) 'load-op) ,c)))