Newer
Older
;;; the problem with writing a defsystem replacement is bootstrapping:
;;; we can't use defsystem to compile it. Hence, all in one file
(defpackage #:asdf
(:export #:defsystem #:oos #:find-system #:run-shell-command
#:find-component ; miscellaneous
#:compile-system #:load-system #:test-system-version
#:operation ; operations
#:output-files #:perform ; operation methods
#:operation-done-p #:explain
#:component #:module #:source-file
#:c-source-file #:cl-source-file #:java-source-file
#:module ; components
#:unix-dso
#:module-components ; component accessors
#:component-pathname
#:component-relative-pathname
#:component-name
#:component-version
#:component-depends-on
#:*component-parent-pathname* ; variables
#:*central-registry*
#:operation-error #:compile-failed #:compile-warned
#:system-definition-error #:system-not-found
#:circular-dependency ; errors
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(in-package #:asdf)
(proclaim '(optimize (debug 3)))
(declaim (optimize (debug 3)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; problems
(define-condition system-definition-error (error)
())
(define-condition circular-dependency (system-definition-error)
((components :initarg :components)))
(define-condition system-not-found (system-definition-error)
((name :initform "(unnamed)" :reader system-name :initarg :name))
(:report (lambda (c s)
(format s "System ~S not found"
(system-name c)))))
(define-condition operation-error (error)
((component :reader error-component :initarg :component)
(operation :reader error-operation :initarg :operation))
(:report (lambda (c s)
(format s "Erred while invoking ~A on ~A"
(error-operation c) (error-component c)))))
(define-condition compile-failed (operation-error) ())
(define-condition compile-warned (operation-error) ())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; components
(defclass component ()
((name :type string :accessor component-name :initarg :name :documentation
"Component name, restricted to portable pathname characters")
(version :accessor component-version :initarg :version)
(depends-on :initform nil
:accessor component-depends-on :initarg :depends-on)
;; no direct accessor for pathname, we do this as a method to allow
;; it to default in funky ways if not supplied
;; :pathname is actually a realy bad choice of name for this slot,
;; because quite often that's not what we actually use it for
(pathname :initarg :pathname)))
(defmethod component-pathname ((component component))
(let ((*default-pathname-defaults* *component-parent-pathname*))
(if (slot-boundp component 'pathname)
(let ((p (slot-value component 'pathname)))
(cond ((pathnamep p)
(merge-pathnames p))
((and (stringp p) (> (length p) 0))
(destructuring-bind (name type) (split p 2 '(#\.))
(merge-pathnames (make-pathname :name name :type type))))
((and (stringp p) (= (length p) 0))
*component-parent-pathname*)
(t
(warn ":pathname argument to ~A is not a pathname designator. Ignoring it" component)
(merge-pathnames (component-relative-pathname component)))))
(merge-pathnames (component-relative-pathname component)))))
(defmethod print-object ((c component) stream)
(print-unreadable-object (c stream :type t :identity t)
(ignore-errors
(prin1 (component-name c) stream))))
(defclass module (component)
((components :accessor module-components :initarg :components)
(default-component-class :accessor module-default-component-class
:initform 'cl-source-file :initarg :default-component-class)))
(defmethod component-relative-pathname ((component module))
(make-pathname :directory `(:relative ,(component-name component))
:host (pathname-host *component-parent-pathname*)))
(defmethod find-component ((module module) name)
(if (slot-boundp module 'components)
(find name (module-components module)
:test #'equal :key #'component-name)))
;;; a component with no parent is a system
(defmethod find-component ((module (eql nil)) name)
(find-system name))
(defclass source-file (component) ())
(defclass cl-source-file (source-file) ())
;;; a reversible registry that lets us query class <-> extension
(defvar *known-extensions*
'(("lisp" . cl-source-file)
("c" . c-source-file)
("java" . java-source-file)))
(defmethod component-relative-pathname ((component source-file))
(make-pathname :name (component-name component)
:type (car (rassoc (class-name (class-of component))
*known-extensions* ))))
(defclass c-source-file (source-file) ())
(defclass java-source-file (source-file) ())
;;; XXX we don't need the first of these if we have the second, probably
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(defvar *component-parent-pathname*)
(defvar *component-parent*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; operations
;;; one of these is instantiated whenever (operate ) is called
(defclass operation ()
((forced-p :initform nil :initarg :force :accessor operation-forced-p )
(visited-components :initform nil :accessor operation-visited-components)
(visiting-components :initform nil :accessor operation-visiting-components)))
(defgeneric perform (operation system))
(defgeneric operation-done-p (operation system))
(defgeneric explain (operation system))
(defgeneric output-files (operation system))
(defmethod visit-component ((o operation) (c component))
(pushnew c (operation-visited-components o)))
(defmethod component-visited-p ((o operation) (c component))
(member c (operation-visited-components o)))
(defmethod (setf visiting-component) (new-value (o operation) (c component))
(if new-value
(pushnew c (operation-visiting-components o))
(setf (operation-visiting-components o)
(remove c (operation-visiting-components o)))))
(defmethod component-visiting-p ((o operation) (c component))
(member c (operation-visiting-components o)))
;;; this needs a new name. should be operation-needs-doing-p
(defmethod operation-done-p ((o operation) (c source-file))
(let ((binaries (output-files o c))
(source-write-date
(and (probe-file (component-pathname c))
(file-write-date (component-pathname c)))))
(and source-write-date binaries
(every (lambda (b)
(and (probe-file b)
(> (file-write-date b) source-write-date)))
binaries))))
(defmethod operation-done-p ((o operation) (c component))
;;; can't tell easily
nil)
;;; So you look at this code and think "why isn't it a bunch of
;;; methods". And the answer is, because standard method combination
;;; runs :before methods most->least-specific, which is back to front
;;; for our purposes. And CLISP apparently doesn't have non-standard
;;; method combinations, so let's keep it simple and aspire to
;;; portability
(defmethod traverse ((operation operation) (c component) function)
; dependencies
(if (and nil (component-visiting-p operation c))
(error 'circular-dependency :components (list c)))
(setf (visiting-component operation c) t)
(mapc (lambda (k)
(traverse operation (find-component *component-parent* k)
function))
(component-depends-on c))
;; constituent bits
(when (typep c 'module)
(let ((*component-parent-pathname*
(component-pathname c))
(*component-parent* c))
(mapc (lambda (c) (traverse operation c function))
(module-components c))))
;; now the thing itself
(unless (component-visited-p operation c)
(if (or (operation-forced-p operation)
(not (operation-done-p operation c)))
(loop
(restart-case
(progn (funcall function operation c)
(return))
(retry-component ())
(skip-component () (return)))))
(setf (visiting-component operation c) nil)
(visit-component operation c)))
(defmethod perform ((operation operation) (c source-file))
(error
"Required method PERFORM not implemented for operation ~A, component ~A"
(class-of operation) (class-of c)))
(defmethod perform ((operation operation) (c module))
nil)
(defmethod explain ((operation operation) (component component))
(format *trace-output* "~&;;; ~A on ~A~%"
operation component))
(defmethod output-files ((operation operation) (c component))
(error "Required method OUTPUT-FILES not implemented for operation ~A"
(class-of operation)))
;;; compile-system
(defclass compile-system (operation)
((proclamations :initarg :proclamations :accessor compile-system-proclamations :initform nil)
(fail-on-error-p :initarg :fail-on-error
:accessor operation-fail-on-error-p :initform t)
(fail-on-warning-p :initarg :fail-on-warning
:accessor operation-fail-on-warning-p :initform nil)))
(defmethod perform ((operation compile-system) (c cl-source-file))
(let ((source-file (component-pathname c)))
(multiple-value-bind (output warnings-p failure-p)
(compile-file source-file)
(when (and warnings-p (operation-fail-on-warning-p operation))
(error 'compile-warned :component c :operation operation))
(when (and failure-p (operation-fail-on-error-p operation))
(error 'compile-failed :component c :operation operation))
;; now load the file. this is a hack which will go away when we
;; work out dependencies properly
(load output))))
(defmethod output-files ((operation compile-system) (c cl-source-file))
(list (compile-file-pathname (component-pathname c))))
;;; load-system
(defclass load-system (operation) ())
(defmethod perform ((o load-system) (c cl-source-file))
(let ((co (make-instance 'compile-system)))
(load (car (output-files co c)))))
(defmethod output-files ((operation load-system) (c component))
nil)
;;; test-system-version
(defclass test-system-version (operation)
((minimum :initarg :minimum :initform ""
:accessor test-system-version-minimum)))
;;; with apologies to christophe rhodes ...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
(defun split (string &optional max (ws '(#\Space #\Tab)))
(flet ((is-ws (char) (find char ws)))
(nreverse
(let ((list nil) (start 0) (words 0) end)
(loop
(when (and max (>= words (1- max)))
(return (cons (subseq string start) list)))
(setf end (position-if #'is-ws string :start start))
(push (subseq string start end) list)
(incf words)
(unless end (return list))
(setf start (1+ end)))))))
(defmethod perform ((operation test-system-version) (c component))
(let ((x (mapcar #'parse-integer
(split (component-version c) nil '(#\.))))
(y (mapcar #'parse-integer
(split
(test-system-version-minimum operation) nil '(#\.)))))
(labels ((bigger (x y)
(cond ((not y) t)
((not x) nil)
((> (car x) (car y)) t)
((= (car x) (car y))
(bigger (cdr x) (cdr y))))))
(and (= (car x) (car y))
(or (not (cdr y)) (bigger (cdr x) (cdr y)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; invoking operations
(defun oos (operation-class system &rest args)
(let ((op (apply #'make-instance operation-class args))
(*component-parent-pathname* *default-pathname-defaults*)
(*component-parent* nil)
(system (if (typep system 'component) system (find-system system))))
(traverse op system 'perform)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; finding systems
(defvar *defined-systems* (make-hash-table :test 'equal))
(defparameter *central-registry*
'(*default-pathname-defaults*
"telent:asdf;systems;"))
(defun find-system (name)
(let* ((name (if (symbolp name) (symbol-name name) name))
(in-memory (gethash name *defined-systems*))
(on-disk
(dolist (dir *central-registry*)
(let* ((defaults (eval dir))
(file (merge-pathnames
(make-pathname
:case :common :name name :type "SYSTEM"
:defaults defaults)
defaults)))
(if (probe-file file) (return file)))) ))
(when (and on-disk
(or (not in-memory)
(< (car in-memory) (file-write-date on-disk))))
(let ((*package* (make-package (gensym (package-name #.*package*))
:use '("CL" "ASDF"))))
(format t ";;; Loading system definition from ~A into ~A~%"
on-disk *package*)
(load on-disk)))
(let ((in-memory (gethash name *defined-systems*)))
(if in-memory
(progn (if on-disk (setf (car in-memory) (file-write-date on-disk)))
(cdr in-memory))
(error 'system-not-found :name name)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; syntax
(defmacro defsystem (name &body options)
(let* ((name (if (symbolp name) (symbol-name name) name))
(args `(:name ,name (process-option-list m (quote ,options)))))
;; this macro is called (sic) during executing of find-system, so
;; we had better not call find-system recursively
`(let ((m (or (cdr (gethash ,name *defined-systems*))
(make-instance 'module :name ,name ))))
(setf (gethash ,name *defined-systems*) (cons 0 m))
(apply #'reinitialize-instance m ,@args))))
(defmethod process-option-list ((c component) options)
(loop for (name value) on options by #'cddr
append (process-option c name value)))
(defmethod process-option ((c component) option value)
(list option value))
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
source-file components defined with (:file "a-string") or "a-string"
will have the string parsed into name and type as if it were a
filename, and an instance of the appropriate source-file subclass
created. If a type is not provided, it will default to the parent's
default constituent type.
|#
(defun create-instance-for-component (parent-component keyword name args)
(let* ((name-bits (split name 2 '(#\.)))
(name (car name-bits))
(extension (second name-bits))
(class
(cond ((eq keyword :file)
(if extension
(cdr (assoc extension *known-extensions* :test 'equal))
(module-default-component-class parent-component)))
((eq keyword :module) 'module)
(t (intern (symbol-name keyword) *package*)))))
(let ((instance (or (find-component parent-component name)
(make-instance class :name name))))
(apply #'reinitialize-instance
instance
:name name (process-option-list instance args))
instance)))
(defmethod process-option ((c component) (option (eql :components)) value)
(list :components
(mapcar
(lambda (i)
(if (consp i)
(create-instance-for-component c (first i) (second i) (cddr i))
(create-instance-for-component c :file (second i) nil)))
value)))
;;; optional extras
;;; run-shell-command functions for other lisp implementations will be
;;; gratefully accepted, if they do the same thing. If the docstring
;;; is ambiguous, send a bug report
#+sbcl
(defun run-shell-command (control-string &rest args)
"Interpolate ARGS into CONTROL-STRING as if by FORMAT, and execute the result using a Bourne-compatible shell, with output to *trace-output*. Returns the shell's exit code."
(let ((command (apply #'format nil control-string args)))
(format *trace-output* "; $ ~A~%" command)
(sb-impl::process-exit-code
(sb-ext:run-program
"/bin/sh"
(list "-c" command)
:input nil :output *trace-output*))))
;;; mk-compatibility
(defmethod process-option ((c component) (option (eql :source-pathname)) value)
(list :pathname value))