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

4 5
(asdf/package:define-package :asdf/find-system
  (:recycle :asdf/find-system :asdf)
6
  (:use :asdf/common-lisp :asdf/driver :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
11
   #:find-system #:locate-system #:load-asd #:with-system-definitions
12 13 14 15 16 17
   #:system-registered-p #:register-system #:registered-systems #:clear-system #:map-systems
   #:system-definition-error #:missing-component #:missing-requires #:missing-parent
   #: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 20
   #:contrib-sysdef-search #:sysdef-find-asdf ;; backward compatibility symbols, functions removed
   #:system-find-preloaded-system #: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
(declaim (ftype (function (&optional t) t) initialize-source-registry)) ; forward reference
27 28 29 30 31 32 33 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

(define-condition system-definition-error (error) ()
  ;; [this use of :report should be redundant, but unfortunately it's not.
  ;; cmucl's lisp::output-instance prefers the kernel:slot-class-print-function
  ;; over print-object; this is always conditions::%print-condition for
  ;; condition objects, which in turn does inheritance of :report options at
  ;; run-time.  fortunately, inheritance means we only need this kludge here in
  ;; order to fix all conditions that build on it.  -- rgr, 28-Jul-02.]
  #+cmu (:report print-object))

(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)
               (apply 'format s (format-control c) (format-arguments c)))))

(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)))))

(defun* sysdef-error (format &rest arguments)
  (error 'formatted-system-definition-error :format-control
         format :format-arguments arguments))

59
(defvar *defined-systems* (make-hash-table :test 'equal)
60 61 62 63 64 65
  "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.")

66 67 68 69 70
(defun* clear-defined-systems ()
  (setf *defined-systems* (make-hash-table :test 'equal)))

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

71 72 73 74 75 76 77
(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))))

78 79 80 81 82
(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 "/")))

83 84 85 86
(defun* system-registered-p (name)
  (gethash (coerce-name name) *defined-systems*))

(defun* registered-systems ()
87 88
  (loop :for registered :being :the :hash-values :of *defined-systems*
    :collect (coerce-name (cdr registered))))
89 90 91 92 93 94 95 96

(defun* register-system (system)
  (check-type system system)
  (let ((name (component-name system)))
    (check-type name string)
    (asdf-message (compatfmt "~&~@<; ~@;Registering ~3i~_~A~@:>~%") system)
    (unless (eq system (cdr (gethash name *defined-systems*)))
      (setf (gethash name *defined-systems*)
97
            (cons (if-let (file (ignore-errors (system-source-file system)))
98
                    (get-file-stamp file))
99
                  system)))))
100 101 102 103 104 105 106 107 108 109 110 111 112 113

(defun* clear-system (name)
  "Clear the entry for a system in the database of systems previously loaded.
Note that this does NOT in any way cause the code of the system to be unloaded."
  ;; 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*))

(defun* map-systems (fn)
  "Apply FN to each defined system.

FN should be a function of one argument. It will be
called with an object of type asdf:system."
114 115
  (loop :for registered :being :the :hash-values :of *defined-systems*
     :do (funcall fn (cdr registered))))
116 117 118 119 120 121

;;; for the sake of keeping things reasonably neat, we adopt a
;;; convention that functions in this list are prefixed SYSDEF-

(defvar *system-definition-search-functions* '())

122 123 124 125 126 127 128 129 130 131 132 133 134
(defun cleanup-system-definition-search-functions ()
  (setf *system-definition-search-functions*
        (append
         ;; Remove known-incompatible sysdef functions from old versions of asdf.
         (remove-if #'(lambda (x) (member x '(contrib-sysdef-search sysdef-find-asdf)))
                    *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
                      sysdef-source-registry-search
135
                      sysdef-find-preloaded-systems)))))
136
(cleanup-system-definition-search-functions)
137 138

