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