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