asdf.lisp 232 KB
Newer Older
1
;; -*- mode: Common-Lisp; Base: 10 ; Syntax: ANSI-Common-Lisp ; coding: utf-8 -*-
2
;;; This is ASDF 2.26.61: Another System Definition Facility.
3
;;;
4
5
;;; Feedback, bug reports, and patches are all welcome:
;;; please mail to <asdf-devel@common-lisp.net>.
Francois-Rene Rideau's avatar
1.647:    
Francois-Rene Rideau committed
6
;;; Note first that the canonical source for ASDF is presently
7
;;; <URL:http://common-lisp.net/project/asdf/>.
8
9
10
11
12
;;;
;;; If you obtained this copy from anywhere else, and you experience
;;; trouble using it, or find bugs, you may want to check at the
;;; location above for a more recent version (and for documentation
;;; and test files, if your copy came without them) before reporting
13
14
15
;;; bugs.  There are usually two "supported" revisions - the git master
;;; branch is the latest development version, whereas the git release
;;; branch may be slightly older but is considered `stable'
16

17
;;; -- LICENSE START
18
;;; (This is the MIT / X Consortium license as taken from
19
20
21
;;;  http://www.opensource.org/licenses/mit-license.html on or about
;;;  Monday; July 13, 2009)
;;;
22
;;; Copyright (c) 2001-2012 Daniel Barlow and contributors
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
;;;
;;; Permission is hereby granted, free of charge, to any person obtaining
;;; a copy of this software and associated documentation files (the
;;; "Software"), to deal in the Software without restriction, including
;;; without limitation the rights to use, copy, modify, merge, publish,
;;; distribute, sublicense, and/or sell copies of the Software, and to
;;; permit persons to whom the Software is furnished to do so, subject to
;;; the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42
43
;;;
;;; -- LICENSE END
44

45
46
;;; The problem with writing a defsystem replacement is bootstrapping:
;;; we can't use defsystem to compile it.  Hence, all in one file.
Daniel Barlow's avatar
Daniel Barlow committed
47

48
49
#+xcvb (module ())

50
51
(cl:in-package :common-lisp-user)
#+genera (in-package :future-common-lisp-user)
52

53
#-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
54
(error "ASDF is not supported on your implementation. Please help us port it.")
55

56
57
58
59
60
61
;;;; Create and setup packages in a way that is compatible with hot-upgrade.
;;;; See https://bugs.launchpad.net/asdf/+bug/485687
;;;; See these two eval-when forms, and more near the end of the file.

(eval-when (:load-toplevel :compile-toplevel :execute)
  ;;; Before we do anything, some implementation-dependent tweaks
62
  ;; (declaim (optimize (speed 1) (debug 3) (safety 3))) ; NO: trust implementation defaults.
63
64
65
  #+allegro
  (setf excl::*autoload-package-name-alist*
        (remove "asdf" excl::*autoload-package-name-alist*
66
                :test 'equalp :key 'car)) ; need that BEFORE any mention of package ASDF as below
67
68
  #+gcl ;; Debian's GCL 2.7 has bugs with compiling multiple-value stuff, but can run ASDF 2.011
  (when (or (< system::*gcl-major-version* 2) ;; GCL 2.6 fails to fully compile ASDF at all
69
70
            (and (= system::*gcl-major-version* 2)
                 (< system::*gcl-minor-version* 7)))
71
72
    (pushnew 'ignorable pcl::*variable-declarations-without-argument*)
    (pushnew :gcl<2.7 *features*))
73
74
  #+(or abcl (and allegro ics) (and (or clisp cmu ecl mkcl) unicode)
        clozure lispworks (and sbcl sb-unicode) scl)
75
  (pushnew :asdf-unicode *features*)
76
77
78
79
  ;;; make package if it doesn't exist yet.
  ;;; DEFPACKAGE may cause errors on discrepancies, so we avoid it.
  (unless (find-package :asdf)
    (make-package :asdf :use '(:common-lisp))))
80

81
#+gcl (defpackage :asdf (:use :cl) ;; GCL treats defpackage magically and needs this
82
83
        #+gcl<2.7 (:shadowing-import-from :system :*load-pathname*) ;; GCL 2.6 sucks
        #+gcl<2.7 (:shadow :type-of))
84

85
(in-package :asdf)
86

87
(eval-when (:load-toplevel :compile-toplevel :execute)
88
  ;;; This would belong amongst implementation-dependent tweaks above,
89
  ;;; except that the defun has to be in package asdf.
90
91
  #+ecl (defun use-ecl-byte-compiler-p () (and (member :ecl-bytecmp *features*) t))
  #+ecl (unless (use-ecl-byte-compiler-p) (require :cmp))
92
  #+mkcl (require :cmp)
93
  #+mkcl (setq clos::*redefine-class-in-place* t) ;; Make sure we have strict ANSI class redefinition semantics
94

95
96
97
98
99
100
  ;;; Important notice for whom it concerns. The crux of the matter is that
  ;;; TRAVERSE can be completely refactored, and so after the find-system returns, it's too late.
  (defun asdf-upgrade-error ()
    (error "When a system transitively depends on ASDF, it must :defsystem-depends-on (:asdf)~%~
            Otherwise, when you upgrade ASDF, you must do it before you operate on any system.~%"))

101
  ;;; Package setup, step 2.
102
103
  (defvar *asdf-version* nil)
  (defvar *upgraded-p* nil)
104
  (defvar *asdf-verbose* nil) ; was t from 2.000 to 2.014.12.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
105
106
  (defun find-symbol* (s p)
    (find-symbol (string s) p))
107
108
  ;; Strip out formatting that is not supported on Genera.
  ;; Has to be inside the eval-when to make Lispworks happy (!)
109
110
  (defun strcat (&rest strings)
    (apply 'concatenate 'string strings))
111
  (defmacro compatfmt (format)
112
    #+(or gcl genera)
113
    (loop :for (unsupported . replacement) :in
114
115
      (append
       '(("~3i~_" . ""))
116
       #+(or genera gcl<2.7) '(("~@<" . "") ("; ~@;" . "; ") ("~@:>" . "") ("~:>" . ""))) :do
117
      (loop :for found = (search unsupported format) :while found :do
118
119
        (setf format (strcat (subseq format 0 found) replacement
                             (subseq format (+ found (length unsupported)))))))
120
    format)
121
  (let* (;; For bug reporting sanity, please always bump this version when you modify this file.
122
123
         ;; Please also modify asdf.asd to reflect this change. The script bin/bump-version
         ;; can help you do these changes in synch (look at the source for documentation).
124
         ;; Relying on its automation, the version is now redundantly present on top of this file.
125
126
         ;; "2.345" would be an official release
         ;; "2.345.6" would be a development version in the official upstream
127
128
         ;; "2.345.0.7" would be your seventh local modification of official release 2.345
         ;; "2.345.6.7" would be your seventh local modification of development version 2.345.6
129
         (asdf-version "2.26.61")
130
         (existing-asdf (find-class 'component nil))
131
         (existing-version *asdf-version*)
132
133
         (already-there (equal asdf-version existing-version)))
    (unless (and existing-asdf already-there)
134
      (when (and existing-asdf *asdf-verbose*)
135
        (format *trace-output*
136
137
                (compatfmt "~&~@<; ~@;Upgrading ASDF ~@[from version ~A ~]to version ~A~@:>~%")
                existing-version asdf-version))
138
      (labels
139
          ((present-symbol-p (symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
140
             (member (nth-value 1 (find-symbol* symbol package)) '(:internal :external)))
141
142
143
144
145
146
147
           (present-symbols (package)
             ;; #-genera (loop :for s :being :the :present-symbols :in package :collect s) #+genera
             (let (l)
               (do-symbols (s package)
                 (when (present-symbol-p s package) (push s l)))
               (reverse l)))
           (unlink-package (package)
148
149
             (let ((u (find-package package)))
               (when u
150
                 (ensure-unintern u (present-symbols u))
151
152
153
                 (loop :for p :in (package-used-by-list u) :do
                   (unuse-package u p))
                 (delete-package u))))
154
           (ensure-exists (name nicknames use)
155
156
157
158
159
160
161
162
163
164
             (let ((previous
                    (remove-duplicates
                     (mapcar #'find-package (cons name nicknames))
                     :from-end t)))
               ;; do away with packages with conflicting (nick)names
               (map () #'unlink-package (cdr previous))
               ;; reuse previous package with same name
               (let ((p (car previous)))
                 (cond
                   (p
165
166
                    (rename-package p name nicknames)
                    (ensure-use p use)
167
168
169
                    p)
                   (t
                    (make-package name :nicknames nicknames :use use))))))
170
171
172
           (intern* (symbol package)
             (intern (string symbol) package))
           (remove-symbol (symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
173
             (let ((sym (find-symbol* symbol package)))
174
               (when sym
175
                 #-cormanlisp (unexport sym package)
176
177
                 (unintern sym package)
                 sym)))
178
           (ensure-unintern (package symbols)
179
180
181
182
183
             (loop :with packages = (list-all-packages)
               :for sym :in symbols
               :for removed = (remove-symbol sym package)
               :when removed :do
               (loop :for p :in packages :do
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
184
                 (when (eq removed (find-symbol* sym p))
185
                   (unintern removed p)))))
186
187
188
           (ensure-shadow (package symbols)
             (shadow symbols package))
           (ensure-use (package use)
189
190
191
192
193
194
             (dolist (used (package-use-list package))
               (unless (member (package-name used) use :test 'string=)
                 (unuse-package used)
                 (do-external-symbols (sym used)
                   (when (eq sym (find-symbol* sym package))
                     (remove-symbol sym package)))))
195
196
             (dolist (used (reverse use))
               (do-external-symbols (sym used)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
197
                 (unless (eq sym (find-symbol* sym package))
198
199
                   (remove-symbol sym package)))
               (use-package used package)))
200
201
202
203
204
205
206
207
           #-ecl (ensure-fmakunbound (package symbols)
                   (loop :for name :in symbols
                         :for sym = (find-symbol* name package)
                         :when sym :do (fmakunbound sym)))
           #-ecl (ensure-fmakunbound-setf (package symbols)
                   (loop :for name :in symbols
                         :for sym = (find-symbol* name package)
                         :when sym :do #-gcl (fmakunbound `(setf ,sym))))
