0.8.12.40:
[sbcl.git] / contrib / asdf / asdf.lisp
1 ;;; This is asdf: Another System Definition Facility.  1.79
2 ;;;
3 ;;; Feedback, bug reports, and patches are all welcome: please mail to
4 ;;; <cclan-list@lists.sf.net>.  But note first that the canonical
5 ;;; source for asdf is presently the cCLan CVS repository at
6 ;;; <URL:http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/cclan/asdf/>
7 ;;;
8 ;;; If you obtained this copy from anywhere else, and you experience
9 ;;; trouble using it, or find bugs, you may want to check at the
10 ;;; location above for a more recent version (and for documentation
11 ;;; and test files, if your copy came without them) before reporting
12 ;;; bugs.  There are usually two "supported" revisions - the CVS HEAD
13 ;;; is the latest development version, whereas the revision tagged
14 ;;; RELEASE may be slightly older but is considered `stable'
15
16 ;;; Copyright (c) 2001-2003 Daniel Barlow and contributors
17 ;;;
18 ;;; Permission is hereby granted, free of charge, to any person obtaining
19 ;;; a copy of this software and associated documentation files (the
20 ;;; "Software"), to deal in the Software without restriction, including
21 ;;; without limitation the rights to use, copy, modify, merge, publish,
22 ;;; distribute, sublicense, and/or sell copies of the Software, and to
23 ;;; permit persons to whom the Software is furnished to do so, subject to
24 ;;; the following conditions:
25 ;;;
26 ;;; The above copyright notice and this permission notice shall be
27 ;;; included in all copies or substantial portions of the Software.
28 ;;;
29 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 ;;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 ;;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 ;;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
37 ;;; the problem with writing a defsystem replacement is bootstrapping:
38 ;;; we can't use defsystem to compile it.  Hence, all in one file
39
40 (defpackage #:asdf
41   (:export #:defsystem #:oos #:operate #:find-system #:run-shell-command
42            #:system-definition-pathname #:find-component ; miscellaneous
43            #:hyperdocumentation #:hyperdoc
44            
45            #:compile-op #:load-op #:load-source-op #:test-system-version
46            #:test-op
47            #:operation                  ; operations
48            #:feature                    ; sort-of operation
49            #:version                    ; metaphorically sort-of an operation
50            
51            #:output-files #:perform     ; operation methods
52            #:operation-done-p #:explain
53            
54            #:component #:source-file 
55            #:c-source-file #:cl-source-file #:java-source-file
56            #:static-file
57            #:doc-file
58            #:html-file
59            #:text-file
60            #:source-file-type
61            #:module                     ; components
62            #:system
63            #:unix-dso
64            
65            #:module-components          ; component accessors
66            #:component-pathname
67            #:component-relative-pathname
68            #:component-name
69            #:component-version
70            #:component-parent
71            #:component-property
72            
73            #:component-depends-on
74
75            #:system-description
76            #:system-long-description
77            #:system-author
78            #:system-maintainer
79            #:system-license
80            
81            #:operation-on-warnings
82            #:operation-on-failure
83            
84            ;#:*component-parent-pathname* 
85            #:*system-definition-search-functions*
86            #:*central-registry*         ; variables
87            #:*compile-file-warnings-behaviour*
88            #:*compile-file-failure-behaviour*
89            #:*asdf-revision*
90            
91            #:operation-error #:compile-failed #:compile-warned #:compile-error
92            #:error-component #:error-operation
93            #:system-definition-error 
94            #:missing-component
95            #:missing-dependency
96            #:circular-dependency        ; errors
97
98            #:retry
99            #:accept                     ; restarts
100            
101            )
102   (:use :cl))
103
104 #+nil
105 (error "The author of this file habitually uses #+nil to comment out forms.  But don't worry, it was unlikely to work in the New Implementation of Lisp anyway")
106
107
108 (in-package #:asdf)
109
110 (defvar *asdf-revision* (let* ((v "1.79")
111                                (colon (or (position #\: v) -1))
112                                (dot (position #\. v)))
113                           (and v colon dot 
114                                (list (parse-integer v :start (1+ colon)
115                                                     :junk-allowed t)
116                                      (parse-integer v :start (1+ dot)
117                                                     :junk-allowed t)))))
118
119 (defvar *compile-file-warnings-behaviour* :warn)
120 (defvar *compile-file-failure-behaviour* #+sbcl :error #-sbcl :warn)
121
122 (defvar *verbose-out* nil)
123
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; utility stuff
126
127 (defmacro aif (test then &optional else)
128   `(let ((it ,test)) (if it ,then ,else)))
129
130 (defun pathname-sans-name+type (pathname)
131   "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
132 and NIL NAME and TYPE components"
133   (make-pathname :name nil :type nil :defaults pathname))
134
135 (define-modify-macro appendf (&rest args) 
136                      append "Append onto list") 
137
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;; classes, condiitons
140
141 (define-condition system-definition-error (error) ()
142   ;; [this use of :report should be redundant, but unfortunately it's not.
143   ;; cmucl's lisp::output-instance prefers the kernel:slot-class-print-function
144   ;; over print-object; this is always conditions::%print-condition for
145   ;; condition objects, which in turn does inheritance of :report options at
146   ;; run-time.  fortunately, inheritance means we only need this kludge here in
147   ;; order to fix all conditions that build on it.  -- rgr, 28-Jul-02.]
148   #+cmu (:report print-object))
149
150 (define-condition formatted-system-definition-error (system-definition-error)
151   ((format-control :initarg :format-control :reader format-control)
152    (format-arguments :initarg :format-arguments :reader format-arguments))
153   (:report (lambda (c s)
154              (apply #'format s (format-control c) (format-arguments c)))))
155
156 (define-condition circular-dependency (system-definition-error)
157   ((components :initarg :components :reader circular-dependency-components)))
158
159 (define-condition missing-component (system-definition-error)
160   ((requires :initform "(unnamed)" :reader missing-requires :initarg :requires)
161    (version :initform nil :reader missing-version :initarg :version)
162    (parent :initform nil :reader missing-parent :initarg :parent)))
163
164 (define-condition missing-dependency (missing-component)
165   ((required-by :initarg :required-by :reader missing-required-by)))
166
167 (define-condition operation-error (error)
168   ((component :reader error-component :initarg :component)
169    (operation :reader error-operation :initarg :operation))
170   (:report (lambda (c s)
171              (format s (formatter "~@<erred while invoking ~A on ~A~@:>")
172                      (error-operation c) (error-component c)))))
173 (define-condition compile-error (operation-error) ())
174 (define-condition compile-failed (compile-error) ())
175 (define-condition compile-warned (compile-error) ())
176
177 (defclass component ()
178   ((name :type string :accessor component-name :initarg :name :documentation
179          "Component name, restricted to portable pathname characters")
180    (version :accessor component-version :initarg :version)
181    (in-order-to :initform nil :initarg :in-order-to)
182    ;;; XXX crap name
183    (do-first :initform nil :initarg :do-first)
184    ;; methods defined using the "inline" style inside a defsystem form:
185    ;; need to store them somewhere so we can delete them when the system
186    ;; is re-evaluated
187    (inline-methods :accessor component-inline-methods :initform nil)
188    (parent :initarg :parent :initform nil :reader component-parent)
189    ;; no direct accessor for pathname, we do this as a method to allow
190    ;; it to default in funky ways if not supplied
191    (relative-pathname :initarg :pathname)
192    (operation-times :initform (make-hash-table )
193                     :accessor component-operation-times)
194    ;; XXX we should provide some atomic interface for updating the
195    ;; component properties
196    (properties :accessor component-properties :initarg :properties
197                :initform nil)))
198
199 ;;;; methods: conditions
200
201 (defmethod print-object ((c missing-dependency) s)
202   (format s (formatter "~@<~A, required by ~A~@:>")
203           (call-next-method c nil)
204           (missing-required-by c)))
205
206 (defun sysdef-error (format &rest arguments)
207   (error 'formatted-system-definition-error :format-control format :format-arguments arguments))
208
209 ;;;; methods: components
210
211 (defmethod print-object ((c missing-component) s)
212   (format s (formatter "~@<component ~S not found~
213                         ~@[ or does not match version ~A~]~
214                         ~@[ in ~A~]~@:>")
215           (missing-requires c)
216           (missing-version c)
217           (when (missing-parent c)
218             (component-name (missing-parent c)))))
219
220 (defgeneric component-system (component)
221   (:documentation "Find the top-level system containing COMPONENT"))
222   
223 (defmethod component-system ((component component))
224   (aif (component-parent component)
225        (component-system it)
226        component))
227
228 (defmethod print-object ((c component) stream)
229   (print-unreadable-object (c stream :type t :identity t)
230     (ignore-errors
231       (prin1 (component-name c) stream))))
232
233 (defclass module (component)
234   ((components :initform nil :accessor module-components :initarg :components)
235    ;; what to do if we can't satisfy a dependency of one of this module's
236    ;; components.  This allows a limited form of conditional processing
237    (if-component-dep-fails :initform :fail
238                            :accessor module-if-component-dep-fails
239                            :initarg :if-component-dep-fails)
240    (default-component-class :accessor module-default-component-class
241      :initform 'cl-source-file :initarg :default-component-class)))
242
243 (defgeneric component-pathname (component)
244   (:documentation "Extracts the pathname applicable for a particular component."))
245
246 (defun component-parent-pathname (component)
247   (aif (component-parent component)
248        (component-pathname it)
249        *default-pathname-defaults*))
250
251 (defgeneric component-relative-pathname (component)
252   (:documentation "Extracts the relative pathname applicable for a particular component."))
253    
254 (defmethod component-relative-pathname ((component module))
255   (or (slot-value component 'relative-pathname)
256       (make-pathname
257        :directory `(:relative ,(component-name component))
258        :host (pathname-host (component-parent-pathname component)))))
259
260 (defmethod component-pathname ((component component))
261   (let ((*default-pathname-defaults* (component-parent-pathname component)))
262     (merge-pathnames (component-relative-pathname component))))
263
264 (defgeneric component-property (component property))
265
266 (defmethod component-property ((c component) property)
267   (cdr (assoc property (slot-value c 'properties) :test #'equal)))
268
269 (defgeneric (setf component-property) (new-value component property))
270
271 (defmethod (setf component-property) (new-value (c component) property)
272   (let ((a (assoc property (slot-value c 'properties) :test #'equal)))
273     (if a
274         (setf (cdr a) new-value)
275         (setf (slot-value c 'properties)
276               (acons property new-value (slot-value c 'properties))))))
277
278 (defclass system (module)
279   ((description :accessor system-description :initarg :description)
280    (long-description
281     :accessor system-long-description :initarg :long-description)
282    (author :accessor system-author :initarg :author)
283    (maintainer :accessor system-maintainer :initarg :maintainer)
284    (licence :accessor system-licence :initarg :licence)))
285
286 ;;; version-satisfies
287
288 ;;; with apologies to christophe rhodes ...
289 (defun split (string &optional max (ws '(#\Space #\Tab)))
290   (flet ((is-ws (char) (find char ws)))
291     (nreverse
292      (let ((list nil) (start 0) (words 0) end)
293        (loop
294         (when (and max (>= words (1- max)))
295           (return (cons (subseq string start) list)))
296         (setf end (position-if #'is-ws string :start start))
297         (push (subseq string start end) list)
298         (incf words)
299         (unless end (return list))
300         (setf start (1+ end)))))))
301
302 (defgeneric version-satisfies (component version))
303
304 (defmethod version-satisfies ((c component) version)
305   (unless (and version (slot-boundp c 'version))
306     (return-from version-satisfies t))
307   (let ((x (mapcar #'parse-integer
308                    (split (component-version c) nil '(#\.))))
309         (y (mapcar #'parse-integer
310                    (split version nil '(#\.)))))
311     (labels ((bigger (x y)
312                (cond ((not y) t)
313                      ((not x) nil)
314                      ((> (car x) (car y)) t)
315                      ((= (car x) (car y))
316                       (bigger (cdr x) (cdr y))))))
317       (and (= (car x) (car y))
318            (or (not (cdr y)) (bigger (cdr x) (cdr y)))))))
319
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321 ;;; finding systems
322
323 (defvar *defined-systems* (make-hash-table :test 'equal))
324 (defun coerce-name (name)
325    (typecase name
326      (component (component-name name))
327      (symbol (string-downcase (symbol-name name)))
328      (string name)
329      (t (sysdef-error (formatter "~@<invalid component designator ~A~@:>")
330                       name))))
331
332 ;;; for the sake of keeping things reasonably neat, we adopt a
333 ;;; convention that functions in this list are prefixed SYSDEF-
334
335 (defvar *system-definition-search-functions*
336   '(sysdef-central-registry-search))
337
338 (defun system-definition-pathname (system)
339   (some (lambda (x) (funcall x system))
340         *system-definition-search-functions*))
341         
342 (defvar *central-registry*
343   '(*default-pathname-defaults*
344     #+nil "/home/dan/src/sourceforge/cclan/asdf/systems/"
345     #+nil "telent:asdf;systems;"))
346
347 (defun sysdef-central-registry-search (system)
348   (let ((name (coerce-name system)))
349     (block nil
350       (dolist (dir *central-registry*)
351         (let* ((defaults (eval dir))
352                (file (and defaults
353                           (make-pathname
354                            :defaults defaults :version :newest
355                            :name name :type "asd" :case :local))))
356           (if (and file (probe-file file))
357               (return file)))))))
358
359
360 (defun find-system (name &optional (error-p t))
361   (let* ((name (coerce-name name))
362          (in-memory (gethash name *defined-systems*))
363          (on-disk (system-definition-pathname name)))    
364     (when (and on-disk
365                (or (not in-memory)
366                    (< (car in-memory) (file-write-date on-disk))))
367       (let ((*package* (make-package (gensym (package-name #.*package*))
368                                      :use '(:cl :asdf))))
369         (format *verbose-out*
370                 (formatter "~&~@<; ~@;loading system definition from ~A into ~A~@:>~%")
371                 ;; FIXME: This wants to be (ENOUGH-NAMESTRING
372                 ;; ON-DISK), but CMUCL barfs on that.
373                 on-disk
374                 *package*)
375         (load on-disk)))
376     (let ((in-memory (gethash name *defined-systems*)))
377       (if in-memory
378           (progn (if on-disk (setf (car in-memory) (file-write-date on-disk)))
379                  (cdr in-memory))
380           (if error-p (error 'missing-component :requires name))))))
381
382 (defun register-system (name system)
383   (format *verbose-out*
384           (formatter "~&~@<; ~@;registering ~A as ~A~@:>~%") system name)
385   (setf (gethash (coerce-name  name) *defined-systems*)
386         (cons (get-universal-time) system)))
387
388 (defun system-registered-p (name)
389   (gethash (coerce-name name) *defined-systems*))
390
391 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 ;;; finding components
393
394 (defgeneric find-component (module name &optional version)
395   (:documentation "Finds the component with name NAME present in the
396 MODULE module; if MODULE is nil, then the component is assumed to be a
397 system."))
398
399 (defmethod find-component ((module module) name &optional version)
400   (if (slot-boundp module 'components)
401       (let ((m (find name (module-components module)
402                      :test #'equal :key #'component-name)))
403         (if (and m (version-satisfies m version)) m))))
404             
405
406 ;;; a component with no parent is a system
407 (defmethod find-component ((module (eql nil)) name &optional version)
408   (let ((m (find-system name nil)))
409     (if (and m (version-satisfies m version)) m)))
410
411 ;;; component subclasses
412
413 (defclass source-file (component) ())
414
415 (defclass cl-source-file (source-file) ())
416 (defclass c-source-file (source-file) ())
417 (defclass java-source-file (source-file) ())
418 (defclass static-file (source-file) ())
419 (defclass doc-file (static-file) ())
420 (defclass html-file (doc-file) ())
421
422 (defgeneric source-file-type (component system))
423 (defmethod source-file-type ((c cl-source-file) (s module)) "lisp")
424 (defmethod source-file-type ((c c-source-file) (s module)) "c")
425 (defmethod source-file-type ((c java-source-file) (s module)) "java")
426 (defmethod source-file-type ((c html-file) (s module)) "html")
427 (defmethod source-file-type ((c static-file) (s module)) nil)
428
429 (defmethod component-relative-pathname ((component source-file))
430   (let* ((*default-pathname-defaults* (component-parent-pathname component))
431          (name-type
432           (make-pathname
433            :name (component-name component)
434            :type (source-file-type component
435                                    (component-system component)))))
436     (if (slot-value component 'relative-pathname)
437         (merge-pathnames
438          (slot-value component 'relative-pathname)
439          name-type)
440         name-type)))
441
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 ;;; operations
444
445 ;;; one of these is instantiated whenever (operate ) is called
446
447 (defclass operation ()
448   ((forced :initform nil :initarg :force :accessor operation-forced)
449    (original-initargs :initform nil :initarg :original-initargs
450                       :accessor operation-original-initargs)
451    (visited-nodes :initform nil :accessor operation-visited-nodes)
452    (visiting-nodes :initform nil :accessor operation-visiting-nodes)
453    (parent :initform nil :initarg :parent :accessor operation-parent)))
454
455 (defmethod print-object ((o operation) stream)
456   (print-unreadable-object (o stream :type t :identity t)
457     (ignore-errors
458       (prin1 (operation-original-initargs o) stream))))
459
460 (defmethod shared-initialize :after ((operation operation) slot-names
461                                      &key force 
462                                      &allow-other-keys)
463   (declare (ignore slot-names force))
464   ;; empty method to disable initarg validity checking
465   )
466
467 (defgeneric perform (operation component))
468 (defgeneric operation-done-p (operation component))
469 (defgeneric explain (operation component))
470 (defgeneric output-files (operation component))
471 (defgeneric input-files (operation component))
472
473 (defun node-for (o c)
474   (cons (class-name (class-of o)) c))
475
476 (defgeneric operation-ancestor (operation)
477   (:documentation   "Recursively chase the operation's parent pointer until we get to the head of the tree"))
478
479 (defmethod operation-ancestor ((operation operation))
480   (aif (operation-parent operation)
481        (operation-ancestor it)
482        operation))
483
484
485 (defun make-sub-operation (c o dep-c dep-o)
486   (let* ((args (copy-list (operation-original-initargs o)))
487          (force-p (getf args :force)))
488     ;; note explicit comparison with T: any other non-NIL force value
489     ;; (e.g. :recursive) will pass through
490     (cond ((and (null (component-parent c))
491                 (null (component-parent dep-c))
492                 (not (eql c dep-c)))
493            (when (eql force-p t)
494              (setf (getf args :force) nil))
495            (apply #'make-instance dep-o
496                   :parent o
497                   :original-initargs args args))
498           ((subtypep (type-of o) dep-o)
499            o)
500           (t 
501            (apply #'make-instance dep-o
502                   :parent o :original-initargs args args)))))
503
504
505 (defgeneric visit-component (operation component data))
506
507 (defmethod visit-component ((o operation) (c component) data)
508   (unless (component-visited-p o c)
509     (push (cons (node-for o c) data)
510           (operation-visited-nodes (operation-ancestor o)))))
511
512 (defgeneric component-visited-p (operation component))
513
514 (defmethod component-visited-p ((o operation) (c component))
515   (assoc (node-for o c)
516          (operation-visited-nodes (operation-ancestor o))
517          :test 'equal))
518
519 (defgeneric (setf visiting-component) (new-value operation component))
520
521 (defmethod (setf visiting-component) (new-value operation component)
522   ;; MCL complains about unused lexical variables
523   (declare (ignorable new-value operation component)))
524
525 (defmethod (setf visiting-component) (new-value (o operation) (c component))
526   (let ((node (node-for o c))
527         (a (operation-ancestor o)))
528     (if new-value
529         (pushnew node (operation-visiting-nodes a) :test 'equal)
530         (setf (operation-visiting-nodes a)
531               (remove node  (operation-visiting-nodes a) :test 'equal)))))
532
533 (defgeneric component-visiting-p (operation component))
534
535 (defmethod component-visiting-p ((o operation) (c component))
536   (let ((node (cons o c)))
537     (member node (operation-visiting-nodes (operation-ancestor o))
538             :test 'equal)))
539
540 (defgeneric component-depends-on (operation component))
541
542 (defmethod component-depends-on ((o operation) (c component))
543   (cdr (assoc (class-name (class-of o))
544               (slot-value c 'in-order-to))))
545
546 (defgeneric component-self-dependencies (operation component))
547
548 (defmethod component-self-dependencies ((o operation) (c component))
549   (let ((all-deps (component-depends-on o c)))
550     (remove-if-not (lambda (x)
551                      (member (component-name c) (cdr x) :test #'string=))
552                    all-deps)))
553     
554 (defmethod input-files ((operation operation) (c component))
555   (let ((parent (component-parent c))
556         (self-deps (component-self-dependencies operation c)))
557     (if self-deps
558         (mapcan (lambda (dep)
559                   (destructuring-bind (op name) dep
560                     (output-files (make-instance op)
561                                   (find-component parent name))))
562                 self-deps)
563         ;; no previous operations needed?  I guess we work with the 
564         ;; original source file, then
565         (list (component-pathname c)))))
566
567 (defmethod input-files ((operation operation) (c module)) nil)
568
569 (defmethod operation-done-p ((o operation) (c component))
570   (let ((out-files (output-files o c))
571         (in-files (input-files o c)))
572     (cond ((and (not in-files) (not out-files))
573            ;; arbitrary decision: an operation that uses nothing to
574            ;; produce nothing probably isn't doing much 
575            t)
576           ((not out-files) 
577            (let ((op-done
578                   (gethash (type-of o)
579                            (component-operation-times c))))
580              (and op-done
581                   (>= op-done
582                       (or (apply #'max
583                                  (mapcar #'file-write-date in-files)) 0)))))
584           ((not in-files) nil)
585           (t
586            (and
587             (every #'probe-file out-files)
588             (> (apply #'min (mapcar #'file-write-date out-files))
589                (apply #'max (mapcar #'file-write-date in-files)) ))))))
590
591 ;;; So you look at this code and think "why isn't it a bunch of
592 ;;; methods".  And the answer is, because standard method combination
593 ;;; runs :before methods most->least-specific, which is back to front
594 ;;; for our purposes.  And CLISP doesn't have non-standard method
595 ;;; combinations, so let's keep it simple and aspire to portability
596
597 (defgeneric traverse (operation component))
598 (defmethod traverse ((operation operation) (c component))
599   (let ((forced nil))
600     (labels ((do-one-dep (required-op required-c required-v)
601                (let* ((dep-c (or (find-component
602                                   (component-parent c)
603                                   ;; XXX tacky.  really we should build the
604                                   ;; in-order-to slot with canonicalized
605                                   ;; names instead of coercing this late
606                                   (coerce-name required-c) required-v)
607                                  (error 'missing-dependency :required-by c
608                                         :version required-v
609                                         :requires required-c)))
610                       (op (make-sub-operation c operation dep-c required-op)))
611                  (traverse op dep-c)))             
612              (do-dep (op dep)
613                (cond ((eq op 'feature)
614                       (or (member (car dep) *features*)
615                           (error 'missing-dependency :required-by c
616                                  :requires (car dep) :version nil)))
617                      (t
618                       (dolist (d dep)
619                         (cond ((consp d)
620                                (assert (string-equal
621                                         (symbol-name (first d))
622                                         "VERSION"))
623                                (appendf forced
624                                         (do-one-dep op (second d) (third d))))
625                               (t
626                                (appendf forced (do-one-dep op d nil)))))))))
627       (aif (component-visited-p operation c)
628            (return-from traverse
629              (if (cdr it) (list (cons 'pruned-op c)) nil)))
630       ;; dependencies
631       (if (component-visiting-p operation c)
632           (error 'circular-dependency :components (list c)))
633       (setf (visiting-component operation c) t)
634       (loop for (required-op . deps) in (component-depends-on operation c)
635             do (do-dep required-op deps))
636       ;; constituent bits
637       (let ((module-ops
638              (when (typep c 'module)
639                (let ((at-least-one nil)
640                      (forced nil)
641                      (error nil))
642                  (loop for kid in (module-components c)
643                        do (handler-case
644                               (appendf forced (traverse operation kid ))
645                             (missing-dependency (condition)
646                               (if (eq (module-if-component-dep-fails c) :fail)
647                                   (error condition))
648                               (setf error condition))
649                             (:no-error (c)
650                               (declare (ignore c))
651                               (setf at-least-one t))))
652                  (when (and (eq (module-if-component-dep-fails c) :try-next)
653                             (not at-least-one))
654                    (error error))
655                  forced))))
656         ;; now the thing itself
657         (when (or forced module-ops
658                   (not (operation-done-p operation c))
659                   (let ((f (operation-forced (operation-ancestor operation))))
660                     (and f (or (not (consp f))
661                                (member (component-name
662                                         (operation-ancestor operation))
663                                        (mapcar #'coerce-name f)
664                                        :test #'string=)))))
665           (let ((do-first (cdr (assoc (class-name (class-of operation))
666                                       (slot-value c 'do-first)))))
667             (loop for (required-op . deps) in do-first
668                   do (do-dep required-op deps)))
669           (setf forced (append (delete 'pruned-op forced :key #'car)
670                                (delete 'pruned-op module-ops :key #'car)
671                                (list (cons operation c))))))
672       (setf (visiting-component operation c) nil)
673       (visit-component operation c (and forced t))
674       forced)))
675   
676
677 (defmethod perform ((operation operation) (c source-file))
678   (sysdef-error
679    (formatter "~@<required method PERFORM not implemented~
680                for operation ~A, component ~A~@:>")
681    (class-of operation) (class-of c)))
682
683 (defmethod perform ((operation operation) (c module))
684   nil)
685
686 (defmethod explain ((operation operation) (component component))
687   (format *verbose-out* "~&;;; ~A on ~A~%"
688           operation component))
689
690 ;;; compile-op
691
692 (defclass compile-op (operation)
693   ((proclamations :initarg :proclamations :accessor compile-op-proclamations :initform nil)
694    (on-warnings :initarg :on-warnings :accessor operation-on-warnings
695                 :initform *compile-file-warnings-behaviour*)
696    (on-failure :initarg :on-failure :accessor operation-on-failure
697                :initform *compile-file-failure-behaviour*)))
698
699 (defmethod perform :before ((operation compile-op) (c source-file))
700   (map nil #'ensure-directories-exist (output-files operation c)))
701
702 (defmethod perform :after ((operation operation) (c component))
703   (setf (gethash (type-of operation) (component-operation-times c))
704         (get-universal-time)))
705
706 ;;; perform is required to check output-files to find out where to put
707 ;;; its answers, in case it has been overridden for site policy
708 (defmethod perform ((operation compile-op) (c cl-source-file))
709   (let ((source-file (component-pathname c))
710         (output-file (car (output-files operation c))))
711     (multiple-value-bind (output warnings-p failure-p)
712         (compile-file source-file
713                       :output-file output-file)
714       ;(declare (ignore output))
715       (when warnings-p
716         (case (operation-on-warnings operation)
717           (:warn (warn
718                   (formatter "~@<COMPILE-FILE warned while ~
719                               performing ~A on ~A.~@:>")
720                   operation c))
721           (:error (error 'compile-warned :component c :operation operation))
722           (:ignore nil)))
723       (when failure-p
724         (case (operation-on-failure operation)
725           (:warn (warn
726                   (formatter "~@<COMPILE-FILE failed while ~
727                               performing ~A on ~A.~@:>")
728                   operation c))
729           (:error (error 'compile-failed :component c :operation operation))
730           (:ignore nil)))
731       (unless output
732         (error 'compile-error :component c :operation operation)))))
733
734 (defmethod output-files ((operation compile-op) (c cl-source-file))
735   (list (compile-file-pathname (component-pathname c))))
736
737 (defmethod perform ((operation compile-op) (c static-file))
738   nil)
739
740 (defmethod output-files ((operation compile-op) (c static-file))
741   nil)
742
743 ;;; load-op
744
745 (defclass load-op (operation) ())
746
747 (defmethod perform ((o load-op) (c cl-source-file))
748   (mapcar #'load (input-files o c)))
749
750 (defmethod perform ((operation load-op) (c static-file))
751   nil)
752 (defmethod operation-done-p ((operation load-op) (c static-file))
753   t)
754
755 (defmethod output-files ((o operation) (c component))
756   nil)
757
758 (defmethod component-depends-on ((operation load-op) (c component))
759   (cons (list 'compile-op (component-name c))
760         (call-next-method)))
761
762 ;;; load-source-op
763
764 (defclass load-source-op (operation) ())
765
766 (defmethod perform ((o load-source-op) (c cl-source-file))
767   (let ((source (component-pathname c)))
768     (setf (component-property c 'last-loaded-as-source)
769           (and (load source)
770                (get-universal-time)))))
771
772 (defmethod perform ((operation load-source-op) (c static-file))
773   nil)
774
775 (defmethod output-files ((operation load-source-op) (c component))
776   nil)
777
778 ;;; FIXME: we simply copy load-op's dependencies.  this is Just Not Right.
779 (defmethod component-depends-on ((o load-source-op) (c component))
780   (let ((what-would-load-op-do (cdr (assoc 'load-op
781                                            (slot-value c 'in-order-to)))))
782     (mapcar (lambda (dep)
783               (if (eq (car dep) 'load-op)
784                   (cons 'load-source-op (cdr dep))
785                   dep))
786             what-would-load-op-do)))
787
788 (defmethod operation-done-p ((o load-source-op) (c source-file))
789   (if (or (not (component-property c 'last-loaded-as-source))
790           (> (file-write-date (component-pathname c))
791              (component-property c 'last-loaded-as-source)))
792       nil t))
793
794 (defclass test-op (operation) ())
795
796 (defmethod perform ((operation test-op) (c component))
797   nil)
798
799 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
800 ;;; invoking operations
801
802 (defun operate (operation-class system &rest args)
803   (let* ((op (apply #'make-instance operation-class
804                     :original-initargs args args))
805          (*verbose-out*
806           (if (getf args :verbose t)
807               *trace-output*
808               (make-broadcast-stream)))
809          (system (if (typep system 'component) system (find-system system)))
810          (steps (traverse op system)))
811     (with-compilation-unit ()
812       (loop for (op . component) in steps do
813             (loop
814              (restart-case 
815                  (progn (perform op component)
816                         (return))
817                (retry ()
818                  :report
819                  (lambda (s)
820                    (format s
821                            (formatter "~@<Retry performing ~S on ~S.~@:>")
822                            op component)))
823                (accept ()
824                  :report
825                  (lambda (s)
826                    (format s
827                            (formatter "~@<Continue, treating ~S on ~S as ~
828                                        having been successful.~@:>")
829                            op component))
830                  (setf (gethash (type-of op)
831                                 (component-operation-times component))
832                        (get-universal-time))
833                  (return))))))))
834
835 (defun oos (&rest args)
836   "Alias of OPERATE function"
837   (apply #'operate args))
838
839 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
840 ;;; syntax
841
842 (defun remove-keyword (key arglist)
843   (labels ((aux (key arglist)
844              (cond ((null arglist) nil)
845                    ((eq key (car arglist)) (cddr arglist))
846                    (t (cons (car arglist) (cons (cadr arglist)
847                                                 (remove-keyword
848                                                  key (cddr arglist))))))))
849     (aux key arglist)))
850
851 (defmacro defsystem (name &body options)
852   (destructuring-bind (&key pathname (class 'system) &allow-other-keys) options
853     (let ((component-options (remove-keyword :class options)))
854       `(progn
855         ;; system must be registered before we parse the body, otherwise
856         ;; we recur when trying to find an existing system of the same name
857         ;; to reuse options (e.g. pathname) from
858         (let ((s (system-registered-p ',name)))
859           (cond ((and s (eq (type-of (cdr s)) ',class))
860                  (setf (car s) (get-universal-time)))
861                 (s
862                  #+clisp
863                  (sysdef-error "Cannot redefine the existing system ~A with a different class" s)
864                  #-clisp
865                  (change-class (cdr s) ',class))
866                 (t
867                  (register-system (quote ,name)
868                                   (make-instance ',class :name ',name)))))
869         (parse-component-form nil (apply
870                                    #'list
871                                    :module (coerce-name ',name)
872                                    :pathname
873                                    (or ,pathname
874                                        (pathname-sans-name+type
875                                         (resolve-symlinks  *load-truename*))
876                                        *default-pathname-defaults*)
877                                    ',component-options))))))
878   
879
880 (defun class-for-type (parent type)
881   (let ((class (find-class
882                 (or (find-symbol (symbol-name type) *package*)
883                     (find-symbol (symbol-name type) #.*package*)) nil)))
884     (or class
885         (and (eq type :file)
886              (or (module-default-component-class parent)
887                  (find-class 'cl-source-file)))
888         (sysdef-error (formatter "~@<don't recognize component type ~A~@:>")
889                       type))))
890
891 (defun maybe-add-tree (tree op1 op2 c)
892   "Add the node C at /OP1/OP2 in TREE, unless it's there already.
893 Returns the new tree (which probably shares structure with the old one)"
894   (let ((first-op-tree (assoc op1 tree)))
895     (if first-op-tree
896         (progn
897           (aif (assoc op2 (cdr first-op-tree))
898                (if (find c (cdr it))
899                    nil
900                    (setf (cdr it) (cons c (cdr it))))
901                (setf (cdr first-op-tree)
902                      (acons op2 (list c) (cdr first-op-tree))))
903           tree)
904         (acons op1 (list (list op2 c)) tree))))
905                 
906 (defun union-of-dependencies (&rest deps)
907   (let ((new-tree nil))
908     (dolist (dep deps)
909       (dolist (op-tree dep)
910         (dolist (op  (cdr op-tree))
911           (dolist (c (cdr op))
912             (setf new-tree
913                   (maybe-add-tree new-tree (car op-tree) (car op) c))))))
914     new-tree))
915
916
917 (defun remove-keys (key-names args)
918   (loop for ( name val ) on args by #'cddr
919         unless (member (symbol-name name) key-names 
920                        :key #'symbol-name :test 'equal)
921         append (list name val)))
922
923 (defvar *serial-depends-on*)
924
925 (defun parse-component-form (parent options)
926   (destructuring-bind
927         (type name &rest rest &key
928               ;; the following list of keywords is reproduced below in the
929               ;; remove-keys form.  important to keep them in sync
930               components pathname default-component-class
931               perform explain output-files operation-done-p
932               depends-on serial in-order-to
933               ;; list ends
934               &allow-other-keys) options
935     (check-component-input type name depends-on components in-order-to)
936     (let* ((other-args (remove-keys
937                         '(components pathname default-component-class
938                           perform explain output-files operation-done-p
939                           depends-on serial in-order-to)
940                         rest))
941            (ret
942             (or (find-component parent name)
943                 (make-instance (class-for-type parent type)))))
944       (when (boundp '*serial-depends-on*)
945         (setf depends-on
946               (concatenate 'list *serial-depends-on* depends-on)))
947       (apply #'reinitialize-instance
948              ret
949              :name (coerce-name name)
950              :pathname pathname
951              :parent parent
952              other-args)
953       (when (typep ret 'module)
954         (setf (module-default-component-class ret)
955               (or default-component-class
956                   (and (typep parent 'module)
957                        (module-default-component-class parent))))
958         (let ((*serial-depends-on* nil))
959           (setf (module-components ret)
960                 (loop for c-form in components
961                       for c = (parse-component-form ret c-form)
962                       collect c
963                       if serial
964                       do (push (component-name c) *serial-depends-on*)))))
965       
966       (setf (slot-value ret 'in-order-to)
967             (union-of-dependencies
968              in-order-to
969              `((compile-op (compile-op ,@depends-on))
970                (load-op (load-op ,@depends-on))))
971             (slot-value ret 'do-first) `((compile-op (load-op ,@depends-on))))
972       
973       (loop for (n v) in `((perform ,perform) (explain ,explain)
974                            (output-files ,output-files)
975                            (operation-done-p ,operation-done-p))
976             do (map 'nil
977                     ;; this is inefficient as most of the stored
978                     ;; methods will not be for this particular gf n
979                     ;; But this is hardly performance-critical
980                     (lambda (m) (remove-method (symbol-function n) m))
981                     (component-inline-methods ret))
982             when v
983             do (destructuring-bind (op qual (o c) &body body) v
984                  (pushnew
985                   (eval `(defmethod ,n ,qual ((,o ,op) (,c (eql ,ret)))
986                           ,@body))
987                   (component-inline-methods ret))))
988       ret)))
989
990 (defun check-component-input (type name depends-on components in-order-to)
991   "A partial test of the values of a component."
992   (unless (listp depends-on)
993     (sysdef-error-component ":depends-on must be a list."
994                             type name depends-on))
995   (unless (listp components)
996     (sysdef-error-component ":components must be NIL or a list of components."
997                             type name components))
998   (unless (and (listp in-order-to) (listp (car in-order-to)))
999     (sysdef-error-component ":in-order-to must be NIL or a list of components."
1000                            type name in-order-to)))
1001
1002 (defun sysdef-error-component (msg type name value)
1003   (sysdef-error (concatenate 'string msg
1004                              "~&The value specified for ~(~A~) ~A is ~W")
1005                 type name value))
1006
1007 (defun resolve-symlinks (path)
1008   #-allegro (truename path)
1009   #+allegro (excl:pathname-resolve-symbolic-links path)
1010   )
1011
1012 ;;; optional extras
1013
1014 ;;; run-shell-command functions for other lisp implementations will be
1015 ;;; gratefully accepted, if they do the same thing.  If the docstring
1016 ;;; is ambiguous, send a bug report
1017
1018 (defun run-shell-command (control-string &rest args)
1019   "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
1020 synchronously execute the result using a Bourne-compatible shell, with
1021 output to *verbose-out*.  Returns the shell's exit code."
1022   (let ((command (apply #'format nil control-string args)))
1023     (format *verbose-out* "; $ ~A~%" command)
1024     #+sbcl
1025     (sb-impl::process-exit-code
1026      (sb-ext:run-program  
1027       "/bin/sh"
1028       (list  "-c" command)
1029       :input nil :output *verbose-out*))
1030     
1031     #+(or cmu scl)
1032     (ext:process-exit-code
1033      (ext:run-program  
1034       "/bin/sh"
1035       (list  "-c" command)
1036       :input nil :output *verbose-out*))
1037
1038     #+allegro
1039     (excl:run-shell-command command :input nil :output *verbose-out*)
1040     
1041     #+lispworks
1042     (system:call-system-showing-output
1043      command
1044      :shell-type "/bin/sh"
1045      :output-stream *verbose-out*)
1046     
1047     #+clisp                             ;XXX not exactly *verbose-out*, I know
1048     (ext:run-shell-command  command :output :terminal :wait t)
1049
1050     #+openmcl
1051     (nth-value 1
1052                (ccl:external-process-status
1053                 (ccl:run-program "/bin/sh" (list "-c" command)
1054                                  :input nil :output *verbose-out*
1055                                  :wait t)))
1056
1057     #-(or openmcl clisp lispworks allegro scl cmu sbcl)
1058     (error "RUN-SHELL-PROGRAM not implemented for this Lisp")
1059     ))
1060
1061
1062 (defgeneric hyperdocumentation (package name doc-type))
1063 (defmethod hyperdocumentation ((package symbol) name doc-type)
1064   (hyperdocumentation (find-package package) name doc-type))
1065
1066 (defun hyperdoc (name doc-type)
1067   (hyperdocumentation (symbol-package name) name doc-type))
1068
1069
1070 (pushnew :asdf *features*)
1071
1072 #+sbcl
1073 (eval-when (:compile-toplevel :load-toplevel :execute)
1074   (when (sb-ext:posix-getenv "SBCL_BUILDING_CONTRIB")
1075     (pushnew :sbcl-hooks-require *features*)))
1076
1077 #+(and sbcl sbcl-hooks-require)
1078 (progn
1079   (defun module-provide-asdf (name)
1080     (handler-bind ((style-warning #'muffle-warning))
1081       (let* ((*verbose-out* (make-broadcast-stream))
1082              (system (asdf:find-system name nil)))
1083         (when system
1084           (asdf:operate 'asdf:load-op name)
1085           t))))
1086
1087   (pushnew
1088    '(merge-pathnames "systems/"
1089      (truename (sb-ext:posix-getenv "SBCL_HOME")))
1090    *central-registry*)
1091   
1092   (pushnew
1093    '(merge-pathnames "site-systems/"
1094      (truename (sb-ext:posix-getenv "SBCL_HOME")))
1095    *central-registry*)
1096   
1097   (pushnew
1098    '(merge-pathnames ".sbcl/systems/"
1099      (user-homedir-pathname))
1100    *central-registry*)
1101   
1102   (pushnew 'module-provide-asdf sb-ext:*module-provider-functions*))
1103
1104 (provide 'asdf)