parse-defsystem.lisp 23.3 KB
Newer Older
1
2
3
;;;; -------------------------------------------------------------------------
;;;; Defsystem

4
(uiop/package:define-package :asdf/parse-defsystem
5
6
7
  (:recycle :asdf/parse-defsystem :asdf/defsystem :asdf)
  (:nicknames :asdf/defsystem) ;; previous name, to be compatible with, in case anyone cares
  (:use :uiop/common-lisp :asdf/driver :asdf/upgrade
8
9
   :asdf/session :asdf/component :asdf/system :asdf/system-registry
   :asdf/find-component :asdf/action :asdf/lisp-action :asdf/operate)
10
  (:import-from :asdf/system #:depends-on #:weakly-depends-on)
11
12
13
14
  ;; these needed for record-additional-system-input-file
  (:import-from :asdf/operation #:make-operation)
  (:import-from :asdf/component #:%additional-input-files)
  (:import-from :asdf/find-system #:define-op)
15
  (:export
16
   #:defsystem #:register-system-definition
17
   #:*default-component-class*
18
   #:determine-system-directory #:parse-component-form
19
   #:non-toplevel-system #:non-system-system #:bad-system-name
20
21
   #:*known-systems-with-bad-secondary-system-names*
   #:known-system-with-bad-secondary-system-names-p
22
   #:sysdef-error-component #:check-component-input
23
24
25
26
   #:explain
   ;; for extending the component types
   #:compute-component-children
   #:class-for-type))
27
(in-package :asdf/parse-defsystem)
28
29

;;; Pathname
30
31
(with-upgradability ()
  (defun determine-system-directory (pathname)
32
33
    ;; The defsystem macro calls this function to determine the pathname of a system as follows:
    ;; 1. If the pathname argument is an pathname object (NOT a namestring),
34
    ;;    that is already an absolute pathname, return it.
35
    ;; 2. Otherwise, the directory containing the LOAD-PATHNAME
36
37
38
39
40
    ;;    is considered (as deduced from e.g. *LOAD-PATHNAME*), and
    ;;    if it is indeed available and an absolute pathname, then
    ;;    the PATHNAME argument is normalized to a relative pathname
    ;;    as per PARSE-UNIX-NAMESTRING (with ENSURE-DIRECTORY T)
    ;;    and merged into that DIRECTORY as per SUBPATHNAME.
41
42
    ;;    Note: avoid *COMPILE-FILE-PATHNAME* because the .asd is loaded as source,
    ;;    but may be from within the EVAL-WHEN of a file compilation.
43
44
45
46
47
48
49
50
51
    ;; If no absolute pathname was found, we return NIL.
    (check-type pathname (or null string pathname))
    (pathname-directory-pathname
     (resolve-symlinks*
      (ensure-absolute-pathname
       (parse-unix-namestring pathname :type :directory)
       #'(lambda () (ensure-absolute-pathname
                     (load-pathname) 'get-pathname-defaults nil))
       nil)))))
52

53

54
55
56
57
(when-upgrading (:version "3.3.4.17")
  ;; This turned into a generic function in 3.3.4.17
  (fmakunbound 'class-for-type))

58
;;; Component class
59
(with-upgradability ()
60
  ;; What :file gets interpreted as, unless overridden by a :default-component-class
61
62
  (defvar *default-component-class* 'cl-source-file)

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  (defgeneric class-for-type (parent type-designator)
    (:documentation
     "Return a CLASS object to be used to instantiate components specified by TYPE-DESIGNATOR in the context of PARENT."))

  (defmethod class-for-type ((parent null) type)
    "If the PARENT is NIL, then TYPE must designate a subclass of SYSTEM."
    (or (coerce-class type :package :asdf/interface :super 'system :error nil)
        (sysdef-error "don't recognize component type ~S in the context of no parent" type)))

  (defmethod class-for-type ((parent parent-component) type)
    (or (coerce-class type :package :asdf/interface :super 'component :error nil)
        (and (eq type :file)
             (coerce-class
              (or (loop :for p = parent :then (component-parent p) :while p
                          :thereis (module-default-component-class p))
                  *default-component-class*)
              :package :asdf/interface :super 'component :error nil))
        (sysdef-error "don't recognize component type ~S" type))))
81

82

83
;;; Check inputs
84
(with-upgradability ()
85
86
87
88
89
90
91
  (define-condition non-system-system (system-definition-error)
    ((name :initarg :name :reader non-system-system-name)
     (class-name :initarg :class-name :reader non-system-system-class-name))
    (:report (lambda (c s)
               (format s (compatfmt "~@<Error while defining system ~S: class ~S isn't a subclass of ~S~@:>")
                       (non-system-system-name c) (non-system-system-class-name c) 'system))))

92
93
94
95
96
97
98
  (define-condition non-toplevel-system (system-definition-error)
    ((parent :initarg :parent :reader non-toplevel-system-parent)
     (name :initarg :name :reader non-toplevel-system-name))
    (:report (lambda (c s)
               (format s (compatfmt "~@<Error while defining system: component ~S claims to have a system ~S as a child~@:>")
                       (non-toplevel-system-parent c) (non-toplevel-system-name c)))))

99
100
101
102
103
104
105
106
107
108
109
  (define-condition bad-system-name (warning)
    ((name :initarg :name :reader component-name)
     (source-file :initarg :source-file :reader system-source-file))
    (:report (lambda (c s)
               (let* ((file (system-source-file c))
                      (name (component-name c))
                      (asd (pathname-name file)))
                 (format s (compatfmt "~@<System definition file ~S contains definition for system ~S. ~
Please only define ~S and secondary systems with a name starting with ~S (e.g. ~S) in that file.~@:>")
                       file name asd (strcat asd "/") (strcat asd "/test"))))))

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  (defun sysdef-error-component (msg type name value)
    (sysdef-error (strcat msg (compatfmt "~&~@<The value specified for ~(~A~) ~A is ~S~@:>"))
                  type name value))

  (defun check-component-input (type name weakly-depends-on
                                depends-on components)
    "A partial test of the values of a component."
    (unless (listp depends-on)
      (sysdef-error-component ":depends-on must be a list."
                              type name depends-on))
    (unless (listp weakly-depends-on)
      (sysdef-error-component ":weakly-depends-on must be a list."
                              type name weakly-depends-on))
    (unless (listp components)
      (sysdef-error-component ":components must be NIL or a list of components."
                              type name components)))

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

  (defun record-additional-system-input-file (pathname component parent)
    (let* ((record-on (if parent
                          (loop :with retval
                                :for par = parent :then (component-parent par)
                                :while par
                                :do (setf retval par)
                                :finally (return retval))
                          component))
           (comp (if (typep record-on 'component)
                     record-on
                     ;; at this point there will be no parent for RECORD-ON
                     (find-component record-on nil)))
           (op (make-operation 'define-op))
           (cell (or (assoc op (%additional-input-files comp))
                       (let ((new-cell (list op)))
                         (push new-cell (%additional-input-files comp))
                         new-cell))))
      (pushnew pathname (cdr cell) :test 'pathname-equal)
      (values)))

148
149
150
  ;; Given a form used as :version specification, in the context of a system definition
  ;; in a file at PATHNAME, for given COMPONENT with given PARENT, normalize the form
  ;; to an acceptable ASDF-format version.
151
152
  (fmakunbound 'normalize-version) ;; signature changed between 2.27 and 2.31
  (defun normalize-version (form &key pathname component parent)
153
154
155
156
    (labels ((invalid (&optional (continuation "using NIL instead"))
               (warn (compatfmt "~@<Invalid :version specifier ~S~@[ for component ~S~]~@[ in ~S~]~@[ from file ~S~]~@[, ~A~]~@:>")
                     form component parent pathname continuation))
             (invalid-parse (control &rest args)
157
               (unless (if-let (target (find-component parent component)) (builtin-system-p target))
158
159
160
161
162
163
164
165
166
167
168
                 (apply 'warn control args)
                 (invalid))))
      (if-let (v (typecase form
                   ((or string null) form)
                   (real
                    (invalid "Substituting a string")
                    (format nil "~D" form)) ;; 1.0 becomes "1.0"
                   (cons
                    (case (first form)
                      ((:read-file-form)
                       (destructuring-bind (subpath &key (at 0)) (rest form)
169
170
171
172
                         (let ((path (subpathname pathname subpath)))
                           (record-additional-system-input-file path component parent)
                           (safe-read-file-form path
                                                :at at :package :asdf-user))))
173
174
                      ((:read-file-line)
                       (destructuring-bind (subpath &key (at 0)) (rest form)
175
176
177
178
                         (let ((path (subpathname pathname subpath)))
                           (record-additional-system-input-file path component parent)
                           (safe-read-file-line (subpathname pathname subpath)
                                                :at at))))
179
180
181
182
183
184
185
                      (otherwise
                       (invalid))))
                   (t
                    (invalid))))
        (if-let (pv (parse-version v #'invalid-parse))
          (unparse-version pv)
          (invalid))))))
186

187

188
189
190
;;; "inline methods"
(with-upgradability ()
  (defparameter* +asdf-methods+
191
      '(perform-with-restarts perform explain output-files operation-done-p))
192
193
194
195
196
197
198
199
200
201
202
203

  (defun %remove-component-inline-methods (component)
    (dolist (name +asdf-methods+)
      (map ()
           ;; this is inefficient as most of the stored
           ;; methods will not be for this particular gf
           ;; But this is hardly performance-critical
           #'(lambda (m)
               (remove-method (symbol-function name) m))
           (component-inline-methods component)))
    (component-inline-methods component) nil)

204
205
206
207
208
209
210
211
  (defparameter *standard-method-combination-qualifiers*
    '(:around :before :after))

;;; Find inline method definitions of the form
;;;
;;;   :perform (test-op :before (operation component) ...)
;;;
;;; in REST (which is the plist of all DEFSYSTEM initargs) and define the specified methods.
212
  (defun %define-component-inline-methods (ret rest)
213
214
    ;; find key-value pairs that look like inline method definitions in REST. For each identified
    ;; definition, parse it and, if it is well-formed, define the method.
215
216
217
218
219
220
221
222
223
224
225
    (loop :for (key value) :on rest :by #'cddr
          :for name = (and (keywordp key) (find key +asdf-methods+ :test 'string=))
          :when name :do
            ;; parse VALUE as an inline method definition of the form
            ;;
            ;;   (OPERATION-NAME [QUALIFIER] (OPERATION-PARAMETER COMPONENT-PARAMETER) &rest BODY)
            (destructuring-bind (operation-name &rest rest) value
              (let ((qualifiers '()))
                ;; ensure that OPERATION-NAME is a symbol.
                (unless (and (symbolp operation-name) (not (null operation-name)))
                  (sysdef-error "Ill-formed inline method: ~S. The first element is not a symbol ~
226
                              designating an operation but ~S."
227
228
229
230
231
232
233
234
235
236
                                value operation-name))
                ;; ensure that REST starts with either a cons (potential lambda list, further checked
                ;; below) or a qualifier accepted by the standard method combination. Everything else
                ;; is ill-formed. In case of a valid qualifier, pop it from REST so REST now definitely
                ;; has to start with the lambda list.
                (cond
                  ((consp (car rest)))
                  ((not (member (car rest)
                                *standard-method-combination-qualifiers*))
                   (sysdef-error "Ill-formed inline method: ~S. Only a single of the standard ~
237
                               qualifiers ~{~S~^ ~} is allowed, not ~S."
238
239
240
241
242
243
244
245
                                 value *standard-method-combination-qualifiers* (car rest)))
                  (t
                   (setf qualifiers (list (pop rest)))))
                ;; REST must start with a two-element lambda list.
                (unless (and (listp (car rest))
                             (length=n-p (car rest) 2)
                             (null (cddar rest)))
                  (sysdef-error "Ill-formed inline method: ~S. The operation name ~S is not followed by ~
246
                              a lambda-list of the form (OPERATION COMPONENT) and a method body."
247
248
249
250
251
252
                                value operation-name))
                ;; define the method.
                (destructuring-bind ((o c) &rest body) rest
                  (pushnew
                   (eval `(defmethod ,name ,@qualifiers ((,o ,operation-name) (,c (eql ,ret))) ,@body))
                   (component-inline-methods ret)))))))
253
254
255
256
257
258
259

  (defun %refresh-component-inline-methods (component rest)
    ;; clear methods, then add the new ones
    (%remove-component-inline-methods component)
    (%define-component-inline-methods component rest)))


260
;;; Main parsing function
261
(with-upgradability ()
262
  (defun parse-dependency-def (dd)
263
264
265
266
267
268
269
    (if (listp dd)
        (case (first dd)
          (:feature
           (unless (= (length dd) 3)
             (sysdef-error "Ill-formed feature dependency: ~s" dd))
           (let ((embedded (parse-dependency-def (third dd))))
             `(:feature ,(second dd) ,embedded)))
270
271
          (feature
           (sysdef-error "`feature' has been removed from the dependency spec language of ASDF. Use :feature instead in ~s." dd))
272
273
274
275
276
277
278
279
280
281
282
          (:require
           (unless (= (length dd) 2)
             (sysdef-error "Ill-formed require dependency: ~s" dd))
           dd)
          (:version
           (unless (= (length dd) 3)
             (sysdef-error "Ill-formed version dependency: ~s" dd))
           `(:version ,(coerce-name (second dd)) ,(third dd)))
          (otherwise (sysdef-error "Ill-formed dependency: ~s" dd)))
      (coerce-name dd)))

283
  (defun parse-dependency-defs (dd-list)
284
285
286
287
    "Parse the dependency defs in DD-LIST into canonical form by translating all
system names contained using COERCE-NAME. Return the result."
    (mapcar 'parse-dependency-def dd-list))

288
289
290
291
292
293
294
295
  (defgeneric compute-component-children (component components serial-p)
    (:documentation
     "Return a list of children for COMPONENT.

COMPONENTS is a list of the explicitly defined children descriptions.

SERIAL-P is non-NIL if each child in COMPONENTS should depend on the previous
children."))
Robert Goldman's avatar
Robert Goldman committed
296

297
  (defun stable-union (s1 s2 &key (test #'eql) (key 'identity))
Robert Goldman's avatar
Robert Goldman committed
298
299
300
   (append s1
     (remove-if #'(lambda (e2) (member (funcall key e2) (funcall key s1) :test test)) s2)))

Robert Goldman's avatar
Robert Goldman committed
301
  (defun parse-component-form (parent options &key previous-serial-components)
302
303
304
305
306
307
308
    (destructuring-bind
        (type name &rest rest &key
                                (builtin-system-p () bspp)
                                ;; the following list of keywords is reproduced below in the
                                ;; remove-plist-keys form.  important to keep them in sync
                                components pathname perform explain output-files operation-done-p
                                weakly-depends-on depends-on serial
309
                                do-first if-component-dep-fails version
310
311
                                ;; list ends
         &allow-other-keys) options
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
312
      (declare (ignore perform explain output-files operation-done-p builtin-system-p))
313
314
315
316
317
318
319
320
      (check-component-input type name weakly-depends-on depends-on components)
      (when (and parent
                 (find-component parent name)
                 (not ;; ignore the same object when rereading the defsystem
                  (typep (find-component parent name)
                         (class-for-type parent type))))
        (error 'duplicate-names :name name))
      (when do-first (error "DO-FIRST is not supported anymore as of ASDF 3"))
321
322
      (let* ((name (coerce-name name))
             (args `(:name ,name
323
324
325
326
327
328
329
                     :pathname ,pathname
                     ,@(when parent `(:parent ,parent))
                     ,@(remove-plist-keys
                        '(:components :pathname :if-component-dep-fails :version
                          :perform :explain :output-files :operation-done-p
                          :weakly-depends-on :depends-on :serial)
                        rest)))
330
331
332
333
             (component (find-component parent name))
             (class (class-for-type parent type)))
        (when (and parent (subtypep class 'system))
          (error 'non-toplevel-system :parent parent :name name))
334
335
        (if component ; preserve identity
            (apply 'reinitialize-instance component args)
336
            (setf component (apply 'make-instance class args)))
337
        (component-pathname component) ; eagerly compute the absolute pathname
338
        (when (typep component 'system)
339
          ;; cache information for introspection
340
341
342
343
344
          (setf (slot-value component 'depends-on)
                (parse-dependency-defs depends-on)
                (slot-value component 'weakly-depends-on)
                ;; these must be a list of systems, cannot be features or versioned systems
                (mapcar 'coerce-name weakly-depends-on)))
345
        (let ((sysfile (system-source-file (component-system component)))) ;; requires the previous
346
          (when (and (typep component 'system) (not bspp))
347
348
            (setf (builtin-system-p component) (lisp-implementation-pathname-p sysfile)))
          (setf version (normalize-version version :component name :parent parent :pathname sysfile)))
349
350
351
        ;; Don't use the accessor: kluge to avoid upgrade issue on CCL 1.8.
        ;; A better fix is required.
        (setf (slot-value component 'version) version)
352
        (when (typep component 'parent-component)
353
354
          (setf (component-children component) (compute-component-children component components serial))
          (compute-children-by-name component))
Robert Goldman's avatar
Robert Goldman committed
355
356
        (when previous-serial-components
          (setf depends-on (stable-union depends-on previous-serial-components :test #'equal)))
357
358
359
360
        (when weakly-depends-on
          ;; ASDF4: deprecate this feature and remove it.
          (appendf depends-on
                   (remove-if (complement #'(lambda (x) (find-system x nil))) weakly-depends-on)))
361
        ;; Used by POIU. ASDF4: rename to component-depends-on?
362
        (setf (component-sideway-dependencies component) depends-on)
363
364
        (%refresh-component-inline-methods component rest)
        (when if-component-dep-fails
365
366
367
          (error "The system definition for ~S uses deprecated ~
            ASDF option :IF-COMPONENT-DEP-FAILS. ~
            Starting with ASDF 3, please use :IF-FEATURE instead"
Robert Goldman's avatar
Robert Goldman committed
368
                 (coerce-name (component-system component))))
369
370
        component)))

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  (defmethod compute-component-children ((component parent-component) components serial-p)
    (loop
      :with previous-components = nil ; list of strings
      :for c-form :in components
      :for c = (parse-component-form component c-form
                                     :previous-serial-components previous-components)
      :for name :of-type string = (component-name c)
      :when serial-p
        ;; if this is an if-feature component, we need to make a serial link
        ;; from previous components to following components -- otherwise should
        ;; the IF-FEATURE component drop out, the chain of serial dependencies will be
        ;; broken.
        :unless (component-if-feature c)
          :do (setf previous-components nil)
        :end
        :and
          :do (push name previous-components)
      :end
      :collect c))

391
392
  ;; the following are all systems that Stas Boukarev maintains and refuses to fix,
  ;; hoping instead to make my life miserable. Instead, I just make ASDF ignore them.
393
  (defparameter* *known-systems-with-bad-secondary-system-names*
394
    (list-to-hash-set '("cl-ppcre" "cl-interpol")))
395
396
397
398
  (defun known-system-with-bad-secondary-system-names-p (asd-name)
    ;; Does .asd file with name ASD-NAME contain known exceptions
    ;; that should be screened out of checking for BAD-SYSTEM-NAME?
    (gethash asd-name *known-systems-with-bad-secondary-system-names*))
399

400
401
402
403
404
405
406
407
  (defun register-system-definition
      (name &rest options &key pathname (class 'system) (source-file () sfp)
                            defsystem-depends-on &allow-other-keys)
    ;; The system must be registered before we parse the body,
    ;; otherwise we recur when trying to find an existing system
    ;; of the same name to reuse options (e.g. pathname) from.
    ;; To avoid infinite recursion in cases where you defsystem a system
    ;; that is registered to a different location to find-system,
408
    ;; we also need to remember it in the asdf-cache.
409
    (nest
410
     (with-asdf-session ())
411
     (let* ((name (coerce-name name))
412
            (source-file (if sfp source-file (resolve-symlinks* (load-pathname))))))
413
     (flet ((fix-case (x) (if (logical-pathname-p source-file) (string-downcase x) x))))
414
415
416
     (let* ((asd-name (and source-file
                           (equal "asd" (fix-case (pathname-type source-file)))
                           (fix-case (pathname-name source-file))))
417
            ;; note that PRIMARY-NAME is a *syntactically* primary name
418
            (primary-name (primary-system-name name)))
419
420
       (when (and asd-name
                  (not (equal asd-name primary-name))
421
                  (not (known-system-with-bad-secondary-system-names-p asd-name)))
422
423
424
425
426
427
428
429
430
431
         (warn (make-condition 'bad-system-name :source-file source-file :name name))))
     (let* (;; NB: handle defsystem-depends-on BEFORE to create the system object,
            ;; so that in case it fails, there is no incomplete object polluting the build.
            (checked-defsystem-depends-on
             (let* ((dep-forms (parse-dependency-defs defsystem-depends-on))
                    (deps (loop :for spec :in dep-forms
                            :when (resolve-dependency-spec nil spec)
                            :collect :it)))
               (load-systems* deps)
               dep-forms))
432
433
            (system (or (find-system-if-being-defined name)
                        (if-let (registered (registered-system name))
434
435
                          (reset-system-class registered 'undefined-system
                                              :name name :source-file source-file)
436
437
                          (register-system (make-instance 'undefined-system
                                                          :name name :source-file source-file)))))
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
            (component-options
             (append
              (remove-plist-keys '(:defsystem-depends-on :class) options)
              ;; cache defsystem-depends-on in canonical form
              (when checked-defsystem-depends-on
                `(:defsystem-depends-on ,checked-defsystem-depends-on))))
            (directory (determine-system-directory pathname)))
       ;; This works hand in hand with asdf/find-system:find-system-if-being-defined:
       (set-asdf-cache-entry `(find-system ,name) (list system)))
     ;; We change-class AFTER we loaded the defsystem-depends-on
     ;; since the class might be defined as part of those.
     (let ((class (class-for-type nil class)))
       (unless (subtypep class 'system)
         (error 'non-system-system :name name :class-name (class-name class)))
       (unless (eq (type-of system) class)
453
         (reset-system-class system class)))
454
     (parse-component-form nil (list* :system name :pathname directory component-options))))
455
456
457

  (defmacro defsystem (name &body options)
    `(apply 'register-system-definition ',name ',options)))