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

4
(uiop/package:define-package :asdf/action
5
  (:nicknames :asdf-action)
6 7
  (:recycle :asdf/action :asdf/plan :asdf)
  (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session :asdf/component :asdf/operation)
8
  (:import-from :asdf/operation #:check-operation-constructor)
9
  #-clisp (:unintern #:required-components #:traverse-action #:traverse-sub-actions)
10
  (:export
11
   #:action #:define-convenience-action-methods
12 13 14
   #:action-description #:format-action
   #:downward-operation #:upward-operation #:sideway-operation #:selfward-operation
   #:non-propagating-operation
15
   #:component-depends-on
16
   #:input-files #:output-files #:output-file #:operation-done-p
17
   #:action-operation #:action-component #:make-action
18
   #:component-operation-time #:mark-operation-done #:compute-action-stamp
19
   #:perform #:perform-with-restarts #:retry #:accept
20
   #:action-path #:find-action
21
   #:operation-definition-warning #:operation-definition-error ;; condition
22 23 24
   #:action-valid-p
   #:circular-dependency #:circular-dependency-actions
   #:call-while-visiting-action #:while-visiting-action))
25 26
(in-package :asdf/action)

27
(eval-when (#-lispworks :compile-toplevel :load-toplevel :execute) ;; LispWorks issues spurious warning
28 29 30 31 32

  (deftype action ()
    "A pair of operation and component uniquely identifies a node in the dependency graph
of steps to be performed while building a system."
    '(cons operation component))
33 34

  (deftype operation-designator ()
35 36
    "An operation designates itself. NIL designates a context-dependent current operation,
and a class-name or class designates the canonical instance of the designated class."
37
    '(or operation null symbol class)))
38

39 40 41 42 43 44 45 46 47
;;; these are pseudo accessors -- let us abstract away the CONS cell representation of plan
;;; actions.
(with-upgradability ()
  (defun make-action (operation component)
    (cons operation component))
  (defun action-operation (action)
    (car action))
  (defun action-component (action)
    (cdr action)))
48

49
;;;; Reified representation for storage or debugging. Note: an action is identified by its class.
50 51
(with-upgradability ()
  (defun action-path (action)
52
    "A readable data structure that identifies the action."
53 54 55 56
    (when action
      (let ((o (action-operation action))
            (c (action-component action)))
        (cons (type-of o) (component-find-path c)))))
57
  (defun find-action (path)
58
    "Reconstitute an action from its action-path"
59
    (destructuring-bind (o . c) path (make-action (make-operation o) (find-component () c)))))
60 61

;;;; Convenience methods
62
(with-upgradability ()
63 64 65 66 67 68 69 70
  ;; A macro that defines convenience methods for a generic function (gf) that
  ;; dispatches on operation and component.  The convenience methods allow users
  ;; to call the gf with operation and/or component designators, that the
  ;; methods will resolve into actual operation and component objects, so that
  ;; the users can interact using readable designators, but developers only have
  ;; to write methods that handle operation and component objects.
  ;; FUNCTION is the generic function name
  ;; FORMALS is its list of arguments, which must include OPERATION and COMPONENT.
71 72
  ;; IF-NO-OPERATION is a form (defaults to NIL) describing what to do if no operation is found.
  ;; IF-NO-COMPONENT is a form (defaults to NIL) describing what to do if no component is found.
73
  (defmacro define-convenience-action-methods
74
      (function formals &key if-no-operation if-no-component)
75 76
    (let* ((rest (gensym "REST"))
           (found (gensym "FOUND"))
77 78 79
           (keyp (equal (last formals) '(&key)))
           (formals-no-key (if keyp (butlast formals) formals))
           (len (length formals-no-key))
80 81 82 83 84 85
           (operation 'operation)
           (component 'component)
           (opix (position operation formals))
           (coix (position component formals))
           (prefix (subseq formals 0 opix))
           (suffix (subseq formals (1+ coix) len))
86
           (more-args (when keyp `(&rest ,rest &key &allow-other-keys))))
87
      (assert (and (integerp opix) (integerp coix) (= coix (1+ opix))))
88 89
      (flet ((next-method (o c)
               (if keyp
90 91
                   `(apply ',function ,@prefix ,o ,c ,@suffix ,rest)
                   `(,function ,@prefix ,o ,c ,@suffix))))
92
        `(progn
93
           (defmethod ,function (,@prefix (,operation string) ,component ,@suffix ,@more-args)
94
             (declare (notinline ,function))
95 96 97
             (let ((,component (find-component () ,component))) ;; do it first, for defsystem-depends-on
               ,(next-method `(safe-read-from-string ,operation :package :asdf/interface) component)))
           (defmethod ,function (,@prefix (,operation symbol) ,component ,@suffix ,@more-args)
98
             (declare (notinline ,function))
99 100
             (if ,operation
                 ,(next-method
101
                   `(make-operation ,operation)
102 103
                   `(or (find-component () ,component) ,if-no-component))
                 ,if-no-operation))
104
           (defmethod ,function (,@prefix (,operation operation) ,component ,@suffix ,@more-args)
105
             (declare (notinline ,function))
106 107
             (if (typep ,component 'component)
                 (error "No defined method for ~S on ~/asdf-action:format-action/"
108
                        ',function (make-action ,operation ,component))
109 110 111
                 (if-let (,found (find-component () ,component))
                    ,(next-method operation found)
                    ,if-no-component))))))))
112

113

114
;;;; Self-description
115 116 117
(with-upgradability ()
  (defgeneric action-description (operation component)
    (:documentation "returns a phrase that describes performing this operation
118 119
on this component, e.g. \"loading /a/b/c\".
You can put together sentences using this phrase."))
120 121
  (defmethod action-description (operation component)
    (format nil (compatfmt "~@<~A on ~A~@:>")
122
            operation component))
123

124
  (defun format-action (stream action &optional colon-p at-sign-p)
125 126
    "FORMAT helper to display an action's action-description.
Use it in FORMAT control strings as ~/asdf-action:format-action/"
127 128 129
    (assert (null colon-p)) (assert (null at-sign-p))
    (destructuring-bind (operation . component) action
      (princ (action-description operation component) stream))))
130 131


132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
;;;; Detection of circular dependencies
(with-upgradability ()
  (defun (action-valid-p) (operation component)
    "Is this action valid to include amongst dependencies?"
    ;; If either the operation or component was resolved to nil, the action is invalid.
    ;; :if-feature will invalidate actions on components for which the features don't apply.
    (and operation component
         (if-let (it (component-if-feature component)) (featurep it) t)))

  (define-condition circular-dependency (system-definition-error)
    ((actions :initarg :actions :reader circular-dependency-actions))
    (:report (lambda (c s)
               (format s (compatfmt "~@<Circular dependency: ~3i~_~S~@:>")
                       (circular-dependency-actions c)))))

  (defun call-while-visiting-action (operation component fun)
    "Detect circular dependencies"
    (with-asdf-session ()
      (with-accessors ((action-set visiting-action-set)
                       (action-list visiting-action-list)) *asdf-session*
        (let ((action (cons operation component)))
          (when (gethash action action-set)
            (error 'circular-dependency :actions
                   (member action (reverse action-list) :test 'equal)))
          (setf (gethash action action-set) t)
          (push action action-list)
          (unwind-protect
               (funcall fun)
            (pop action-list)
            (setf (gethash action action-set) nil))))))

  ;; Syntactic sugar for call-while-visiting-action
  (defmacro while-visiting-action ((o c) &body body)
    `(call-while-visiting-action ,o ,c #'(lambda () ,@body))))


168
;;;; Dependencies
169
(with-upgradability ()
170
  (defgeneric component-depends-on (operation component) ;; ASDF4: rename to component-dependencies
171 172
    (:documentation
     "Returns a list of dependencies needed by the component to perform
173 174
    the operation.  A dependency has one of the following forms:

175 176 177 178 179
      (<operation> <component>*), where <operation> is an operation designator
        with respect to FIND-OPERATION in the context of the OPERATION argument,
        and each <component> is a component designator with respect to
        FIND-COMPONENT in the context of the COMPONENT argument,
        and means that the component depends on
180 181 182 183
        <operation> having been performed on each <component>;

        [Note: an <operation> is an operation designator -- it can be either an
        operation name or an operation object.  Similarly, a <component> may be
184 185
        a component name or a component object.  Also note that, the degenerate
        case of (<operation>) is a no-op.]
186

187
    Methods specialized on subclasses of existing component types
188
    should usually append the results of CALL-NEXT-METHOD to the list."))
189
  (define-convenience-action-methods component-depends-on (operation component))
190 191 192

  (defmethod component-depends-on :around ((o operation) (c component))
    (do-asdf-cache `(component-depends-on ,o ,c)
193
      (call-next-method))))
194 195


196 197
;;;; upward-operation, downward-operation, sideway-operation, selfward-operation
;; These together handle actions that propagate along the component hierarchy or operation universe.
198 199 200
(with-upgradability ()
  (defclass downward-operation (operation)
    ((downward-operation
201
      :initform nil :reader downward-operation
202
      :type operation-designator :allocation :class))
203 204 205 206 207 208
    (:documentation "A DOWNWARD-OPERATION's dependencies propagate down the component hierarchy.
I.e., if O is a DOWNWARD-OPERATION and its DOWNWARD-OPERATION slot designates operation D, then
the action (O . M) of O on module M will depends on each of (D . C) for each child C of module M.
The default value for slot DOWNWARD-OPERATION is NIL, which designates the operation O itself.
E.g. in order for a MODULE to be loaded with LOAD-OP (resp. compiled with COMPILE-OP), all the
children of the MODULE must have been loaded with LOAD-OP (resp. compiled with COMPILE-OP."))
209 210
  (defun downward-operation-depends-on (o c)
    `((,(or (downward-operation o) o) ,@(component-children c))))
211
  (defmethod component-depends-on ((o downward-operation) (c parent-component))
212
    `(,@(downward-operation-depends-on o c) ,@(call-next-method)))
213

214 215
  (defclass upward-operation (operation)
    ((upward-operation
216
      :initform nil :reader upward-operation
217
      :type operation-designator :allocation :class))
218 219 220 221 222 223
    (:documentation "An UPWARD-OPERATION has dependencies that propagate up the component hierarchy.
I.e., if O is an instance of UPWARD-OPERATION, and its UPWARD-OPERATION slot designates operation U,
then the action (O . C) of O on a component C that has the parent P will depends on (U . P).
The default value for slot UPWARD-OPERATION is NIL, which designates the operation O itself.
E.g. in order for a COMPONENT to be prepared for loading or compiling with PREPARE-OP, its PARENT
must first be prepared for loading or compiling with PREPARE-OP."))
224 225
  ;; For backward-compatibility reasons, a system inherits from module and is a child-component
  ;; so we must guard against this case. ASDF4: remove that.
226 227
  (defun upward-operation-depends-on (o c)
    (if-let (p (component-parent c)) `((,(or (upward-operation o) o) ,p))))
228
  (defmethod component-depends-on ((o upward-operation) (c child-component))
229
    `(,@(upward-operation-depends-on o c) ,@(call-next-method)))
230

231 232
  (defclass sideway-operation (operation)
    ((sideway-operation
233
      :initform nil :reader sideway-operation
234
      :type operation-designator :allocation :class))
235 236 237 238 239 240
    (:documentation "A SIDEWAY-OPERATION has dependencies that propagate \"sideway\" to siblings
that a component depends on. I.e. if O is a SIDEWAY-OPERATION, and its SIDEWAY-OPERATION slot
designates operation S (where NIL designates O itself), then the action (O . C) of O on component C
depends on each of (S . D) where D is a declared dependency of C.
E.g. in order for a COMPONENT to be prepared for loading or compiling with PREPARE-OP,
each of its declared dependencies must first be loaded as by LOAD-OP."))
241
  (defun sideway-operation-depends-on (o c)
242
    `((,(or (sideway-operation o) o) ,@(component-sideway-dependencies c))))
243 244
  (defmethod component-depends-on ((o sideway-operation) (c component))
    `(,@(sideway-operation-depends-on o c) ,@(call-next-method)))
245

246 247
  (defclass selfward-operation (operation)
    ((selfward-operation
248
      ;; NB: no :initform -- if an operation depends on others, it must explicitly specify which
249
      :type (or operation-designator list) :reader selfward-operation :allocation :class))
250
    (:documentation "A SELFWARD-OPERATION depends on another operation on the same component.
251 252
I.e., if O is a SELFWARD-OPERATION, and its SELFWARD-OPERATION designates a list of operations L,
then the action (O . C) of O on component C depends on each (S . C) for S in L.
253
E.g. before a component may be loaded by LOAD-OP, it must have been compiled by COMPILE-OP.
254 255
A operation-designator designates a singleton list of the designated operation;
a list of operation-designators designates the list of designated operations;
256 257 258
NIL is not a valid operation designator in that context.  Note that any dependency
ordering between the operations in a list of SELFWARD-OPERATION should be specified separately
in the respective operation's COMPONENT-DEPENDS-ON methods so that they be scheduled properly."))
259 260
  (defun selfward-operation-depends-on (o c)
    (loop :for op :in (ensure-list (selfward-operation o)) :collect `(,op ,c)))
261
  (defmethod component-depends-on ((o selfward-operation) (c component))
262
    `(,@(selfward-operation-depends-on o c) ,@(call-next-method)))
263 264 265

  (defclass non-propagating-operation (operation)
    ()
266
    (:documentation "A NON-PROPAGATING-OPERATION is an operation that propagates
267 268 269 270 271 272 273 274
no dependencies whatsoever.  It is supplied in order that the programmer be able
to specify that s/he is intentionally specifying an operation which invokes no
dependencies.")))


;;;---------------------------------------------------------------------------
;;; Help programmers catch obsolete OPERATION subclasses
;;;---------------------------------------------------------------------------
275 276 277 278 279 280 281 282 283 284
(with-upgradability ()
  (define-condition operation-definition-warning (simple-warning)
    ()
    (:documentation "Warning condition related to definition of obsolete OPERATION objects."))

  (define-condition operation-definition-error (simple-error)
    ()
    (:documentation "Error condition related to definition of incorrect OPERATION objects."))

  (defmethod initialize-instance :before ((o operation) &key)
285
    (check-operation-constructor)
286
    (unless (typep o '(or downward-operation upward-operation sideway-operation
287
                          selfward-operation non-propagating-operation))
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
      (warn 'operation-definition-warning
            :format-control
            "No dependency propagating scheme specified for operation class ~S.
The class needs to be updated for ASDF 3.1 and specify appropriate propagation mixins."
            :format-arguments (list (type-of o)))))

  (defmethod initialize-instance :before ((o non-propagating-operation) &key)
    (when (typep o '(or downward-operation upward-operation sideway-operation selfward-operation))
      (error 'operation-definition-error
             :format-control
             "Inconsistent class: ~S
  NON-PROPAGATING-OPERATION is incompatible with propagating operation classes as superclasses."
             :format-arguments
             (list (type-of o)))))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
303 304
  (defun backward-compatible-depends-on (o c)
    "DEPRECATED: all subclasses of OPERATION used in ASDF should inherit from one of
305 306 307
 DOWNWARD-OPERATION UPWARD-OPERATION SIDEWAY-OPERATION SELFWARD-OPERATION NON-PROPAGATING-OPERATION.
 The function BACKWARD-COMPATIBLE-DEPENDS-ON temporarily provides ASDF2 behaviour for those that
 don't. In the future this functionality will be removed, and the default will be no propagation."
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
308 309
    (uiop/version::notify-deprecated-function
     (version-deprecation *asdf-version* :style-warning "3.2")
310
     `(backward-compatible-depends-on :for-operation ,o))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
311 312
    `(,@(sideway-operation-depends-on o c)
      ,@(when (typep c 'parent-component) (downward-operation-depends-on o c))))
313

314 315 316
  (defmethod component-depends-on ((o operation) (c component))
    `(;; Normal behavior, to allow user-specified in-order-to dependencies
      ,@(cdr (assoc (type-of o) (component-in-order-to c)))
317 318 319 320 321
        ;; For backward-compatibility with ASDF2, any operation that doesn't specify propagation
        ;; or non-propagation through an appropriate mixin will be downward and sideway.
        ,@(unless (typep o '(or downward-operation upward-operation sideway-operation
                             selfward-operation non-propagating-operation))
            (backward-compatible-depends-on o c))))
322 323 324 325

  (defmethod downward-operation ((o operation)) nil)
  (defmethod sideway-operation ((o operation)) nil))

326 327 328 329

;;;---------------------------------------------------------------------------
;;; End of OPERATION class checking
;;;---------------------------------------------------------------------------
330 331 332


;;;; Inputs, Outputs, and invisible dependencies
333
(with-upgradability ()
334
  (defgeneric output-files (operation component)
335
    (:documentation "Methods for this function return two values: a list of output files
336 337 338
corresponding to this action, and a boolean indicating if they have already been subjected
to relevant output translations and should not be further translated.

339
Methods on PERFORM *must* call this function to determine where their outputs are to be located.
340 341
They may rely on the order of the files to discriminate between outputs.
"))
342
  (defgeneric input-files (operation component)
343 344
    (:documentation "A list of input files corresponding to this action.

345
Methods on PERFORM *must* call this function to determine where their inputs are located.
346 347
They may rely on the order of the files to discriminate between inputs.
"))
348
  (defgeneric operation-done-p (operation component)
349
    (:documentation "Returns a boolean which is NIL if the action must be performed (again)."))
350 351 352 353 354 355 356
  (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))

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

357
  ;; Translate output files, unless asked not to. Memoize the result.
358
  (defmethod output-files :around ((operation t) (component t))
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    (do-asdf-cache `(output-files ,operation ,component)
      (values
       (multiple-value-bind (pathnames fixedp) (call-next-method)
         ;; 1- Make sure we have absolute pathnames
         (let* ((directory (pathname-directory-pathname
                            (component-pathname (find-component () component))))
                (absolute-pathnames
                  (loop
                    :for pathname :in pathnames
                    :collect (ensure-absolute-pathname pathname directory))))
           ;; 2- Translate those pathnames as required
           (if fixedp
               absolute-pathnames
               (mapcar *output-translation-function* absolute-pathnames))))
       t)))
  (defmethod output-files ((o operation) (c component))
    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)))

382
  ;; Memoize input files.
383 384 385 386
  (defmethod input-files :around (operation component)
    (do-asdf-cache `(input-files ,operation ,component)
      (call-next-method)))

387
  ;; By default an action has no input-files.
388
  (defmethod input-files ((o operation) (c component))
389 390
    nil)

391 392 393 394
  ;; An action with a selfward-operation by default gets its input-files from the output-files of
  ;; the actions using selfward-operations it depends on (and the same component),
  ;; or if there are none, on the component-pathname of the component if it's a file
  ;; -- and then on the results of the next-method.
395 396 397 398 399 400
  (defmethod input-files ((o selfward-operation) (c component))
    `(,@(or (loop :for dep-o :in (ensure-list (selfward-operation o))
                  :append (or (output-files dep-o c) (input-files dep-o c)))
            (if-let ((pathname (component-pathname c)))
              (and (file-pathname-p pathname) (list pathname))))
      ,@(call-next-method))))
401 402 403


;;;; Done performing
404
(with-upgradability ()
405 406 407 408 409
  ;; ASDF4: hide it behind plan-action-stamp
  (defgeneric component-operation-time (operation component)
    (:documentation "Return the timestamp for when an action was last performed"))
  (defgeneric (setf component-operation-time) (time operation component)
    (:documentation "Update the timestamp for when an action was last performed"))
410
  (define-convenience-action-methods component-operation-time (operation component))
411

412 413 414 415 416 417
  ;; ASDF4: hide it behind (setf plan-action-stamp)
  (defgeneric mark-operation-done (operation component)
    (:documentation "Mark a action as having been just done.

Updates the action's COMPONENT-OPERATION-TIME to match the COMPUTE-ACTION-STAMP
using the JUST-DONE flag."))
418 419
  (defgeneric compute-action-stamp (plan operation component &key just-done)
    (:documentation "Has this action been successfully done already,
420
and at what known timestamp has it been done at or will it be done at?
421 422 423 424
* PLAN is a plan object modelling future effects of actions,
  or NIL to denote what actually happened.
* OPERATION and COMPONENT denote the action.
Takes keyword JUST-DONE:
425 426 427
* 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.
428 429
Returns two values:
* a STAMP saying when it was done or will be done,
430
  or T if the action involves files that need to be recomputed.
431 432 433
* 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."))

434
  (defmethod component-operation-time ((o operation) (c component))
435
    (gethash o (component-operation-times c)))
436

437
  (defmethod (setf component-operation-time) (stamp (o operation) (c component))
438
    (assert stamp () "invalid null stamp for ~A" (action-description o c))
439
    (setf (gethash o (component-operation-times c)) stamp))
440

441
  (defmethod mark-operation-done ((o operation) (c component))
442 443 444
    (let ((stamp (compute-action-stamp nil o c :just-done t)))
      (assert stamp () "Failed to compute a stamp for completed action ~A" (action-description o c))1
      (setf (component-operation-time o c) stamp))))
445 446 447


;;;; Perform
448
(with-upgradability ()
449
  (defgeneric perform (operation component)
450
    (:documentation "PERFORM an action, consuming its input-files and building its output-files"))
451 452
  (define-convenience-action-methods perform (operation component))

453
  (defmethod perform :around ((o operation) (c component))
454
    (while-visiting-action (o c) (call-next-method)))
455 456 457 458 459 460 461
  (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))
    nil)
  (defmethod perform ((o operation) (c source-file))
462 463 464 465 466
    ;; For backward compatibility, don't error on operations that don't specify propagation.
    (when (typep o '(or downward-operation upward-operation sideway-operation
                     selfward-operation non-propagating-operation))
      (sysdef-error
       (compatfmt "~@<Required method ~S not implemented for ~/asdf-action:format-action/~@:>")
467
       'perform (make-action o c))))
468

469 470 471
  ;; The restarts of the perform-with-restarts variant matter in an interactive context.
  ;; The retry strategies of p-w-r itself, and/or the background workers of a multiprocess build
  ;; may call perform directly rather than call p-w-r.
472
  (defgeneric perform-with-restarts (operation component)
473
    (:documentation "PERFORM an action in a context where suitable restarts are in place."))
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  (defmethod perform-with-restarts (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.~@:>")
                    (action-description operation component))))
        (accept ()
          :report
          (lambda (s)
            (format s (compatfmt "~@<Continue, treating ~A as having been successful.~@:>")
                    (action-description operation component)))
          (mark-operation-done operation component)
          (return))))))