0.9.11.42:
[sbcl.git] / contrib / asdf / asdf.lisp
1 ;;; This is asdf: Another System Definition Facility.  1.94
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.94")
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         (merge-pathnames
446          relative-pathname
447          (make-pathname
448           :type (source-file-type component (component-system component))))
449         (let* ((*default-pathname-defaults*
450                 (component-parent-pathname component))
451                (name-type
452                 (make-pathname
453                  :name (component-name component)
454                  :type (source-file-type component
455                                          (component-system component)))))
456           name-type))))
457
458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459 ;;; operations
460
461 ;;; one of these is instantiated whenever (operate ) is called
462
463 (defclass operation ()
464   ((forced :initform nil :initarg :force :accessor operation-forced)
465    (original-initargs :initform nil :initarg :original-initargs
466                       :accessor operation-original-initargs)
467    (visited-nodes :initform nil :accessor operation-visited-nodes)
468    (visiting-nodes :initform nil :accessor operation-visiting-nodes)
469    (parent :initform nil :initarg :parent :accessor operation-parent)))
470
471 (defmethod print-object ((o operation) stream)
472   (print-unreadable-object (o stream :type t :identity t)
473     (ignore-errors
474       (prin1 (operation-original-initargs o) stream))))
475
476 (defmethod shared-initialize :after ((operation operation) slot-names
477                                      &key force
478                                      &allow-other-keys)
479   (declare (ignore slot-names force))
480   ;; empty method to disable initarg validity checking
481   )
482
483 (defgeneric perform (operation component))
484 (defgeneric operation-done-p (operation component))
485 (defgeneric explain (operation component))
486 (defgeneric output-files (operation component))
487 (defgeneric input-files (operation component))
488
489 (defun node-for (o c)
490   (cons (class-name (class-of o)) c))
491
492 (defgeneric operation-ancestor (operation)
493   (:documentation   "Recursively chase the operation's parent pointer until we get to the head of the tree"))
494
495 (defmethod operation-ancestor ((operation operation))
496   (aif (operation-parent operation)
497        (operation-ancestor it)
498        operation))
499
500
501 (defun make-sub-operation (c o dep-c dep-o)
502   (let* ((args (copy-list (operation-original-initargs o)))
503          (force-p (getf args :force)))
504     ;; note explicit comparison with T: any other non-NIL force value
505     ;; (e.g. :recursive) will pass through
506     (cond ((and (null (component-parent c))
507                 (null (component-parent dep-c))
508                 (not (eql c dep-c)))
509            (when (eql force-p t)
510              (setf (getf args :force) nil))
511            (apply #'make-instance dep-o
512                   :parent o
513                   :original-initargs args args))
514           ((subtypep (type-of o) dep-o)
515            o)
516           (t
517            (apply #'make-instance dep-o
518                   :parent o :original-initargs args args)))))
519
520
521 (defgeneric visit-component (operation component data))
522
523 (defmethod visit-component ((o operation) (c component) data)
524   (unless (component-visited-p o c)
525     (push (cons (node-for o c) data)
526           (operation-visited-nodes (operation-ancestor o)))))
527
528 (defgeneric component-visited-p (operation component))
529
530 (defmethod component-visited-p ((o operation) (c component))
531   (assoc (node-for o c)
532          (operation-visited-nodes (operation-ancestor o))
533          :test 'equal))
534
535 (defgeneric (setf visiting-component) (new-value operation component))
536
537 (defmethod (setf visiting-component) (new-value operation component)
538   ;; MCL complains about unused lexical variables
539   (declare (ignorable new-value operation component)))
540
541 (defmethod (setf visiting-component) (new-value (o operation) (c component))
542   (let ((node (node-for o c))
543         (a (operation-ancestor o)))
544     (if new-value
545         (pushnew node (operation-visiting-nodes a) :test 'equal)
546         (setf (operation-visiting-nodes a)
547               (remove node  (operation-visiting-nodes a) :test 'equal)))))
548
549 (defgeneric component-visiting-p (operation component))
550
551 (defmethod component-visiting-p ((o operation) (c component))
552   (let ((node (cons o c)))
553     (member node (operation-visiting-nodes (operation-ancestor o))
554             :test 'equal)))
555
556 (defgeneric component-depends-on (operation component))
557
558 (defmethod component-depends-on ((o operation) (c component))
559   (cdr (assoc (class-name (class-of o))
560               (slot-value c 'in-order-to))))
561
562 (defgeneric component-self-dependencies (operation component))
563
564 (defmethod component-self-dependencies ((o operation) (c component))
565   (let ((all-deps (component-depends-on o c)))
566     (remove-if-not (lambda (x)
567                      (member (component-name c) (cdr x) :test #'string=))
568                    all-deps)))
569
570 (defmethod input-files ((operation operation) (c component))
571   (let ((parent (component-parent c))
572         (self-deps (component-self-dependencies operation c)))
573     (if self-deps
574         (mapcan (lambda (dep)
575                   (destructuring-bind (op name) dep
576                     (output-files (make-instance op)
577                                   (find-component parent name))))
578                 self-deps)
579         ;; no previous operations needed?  I guess we work with the
580         ;; original source file, then
581         (list (component-pathname c)))))
582
583 (defmethod input-files ((operation operation) (c module)) nil)
584
585 (defmethod operation-done-p ((o operation) (c component))
586   (let ((out-files (output-files o c))
587         (in-files (input-files o c)))
588     (cond ((and (not in-files) (not out-files))
589            ;; arbitrary decision: an operation that uses nothing to
590            ;; produce nothing probably isn't doing much
591            t)
592           ((not out-files)
593            (let ((op-done
594                   (gethash (type-of o)
595                            (component-operation-times c))))
596              (and op-done
597                   (>= op-done
598                       (or (apply #'max
599                                  (mapcar #'file-write-date in-files)) 0)))))
600           ((not in-files) nil)
601           (t
602            (and
603             (every #'probe-file out-files)
604             (> (apply #'min (mapcar #'file-write-date out-files))
605                (apply #'max (mapcar #'file-write-date in-files)) ))))))
606
607 ;;; So you look at this code and think "why isn't it a bunch of
608 ;;; methods".  And the answer is, because standard method combination
609 ;;; runs :before methods most->least-specific, which is back to front
610 ;;; for our purposes.  And CLISP doesn't have non-standard method
611 ;;; combinations, so let's keep it simple and aspire to portability
612
613 (defgeneric traverse (operation component))
614 (defmethod traverse ((operation operation) (c component))
615   (let ((forced nil))
616     (labels ((do-one-dep (required-op required-c required-v)
617                (let* ((dep-c (or (find-component
618                                   (component-parent c)
619                                   ;; XXX tacky.  really we should build the
620                                   ;; in-order-to slot with canonicalized
621                                   ;; names instead of coercing this late
622                                   (coerce-name required-c) required-v)
623                                  (error 'missing-dependency :required-by c
624                                         :version required-v
625                                         :requires required-c)))
626                       (op (make-sub-operation c operation dep-c required-op)))
627                  (traverse op dep-c)))
628              (do-dep (op dep)
629                (cond ((eq op 'feature)
630                       (or (member (car dep) *features*)
631                           (error 'missing-dependency :required-by c
632                                  :requires (car dep) :version nil)))
633                      (t
634                       (dolist (d dep)
635                         (cond ((consp d)
636                                (assert (string-equal
637                                         (symbol-name (first d))
638                                         "VERSION"))
639                                (appendf forced
640                                         (do-one-dep op (second d) (third d))))
641                               (t
642                                (appendf forced (do-one-dep op d nil)))))))))
643       (aif (component-visited-p operation c)
644            (return-from traverse
645              (if (cdr it) (list (cons 'pruned-op c)) nil)))
646       ;; dependencies
647       (if (component-visiting-p operation c)
648           (error 'circular-dependency :components (list c)))
649       (setf (visiting-component operation c) t)
650       (loop for (required-op . deps) in (component-depends-on operation c)
651             do (do-dep required-op deps))
652       ;; constituent bits
653       (let ((module-ops
654              (when (typep c 'module)
655                (let ((at-least-one nil)
656                      (forced nil)
657                      (error nil))
658                  (loop for kid in (module-components c)
659                        do (handler-case
660                               (appendf forced (traverse operation kid ))
661                             (missing-dependency (condition)
662                               (if (eq (module-if-component-dep-fails c) :fail)
663                                   (error condition))
664                               (setf error condition))
665                             (:no-error (c)
666                               (declare (ignore c))
667                               (setf at-least-one t))))
668                  (when (and (eq (module-if-component-dep-fails c) :try-next)
669                             (not at-least-one))
670                    (error error))
671                  forced))))
672         ;; now the thing itself
673         (when (or forced module-ops
674                   (not (operation-done-p operation c))
675                   (let ((f (operation-forced (operation-ancestor operation))))
676                     (and f (or (not (consp f))
677                                (member (component-name
678                                         (operation-ancestor operation))
679                                        (mapcar #'coerce-name f)
680                                        :test #'string=)))))
681           (let ((do-first (cdr (assoc (class-name (class-of operation))
682                                       (slot-value c 'do-first)))))
683             (loop for (required-op . deps) in do-first
684                   do (do-dep required-op deps)))
685           (setf forced (append (delete 'pruned-op forced :key #'car)
686                                (delete 'pruned-op module-ops :key #'car)
687                                (list (cons operation c))))))
688       (setf (visiting-component operation c) nil)
689       (visit-component operation c (and forced t))
690       forced)))
691
692
693 (defmethod perform ((operation operation) (c source-file))
694   (sysdef-error
695    "~@<required method PERFORM not implemented ~
696     for operation ~A, component ~A~@:>"
697    (class-of operation) (class-of c)))
698
699 (defmethod perform ((operation operation) (c module))
700   nil)
701
702 (defmethod explain ((operation operation) (component component))
703   (format *verbose-out* "~&;;; ~A on ~A~%" operation component))
704
705 ;;; compile-op
706
707 (defclass compile-op (operation)
708   ((proclamations :initarg :proclamations :accessor compile-op-proclamations :initform nil)
709    (on-warnings :initarg :on-warnings :accessor operation-on-warnings
710                 :initform *compile-file-warnings-behaviour*)
711    (on-failure :initarg :on-failure :accessor operation-on-failure
712                :initform *compile-file-failure-behaviour*)))
713
714 (defmethod perform :before ((operation compile-op) (c source-file))
715   (map nil #'ensure-directories-exist (output-files operation c)))
716
717 (defmethod perform :after ((operation operation) (c component))
718   (setf (gethash (type-of operation) (component-operation-times c))
719         (get-universal-time)))
720
721 ;;; perform is required to check output-files to find out where to put
722 ;;; its answers, in case it has been overridden for site policy
723 (defmethod perform ((operation compile-op) (c cl-source-file))
724   #-:broken-fasl-loader
725   (let ((source-file (component-pathname c))
726         (output-file (car (output-files operation c))))
727     (multiple-value-bind (output warnings-p failure-p)
728         (compile-file source-file
729                       :output-file output-file)
730       ;(declare (ignore output))
731       (when warnings-p
732         (case (operation-on-warnings operation)
733           (:warn (warn
734                   "~@<COMPILE-FILE warned while performing ~A on ~A.~@:>"
735                   operation c))
736           (:error (error 'compile-warned :component c :operation operation))
737           (:ignore nil)))
738       (when failure-p
739         (case (operation-on-failure operation)
740           (:warn (warn
741                   "~@<COMPILE-FILE failed while performing ~A on ~A.~@:>"
742                   operation c))
743           (:error (error 'compile-failed :component c :operation operation))
744           (:ignore nil)))
745       (unless output
746         (error 'compile-error :component c :operation operation)))))
747
748 (defmethod output-files ((operation compile-op) (c cl-source-file))
749   #-:broken-fasl-loader (list (compile-file-pathname (component-pathname c)))
750   #+:broken-fasl-loader (list (component-pathname c)))
751
752 (defmethod perform ((operation compile-op) (c static-file))
753   nil)
754
755 (defmethod output-files ((operation compile-op) (c static-file))
756   nil)
757
758 ;;; load-op
759
760 (defclass load-op (operation) ())
761
762 (defmethod perform ((o load-op) (c cl-source-file))
763   (mapcar #'load (input-files o c)))
764
765 (defmethod perform ((operation load-op) (c static-file))
766   nil)
767 (defmethod operation-done-p ((operation load-op) (c static-file))
768   t)
769
770 (defmethod output-files ((o operation) (c component))
771   nil)
772
773 (defmethod component-depends-on ((operation load-op) (c component))
774   (cons (list 'compile-op (component-name c))
775         (call-next-method)))
776
777 ;;; load-source-op
778
779 (defclass load-source-op (operation) ())
780
781 (defmethod perform ((o load-source-op) (c cl-source-file))
782   (let ((source (component-pathname c)))
783     (setf (component-property c 'last-loaded-as-source)
784           (and (load source)
785                (get-universal-time)))))
786
787 (defmethod perform ((operation load-source-op) (c static-file))
788   nil)
789
790 (defmethod output-files ((operation load-source-op) (c component))
791   nil)
792
793 ;;; FIXME: we simply copy load-op's dependencies.  this is Just Not Right.
794 (defmethod component-depends-on ((o load-source-op) (c component))
795   (let ((what-would-load-op-do (cdr (assoc 'load-op
796                                            (slot-value c 'in-order-to)))))
797     (mapcar (lambda (dep)
798               (if (eq (car dep) 'load-op)
799                   (cons 'load-source-op (cdr dep))
800                   dep))
801             what-would-load-op-do)))
802
803 (defmethod operation-done-p ((o load-source-op) (c source-file))
804   (if (or (not (component-property c 'last-loaded-as-source))
805           (> (file-write-date (component-pathname c))
806              (component-property c 'last-loaded-as-source)))
807       nil t))
808
809 (defclass test-op (operation) ())
810
811 (defmethod perform ((operation test-op) (c component))
812   nil)
813
814 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
815 ;;; invoking operations
816
817 (defun operate (operation-class system &rest args)
818   (let* ((op (apply #'make-instance operation-class
819                     :original-initargs args args))
820          (*verbose-out*
821           (if (getf args :verbose t)
822               *trace-output*
823               (make-broadcast-stream)))
824          (system (if (typep system 'component) system (find-system system)))
825          (steps (traverse op system)))
826     (with-compilation-unit ()
827       (loop for (op . component) in steps do
828             (loop
829              (restart-case
830                  (progn (perform op component)
831                         (return))
832                (retry ()
833                  :report
834                  (lambda (s)
835                    (format s "~@<Retry performing ~S on ~S.~@:>"
836                            op component)))
837                (accept ()
838                  :report
839                  (lambda (s)
840                    (format s
841                            "~@<Continue, treating ~S on ~S as ~
842                             having been successful.~@:>"
843                            op component))
844                  (setf (gethash (type-of op)
845                                 (component-operation-times component))
846                        (get-universal-time))
847                  (return))))))))
848
849 (defun oos (&rest args)
850   "Alias of OPERATE function"
851   (apply #'operate args))
852
853 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
854 ;;; syntax
855
856 (defun remove-keyword (key arglist)
857   (labels ((aux (key arglist)
858              (cond ((null arglist) nil)
859                    ((eq key (car arglist)) (cddr arglist))
860                    (t (cons (car arglist) (cons (cadr arglist)
861                                                 (remove-keyword
862                                                  key (cddr arglist))))))))
863     (aux key arglist)))
864
865 (defmacro defsystem (name &body options)
866   (destructuring-bind (&key pathname (class 'system) &allow-other-keys) options
867     (let ((component-options (remove-keyword :class options)))
868       `(progn
869         ;; system must be registered before we parse the body, otherwise
870         ;; we recur when trying to find an existing system of the same name
871         ;; to reuse options (e.g. pathname) from
872         (let ((s (system-registered-p ',name)))
873           (cond ((and s (eq (type-of (cdr s)) ',class))
874                  (setf (car s) (get-universal-time)))
875                 (s
876                  #+clisp
877                  (sysdef-error "Cannot redefine the existing system ~A with a different class" s)
878                  #-clisp
879                  (change-class (cdr s) ',class))
880                 (t
881                  (register-system (quote ,name)
882                                   (make-instance ',class :name ',name)))))
883         (parse-component-form nil (apply
884                                    #'list
885                                    :module (coerce-name ',name)
886                                    :pathname
887                                    (or ,pathname
888                                        (pathname-sans-name+type
889                                         (resolve-symlinks  *load-truename*))
890                                        *default-pathname-defaults*)
891                                    ',component-options))))))
892
893
894 (defun class-for-type (parent type)
895   (let ((class
896          (find-class
897           (or (find-symbol (symbol-name type) *package*)
898               (find-symbol (symbol-name type) #.(package-name *package*)))
899           nil)))
900     (or class
901         (and (eq type :file)
902              (or (module-default-component-class parent)
903                  (find-class 'cl-source-file)))
904         (sysdef-error "~@<don't recognize component type ~A~@:>" type))))
905
906 (defun maybe-add-tree (tree op1 op2 c)
907   "Add the node C at /OP1/OP2 in TREE, unless it's there already.
908 Returns the new tree (which probably shares structure with the old one)"
909   (let ((first-op-tree (assoc op1 tree)))
910     (if first-op-tree
911         (progn
912           (aif (assoc op2 (cdr first-op-tree))
913                (if (find c (cdr it))
914                    nil
915                    (setf (cdr it) (cons c (cdr it))))
916                (setf (cdr first-op-tree)
917                      (acons op2 (list c) (cdr first-op-tree))))
918           tree)
919         (acons op1 (list (list op2 c)) tree))))
920
921 (defun union-of-dependencies (&rest deps)
922   (let ((new-tree nil))
923     (dolist (dep deps)
924       (dolist (op-tree dep)
925         (dolist (op  (cdr op-tree))
926           (dolist (c (cdr op))
927             (setf new-tree
928                   (maybe-add-tree new-tree (car op-tree) (car op) c))))))
929     new-tree))
930
931
932 (defun remove-keys (key-names args)
933   (loop for ( name val ) on args by #'cddr
934         unless (member (symbol-name name) key-names
935                        :key #'symbol-name :test 'equal)
936         append (list name val)))
937
938 (defvar *serial-depends-on*)
939
940 (defun parse-component-form (parent options)
941   (destructuring-bind
942         (type name &rest rest &key
943               ;; the following list of keywords is reproduced below in the
944               ;; remove-keys form.  important to keep them in sync
945               components pathname default-component-class
946               perform explain output-files operation-done-p
947               weakly-depends-on
948               depends-on serial in-order-to
949               ;; list ends
950               &allow-other-keys) options
951     (check-component-input type name weakly-depends-on depends-on components in-order-to)
952
953     (when (and parent
954              (find-component parent name)
955              ;; ignore the same object when rereading the defsystem
956              (not
957               (typep (find-component parent name)
958                      (class-for-type parent type))))
959       (error 'duplicate-names :name name))
960
961     (let* ((other-args (remove-keys
962                         '(components pathname default-component-class
963                           perform explain output-files operation-done-p
964                           weakly-depends-on
965                           depends-on serial in-order-to)
966                         rest))
967            (ret
968             (or (find-component parent name)
969                 (make-instance (class-for-type parent type)))))
970       (when weakly-depends-on
971         (setf depends-on (append depends-on (remove-if (complement #'find-system) weakly-depends-on))))
972       (when (boundp '*serial-depends-on*)
973         (setf depends-on
974               (concatenate 'list *serial-depends-on* depends-on)))
975       (apply #'reinitialize-instance
976              ret
977              :name (coerce-name name)
978              :pathname pathname
979              :parent parent
980              other-args)
981       (when (typep ret 'module)
982         (setf (module-default-component-class ret)
983               (or default-component-class
984                   (and (typep parent 'module)
985                        (module-default-component-class parent))))
986         (let ((*serial-depends-on* nil))
987           (setf (module-components ret)
988                 (loop for c-form in components
989                       for c = (parse-component-form ret c-form)
990                       collect c
991                       if serial
992                       do (push (component-name c) *serial-depends-on*))))
993
994         ;; check for duplicate names
995         (let ((name-hash (make-hash-table :test #'equal)))
996           (loop for c in (module-components ret)
997                 do
998                 (if (gethash (component-name c)
999                              name-hash)
1000                     (error 'duplicate-names
1001                            :name (component-name c))
1002                   (setf (gethash (component-name c)
1003                                  name-hash)
1004                         t)))))
1005
1006       (setf (slot-value ret 'in-order-to)
1007             (union-of-dependencies
1008              in-order-to
1009              `((compile-op (compile-op ,@depends-on))
1010                (load-op (load-op ,@depends-on))))
1011             (slot-value ret 'do-first) `((compile-op (load-op ,@depends-on))))
1012
1013       (loop for (n v) in `((perform ,perform) (explain ,explain)
1014                            (output-files ,output-files)
1015                            (operation-done-p ,operation-done-p))
1016             do (map 'nil
1017                     ;; this is inefficient as most of the stored
1018                     ;; methods will not be for this particular gf n
1019                     ;; But this is hardly performance-critical
1020                     (lambda (m) (remove-method (symbol-function n) m))
1021                     (component-inline-methods ret))
1022             when v
1023             do (destructuring-bind (op qual (o c) &body body) v
1024                  (pushnew
1025                   (eval `(defmethod ,n ,qual ((,o ,op) (,c (eql ,ret)))
1026                           ,@body))
1027                   (component-inline-methods ret))))
1028       ret)))
1029
1030 (defun check-component-input (type name weakly-depends-on depends-on components in-order-to)
1031   "A partial test of the values of a component."
1032   (when weakly-depends-on (warn "We got one! XXXXX"))
1033   (unless (listp depends-on)
1034     (sysdef-error-component ":depends-on must be a list."
1035                             type name depends-on))
1036   (unless (listp weakly-depends-on)
1037     (sysdef-error-component ":weakly-depends-on must be a list."
1038                             type name weakly-depends-on))
1039   (unless (listp components)
1040     (sysdef-error-component ":components must be NIL or a list of components."
1041                             type name components))
1042   (unless (and (listp in-order-to) (listp (car in-order-to)))
1043     (sysdef-error-component ":in-order-to must be NIL or a list of components."
1044                            type name in-order-to)))
1045
1046 (defun sysdef-error-component (msg type name value)
1047   (sysdef-error (concatenate 'string msg
1048                              "~&The value specified for ~(~A~) ~A is ~W")
1049                 type name value))
1050
1051 (defun resolve-symlinks (path)
1052   #-allegro (truename path)
1053   #+allegro (excl:pathname-resolve-symbolic-links path)
1054   )
1055
1056 ;;; optional extras
1057
1058 ;;; run-shell-command functions for other lisp implementations will be
1059 ;;; gratefully accepted, if they do the same thing.  If the docstring
1060 ;;; is ambiguous, send a bug report
1061
1062 (defun run-shell-command (control-string &rest args)
1063   "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
1064 synchronously execute the result using a Bourne-compatible shell, with
1065 output to *verbose-out*.  Returns the shell's exit code."
1066   (let ((command (apply #'format nil control-string args)))
1067     (format *verbose-out* "; $ ~A~%" command)
1068     #+sbcl
1069     (sb-impl::process-exit-code
1070      (sb-ext:run-program
1071       #+win32 "sh" #-win32 "/bin/sh"
1072       (list  "-c" command)
1073       #+win32 #+win32 :search t
1074       :input nil :output *verbose-out*))
1075
1076     #+(or cmu scl)
1077     (ext:process-exit-code
1078      (ext:run-program
1079       "/bin/sh"
1080       (list  "-c" command)
1081       :input nil :output *verbose-out*))
1082
1083     #+allegro
1084     (excl:run-shell-command command :input nil :output *verbose-out*)
1085
1086     #+lispworks
1087     (system:call-system-showing-output
1088      command
1089      :shell-type "/bin/sh"
1090      :output-stream *verbose-out*)
1091
1092     #+clisp                             ;XXX not exactly *verbose-out*, I know
1093     (ext:run-shell-command  command :output :terminal :wait t)
1094
1095     #+openmcl
1096     (nth-value 1
1097                (ccl:external-process-status
1098                 (ccl:run-program "/bin/sh" (list "-c" command)
1099                                  :input nil :output *verbose-out*
1100                                  :wait t)))
1101     #+ecl ;; courtesy of Juan Jose Garcia Ripoll
1102     (si:system command)
1103     #-(or openmcl clisp lispworks allegro scl cmu sbcl ecl)
1104     (error "RUN-SHELL-PROGRAM not implemented for this Lisp")
1105     ))
1106
1107
1108 (defgeneric hyperdocumentation (package name doc-type))
1109 (defmethod hyperdocumentation ((package symbol) name doc-type)
1110   (hyperdocumentation (find-package package) name doc-type))
1111
1112 (defun hyperdoc (name doc-type)
1113   (hyperdocumentation (symbol-package name) name doc-type))
1114
1115
1116 (pushnew :asdf *features*)
1117
1118 #+sbcl
1119 (eval-when (:compile-toplevel :load-toplevel :execute)
1120   (when (sb-ext:posix-getenv "SBCL_BUILDING_CONTRIB")
1121     (pushnew :sbcl-hooks-require *features*)))
1122
1123 #+(and sbcl sbcl-hooks-require)
1124 (progn
1125   (defun module-provide-asdf (name)
1126     (handler-bind ((style-warning #'muffle-warning))
1127       (let* ((*verbose-out* (make-broadcast-stream))
1128              (system (asdf:find-system name nil)))
1129         (when system
1130           (asdf:operate 'asdf:load-op name)
1131           t))))
1132
1133   (defun contrib-sysdef-search (system)
1134     (let* ((name (coerce-name system))
1135            (home (truename (sb-ext:posix-getenv "SBCL_HOME")))
1136            (contrib (merge-pathnames
1137                      (make-pathname :directory `(:relative ,name)
1138                                     :name name
1139                                     :type "asd"
1140                                     :case :local
1141                                     :version :newest)
1142                      home)))
1143       (probe-file contrib)))
1144
1145   (pushnew
1146    '(merge-pathnames "site-systems/"
1147      (truename (sb-ext:posix-getenv "SBCL_HOME")))
1148    *central-registry*)
1149
1150   (pushnew
1151    '(merge-pathnames ".sbcl/systems/"
1152      (user-homedir-pathname))
1153    *central-registry*)
1154
1155   (pushnew 'module-provide-asdf sb-ext:*module-provider-functions*)
1156   (pushnew 'contrib-sysdef-search *system-definition-search-functions*))
1157
1158 (provide 'asdf)