208
           (ensure-export (package export)
209
210
211
             (let ((formerly-exported-symbols nil)
                   (bothly-exported-symbols nil)
                   (newly-exported-symbols nil))
212
               (do-external-symbols (sym package)
213
214
215
216
                 (if (member sym export :test 'string-equal)
                     (push sym bothly-exported-symbols)
                     (push sym formerly-exported-symbols)))
               (loop :for sym :in export :do
217
                 (unless (member sym bothly-exported-symbols :test 'equal)
218
219
220
221
                   (push sym newly-exported-symbols)))
               (loop :for user :in (package-used-by-list package)
                 :for shadowing = (package-shadowing-symbols user) :do
                 (loop :for new :in newly-exported-symbols
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
222
                   :for old = (find-symbol* new user)
223
224
225
226
                   :when (and old (not (member old shadowing)))
                   :do (unintern old user)))
               (loop :for x :in newly-exported-symbols :do
                 (export (intern* x package)))))
227
           (ensure-package (name &key nicknames use unintern
228
                                 shadow export fmakunbound fmakunbound-setf)
229
             (let* ((p (ensure-exists name nicknames use)))
230
               #-ecl (ensure-fmakunbound p fmakunbound) #+ecl fmakunbound ;; do it later on ECL
231
               #-ecl (ensure-fmakunbound-setf p fmakunbound-setf) #+ecl fmakunbound-setf
232
               (ensure-unintern p unintern)
233
234
235
236
237
               (ensure-shadow p shadow)
               (ensure-export p export)
               p)))
        (macrolet
            ((pkgdcl (name &key nicknames use export
238
                           fmakunbound fmakunbound-setf unintern shadow)
239
240
                 `(ensure-package
                   ',name :nicknames ',nicknames :use ',use :export ',export
241
242
                   :shadow ',shadow :unintern ',unintern
                   :fmakunbound ',fmakunbound :fmakunbound-setf ',fmakunbound-setf)))
243
244
          (pkgdcl
           :asdf
245
           :use (:common-lisp)
246
           :fmakunbound
247
248
249
           (#:perform #:explain #:output-files #:operation-done-p #:traverse-action #:traverse
            #:compute-action-stamp #:component-load-dependencies #:action-valid-p
            #:component-depends-on #:perform-plan #:mark-operation-done #:operation-forced
250
            #:perform-with-restarts #:component-relative-pathname
251
            #:system-source-file #:operate #:find-system #:find-component #:find-operation
252
            #:apply-output-translations #:translate-pathname* #:resolve-location
253
            #:system-relative-pathname #:source-file-type #:builtin-system-p
254
            #:inherit-source-registry #:process-source-registry #:process-source-registry-directive
255
            #:compile-file* #:source-file-type #:trivial-system-p)
256
           :fmakunbound-setf (#:output-translations)
257
           :unintern
258
259
           (#:*asdf-revision* #:around #:asdf-method-combination #:split #:make-collector
            #:do-dep #:do-one-dep #:do-traverse #:visit-action #:component-visited-p
260
            #+(or ecl mkcl) #:output-files #+ecl #:trivial-system-p
261
            #:loaded-systems ; makes for annoying SLIME completion
262
            #:output-files-for-system-and-operation) ; obsolete ASDF-BINARY-LOCATION function
263
           :export
264
           (#:defsystem #:find-system #:locate-system #:coerce-name
265
            #:oos #:operate #:traverse #:perform-plan
266
            #:system-definition-pathname #:with-system-definitions
267
            #:search-for-system-definition #:find-component #:component-find-path
268
269
            #:compile-system #:load-system #:load-systems
            #:require-system #:test-system #:clear-system
270
271
272
273
            #:operation #:upward-operation #:downward-operation
            #:load-op #:prepare-op #:compile-op #:load-fasl-op
            #:prepare-source-op #:load-source-op #:test-op
            #:feature #:version #:version-satisfies #:upgrade-asdf
274
            #:implementation-identifier #:implementation-type #:hostname
275
            #:input-files #:output-files #:output-file #:perform
276
            #:operation-done-p #:explain #:component-sibling-dependencies
277
278
279
280
281
282
283
284
            #:component-load-dependencies #:run-shell-command ; deprecated, do not use
            #:precompiled-system #:compiled-file
            #+ecl #:make-build #+mkcl #:bundle-system

            #:component #:parent-component #:child-component #:system #:module
            #:source-file #:c-source-file #:java-source-file
            #:cl-source-file #:cl-source-file.cl #:cl-source-file.lsp
            #:static-file #:doc-file #:html-file :text-file
285
286
287
            #:source-file-type
            #:unix-dso

288
289
            #:component-children          ; component accessors
            #:component-children-by-name
290
291
292
293
294
295
296
            #:component-pathname
            #:component-relative-pathname
            #:component-name
            #:component-version
            #:component-parent
            #:component-property
            #:component-system
297
298
            #:component-encoding
            #:component-external-format
299

300
301
302
            #:component-depends-on ; backward-compatible name rather than action-depends-on
            #:module-components ; backward-compatibility
            #:operation-on-warnings #:operation-on-failure ; backward-compatibility
303

304
305
306
307
308
309
310
311
312
313
314
            #:system-description
            #:system-long-description
            #:system-author
            #:system-maintainer
            #:system-license
            #:system-licence
            #:system-source-file
            #:system-source-directory
            #:system-relative-pathname
            #:map-systems

315
            #:operation-description
316
317
318

            #:*system-definition-search-functions*   ; variables
            #:*central-registry*
319
320
321
            #:*compile-file-warnings-behaviour*
            #:*compile-file-failure-behaviour*
            #:*resolve-symlinks*
322
            #:*load-system-operation*
323
            #:*asdf-verbose*
324
            #:*verbose-out*
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

            #:asdf-version

            #:operation-error #:compile-failed #:compile-warned #:compile-error
            #:error-name
            #:error-pathname
            #:load-system-definition-error
            #:error-component #:error-operation
            #:system-definition-error
            #:missing-component
            #:missing-component-of-version
            #:missing-dependency
            #:missing-dependency-of-version
            #:circular-dependency        ; errors
            #:duplicate-names

            #:try-recompiling
            #:retry
            #:accept                     ; restarts
            #:coerce-entry-to-directory
            #:remove-entry-from-registry

347
            #:*encoding-detection-hook*
348
349
350
351
            #:*encoding-external-format-hook*
            #:*default-encoding*
            #:*utf-8-external-format*

352
            #:clear-configuration
353
            #:*output-translations-parameter*
354
355
356
357
358
            #:initialize-output-translations
            #:disable-output-translations
            #:clear-output-translations
            #:ensure-output-translations
            #:apply-output-translations
359
            #:compile-file*
360
361
362
            #:compile-file-pathname*
            #:enable-asdf-binary-locations-compatibility
            #:*default-source-registries*
363
            #:*source-registry-parameter*
364
365
366
367
            #:initialize-source-registry
            #:compute-source-registry
            #:clear-source-registry
            #:ensure-source-registry
368
            #:process-source-registry
369
            #:system-registered-p #:registered-systems #:already-loaded-systems
370
            #:resolve-location
371
            #:asdf-message
372
373
374
375
376
377
378
            #:user-output-translations-pathname
            #:system-output-translations-pathname
            #:user-output-translations-directory-pathname
            #:system-output-translations-directory-pathname
            #:user-source-registry
            #:system-source-registry
            #:user-source-registry-directory
379
            #:system-source-registry-directory)))
380
        #+genera (import 'scl:boolean :asdf)
381
382
383
384
        (setf *asdf-version* asdf-version
              *upgraded-p* (if existing-version
                               (cons existing-version *upgraded-p*)
                               *upgraded-p*))))))
Daniel Barlow's avatar
Daniel Barlow committed
385

386
387
388
;;;; -------------------------------------------------------------------------
;;;; User-visible parameters
;;;;
389
390
391
(defvar *resolve-symlinks* t
  "Determine whether or not ASDF resolves symlinks when defining systems.

392
Defaults to T.")
393

394
395
396
397
(defvar *compile-file-warnings-behaviour*
  (or #+clisp :ignore :warn)
  "How should ASDF react if it encounters a warning when compiling a file?
Valid values are :error, :warn, and :ignore.")
398

399
(defvar *compile-file-failure-behaviour*
400
  (or #+(or mkcl sbcl) :error #+clisp :ignore :warn)
401
402
403
  "How should ASDF react if it encounters a failure (per the ANSI spec of COMPILE-FILE)
when compiling a file?  Valid values are :error, :warn, and :ignore.
Note that ASDF ALWAYS raises an error if it fails to create an output file when compiling.")
Daniel Barlow's avatar
   
Daniel Barlow committed
404

405
(defvar *verbose-out* nil)
406

407
(defparameter +asdf-methods+
408
  '(perform-with-restarts perform explain output-files operation-done-p))
409

410
411
412
413
414
(defvar *load-system-operation* 'load-op
  "Operation used by ASDF:LOAD-SYSTEM. By default, ASDF:LOAD-OP.
You may override it with e.g. ASDF:LOAD-FASL-OP from asdf-bundle,
or ASDF:LOAD-SOURCE-OP if your fasl loading is somehow broken.")

415
(defvar *compile-file-function* 'compile-file*
416
417
  "Function used to compile lisp files.")

418
#+allegro
419
(eval-when (:compile-toplevel :execute)
420
421
422
423
424
425
  (defparameter *acl-warn-save*
                (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
                  excl:*warn-on-nested-reader-conditionals*))
  (when (boundp 'excl:*warn-on-nested-reader-conditionals*)
    (setf excl:*warn-on-nested-reader-conditionals* nil)))

426
427
428
429
430
431
432
433
;;;; -------------------------------------------------------------------------
;;;; Resolve forward references

(declaim (ftype (function (t) t)
                format-arguments format-control
                error-name error-pathname error-condition
                duplicate-names-name
                error-component error-operation
434
435
                component-children component-children-by-name
                circular-dependency-actions
436
437
438
                condition-arguments condition-form
                condition-format condition-location
                coerce-name)
439
         (ftype (function (&optional t) (values)) initialize-source-registry)
440
         #-(or cormanlisp gcl<2.7)
441
         (ftype (function (t t) t) (setf component-children-by-name)))
442
443

;;;; -------------------------------------------------------------------------
444
;;;; Compatibility various implementations
445
446
447
#+cormanlisp
(progn
  (deftype logical-pathname () nil)
448
  (defun make-broadcast-stream () *error-output*)
449
  (defun translate-logical-pathname (x) x)
450
  (defun file-namestring (p)
451
    (setf p (pathname p))
452
    (format nil "~@[~A~]~@[.~A~]" (pathname-name p) (pathname-type p))))
453

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#.(or #+mcl ;; the #$ doesn't work on other lisps, even protected by #+mcl
      (read-from-string
       "(eval-when (:compile-toplevel :load-toplevel :execute)
          (ccl:define-entry-point (_getenv \"getenv\") ((name :string)) :string)
          (ccl:define-entry-point (_system \"system\") ((name :string)) :int)
          ;; Note: ASDF may expect user-homedir-pathname to provide
          ;; the pathname of the current user's home directory, whereas
          ;; MCL by default provides the directory from which MCL was started.
          ;; See http://code.google.com/p/mcl/wiki/Portability
          (defun current-user-homedir-pathname ()
            (ccl::findfolder #$kuserdomain #$kCurrentUserFolderType))
          (defun probe-posix (posix-namestring)
            \"If a file exists for the posix namestring, return the pathname\"
            (ccl::with-cstrs ((cpath posix-namestring))
              (ccl::rlet ((is-dir :boolean)
                          (fsref :fsref))
                (when (eq #$noerr (#_fspathmakeref cpath fsref is-dir))
                  (ccl::%path-from-fsref fsref is-dir))))))"))

473
474
475
476
477
478
#+genera
(unless (fboundp 'ensure-directories-exist)
  (defun ensure-directories-exist (path)
    (fs:create-directories-recursively (pathname path))))

#+gcl<2.7
479
(progn ;; Doesn't support either logical-pathnames or output-translations.
480
481
482
483
484
485
486
487
  (deftype logical-pathname () nil)
  (defun wild-pathname-p (path) (declare (ignore path)) nil)
  (defun translate-logical-pathname (x) x)
  (defvar *compile-file-pathname* nil)
  (defun pathname-match-p (in-pathname wild-pathname)
    (declare (ignore in-wildname wild-wildname)) nil)
  (defun translate-pathname (source from-wildname to-wildname &key)
    (declare (ignore from-wildname to-wildname)) source)
488
  (defun type-of (x) (class-name (class-of x)))
489
  (defun %print-unreadable-object (object stream type identity thunk)
490
    (format stream "#<~@[~S ~]" (when type (type-of object)))
491
492
493
494
495
496
497
498
499
    (funcall thunk)
    (format stream "~@[ ~X~]>" (when identity (system:address object))))
  (defmacro with-compilation-unit (options &body body)
    (declare (ignore options)) `(progn ,@body))
  (defmacro print-unreadable-object ((object stream &key type identity) &body body)
    `(%print-unreadable-object ,object ,stream ,type ,identity (lambda () ,@body)))
  (defun ensure-directories-exist (path)
    (run-shell-command "mkdir -p ~S" (namestring (pathname-directory-pathname path)))))

500
;;;; -------------------------------------------------------------------------
501
502
;;;; General Purpose Utilities

503
504
505
506
(macrolet
    ((defdef (def* def)
       `(defmacro ,def* (name formals &rest rest)
          `(progn
507
508
509
             #+(or ecl gcl)
             ,(when (and #+gcl<2.7 (symbolp name))
                `(fmakunbound ',name))
510
511
512
             #-gcl ; gcl 2.7.0 notinline functions lose secondary return values :-(
             ,(when (and #+ecl (symbolp name)) ; fails for setf functions on ecl
                `(declaim (notinline ,name)))
513
514
515
             (,',def ,name ,formals ,@rest)))))
  (defdef defgeneric* defgeneric)
  (defdef defun* defun))
516

517
(defmacro while-collecting ((&rest collectors) &body body)
518
519
520
521
522
523
524
525
526
527
  "COLLECTORS should be a list of names for collections.  A collector
defines a function that, when applied to an argument inside BODY, will
add its argument to the corresponding collection.  Returns multiple values,
a list for each collection, in order.
   E.g.,
\(while-collecting \(foo bar\)
           \(dolist \(x '\(\(a 1\) \(b 2\) \(c 3\)\)\)
             \(foo \(first x\)\)
             \(bar \(second x\)\)\)\)
Returns two values: \(A B C\) and \(1 2 3\)."
528
529
530
  (let ((vars (mapcar #'(lambda (x) (gensym (symbol-name x))) collectors))
        (initial-values (mapcar (constantly nil) collectors)))
    `(let ,(mapcar #'list vars initial-values)
531
       (flet ,(mapcar #'(lambda (c v) `(,c (x) (push x ,v) (values))) collectors vars)
532
         ,@body
533
         (values ,@(mapcar #'(lambda (v) `(reverse ,v)) vars))))))
534

535
(defmacro aif (test then &optional else)
536
  "Anaphoric version of IF, On Lisp style"
537
538
  `(let ((it ,test)) (if it ,then ,else)))

539
(defun* pathname-directory-pathname (pathname)
540
541
  "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
and NIL NAME, TYPE and VERSION components"
542
543
  (when pathname
    (make-pathname :name nil :type nil :version nil :defaults pathname)))
544

545
(defun* normalize-pathname-directory-component (directory)
546
  "Given a pathname directory component, return an equivalent form that is a list"
547
  #+gcl<2.7 (setf directory (substitute :back :parent directory))
548
  (cond
549
    #-(or cmu sbcl scl) ;; these implementations already normalize directory components.
550
551
552
553
    ((stringp directory) `(:absolute ,directory))
    #+gcl<2.7
    ((and (consp directory) (eq :root (first directory)))
     `(:absolute ,@(rest directory)))
554
555
556
    ((or (null directory)
         (and (consp directory) (member (first directory) '(:absolute :relative))))
     directory)
557
558
559
    #+gcl<2.7
    ((consp directory)
     `(:relative ,@directory))
560
    (t
561
     (error (compatfmt "~@<Unrecognized pathname directory component ~S~@:>") directory))))
562
563

(defun* merge-pathname-directory-components (specified defaults)
564
  ;; Helper for merge-pathnames* that handles directory components.
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
  (let ((directory (normalize-pathname-directory-component specified)))
    (ecase (first directory)
      ((nil) defaults)
      (:absolute specified)
      (:relative
       (let ((defdir (normalize-pathname-directory-component defaults))
             (reldir (cdr directory)))
         (cond
           ((null defdir)
            directory)
           ((not (eq :back (first reldir)))
            (append defdir reldir))
           (t
            (loop :with defabs = (first defdir)
              :with defrev = (reverse (rest defdir))
              :while (and (eq :back (car reldir))
                          (or (and (eq :absolute defabs) (null defrev))
                              (stringp (car defrev))))
              :do (pop reldir) (pop defrev)
              :finally (return (cons defabs (append (reverse defrev) reldir)))))))))))

586
(defun* make-pathname-component-logical (x)
587
  "Make a pathname component suitable for use in a logical-pathname"
588
589
590
591
592
593
  (typecase x
    ((eql :unspecific) nil)
    #+clisp (string (string-upcase x))
    #+clisp (cons (mapcar 'make-pathname-component-logical x))
    (t x)))

594
595
(defun* denormalize-pathname-directory-component (directory-component)
  #-gcl<2.7 directory-component
596
  #+gcl<2.7
597
598
599
600
601
602
  (let ((d (substitute-if :parent (lambda (x) (member x '(:up :back)))
                          directory-component)))
    (cond
      ((and (consp d) (eq :relative (first d))) (rest d))
      ((and (consp d) (eq :absolute (first d))) `(:root ,@(rest d)))
      (t d))))
603

604
605
606
607
608
609
610
(defun* make-pathname* (&rest keys &key (directory nil directoryp)
                              host device name type version defaults)
  (declare (ignore host device name type version defaults))
  (apply 'make-pathname
         (append (when directoryp
                   `(:directory ,(denormalize-pathname-directory-component directory)))
                 keys)))
611

612
613
614
(defun* make-pathname-logical (pathname host)
  "Take a PATHNAME's directory, name, type and version components,
and make a new pathname with corresponding components and specified logical HOST"
615
  (make-pathname*
616
617
618
619
620
   :host host
   :directory (make-pathname-component-logical (pathname-directory pathname))
   :name (make-pathname-component-logical (pathname-name pathname))
   :type (make-pathname-component-logical (pathname-type pathname))
   :version (make-pathname-component-logical (pathname-version pathname))))
621

622
(defun* merge-pathnames* (specified &optional (defaults *default-pathname-defaults*))
623
624
625
626
627
  "MERGE-PATHNAMES* is like MERGE-PATHNAMES except that
if the SPECIFIED pathname does not have an absolute directory,
then the HOST and DEVICE both come from the DEFAULTS, whereas
if the SPECIFIED pathname does have an absolute directory,
then the HOST and DEVICE both come from the SPECIFIED.
628
629
630
Also, if either argument is NIL, then the other argument is returned unmodified."
  (when (null specified) (return-from merge-pathnames* defaults))
  (when (null defaults) (return-from merge-pathnames* specified))
631
632
633
  #+scl
  (ext:resolve-pathname specified defaults)
  #-scl
634
635
  (let* ((specified (pathname specified))
         (defaults (pathname defaults))
636
         (directory (normalize-pathname-directory-component (pathname-directory specified)))
637
638
639
         (name (or (pathname-name specified) (pathname-name defaults)))
         (type (or (pathname-type specified) (pathname-type defaults)))
         (version (or (pathname-version specified) (pathname-version defaults))))
640
    (labels ((unspecific-handler (p)
641
               (if (typep p 'logical-pathname) #'make-pathname-component-logical #'identity)))
642
      (multiple-value-bind (host device directory unspecific-handler)
643
          (ecase (first directory)
644
645
646
647
648
            ((:absolute)
             (values (pathname-host specified)
                     (pathname-device specified)
                     directory
                     (unspecific-handler specified)))
649
            ((nil :relative)
650
651
             (values (pathname-host defaults)
                     (pathname-device defaults)
652
                     (merge-pathname-directory-components directory (pathname-directory defaults))
653
                     (unspecific-handler defaults))))
654
655
656
657
        (make-pathname* :host host :device device :directory directory
                        :name (funcall unspecific-handler name)
                        :type (funcall unspecific-handler type)
                        :version (funcall unspecific-handler version))))))
658

659
660
661
662
(defun* pathname-parent-directory-pathname (pathname)
  "Returns a new pathname with same HOST, DEVICE, DIRECTORY as PATHNAME,
and NIL NAME, TYPE and VERSION components"
  (when pathname
663
664
665
666
    (make-pathname* :name nil :type nil :version nil
                    :directory (merge-pathname-directory-components
                                '(:relative :back) (pathname-directory pathname))
                    :defaults pathname)))
667

668
(define-modify-macro appendf (&rest args)
669
670
671
672
  append "Append onto list") ;; only to be used on short lists.

(define-modify-macro orf (&rest args)
  or "or a flag")
Daniel Barlow's avatar
   
Daniel Barlow committed
673

674
(defun* first-char (s)
675
676
  (and (stringp s) (plusp (length s)) (char s 0)))

677
(defun* last-char (s)
678
679
  (and (stringp s) (plusp (length s)) (char s (1- (length s)))))

680
681
(defun* list-to-hash-set (list &aux (h (make-hash-table :test 'equal)))
  (dolist (x list h) (setf (gethash x h) t)))
682

683
(defun* asdf-message (format-string &rest format-args)
684
  #-gcl<2.7 (declare (dynamic-extent format-args))
685
686
  (when *verbose-out*
    (apply 'format *verbose-out* format-string format-args)))
687

688
(defun* split-string (string &key max (separator '(#\Space #\Tab)))
689
690
  "Split STRING into a list of components separated by
any of the characters in the sequence SEPARATOR.
691
692
693
If MAX is specified, then no more than max(1,MAX) components will be returned,
starting the separation from the end, e.g. when called with arguments
 \"a.b.c.d.e\" :max 3 :separator \".\" it will return (\"a.b.c\" \"d\" \"e\")."
694
  (catch nil
695
696
    (let ((list nil) (words 0) (end (length string)))
      (flet ((separatorp (char) (find char separator))
697
             (done () (throw nil (cons (subseq string 0 end) list))))
698
699
700
701
702
703
704
705
706
        (loop
          :for start = (if (and max (>= words (1- max)))
                           (done)
                           (position-if #'separatorp string :end end :from-end t)) :do
          (when (null start)
            (done))
          (push (subseq string (1+ start) end) list)
          (incf words)
          (setf end start))))))
707

708
(defun* split-name-type (filename)
709
710
711
  (let ((unspecific
         ;; Giving :unspecific as argument to make-pathname is not portable.
         ;; See CLHS make-pathname and 19.2.2.2.3.
712
         ;; We only use it on implementations that support it,
713
         #+(or abcl allegro clozure cmu gcl genera lispworks mkcl sbcl scl xcl) :unspecific
714
         #+(or clisp ecl #|These haven't been tested:|# cormanlisp mcl) nil))
715
716
717
718
719
    (destructuring-bind (name &optional (type unspecific))
        (split-string filename :max 2 :separator ".")
      (if (equal name "")
          (values filename unspecific)
          (values name type)))))
720

721
(defun* component-name-to-pathname-components (s &key force-directory force-relative)
722
723
724
725
726
727
728
729
730
731
  "Splits the path string S, returning three values:
A flag that is either :absolute or :relative, indicating
   how the rest of the values are to be interpreted.
A directory path --- a list of strings, suitable for
   use with MAKE-PATHNAME when prepended with the flag
   value.
A filename with type extension, possibly NIL in the
   case of a directory pathname.
FORCE-DIRECTORY forces S to be interpreted as a directory
pathname \(third return value will be NIL, final component
732
733
734
735
736
of S will be treated as part of the directory path.

The intention of this function is to support structured component names,
e.g., \(:file \"foo/bar\"\), which will be unpacked to relative
pathnames."
737
  (check-type s string)
738
  (when (find #\: s)
739
    (error (compatfmt "~@<A portable ASDF pathname designator cannot include a #\: character: ~3i~_~S~@:>") s))
740
  (let* ((components (split-string s :separator "/"))
741
742
743
         (last-comp (car (last components))))
    (multiple-value-bind (relative components)
        (if (equal (first components) "")
744
            (if (equal (first-char s) #\/)
745
746
                (progn
                  (when force-relative
747
                    (error (compatfmt "~@<Absolute pathname designator not allowed: ~3i~_~S~@:>") s))
748
                  (values :absolute (cdr components)))
749
                (values :relative nil))
750
          (values :relative components))
751
752
      (setf components (remove-if #'(lambda (x) (member x '("" ".") :test #'equal)) components))
      (setf components (substitute :back ".." components :test #'equal))
753
754
      (cond
        ((equal last-comp "")
755
         (values relative components nil)) ; "" already removed
756
757
758
759
        (force-directory
         (values relative components nil))
        (t
         (values relative (butlast components) last-comp))))))
760

761
(defun* remove-keys (key-names args)
762
763
764
765
766
  (loop :for (name val) :on args :by #'cddr
    :unless (member (symbol-name name) key-names
                    :key #'symbol-name :test 'equal)
    :append (list name val)))

767
(defun* remove-keyword (key args)
768
769
770
  (loop :for (k v) :on args :by #'cddr
    :unless (eq k key)
    :append (list k v)))
771

772
(defun* getenv (x)
773
  (declare (ignorable x))
774
  #+(or abcl clisp ecl xcl) (ext:getenv x)
775
776
777
  #+allegro (sys:getenv x)
  #+clozure (ccl:getenv x)
  #+(or cmu scl) (cdr (assoc x ext:*environment-list* :test #'string=))
778
779
780
781
782
783
784
785
786
787
  #+cormanlisp
  (let* ((buffer (ct:malloc 1))
         (cname (ct:lisp-string-to-c-string x))
         (needed-size (win:getenvironmentvariable cname buffer 0))
         (buffer1 (ct:malloc (1+ needed-size))))
    (prog1 (if (zerop (win:getenvironmentvariable cname buffer1 needed-size))
               nil
               (ct:c-string-to-lisp-string buffer1))
      (ct:free buffer)
      (ct:free buffer1)))
788
789
790
791
792
793
794
  #+gcl (system:getenv x)
  #+genera nil
  #+lispworks (lispworks:environment-variable x)
  #+mcl (ccl:with-cstrs ((name x))
          (let ((value (_getenv name)))
            (unless (ccl:%null-ptr-p value)
              (ccl:%get-cstring value))))
795
  #+mkcl (#.(or (find-symbol* 'getenv :si) (find-symbol* 'getenv :mk-ext)) x)
796
  #+sbcl (sb-ext:posix-getenv x)
797
  #-(or abcl allegro clisp clozure cmu cormanlisp ecl gcl genera lispworks mcl mkcl sbcl scl xcl)
798
  (error "~S is not supported on your implementation" 'getenv))
799

800
(defun* directory-pathname-p (pathname)
801
  "Does PATHNAME represent a directory?
802
803

A directory-pathname is a pathname _without_ a filename. The three
804
805
ways that the filename components can be missing are for it to be NIL,
:UNSPECIFIC or the empty string.
806

807
Note that this does _not_ check to see that PATHNAME points to an
808
actually-existing directory."
809
810
811
812
813
814
815
816
  (when pathname
    (let ((pathname (pathname pathname)))
      (flet ((check-one (x)
               (member x '(nil :unspecific "") :test 'equal)))
        (and (not (wild-pathname-p pathname))
             (check-one (pathname-name pathname))
             (check-one (pathname-type pathname))
             t)))))
817

818
(defun* ensure-directory-pathname (pathspec)
819
  "Converts the non-wild pathname designator PATHSPEC to directory form."
820
821
  (cond
   ((stringp pathspec)
822
    (ensure-directory-pathname (pathname pathspec)))
823
   ((not (pathnamep pathspec))
824
    (error (compatfmt "~@<Invalid pathname designator ~S~@:>") pathspec))
825
   ((wild-pathname-p pathspec)
826
    (error (compatfmt "~@<Can't reliably convert wild pathname ~3i~_~S~@:>") pathspec))
827
828
829
   ((directory-pathname-p pathspec)
    pathspec)
   (t
830
831
832
833
834
    (make-pathname* :directory (append (or (normalize-pathname-directory-component
                                            (pathname-directory pathspec))
                                           (list :relative))
                                       (list (file-namestring pathspec)))
                    :name nil :type nil :version nil :defaults pathspec))))
835

836
(defun* absolute-pathname-p (pathspec)
837
  (and (typep pathspec '(or pathname string))
838
839
       (eq :absolute (car (normalize-pathname-directory-component
                           (pathname-directory (pathname pathspec)))))))
840

841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
(defun* coerce-pathname (name &key type defaults)
  "coerce NAME into a PATHNAME.
When given a string, portably decompose it into a relative pathname:
#\\/ separates subdirectories. The last #\\/-separated string is as follows:
if TYPE is NIL, its last #\\. if any separates name and type from from type;
if TYPE is a string, it is the type, and the whole string is the name;
if TYPE is :DIRECTORY, the string is a directory component;
if the string is empty, it's a directory.
Any directory named .. is read as :BACK.
Host, device and version components are taken from DEFAULTS."
  ;; The defaults are required notably because they provide the default host
  ;; to the below make-pathname, which may crucially matter to people using
  ;; merge-pathnames with non-default hosts,  e.g. for logical-pathnames.
  ;; NOTE that the host and device slots will be taken from the defaults,
  ;; but that should only matter if you later merge relative pathnames with
  ;; CL:MERGE-PATHNAMES instead of ASDF:MERGE-PATHNAMES*
  (etypecase name
    ((or null pathname)
     name)
    (symbol
     (coerce-pathname (string-downcase name) :type type :defaults defaults))
    (string
     (multiple-value-bind (relative path filename)
         (component-name-to-pathname-components name :force-directory (eq type :directory)
                                                :force-relative t)
       (multiple-value-bind (name type)
           (cond
             ((or (eq type :directory) (null filename))
              (values nil nil))
             (type
              (values filename type))
             (t
              (split-name-type filename)))
874
         (apply 'make-pathname* :directory (cons relative path) :name name :type type
875
876
877
                (when defaults `(:defaults ,defaults))))))))

(defun* merge-component-name-type (name &key type defaults)
878
  ;; For backward-compatibility only, for people using internals.
879
880
881
882
883
884
885
886
  ;; Will be removed in a future release, e.g. 2.016.
  (warn "Please don't use ASDF::MERGE-COMPONENT-NAME-TYPE. Use ASDF:COERCE-PATHNAME.")
  (coerce-pathname name :type type :defaults defaults))

(defun* subpathname (pathname subpath &key type)
  (and pathname (merge-pathnames* (coerce-pathname subpath :type type)
                                  (pathname-directory-pathname pathname))))

887
(defun* subpathname* (pathname subpath &key type)
888
889
890
  (and pathname
       (subpathname (ensure-directory-pathname pathname) subpath :type type)))

891
(defun* length=n-p (x n) ;is it that (= (length x) n) ?
892
893
894
895
896
897
898
899
  (check-type n (integer 0 *))
  (loop
    :for l = x :then (cdr l)
    :for i :downfrom n :do
    (cond
      ((zerop i) (return (null l)))
      ((not (consp l)) (return nil)))))

900
(defun* string-suffix-p (s suffix)
901
902
903
904
905
  (check-type s string)
  (check-type suffix string)
  (let ((start (- (length s) (length suffix))))
    (and (<= 0 start)
         (string-equal s suffix :start1 start))))
906

907
(defun* read-file-forms (file)
908
909
910
911
912
913
  (with-open-file (in file)
    (loop :with eof = (list nil)
     :for form = (read in nil eof)
     :until (eq form eof)
     :collect form)))

914
(defun* pathname-root (pathname)
915
916
917
918
919
  (make-pathname* :directory '(:absolute)
                  :name nil :type nil :version nil
                  :defaults pathname ;; host device, and on scl, *some*
                  ;; scheme-specific parts: port username password, not others:
                  . #.(or #+scl '(:parameters nil :query nil :fragment nil))))
920

921
(defun* probe-file* (p)
922
923
  "when given a pathname P, probes the filesystem for a file or directory
with given pathname and if it exists return its truename."
924
  (etypecase p
925
926
927
    (null nil)
    (string (probe-file* (parse-namestring p)))
    (pathname (unless (wild-pathname-p p)
928
                #.(or #+(or allegro clozure cmu cormanlisp ecl lispworks mkcl sbcl scl)
929
                      '(probe-file p)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
930
931
                      #+clisp (aif (find-symbol* '#:probe-pathname :ext)
                                   `(ignore-errors (,it p)))
932
933
934
935
936
937
                      #+gcl<2.7
                      '(or (probe-file p)
                        (and (directory-pathname-p p)
                         (ignore-errors
                          (ensure-directory-pathname
                           (truename (subpathname (ensure-directory-pathname p) "."))))))
938
                      '(ignore-errors (truename p)))))))
939

940
(defun* truenamize (pathname &optional (defaults *default-pathname-defaults*))
941
  "Resolve as much of a pathname as possible"
942
  (block nil
943
944
    (when (typep pathname '(or null logical-pathname)) (return pathname))
    (let ((p (merge-pathnames* pathname defaults)))
945
      (when (typep p 'logical-pathname) (return p))
946
947
      (let ((found (probe-file* p)))
        (when found (return found)))
948
949
950
951
952
      (unless (absolute-pathname-p p)
        (let ((true-defaults (ignore-errors (truename defaults))))
          (when true-defaults
            (setf p (merge-pathnames pathname true-defaults)))))
      (unless (absolute-pathname-p p) (return p))
953
      (let ((sofar (probe-file* (pathname-root p))))
954
        (unless sofar (return p))
955
        (flet ((solution (directories)
956
                 (merge-pathnames*