find-system.lisp 21.9 KB
Newer Older
1
2
3
;;;; -------------------------------------------------------------------------
;;;; Finding systems

4
(uiop/package:define-package :asdf/find-system
5
  (:recycle :asdf/find-system :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade
7
   :asdf/component :asdf/system :asdf/cache)
8
  (:export
9
   #:remove-entry-from-registry #:coerce-entry-to-directory
10
   #:coerce-name #:primary-system-name #:coerce-filename
11
   #:find-system #:locate-system #:load-asd #:with-system-definitions
12
   #:system-registered-p #:register-system #:registered-systems #:clear-system #:map-systems
13
   #:missing-component #:missing-requires #:missing-parent
14
15
16
17
   #:formatted-system-definition-error #:format-control #:format-arguments #:sysdef-error
   #:load-system-definition-error #:error-name #:error-pathname #:error-condition
   #:*system-definition-search-functions* #:search-for-system-definition
   #:*central-registry* #:probe-asd #:sysdef-central-registry-search
18
   #:find-system-if-being-defined #:*systems-being-defined*
19
   #:contrib-sysdef-search #:sysdef-find-asdf ;; backward compatibility symbols, functions removed
20
   #:sysdef-preloaded-system-search #:register-preloaded-system #:*preloaded-systems*
21
   #:clear-defined-systems #:*defined-systems*
22
23
24
25
   ;; defined in source-registry, but specially mentioned here:
   #:initialize-source-registry #:sysdef-source-registry-search))
(in-package :asdf/find-system)

26
27
28
29
30
31
32
33
34
35
36
(with-upgradability ()
  (declaim (ftype (function (&optional t) t) initialize-source-registry)) ; forward reference

  (define-condition missing-component (system-definition-error)
    ((requires :initform "(unnamed)" :reader missing-requires :initarg :requires)
     (parent :initform nil :reader missing-parent :initarg :parent)))

  (define-condition formatted-system-definition-error (system-definition-error)
    ((format-control :initarg :format-control :reader format-control)
     (format-arguments :initarg :format-arguments :reader format-arguments))
    (:report (lambda (c s)
37
38
               (apply 'format s (format-control c) (format-arguments c)))))

39
40
41
42
43
44
45
  (define-condition load-system-definition-error (system-definition-error)
    ((name :initarg :name :reader error-name)
     (pathname :initarg :pathname :reader error-pathname)
     (condition :initarg :condition :reader error-condition))
    (:report (lambda (c s)
               (format s (compatfmt "~@<Error while trying to load definition for system ~A from pathname ~A: ~3i~_~A~@:>")
                       (error-name c) (error-pathname c) (error-condition c)))))
46

47
48
49
  (defun sysdef-error (format &rest arguments)
    (error 'formatted-system-definition-error :format-control
           format :format-arguments arguments))
50

51
52
53
54
55
56
57
58
59
60
61
62
  (defun coerce-name (name)
    (typecase name
      (component (component-name name))
      (symbol (string-downcase (symbol-name name)))
      (string name)
      (t (sysdef-error (compatfmt "~@<Invalid component designator: ~3i~_~A~@:>") name))))

  (defun primary-system-name (name)
    ;; When a system name has slashes, the file with defsystem is named by
    ;; the first of the slash-separated components.
    (first (split-string (coerce-name name) :separator "/")))

63
64
65
  (defun coerce-filename (name)
    (frob-substrings (coerce-name name) '("/" ":" "\\") "--"))

66
67
68
69
70
71
72
  (defvar *defined-systems* (make-hash-table :test 'equal)
    "This is a hash table whose keys are strings, being the
names of the systems, and whose values are pairs, the first
element of which is a universal-time indicating when the
system definition was last updated, and the second element
of which is a system object.")

73
74
75
76
77
78
79
  (defun system-registered-p (name)
    (gethash (coerce-name name) *defined-systems*))

  (defun registered-systems ()
    (loop :for registered :being :the :hash-values :of *defined-systems*
          :collect (coerce-name (cdr registered))))

80
81
82
83
84
85
86
87
88
89
90
91
92
  (defun check-acceptable-system-name (name)
    (flet ((acceptable-char-p (c)
                              (or
                               ;; these two are acceptable characters for logical pathnames
                               (alphanumericp c)
                               (eql c #\-)
                               ;; forward slash has a special meaning to ASDF
                               (eql c #\/))))
      (let ((unacceptable (find-if-not #'acceptable-char-p name)))
        (when unacceptable
          (sysdef-error (compatfmt "~@<Bad character for system name: ~c in ~3i~_~A~@:>") unacceptable name)))
      t))

93
94
95
96
  (defun register-system (system)
    (check-type system system)
    (let ((name (component-name system)))
      (check-type name string)
97
      (check-acceptable-system-name name)
98
99
100
101
102
103
104
      (asdf-message (compatfmt "~&~@<; ~@;Registering ~3i~_~A~@:>~%") system)
      (unless (eq system (cdr (gethash name *defined-systems*)))
        (setf (gethash name *defined-systems*)
              (cons (if-let (file (ignore-errors (system-source-file system)))
                      (get-file-stamp file))
                    system)))))

105
106
107
108
109
110
  (defun clear-defined-systems ()
    ;; Invalidate all systems but ASDF itself, if registered.
    (let ((asdf (cdr (system-registered-p :asdf))))
      (setf *defined-systems* (make-hash-table :test 'equal))
      (when asdf
        (setf (component-version asdf) *asdf-version*)
111
        (setf (builtin-system-p asdf) t)
112
113
114
115
116
        (register-system asdf)))
    (values))

  (register-hook-function '*post-upgrade-cleanup-hook* 'clear-defined-systems nil)

117
118
  (defun clear-system (name)
    "Clear the entry for a system in the database of systems previously loaded.
119
Note that this does NOT in any way cause the code of the system to be unloaded."
120
121
122
123
    ;; There is no "unload" operation in Common Lisp, and
    ;; a general such operation cannot be portably written,
    ;; considering how much CL relies on side-effects to global data structures.
    (remhash (coerce-name name) *defined-systems*))
124

125
126
  (defun map-systems (fn)
    "Apply FN to each defined system.
127
128
129

FN should be a function of one argument. It will be
called with an object of type asdf:system."
130
131
    (loop :for registered :being :the :hash-values :of *defined-systems*
          :do (funcall fn (cdr registered)))))
132
133
134

;;; for the sake of keeping things reasonably neat, we adopt a
;;; convention that functions in this list are prefixed SYSDEF-
135
136
137
138
139
140
141
(with-upgradability ()
  (defvar *system-definition-search-functions* '())

  (defun cleanup-system-definition-search-functions ()
    (setf *system-definition-search-functions*
          (append
           ;; Remove known-incompatible sysdef functions from old versions of asdf.
142
           (remove-if #'(lambda (x) (member x '(contrib-sysdef-search sysdef-find-asdf sysdef-preloaded-system-search)))
143
144
145
146
147
148
149
                      *system-definition-search-functions*)
           ;; Tuck our defaults at the end of the list if they were absent.
           ;; This is imperfect, in case they were removed on purpose,
           ;; but then it will be the responsibility of whoever does that
           ;; to upgrade asdf before he does such a thing rather than after.
           (remove-if #'(lambda (x) (member x *system-definition-search-functions*))
                      '(sysdef-central-registry-search
150
                        sysdef-source-registry-search)))))
151
152
153
  (cleanup-system-definition-search-functions)

  (defun search-for-system-definition (system)
154
155
156
157
158
    (let ((name (coerce-name system)))
      (flet ((try (f) (if-let ((x (funcall f name))) (return-from search-for-system-definition x))))
        (try 'find-system-if-being-defined)
        (map () #'try *system-definition-search-functions*)
        (try 'sysdef-preloaded-system-search))))
159
160
161

  (defvar *central-registry* nil
    "A list of 'system directory designators' ASDF uses to find systems.
162
163
164
165
166
167
168
169
170

A 'system directory designator' is a pathname or an expression
which evaluates to a pathname. For example:

    (setf asdf:*central-registry*
          (list '*default-pathname-defaults*
                #p\"/home/me/cl/systems/\"
                #p\"/usr/share/common-lisp/systems/\"))

171
This is for backward compatibility.
172
173
174
Going forward, we recommend new users should be using the source-registry.
")

175
  (defun probe-asd (name defaults &key truename)
176
    (block nil
177
178
179
180
181
182
183
184
185
      (when (directory-pathname-p defaults)
        (if-let (file (probe-file*
                       (ensure-absolute-pathname
                        (parse-unix-namestring name :type "asd")
                        #'(lambda () (ensure-absolute-pathname defaults 'get-pathname-defaults nil))
                        nil)
                       :truename truename))
          (return file))
        #-(or clisp genera) ; clisp doesn't need it, plain genera doesn't have read-sequence(!)
186
        (when (and (os-windows-p) (physical-pathname-p defaults))
187
188
189
190
191
192
          (let ((shortcut
                  (make-pathname
                   :defaults defaults :case :local
                   :name (strcat name ".asd")
                   :type "lnk")))
            (when (probe-file* shortcut)
193
              (ensure-pathname (parse-windows-shortcut shortcut) :namestring :native)))))))
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

  (defun sysdef-central-registry-search (system)
    (let ((name (primary-system-name system))
          (to-remove nil)
          (to-replace nil))
      (block nil
        (unwind-protect
             (dolist (dir *central-registry*)
               (let ((defaults (eval dir))
                     directorized)
                 (when defaults
                   (cond ((directory-pathname-p defaults)
                          (let* ((file (probe-asd name defaults :truename *resolve-symlinks*)))
                            (when file
                              (return file))))
                         (t
                          (restart-case
                              (let* ((*print-circle* nil)
                                     (message
                                       (format nil
                                               (compatfmt "~@<While searching for system ~S: ~3i~_~S evaluated to ~S which is not an absolute directory.~@:>")
                                               system dir defaults)))
                                (error message))
                            (remove-entry-from-registry ()
                              :report "Remove entry from *central-registry* and continue"
                              (push dir to-remove))
                            (coerce-entry-to-directory ()
                              :test (lambda (c) (declare (ignore c))
                                      (and (not (directory-pathname-p defaults))
                                           (directory-pathname-p
                                            (setf directorized
                                                  (ensure-directory-pathname defaults)))))
                              :report (lambda (s)
                                        (format s (compatfmt "~@<Coerce entry to ~a, replace ~a and continue.~@:>")
                                                directorized dir))
                              (push (cons dir directorized) to-replace))))))))
          ;; cleanup
          (dolist (dir to-remove)
            (setf *central-registry* (remove dir *central-registry*)))
          (dolist (pair to-replace)
            (let* ((current (car pair))
                   (new (cdr pair))
                   (position (position current *central-registry*)))
              (setf *central-registry*
                    (append (subseq *central-registry* 0 position)
                            (list new)
                            (subseq *central-registry* (1+ position))))))))))

242
243
  (defvar *preloaded-systems* (make-hash-table :test 'equal))

244
245
246
247
248
  (defun make-preloaded-system (name keys)
    (apply 'make-instance (getf keys :class 'system)
           :name name :source-file (getf keys :source-file)
           (remove-plist-keys '(:class :name :source-file) keys)))

249
250
251
252
  (defun sysdef-preloaded-system-search (requested)
    (let ((name (coerce-name requested)))
      (multiple-value-bind (keys foundp) (gethash name *preloaded-systems*)
        (when foundp
253
          (make-preloaded-system name keys)))))
254
255
256
257

  (defun register-preloaded-system (system-name &rest keys)
    (setf (gethash (coerce-name system-name) *preloaded-systems*) keys))

258
  (dolist (s '("asdf" "uiop" "asdf-driver" "asdf-defsystem" "asdf-package-system"))
259
    ;; don't bother with these, no one relies on them: "asdf-utils" "asdf-bundle"
260
    (register-preloaded-system s :version *asdf-version*))
261

262
263
264
265
266
267
268
269
270
271
272
273
  (defmethod find-system ((name null) &optional (error-p t))
    (when error-p
      (sysdef-error (compatfmt "~@<NIL is not a valid system name~@:>"))))

  (defmethod find-system (name &optional (error-p t))
    (find-system (coerce-name name) error-p))

  (defvar *systems-being-defined* nil
    "A hash-table of systems currently being defined keyed by name, or NIL")

  (defun find-system-if-being-defined (name)
    (when *systems-being-defined*
274
275
      ;; notable side effect: mark the system as being defined, to avoid infinite loops
      (ensure-gethash (coerce-name name) *systems-being-defined* nil)))
276
277
278
279
280
281
282

  (defun call-with-system-definitions (thunk)
    (if *systems-being-defined*
        (call-with-asdf-cache thunk)
        (let ((*systems-being-defined* (make-hash-table :test 'equal)))
          (call-with-asdf-cache thunk))))

283
284
285
286
287
288
  (defun clear-systems-being-defined ()
    (when *systems-being-defined*
      (clrhash *systems-being-defined*)))

  (register-hook-function '*post-upgrade-cleanup-hook* 'clear-systems-being-defined)

289
290
291
  (defmacro with-system-definitions ((&optional) &body body)
    `(call-with-system-definitions #'(lambda () ,@body)))

292
  (defun load-asd (pathname &key name (external-format (encoding-external-format (detect-encoding pathname))) &aux (readtable *readtable*) (print-pprint-dispatch *print-pprint-dispatch*))
293
294
295
296
    ;; Tries to load system definition with canonical NAME from PATHNAME.
    (with-system-definitions ()
      (with-standard-io-syntax
        (let ((*package* (find-package :asdf-user))
297
              ;; Note that our backward-compatible *readtable* is
298
              ;; a global readtable that gets globally side-effected. Ouch.
299
              ;; Same for the *print-pprint-dispatch* table.
300
301
              ;; We should do something about that for ASDF3 if possible, or else ASDF4.
              (*readtable* readtable)
302
              (*print-pprint-dispatch* print-pprint-dispatch)
303
304
305
              (*print-readably* nil)
              (*default-pathname-defaults*
                ;; resolve logical-pathnames so they won't wreak havoc in parsing namestrings.
306
                (pathname-directory-pathname (physicalize-pathname pathname))))
307
308
309
310
311
312
313
          (handler-bind
              ((error #'(lambda (condition)
                          (error 'load-system-definition-error
                                 :name name :pathname pathname
                                 :condition condition))))
            (asdf-message (compatfmt "~&~@<; ~@;Loading system definition~@[ for ~A~] from ~A~@:>~%")
                          name pathname)
314
            (load* pathname :external-format external-format))))))
315

316
317
318
319
320
321
322
323
324
  (defvar *old-asdf-systems* (make-hash-table :test 'equal))

  (defun check-not-old-asdf-system (name pathname)
    (or (not (equal name "asdf"))
        (null pathname)
        (let* ((version-pathname (subpathname pathname "version.lisp-expr"))
               (version (and (probe-file* version-pathname :truename nil)
                             (read-file-form version-pathname)))
               (old-version (asdf-version)))
325
326
327
328
329
330
331
332
333
334
335
          (cond
            ((version< old-version version) t) ;; newer version: good!
            ((equal old-version version) nil) ;; same version: don't load, but don't warn
            (t ;; old version: bad
             (ensure-gethash
              (list (namestring pathname) version) *old-asdf-systems*
              #'(lambda ()
                 (let ((old-pathname
                         (if-let (pair (system-registered-p "asdf"))
                           (system-source-file (cdr pair)))))
                   (warn "~@<~
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        You are using ASDF version ~A ~:[(probably from (require \"asdf\") ~
        or loaded by quicklisp)~;from ~:*~S~] and have an older version of ASDF ~
        ~:[(and older than 2.27 at that)~;~:*~A~] registered at ~S. ~
        Having an ASDF installed and registered is the normal way of configuring ASDF to upgrade itself, ~
        and having an old version registered is a configuration error. ~
        ASDF will ignore this configured system rather than downgrade itself. ~
        In the future, you may want to either: ~
        (a) upgrade this configured ASDF to a newer version, ~
        (b) install a newer ASDF and register it in front of the former in your configuration, or ~
        (c) uninstall or unregister this and any other old version of ASDF from your configuration. ~
        Note that the older ASDF might be registered implicitly through configuration inherited ~
        from your system installation, in which case you might have to specify ~
        :ignore-inherited-configuration in your in your ~~/.config/common-lisp/source-registry.conf ~
        or other source-registry configuration file, environment variable or lisp parameter. ~
        Indeed, a likely offender is an obsolete version of the cl-asdf debian or ubuntu package, ~
        that you might want to upgrade (if a recent enough version is available) ~
        or else remove altogether (since most implementations ship with a recent asdf); ~
        if you lack the system administration rights to upgrade or remove this package, ~
        then you might indeed want to either install and register a more recent version, ~
        or use :ignore-inherited-configuration to avoid registering the old one. ~
        Please consult ASDF documentation and/or experts.~@:>~%"
357
358
                         old-version old-pathname version pathname))))
             nil))))) ;; only issue the warning the first time, but always return nil
359

360
361
  (defun locate-system (name)
    "Given a system NAME designator, try to locate where to load the system from.
362
363
364
365
366
367
368
369
Returns five values: FOUNDP FOUND-SYSTEM PATHNAME PREVIOUS PREVIOUS-TIME
FOUNDP is true when a system was found,
either a new unregistered one or a previously registered one.
FOUND-SYSTEM when not null is a SYSTEM object that may be REGISTER-SYSTEM'ed as is
PATHNAME when not null is a path from where to load the system,
either associated with FOUND-SYSTEM, or with the PREVIOUS system.
PREVIOUS when not null is a previously loaded SYSTEM object of same name.
PREVIOUS-TIME when not null is the time at which the PREVIOUS system was loaded."
370
371
372
373
374
375
376
    (let* ((name (coerce-name name))
           (in-memory (system-registered-p name)) ; load from disk if absent or newer on disk
           (previous (cdr in-memory))
           (previous (and (typep previous 'system) previous))
           (previous-time (car in-memory))
           (found (search-for-system-definition name))
           (found-system (and (typep found 'system) found))
377
378
379
380
381
           (pathname (ensure-pathname
                      (or (and (typep found '(or pathname string)) (pathname found))
                          (and found-system (system-source-file found-system))
                          (and previous (system-source-file previous)))
                     :want-absolute t :resolve-symlinks *resolve-symlinks*))
382
383
           (foundp (and (or found-system pathname previous) t)))
      (check-type found (or null pathname system))
384
385
386
387
388
389
390
      (unless (check-not-old-asdf-system name pathname)
        (cond
          (previous (setf found nil pathname nil))
          (t
           (setf found (sysdef-preloaded-system-search "asdf"))
           (assert (typep found 'system))
           (setf found-system found pathname nil))))
391
392
393
394
      (values foundp found-system pathname previous previous-time)))

  (defmethod find-system ((name string) &optional (error-p t))
    (with-system-definitions ()
395
396
397
398
      (let ((primary-name (primary-system-name name)))
        (unless (or (equal name primary-name)
                    (nth-value 1 (gethash primary-name *systems-being-defined*)))
          (find-system primary-name nil)))
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
      (loop
        (restart-case
            (multiple-value-bind (foundp found-system pathname previous previous-time)
                (locate-system name)
              (assert (eq foundp (and (or found-system pathname previous) t)))
              (let ((previous-pathname (and previous (system-source-file previous)))
                    (system (or previous found-system)))
                (when (and found-system (not previous))
                  (register-system found-system))
                (when (and system pathname)
                  (setf (system-source-file system) pathname))
                (when (and pathname
                           (let ((stamp (get-file-stamp pathname)))
                             (and stamp
                                  (not (and previous
                                            (or (pathname-equal pathname previous-pathname)
                                                (and pathname previous-pathname
                                                     (pathname-equal
417
418
                                                      (physicalize-pathname pathname)
                                                      (physicalize-pathname previous-pathname))))
419
                                            (stamp<= stamp previous-time))))))
420
                  ;; only load when it's a pathname that is different or has newer content, and not an old asdf
421
422
423
424
425
426
427
428
429
430
431
432
433
                  (load-asd pathname :name name)))
              (let ((in-memory (system-registered-p name))) ; try again after loading from disk if needed
                (return
                  (cond
                    (in-memory
                     (when pathname
                       (setf (car in-memory) (get-file-stamp pathname)))
                     (cdr in-memory))
                    (error-p
                     (error 'missing-component :requires name))))))
          (reinitialize-source-registry-and-retry ()
            :report (lambda (s)
                      (format s (compatfmt "~@<Retry finding system ~A after reinitializing the source-registry.~@:>") name))
434
            (initialize-source-registry)))))))
435