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