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