plan.lisp 28.5 KB
Newer Older
1 2 3
;;;; -------------------------------------------------------------------------
;;;; Plan

4
(uiop/package:define-package :asdf/plan
5
  (:recycle :asdf/plan :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade
7
   :asdf/component :asdf/operation :asdf/system
8
   :asdf/cache :asdf/find-system :asdf/find-component
9
   :asdf/operation :asdf/action :asdf/lisp-action)
10
  (:export
11
   #:component-operation-time
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
12
   #:plan #:plan-traversal #:sequential-plan #:*default-plan-class*
13 14 15
   #:planned-action-status #:plan-action-status #:action-already-done-p
   #:circular-dependency #:circular-dependency-actions
   #:node-for #:needed-in-image-p
16
   #:action-index #:action-planned-p #:action-valid-p
17
   #:plan-record-dependency
18
   #:normalize-forced-systems #:action-forced-p #:action-forced-not-p
19
   #:map-direct-dependencies #:reduce-direct-dependencies #:direct-dependencies
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
20
   #:compute-action-stamp #:traverse-action
21
   #:circular-dependency #:circular-dependency-actions
22
   #:call-while-visiting-action #:while-visiting-action
23
   #:make-plan #:plan-actions #:perform-plan #:plan-operates-on-p
24 25
   #:planned-p #:index #:forced #:forced-not #:total-action-count
   #:planned-action-count #:planned-output-action-count #:visited-actions
26
   #:visiting-action-set #:visiting-action-list #:plan-actions-r
27
   #:required-components #:filtered-sequential-plan
28
   #:plan-system
29 30
   #:plan-action-filter #:plan-component-type #:plan-keep-operation #:plan-keep-component
   #:traverse-actions #:traverse-sub-actions))
31
(in-package :asdf/plan)
32

33
;;;; Generic plan traversal class
34
(with-upgradability ()
35 36
  (defclass plan () ()
    (:documentation "Base class for a plan based on which ASDF can build a system"))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
37
  (defclass plan-traversal (plan)
38 39 40
    (;; The system for which the plan is computed
     (system :initform nil :initarg :system :accessor plan-system)
     ;; Tables of systems specified via :force and :force-not arguments
41 42
     (forced :initform nil :initarg :force :accessor plan-forced)
     (forced-not :initform nil :initarg :force-not :accessor plan-forced-not)
43 44
     ;; Counts of total actions in plan, actions that need to be performed,
     ;; actions that need to be performed that have a non-empty list of output-files.
45 46 47
     (total-action-count :initform 0 :accessor plan-total-action-count)
     (planned-action-count :initform 0 :accessor plan-planned-action-count)
     (planned-output-action-count :initform 0 :accessor plan-planned-output-action-count)
48
     ;; Table that to actions already visited while walking the dependencies associates status
49
     (visited-actions :initform (make-hash-table :test 'equal) :accessor plan-visited-actions)
50 51 52 53 54
     ;; Actions that depend on those being currently walked through, to detect circularities
     (visiting-action-set ;; as a set
      :initform (make-hash-table :test 'equal) :accessor plan-visiting-action-set)
     (visiting-action-list :initform () :accessor plan-visiting-action-list)) ;; as a list
    (:documentation "Base class for plans that simply traverse dependencies")))
55 56


57
;;;; Planned action status
58 59 60
(with-upgradability ()
  (defgeneric plan-action-status (plan operation component)
    (:documentation "Returns the ACTION-STATUS associated to
61 62
the action of OPERATION on COMPONENT in the PLAN"))

63 64
  (defgeneric (setf plan-action-status) (new-status plan operation component)
    (:documentation "Sets the ACTION-STATUS associated to
65 66
the action of OPERATION on COMPONENT in the PLAN"))

67 68 69 70 71 72 73 74 75 76 77 78 79 80
  (defclass planned-action-status (action-status)
    ((planned-p
      :initarg :planned-p :reader action-planned-p
      :documentation "a boolean, true iff the action was included in the plan.")
     (index
      :initarg :index :reader action-index
      :documentation "an integer, counting all traversed actions in traversal order."))
    (:documentation "Status of an action in a plan"))

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

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
81 82
  (defmethod action-planned-p ((action-status t))
    t) ; default method for non planned-action-status objects
83

84 85
  ;; TODO: either confirm there are no operation-original-initargs, eliminate NODE-FOR,
  ;; and use (CONS O C); or keep the operation initargs, and here use MAKE-OPERATION.
86
  ;; However, see also component-operation-time and mark-operation-done
87 88 89
  (defun node-for (o c)
    "Given operation O and component C, return an object to use as key in action-indexed tables."
    (cons (type-of o) c))
90 91

  (defun action-already-done-p (plan operation component)
92
    "According to this plan, is this action already done and up to date?"
93 94 95 96 97 98 99 100 101 102 103 104 105
    (action-done-p (plan-action-status plan operation component)))

  (defmethod plan-action-status ((plan null) (o operation) (c component))
    (multiple-value-bind (stamp done-p) (component-operation-time o c)
      (make-instance 'action-status :stamp stamp :done-p done-p)))

  (defmethod (setf plan-action-status) (new-status (plan null) (o operation) (c component))
    (let ((to (type-of o))
          (times (component-operation-times c)))
      (if (action-done-p new-status)
          (remhash to times)
          (setf (gethash to times) (action-stamp new-status))))
    new-status))
106 107 108


;;;; forcing
109
(with-upgradability ()
110 111 112 113 114 115 116 117 118 119 120 121 122
  (defgeneric action-forced-p (plan operation component)
    (:documentation "Is this action forced to happen in this plan?"))
  (defgeneric action-forced-not-p (plan operation component)
    (:documentation "Is this action forced to not happen in this plan?
Takes precedence over action-forced-p."))

  (defun normalize-forced-systems (force system)
    "Given a SYSTEM on which operate is called and the specified FORCE argument,
extract a hash-set of systems that are forced, or a predicate on system names,
or NIL if none are forced, or :ALL if all are."
    (etypecase force
      ((or (member nil :all) hash-table function) force)
      (cons (list-to-hash-set (mapcar #'coerce-name force)))
123 124
      ((eql t) (when system (list-to-hash-set (list (coerce-name system)))))))

125 126 127 128
  (defun normalize-forced-not-systems (force-not system)
    "Given a SYSTEM on which operate is called, the specified FORCE-NOT argument,
and the set of IMMUTABLE systems, extract a hash-set of systems that are effectively forced-not,
or predicate on system names, or NIL if none are forced, or :ALL if all are."
129
    (let ((requested
130 131 132
            (etypecase force-not
              ((or (member nil :all) hash-table function) force-not)
              (cons (list-to-hash-set (mapcar #'coerce-name force-not)))
133 134
              ((eql t) (if system (let ((name (coerce-name system)))
                                    #'(lambda (x) (not (equal x name))))
135
                           :all)))))
136
      (if (and *immutable-systems* requested)
137 138
          #'(lambda (x) (or (call-function requested x)
                            (call-function *immutable-systems* x)))
139 140
          (or *immutable-systems* requested))))

141
  ;; TODO: shouldn't we be looking up the primary system name, rather than the system name?
142
  (defun action-override-p (plan operation component override-accessor)
143 144
    "Given a plan, an action, and a function that given the plan accesses a set of overrides
(i.e. force or force-not), see if the override applies to the current action."
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
145
    (declare (ignore operation))
146 147
    (call-function (funcall override-accessor plan)
                   (coerce-name (component-system (find-component () component)))))
148 149 150 151 152 153

  (defmethod action-forced-p (plan operation component)
    (and
     ;; Did the user ask us to re-perform the action?
     (action-override-p plan operation component 'plan-forced)
     ;; You really can't force a builtin system and :all doesn't apply to it,
154
     ;; except if it's the specifically the system currently being built.
155 156 157 158 159
     (not (let ((system (component-system component)))
            (and (builtin-system-p system)
                 (not (eq system (plan-system plan))))))))

  (defmethod action-forced-not-p (plan operation component)
160 161 162
    ;; Did the user ask us to not re-perform the action?
    ;; NB: force-not takes precedence over force, as it should
    (action-override-p plan operation component 'plan-forced-not))
163

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
164
  (defmethod action-forced-p ((plan null) (operation operation) (component component))
165 166
    nil)

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
167
  (defmethod action-forced-not-p ((plan null) (operation operation) (component component))
168
    nil))
169 170 171


;;;; action-valid-p
172 173 174
(with-upgradability ()
  (defgeneric action-valid-p (plan operation component)
    (:documentation "Is this action valid to include amongst dependencies?"))
175
  ;; :if-feature will invalidate actions on components for which the features don't apply.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
176
  (defmethod action-valid-p ((plan t) (o operation) (c component))
177
    (if-let (it (component-if-feature c)) (featurep it) t))
178
  ;; If either the operation or component was resolved to nil, the action is invalid.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
179 180
  (defmethod action-valid-p ((plan t) (o null) (c t)) nil)
  (defmethod action-valid-p ((plan t) (o t) (c null)) nil)
181 182
  ;; If the plan is null, i.e., we're looking at reality,
  ;; then any action with actual operation and component objects is valid.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
183
  (defmethod action-valid-p ((plan null) (o operation) (c component)) t))
184 185

;;;; Is the action needed in this image?
186 187
(with-upgradability ()
  (defgeneric needed-in-image-p (operation component)
188 189
    (:documentation "Is the action of OPERATION on COMPONENT needed in the current image
to be meaningful, or could it just as well have been done in another Lisp image?"))
190

191 192 193 194 195 196
  (defmethod needed-in-image-p ((o operation) (c component))
    ;; We presume that actions that modify the filesystem don't need be run
    ;; in the current image if they have already been done in another,
    ;; and can be run in another process (e.g. a fork),
    ;; whereas those that don't are meant to side-effect the current image and can't.
    (not (output-files o c))))
197 198 199


;;;; Visiting dependencies of an action and computing action stamps
200
(with-upgradability ()
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
201
  (defun (map-direct-dependencies) (plan operation component fun)
202
    "Call FUN on all the valid dependencies of the given action in the given plan"
203
    (loop* :for (dep-o-spec . dep-c-specs) :in (component-depends-on operation component)
204 205 206 207
           :for dep-o = (find-operation operation dep-o-spec)
           :when dep-o
           :do (loop :for dep-c-spec :in dep-c-specs
                     :for dep-c = (and dep-c-spec (resolve-dependency-spec component dep-c-spec))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
208
                     :when (and dep-c (action-valid-p plan dep-o dep-c))
209
                       :do (funcall fun dep-o dep-c))))
210

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
211
  (defun (reduce-direct-dependencies) (plan operation component combinator seed)
212 213 214
    "Reduce the direct dependencies to a value computed by iteratively calling COMBINATOR
for each dependency action on the dependency's operation and component and an accumulator
initialized with SEED."
215
    (map-direct-dependencies
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
216
     plan operation component
217 218 219 220
     #'(lambda (dep-o dep-c)
         (setf seed (funcall combinator dep-o dep-c seed))))
    seed)

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
221
  (defun (direct-dependencies) (plan operation component)
222
    "Compute a list of the direct dependencies of the action within the plan"
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
223
    (reduce-direct-dependencies plan operation component #'acons nil))
224

225 226 227 228
  ;; In a distant future, get-file-stamp, component-operation-time and latest-stamp
  ;; shall also be parametrized by the plan, or by a second model object,
  ;; so they need not refer to the state of the filesystem,
  ;; and the stamps could be cryptographic checksums rather than timestamps.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
229
  ;; Such a change remarkably would only affect COMPUTE-ACTION-STAMP.
230 231

  (defmethod compute-action-stamp (plan (o operation) (c component) &key just-done)
232 233 234 235 236 237 238 239 240 241
    ;; Given an action, figure out at what time in the past it has been done,
    ;; or if it has just been done, return the time that it has.
    ;; Returns two values:
    ;; 1- the TIMESTAMP of the action if it has already been done and is up to date,
    ;;   or T is either hasn't been done or is out of date.
    ;; 2- the DONE-IN-IMAGE-P boolean flag that is T if the action has already been done
    ;;   in the current image, or NIL if it hasn't.
    ;; Note that if e.g. LOAD-OP only depends on up-to-date files, but
    ;; hasn't been done in the current image yet, then it can have a non-T timestamp,
    ;; yet a NIL done-in-image-p flag.
242 243 244
    (nest
     (block ())
     (let ((dep-stamp ; collect timestamp from dependencies (or T if forced or out-of-date)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
245 246 247 248 249 250 251
             (reduce-direct-dependencies
              plan o c
              #'(lambda (o c stamp)
                  (if-let (it (plan-action-status plan o c))
                    (latest-stamp stamp (action-stamp it))
                    t))
              nil)))
252 253 254 255 256 257 258 259 260
       ;; out-of-date dependency: don't bother expensively querying the filesystem
       (when (and (eq dep-stamp t) (not just-done)) (return (values t nil))))
     ;; collect timestamps from inputs, and exit early if any is missing
     (let* ((in-files (input-files o c))
            (in-stamps (mapcar #'get-file-stamp in-files))
            (missing-in (loop :for f :in in-files :for s :in in-stamps :unless s :collect f))
            (latest-in (stamps-latest (cons dep-stamp in-stamps))))
       (when (and missing-in (not just-done)) (return (values t nil))))
     ;; collect timestamps from outputs, and exit early if any is missing
261
     (let* ((out-files (remove-if 'null (output-files o c)))
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            (out-stamps (mapcar (if just-done 'register-file-stamp 'get-file-stamp) out-files))
            (missing-out (loop :for f :in out-files :for s :in out-stamps :unless s :collect f))
            (earliest-out (stamps-earliest out-stamps)))
       (when (and missing-out (not just-done)) (return (values t nil))))
     (let* (;; There are three kinds of actions:
            (out-op (and out-files t)) ; those that create files on the filesystem
            ;;(image-op (and in-files (null out-files))) ; those that load stuff into the image
            ;;(null-op (and (null out-files) (null in-files))) ; placeholders that do nothing
            ;; When was the thing last actually done? (Now, or ask.)
            (op-time (or just-done (component-operation-time o c)))
            ;; Time stamps from the files at hand, and whether any is missing
            (all-present (not (or missing-in missing-out)))
            ;; Has any input changed since we last generated the files?
            (up-to-date-p (stamp<= latest-in earliest-out))
            ;; If everything is up to date, the latest of inputs and outputs is our stamp
            (done-stamp (stamps-latest (cons latest-in out-stamps))))
       ;; Warn if some files are missing:
       ;; either our model is wrong or some other process is messing with our files.
       (when (and just-done (not all-present))
         (warn "~A completed without ~:[~*~;~*its input file~:p~2:*~{ ~S~}~*~]~
                ~:[~; or ~]~:[~*~;~*its output file~:p~2:*~{ ~S~}~*~]"
               (action-description o c)
               missing-in (length missing-in) (and missing-in missing-out)
               missing-out (length missing-out))))
     ;; Note that we use stamp<= instead of stamp< to play nice with generated files.
     ;; Any race condition is intrinsic to the limited timestamp resolution.
     (if (or just-done ;; The done-stamp is valid: if we're just done, or
             ;; if all filesystem effects are up-to-date and there's no invalidating reason.
             (and all-present up-to-date-p (operation-done-p o c) (not (action-forced-p plan o c))))
         (values done-stamp ;; return the hard-earned timestamp
                 (or just-done
                     out-op ;; a file-creating op is done when all files are up to date
                     ;; a image-effecting a placeholder op is done when it was actually run,
                     (and op-time (eql op-time done-stamp)))) ;; with the matching stamp
         ;; done-stamp invalid: return a timestamp in an indefinite future, action not done yet
         (values t nil)))))
298 299


300 301 302
;;;; Generic support for plan-traversal
(with-upgradability ()
  (defmethod initialize-instance :after ((plan plan-traversal)
303
                                         &key force force-not system
304 305
                                         &allow-other-keys)
    (with-slots (forced forced-not) plan
306 307
      (setf forced (normalize-forced-systems force system))
      (setf forced-not (normalize-forced-not-systems force-not system))))
308

309 310 311 312 313 314 315 316 317 318 319
  (defgeneric plan-actions (plan)
    (:documentation "Extract from a plan a list of actions to perform in sequence"))
  (defmethod plan-actions ((plan list))
    plan)

  (defmethod (setf plan-action-status) (new-status (p plan-traversal) (o operation) (c component))
    (setf (gethash (node-for o c) (plan-visited-actions p)) new-status))

  (defmethod plan-action-status ((p plan-traversal) (o operation) (c component))
    (or (and (action-forced-not-p p o c) (plan-action-status nil o c))
        (values (gethash (node-for o c) (plan-visited-actions p)))))
320

321 322
  (defmethod action-valid-p ((p plan-traversal) (o operation) (s system))
    (and (not (action-forced-not-p p o s)) (call-next-method)))
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337
  (defgeneric plan-record-dependency (plan operation component)
    (:documentation "Record an action as a dependency in the current plan")))


;;;; Detection of circular dependencies
(with-upgradability ()
  (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)))))

  (defgeneric call-while-visiting-action (plan operation component function)
    (:documentation "Detect circular dependencies"))
338 339 340 341 342 343 344 345 346 347 348 349 350

  (defmethod call-while-visiting-action ((plan plan-traversal) operation component fun)
    (with-accessors ((action-set plan-visiting-action-set)
                     (action-list plan-visiting-action-list)) plan
      (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)
351 352 353 354 355
          (setf (gethash action action-set) nil)))))

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


;;;; Actual traversal: traverse-action
359 360 361
(with-upgradability ()
  (defgeneric traverse-action (plan operation component needed-in-image-p))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
362 363 364 365 366 367 368 369
  ;; TRAVERSE-ACTION, in the context of a given PLAN object that accumulates dependency data,
  ;; visits the action defined by its OPERATION and COMPONENT arguments,
  ;; and all its transitive dependencies (unless already visited),
  ;; in the context of the action being (or not) NEEDED-IN-IMAGE-P,
  ;; i.e. needs to be done in the current image vs merely have been done in a previous image.
  ;; For actions that are up-to-date, it returns a STAMP identifying the state of the action
  ;; (that's timestamp, but it could be a cryptographic digest in some ASDF extension),
  ;; or T if the action needs to be done again.
370 371 372 373 374
  ;;
  ;; Note that for an XCVB-like plan with one-image-per-file-outputting-action,
  ;; the below method would be insufficient, since it assumes a single image
  ;; to traverse each node at most twice; non-niip actions would be traversed only once,
  ;; but niip nodes could be traversed once per image, i.e. once plus once per non-niip action.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
375

376 377
  (defmethod traverse-action (plan operation component needed-in-image-p)
    (block nil
378 379
      ;; ACTION-VALID-P among other things, handles forcing logic, including FORCE-NOT,
      ;; and IF-FEATURE filtering.
380
      (unless (action-valid-p plan operation component) (return nil))
381
      ;; the following hook is needed by POIU, which tracks a full dependency graph,
382
      ;; instead of just a dependency order as in vanilla ASDF
383
      (plan-record-dependency plan operation component)
384
      ;; needed in image distinguishes b/w things that must happen in the
385 386 387
      ;; current image and those things that simply need to have been done in a previous one.
      (let* ((aniip (needed-in-image-p operation component)) ; action-specific needed-in-image
             ;; effective niip: meaningful for the action and required by the plan as traversed
388
             (eniip (and aniip needed-in-image-p))
389
             ;; status: have we traversed that action previously, and if so what was its status?
390 391
             (status (plan-action-status plan operation component)))
        (when (and status (or (action-done-p status) (action-planned-p status) (not eniip)))
392 393
          (return (action-stamp status))) ; Already visited with sufficient need-in-image level!
        (labels ((visit-action (niip) ; We may visit the action twice, once with niip NIL, then T
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
394 395
                   (map-direct-dependencies ; recursively traverse dependencies
                    plan operation component #'(lambda (o c) (traverse-action plan o c niip)))
396 397
                   (multiple-value-bind (stamp done-p) ; AFTER dependencies have been traversed,
                       (compute-action-stamp plan operation component) ; compute action stamp
398
                     (let ((add-to-plan-p (or (eql stamp t) (and niip (not done-p)))))
399 400 401
                       (cond ; it needs be done if it's out of date or needed in image but absent
                         ((and add-to-plan-p (not niip)) ; if we need to do it,
                          (visit-action t)) ; then we need to do it *in the (current) image*!
402
                         (t
403
                          (setf (plan-action-status plan operation component) ; update status:
404 405
                                (make-instance
                                 'planned-action-status
406 407 408 409 410 411
                                 :stamp stamp ; computed stamp
                                 :done-p (and done-p (not add-to-plan-p)) ; done *and* up-to-date?
                                 :planned-p add-to-plan-p ; included in list of things to be done?
                                 :index (if status ; index of action amongst all nodes in traversal
                                            (action-index status) ;; if already visited, keep index
                                            (incf (plan-total-action-count plan))))) ; else new index
412 413
                          (when (and done-p (not add-to-plan-p))
                            (setf (component-operation-time operation component) stamp))
414 415 416 417 418
                          (when add-to-plan-p ; if it needs to be added to the plan,
                            (incf (plan-planned-action-count plan)) ; count it
                            (unless aniip ; if it's output-producing,
                              (incf (plan-planned-output-action-count plan)))) ; count it
                          stamp)))))) ; return the stamp
419
          (while-visiting-action (plan operation component) ; maintain context, handle circularity.
420
            (visit-action eniip))))))) ; visit the action
421 422 423


;;;; Sequential plans (the default)
424 425
(with-upgradability ()
  (defclass sequential-plan (plan-traversal)
426 427
    ((actions-r :initform nil :accessor plan-actions-r))
    (:documentation "Simplest, default plan class, accumulating a sequence of actions"))
428

429 430
  (defmethod plan-actions ((plan sequential-plan))
    (reverse (plan-actions-r plan)))
431

432
  ;; No need to record a dependency to build a full graph, just accumulate nodes in order.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
433
  (defmethod plan-record-dependency ((plan sequential-plan) (o operation) (c component))
434
    (values))
435

436 437 438 439
  (defmethod (setf plan-action-status) :after
      (new-status (p sequential-plan) (o operation) (c component))
    (when (action-planned-p new-status)
      (push (cons o c) (plan-actions-r p)))))
440

441

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
442
;;;; High-level interface: traverse, perform-plan, plan-operates-on-p
443
(with-upgradability ()
444
  (defgeneric make-plan (plan-class operation component &key &allow-other-keys)
445
    (:documentation "Generate and return a plan for performing OPERATION on COMPONENT."))
446 447
  (define-convenience-action-methods make-plan (plan-class operation component &key))

448 449 450 451
  (defgeneric perform-plan (plan &key)
    (:documentation "Actually perform a plan and build the requested actions"))
  (defgeneric plan-operates-on-p (plan component)
    (:documentation "Does this PLAN include any operation on given COMPONENT?"))
452

453 454
  (defvar *default-plan-class* 'sequential-plan
    "The default plan class to use when building with ASDF")
455

456
  (defmethod make-plan (plan-class (o operation) (c component) &rest keys &key &allow-other-keys)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
457
    (let ((plan (apply 'make-instance (or plan-class *default-plan-class*)
458
                       :system (component-system c) keys)))
459
      (traverse-action plan o c t)
460
      plan))
461

462
  (defmethod perform-plan :around ((plan t) &key)
463
    #+xcl (declare (ignorable plan))
464 465 466 467
    (let ((*package* *package*)
          (*readtable* *readtable*))
      (with-compilation-unit () ;; backward-compatibility.
        (call-next-method))))   ;; Going forward, see deferred-warning support in lisp-build.
468

469 470 471
  (defmethod perform-plan ((plan t) &rest keys &key &allow-other-keys)
    (apply 'perform-plan (plan-actions plan) keys))

472 473 474 475
  (defmethod perform-plan ((steps list) &key force &allow-other-keys)
    (loop* :for (o . c) :in steps
           :when (or force (not (nth-value 1 (compute-action-stamp nil o c))))
           :do (perform-with-restarts o c)))
476

477 478 479
  (defmethod plan-operates-on-p ((plan plan-traversal) (component-path list))
    (plan-operates-on-p (plan-actions plan) component-path))

480 481 482
  (defmethod plan-operates-on-p ((plan list) (component-path list))
    (find component-path (mapcar 'cdr plan)
          :test 'equal :key 'component-find-path)))
483

484

485
;;;; Incidental traversals
486 487 488

;;; Making a FILTERED-SEQUENTIAL-PLAN can be used to, e.g., all of the source
;;; files required by a bundling operation.
489 490 491 492 493
(with-upgradability ()
  (defclass filtered-sequential-plan (sequential-plan)
    ((action-filter :initform t :initarg :action-filter :reader plan-action-filter)
     (component-type :initform t :initarg :component-type :reader plan-component-type)
     (keep-operation :initform t :initarg :keep-operation :reader plan-keep-operation)
494 495
     (keep-component :initform t :initarg :keep-component :reader plan-keep-component))
    (:documentation "A variant of SEQUENTIAL-PLAN that only records a subset of actions."))
496 497

  (defmethod initialize-instance :after ((plan filtered-sequential-plan)
498
                                         &key force force-not
499
                                         other-systems)
500
    (declare (ignore force force-not))
501 502 503
    ;; Ignore force and force-not, rely on other-systems:
    ;; force traversal of what we're interested in, i.e. current system or also others;
    ;; force-not traversal of what we're not interested in, i.e. other systems unless other-systems.
504
    (with-slots (forced forced-not action-filter system) plan
505 506
      (setf forced (normalize-forced-systems (if other-systems :all t) system))
      (setf forced-not (normalize-forced-not-systems (if other-systems nil t) system))
507 508 509 510 511 512 513 514
      (setf action-filter (ensure-function action-filter))))

  (defmethod action-valid-p ((plan filtered-sequential-plan) o c)
    (and (funcall (plan-action-filter plan) o c)
         (typep c (plan-component-type plan))
         (call-next-method)))

  (defmethod traverse-actions (actions &rest keys &key plan-class &allow-other-keys)
515
    "Given a list of actions, build a plan with these actions as roots."
516
    (let ((plan (apply 'make-instance (or plan-class 'filtered-sequential-plan) keys)))
517
      (loop* :for (o . c) :in actions :do (traverse-action plan o c t))
518
      plan))
519

520
  (define-convenience-action-methods traverse-sub-actions (operation component &key))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
521 522 523
  (defmethod traverse-sub-actions ((operation operation) (component component)
                                   &rest keys &key &allow-other-keys)
    (apply 'traverse-actions (direct-dependencies t operation component)
524 525 526 527 528
           :system (component-system component) keys))

  (defmethod plan-actions ((plan filtered-sequential-plan))
    (with-slots (keep-operation keep-component) plan
      (loop* :for (o . c) :in (call-next-method)
529
             :when (and (typep o keep-operation) (typep c keep-component))
530 531 532
             :collect (cons o c))))

  (defmethod required-components (system &rest keys &key (goal-operation 'load-op) &allow-other-keys)
533 534
    "Given a SYSTEM and a GOAL-OPERATION (default LOAD-OP), traverse the dependencies and
return a list of the components involved in building the desired action."
535
    (remove-duplicates
536 537 538
     (mapcar 'cdr (plan-actions
                   (apply 'traverse-sub-actions goal-operation system
                          (remove-plist-key :goal-operation keys))))
539
     :from-end t)))
540