(defun* search-for-system-definition (system)
139 140 141
  (some (let ((name (coerce-name system))) #'(lambda (x) (funcall x name)))
        (cons 'find-system-if-being-defined
              *system-definition-search-functions*)))
142 143 144 145 146 147 148 149 150 151 152 153

(defvar *central-registry* nil
"A list of 'system directory designators' ASDF uses to find systems.

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/\"))

154
This is for backward compatibility.
155 156 157
Going forward, we recommend new users should be using the source-registry.
")

158
(defun* probe-asd (name defaults &key truename)
159 160
  (block nil
    (when (directory-pathname-p defaults)
161
      (if-let (file (probe-file*
162
                     (ensure-absolute-pathname
163
                      (parse-unix-namestring name :type "asd")
164
                      #'(lambda () (ensure-absolute-pathname defaults 'get-pathname-defaults nil))
165 166 167
                      nil)
                     :truename truename))
        (return file))
168 169 170 171
      #-(or clisp genera) ; clisp doesn't need it, plain genera doesn't have read-sequence(!)
      (when (os-windows-p)
        (let ((shortcut
               (make-pathname
172
                :defaults defaults :case :local
173 174 175 176 177 178 179 180
                :name (strcat name ".asd")
                :type "lnk")))
          (when (probe-file* shortcut)
            (let ((target (parse-windows-shortcut shortcut)))
              (when target
                (return (pathname target))))))))))

(defun* sysdef-central-registry-search (system)
181
  (let ((name (primary-system-name system))
182 183 184 185 186
        (to-remove nil)
        (to-replace nil))
    (block nil
      (unwind-protect
           (dolist (dir *central-registry*)
187 188
             (let ((defaults (eval dir))
                   directorized)
189
               (when defaults
190
                 (cond ((directory-pathname-p defaults)
191
                        (let* ((file (probe-asd name defaults :truename *resolve-symlinks*)))
192 193 194 195 196 197 198
                          (when file
                            (return file))))
                       (t
                        (restart-case
                            (let* ((*print-circle* nil)
                                   (message
                                    (format nil
199
                                            (compatfmt "~@<While searching for system ~S: ~3i~_~S evaluated to ~S which is not an absolute directory.~@:>")
200 201 202 203 204 205
                                            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 ()
206 207 208 209 210
                            :test (lambda (c) (declare (ignore c))
                                    (and (not (directory-pathname-p defaults))
                                         (directory-pathname-p
                                          (setf directorized
                                                (ensure-directory-pathname defaults)))))
211 212
                            :report (lambda (s)
                                      (format s (compatfmt "~@<Coerce entry to ~a, replace ~a and continue.~@:>")
213 214
                                              directorized dir))
                            (push (cons dir directorized) to-replace))))))))
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
        ;; 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))))))))))

(defmethod find-system ((name null) &optional (error-p t))
  (declare (ignorable name))
  (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*
    (gethash (coerce-name name) *systems-being-defined*)))

(defun* call-with-system-definitions (thunk)
  (if *systems-being-defined*
244
      (call-with-asdf-cache thunk)
245
      (let ((*systems-being-defined* (make-hash-table :test 'equal)))
246
        (call-with-asdf-cache thunk))))
247 248 249 250

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

251
(defun* load-asd (pathname &key name (external-format (encoding-external-format (detect-encoding pathname))))
252 253
  ;; Tries to load system definition with canonical NAME from PATHNAME.
  (with-system-definitions ()
254 255
    (with-standard-io-syntax
      (let ((*package* (find-package :asdf-user))
256
            (*print-readably* nil)
257 258 259 260 261 262 263 264 265 266 267 268
            (*default-pathname-defaults*
              ;; resolve logical-pathnames so they won't wreak havoc in parsing namestrings.
              (pathname-directory-pathname (translate-logical-pathname pathname))))
        (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)
          (with-muffled-loader-conditions ()
            (load* pathname :external-format external-format)))))))
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

(defun* locate-system (name)
  "Given a system NAME designator, try to locate where to load the system from.
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."
  (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))
         (pathname (or (and (typep found '(or pathname string)) (pathname found))
                       (and found-system (system-source-file found-system))
                       (and previous (system-source-file previous))))
290
         (pathname (ensure-pathname (resolve-symlinks* pathname) :want-absolute t))
291 292
         (foundp (and (or found-system pathname previous) t)))
    (check-type found (or null pathname system))
293 294
    (values foundp found-system pathname previous previous-time)))

295 296 297 298 299 300
(defmethod find-system ((name string) &optional (error-p t))
  (with-system-definitions ()
    (loop
      (restart-case
          (multiple-value-bind (foundp found-system pathname previous previous-time)
              (locate-system name)
301
            (assert (eq foundp (and (or found-system pathname previous) t)))
302 303 304 305 306 307 308
            (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
309 310 311 312 313 314 315 316 317
                         (let ((stamp (get-file-stamp pathname)))
                           (and stamp
                                (not (and previous
                                          (or (pathname-equal pathname previous-pathname)
                                              (and pathname previous-pathname
                                                   (pathname-equal
                                                    (translate-logical-pathname pathname)
                                                    (translate-logical-pathname previous-pathname))))
                                          (stamp<= stamp previous-time))))))
318
                ;; only load when it's a pathname that is different or has newer content
319
                (load-asd pathname :name name)))
320 321 322 323 324
            (let ((in-memory (system-registered-p name))) ; try again after loading from disk if needed
              (return
                (cond
                  (in-memory
                   (when pathname
325
                     (setf (car in-memory) (get-file-stamp pathname)))
326 327 328 329 330 331 332 333
                   (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))
          (initialize-source-registry))))))

334
(defvar *preloaded-systems* (make-hash-table :test 'equal))
335

336 337 338 339 340
(defun* sysdef-find-preloaded-systems (requested)
  (let ((name (coerce-name requested)))
    (multiple-value-bind (keys foundp) (gethash name *preloaded-systems*)
      (when foundp
        (apply 'make-instance 'system :name name :source-file (getf keys :source-file) keys)))))
341

342 343 344 345 346
(defun register-preloaded-system (system-name &rest keys)
  (setf (gethash (coerce-name system-name) *preloaded-systems*) keys))

(register-preloaded-system "asdf")
(register-preloaded-system "asdf-driver")
347

348