output-translations.lisp 16 KB
Newer Older
1 2 3
;;;; ---------------------------------------------------------------------------
;;;; asdf-output-translations

4
(uiop/package:define-package :asdf/output-translations
5
  (:recycle :asdf/output-translations :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade)
7
  (:export
8
   #:*output-translations* #:*output-translations-parameter*
9 10 11 12 13 14 15 16 17 18 19 20
   #:invalid-output-translation
   #:output-translations #:output-translations-initialized-p
   #:initialize-output-translations #:clear-output-translations
   #:disable-output-translations #:ensure-output-translations
   #:apply-output-translations
   #:validate-output-translations-directive #:validate-output-translations-form
   #:validate-output-translations-file #:validate-output-translations-directory
   #:parse-output-translations-string #:wrapping-output-translations
   #:user-output-translations-pathname #:system-output-translations-pathname
   #:user-output-translations-directory-pathname #:system-output-translations-directory-pathname
   #:environment-output-translations #:process-output-translations
   #:compute-output-translations
21
   #+abcl #:translate-jar-pathname
22 23 24
   ))
(in-package :asdf/output-translations)

25 26
;; (setf output-translations) between 2.27 and 3.0.3 was using a defsetf macro
;; for the sake of obsolete versions of GCL 2.6. Make sure it doesn't come to haunt us.
27
#-mkcl ; mkcl 1.1.9 can't fmakunbound a setf function.
28
(when-upgrading (:version "3.1.2") (fmakunbound '(setf output-translations)))
29

30 31 32
(with-upgradability ()
  (define-condition invalid-output-translation (invalid-configuration warning)
    ((format :initform (compatfmt "~@<Invalid asdf output-translation ~S~@[ in ~S~]~@{ ~@?~}~@:>"))))
33

34 35
  (defvar *output-translations* ()
    "Either NIL (for uninitialized), or a list of one element,
36 37 38 39
said element itself being a sorted list of mappings.
Each mapping is a pair of a source pathname and destination pathname,
and the order is by decreasing length of namestring of the source pathname.")

40
  (defun output-translations ()
41
    "Return the configured output-translations, if any"
42
    (car *output-translations*))
43

44
  ;; Set the output-translations, by sorting the provided new-value.
45 46 47 48 49 50 51 52
  (defun set-output-translations (new-value)
    (setf *output-translations*
          (list
           (stable-sort (copy-list new-value) #'>
                        :key #'(lambda (x)
                                 (etypecase (car x)
                                   ((eql t) -1)
                                   (pathname
53 54 55
                                    (let ((directory
                                           (normalize-pathname-directory-component
                                            (pathname-directory (car x)))))
56 57
                                      (if (listp directory) (length directory) 0))))))))
    new-value)
58
  (defun (setf output-translations) (new-value) (set-output-translations new-value))
59

60
  (defun output-translations-initialized-p ()
61
    "Have the output-translations been initialized yet?"
62
    (and *output-translations* t))
63

64 65 66 67 68
  (defun clear-output-translations ()
    "Undoes any initialization of the output translations."
    (setf *output-translations* '())
    (values))
  (register-clear-configuration-hook 'clear-output-translations)
69

70 71 72

  ;;; Validation of the configuration directives...

73 74 75 76 77 78 79 80 81 82 83
  (defun validate-output-translations-directive (directive)
    (or (member directive '(:enable-user-cache :disable-cache nil))
        (and (consp directive)
             (or (and (length=n-p directive 2)
                      (or (and (eq (first directive) :include)
                               (typep (second directive) '(or string pathname null)))
                          (and (location-designator-p (first directive))
                               (or (location-designator-p (second directive))
                                   (location-function-p (second directive))))))
                 (and (length=n-p directive 1)
                      (location-designator-p (first directive)))))))
84

85 86 87 88 89 90
  (defun validate-output-translations-form (form &key location)
    (validate-configuration-form
     form
     :output-translations
     'validate-output-translations-directive
     :location location :invalid-form-reporter 'invalid-output-translation))
91

92 93 94
  (defun validate-output-translations-file (file)
    (validate-configuration-file
     file 'validate-output-translations-form :description "output translations"))
95

96 97 98 99
  (defun validate-output-translations-directory (directory)
    (validate-configuration-directory
     directory :output-translations 'validate-output-translations-directive
               :invalid-form-reporter 'invalid-output-translation))
100

101 102

  ;;; Parse the ASDF_OUTPUT_TRANSLATIONS environment variable and/or some file contents
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  (defun parse-output-translations-string (string &key location)
    (cond
      ((or (null string) (equal string ""))
       '(:output-translations :inherit-configuration))
      ((not (stringp string))
       (error (compatfmt "~@<Environment string isn't: ~3i~_~S~@:>") string))
      ((eql (char string 0) #\")
       (parse-output-translations-string (read-from-string string) :location location))
      ((eql (char string 0) #\()
       (validate-output-translations-form (read-from-string string) :location location))
      (t
       (loop
         :with inherit = nil
         :with directives = ()
         :with start = 0
         :with end = (length string)
         :with source = nil
         :with separator = (inter-directory-separator)
         :for i = (or (position separator string :start start) end) :do
           (let ((s (subseq string start i)))
             (cond
               (source
                (push (list source (if (equal "" s) nil s)) directives)
                (setf source nil))
               ((equal "" s)
                (when inherit
                  (error (compatfmt "~@<Only one inherited configuration allowed: ~3i~_~S~@:>")
                         string))
                (setf inherit t)
                (push :inherit-configuration directives))
               (t
                (setf source s)))
             (setf start (1+ i))
             (when (> start end)
               (when source
                 (error (compatfmt "~@<Uneven number of components in source to destination mapping: ~3i~_~S~@:>")
                        string))
               (unless inherit
                 (push :ignore-inherited-configuration directives))
               (return `(:output-translations ,@(nreverse directives)))))))))
143

144 145

  ;; The default sources of configuration for output-translations
146
  (defparameter* *default-output-translations*
147 148 149 150 151
    '(environment-output-translations
      user-output-translations-pathname
      user-output-translations-directory-pathname
      system-output-translations-pathname
      system-output-translations-directory-pathname))
152

153 154
  ;; Compulsory implementation-dependent wrapping for the translations:
  ;; handle implementation-provided systems.
155 156
  (defun wrapping-output-translations ()
    `(:output-translations
157 158
    ;; Some implementations have precompiled ASDF systems,
    ;; so we must disable translations for implementation paths.
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
159
      #+(or clasp #|clozure|# ecl mkcl sbcl)
160 161 162 163 164 165 166 167 168 169
      ,@(let ((h (resolve-symlinks* (lisp-implementation-directory))))
          (when h `(((,h ,*wild-path*) ()))))
      #+mkcl (,(translate-logical-pathname "CONTRIB:") ())
      ;; All-import, here is where we want user stuff to be:
      :inherit-configuration
      ;; These are for convenience, and can be overridden by the user:
      #+abcl (#p"/___jar___file___root___/**/*.*" (:user-cache #p"**/*.*"))
      #+abcl (#p"jar:file:/**/*.jar!/**/*.*" (:function translate-jar-pathname))
      ;; We enable the user cache by default, and here is the place we do:
      :enable-user-cache))
170

171
  ;; Relative pathnames of output-translations configuration to XDG configuration directory
172 173
  (defparameter *output-translations-file* (parse-unix-namestring "common-lisp/asdf-output-translations.conf"))
  (defparameter *output-translations-directory* (parse-unix-namestring "common-lisp/asdf-output-translations.conf.d/"))
174

175
  ;; Locating various configuration pathnames, depending on input or output intent.
176
  (defun user-output-translations-pathname (&key (direction :input))
177
    (xdg-config-pathname *output-translations-file* direction))
178
  (defun system-output-translations-pathname (&key (direction :input))
179
    (find-preferred-file (system-config-pathnames *output-translations-file*)
180
                         :direction direction))
181
  (defun user-output-translations-directory-pathname (&key (direction :input))
182
    (xdg-config-pathname *output-translations-directory* direction))
183
  (defun system-output-translations-directory-pathname (&key (direction :input))
184
    (find-preferred-file (system-config-pathnames *output-translations-directory*)
185
                         :direction direction))
186 187
  (defun environment-output-translations ()
    (getenv "ASDF_OUTPUT_TRANSLATIONS"))
188

189 190 191

  ;;; Processing the configuration.

192
  (defgeneric process-output-translations (spec &key inherit collect))
193

194 195 196
  (defun inherit-output-translations (inherit &key collect)
    (when inherit
      (process-output-translations (first inherit) :collect collect :inherit (rest inherit))))
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  (defun* (process-output-translations-directive) (directive &key inherit collect)
    (if (atom directive)
        (ecase directive
          ((:enable-user-cache)
           (process-output-translations-directive '(t :user-cache) :collect collect))
          ((:disable-cache)
           (process-output-translations-directive '(t t) :collect collect))
          ((:inherit-configuration)
           (inherit-output-translations inherit :collect collect))
          ((:ignore-inherited-configuration :ignore-invalid-entries nil)
           nil))
        (let ((src (first directive))
              (dst (second directive)))
          (if (eq src :include)
              (when dst
                (process-output-translations (pathname dst) :inherit nil :collect collect))
              (when src
                (let ((trusrc (or (eql src t)
                                  (let ((loc (resolve-location src :ensure-directory t :wilden t)))
                                    (if (absolute-pathname-p loc) (resolve-symlinks* loc) loc)))))
                  (cond
                    ((location-function-p dst)
                     (funcall collect
221
                              (list trusrc (ensure-function (second dst)))))
222
                    ((typep dst 'boolean)
223 224
                     (funcall collect (list trusrc t)))
                    (t
225
                     (let* ((trudst (resolve-location dst :ensure-directory t :wilden t)))
226 227
                       (funcall collect (list trudst t))
                       (funcall collect (list trusrc trudst)))))))))))
228

229 230 231 232
  (defmethod process-output-translations ((x symbol) &key
                                                       (inherit *default-output-translations*)
                                                       collect)
    (process-output-translations (funcall x) :inherit inherit :collect collect))
233
  (defmethod process-output-translations ((pathname pathname) &key inherit collect)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    (cond
      ((directory-pathname-p pathname)
       (process-output-translations (validate-output-translations-directory pathname)
                                    :inherit inherit :collect collect))
      ((probe-file* pathname :truename *resolve-symlinks*)
       (process-output-translations (validate-output-translations-file pathname)
                                    :inherit inherit :collect collect))
      (t
       (inherit-output-translations inherit :collect collect))))
  (defmethod process-output-translations ((string string) &key inherit collect)
    (process-output-translations (parse-output-translations-string string)
                                 :inherit inherit :collect collect))
  (defmethod process-output-translations ((x null) &key inherit collect)
    (inherit-output-translations inherit :collect collect))
  (defmethod process-output-translations ((form cons) &key inherit collect)
    (dolist (directive (cdr (validate-output-translations-form form)))
      (process-output-translations-directive directive :inherit inherit :collect collect)))
251

252 253 254

  ;;; Top-level entry-points to configure output-translations

255 256 257 258 259 260 261
  (defun compute-output-translations (&optional parameter)
    "read the configuration, return it"
    (remove-duplicates
     (while-collecting (c)
       (inherit-output-translations
        `(wrapping-output-translations ,parameter ,@*default-output-translations*) :collect #'c))
     :test 'equal :from-end t))
262

263 264
  ;; Saving the user-provided parameter to output-translations, if any,
  ;; so we can recompute the translations after code upgrade.
265
  (defvar *output-translations-parameter* nil)
266

267
  ;; Main entry-point for users.
268 269
  (defun initialize-output-translations (&optional (parameter *output-translations-parameter*))
    "read the configuration, initialize the internal configuration variable,
270
return the configuration"
271 272
    (setf *output-translations-parameter* parameter
          (output-translations) (compute-output-translations parameter)))
273

274 275
  (defun disable-output-translations ()
    "Initialize output translations in a way that maps every file to itself,
276
effectively disabling the output translation facility."
277 278
    (initialize-output-translations
     '(:output-translations :disable-cache :ignore-inherited-configuration)))
279

280 281 282 283 284 285 286 287
  ;; checks an initial variable to see whether the state is initialized
  ;; or cleared. In the former case, return current configuration; in
  ;; the latter, initialize.  ASDF will call this function at the start
  ;; of (asdf:find-system).
  (defun ensure-output-translations ()
    (if (output-translations-initialized-p)
        (output-translations)
        (initialize-output-translations)))
288

289 290

  ;; Top-level entry-point to _use_ output-translations
291
  (defun* (apply-output-translations) (path)
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    (etypecase path
      (logical-pathname
       path)
      ((or pathname string)
       (ensure-output-translations)
       (loop* :with p = (resolve-symlinks* path)
              :for (source destination) :in (car *output-translations*)
              :for root = (when (or (eq source t)
                                    (and (pathnamep source)
                                         (not (absolute-pathname-p source))))
                            (pathname-root p))
              :for absolute-source = (cond
                                       ((eq source t) (wilden root))
                                       (root (merge-pathnames* source root))
                                       (t source))
              :when (or (eq source t) (pathname-match-p p absolute-source))
              :return (translate-pathname* p absolute-source destination root source)
              :finally (return p)))))
310

311

312
  ;; Hook into uiop's output-translation mechanism
313
  #-cormanlisp
314 315
  (setf *output-translation-function* 'apply-output-translations)

316 317 318

  ;;; Implementation-dependent hacks
  #+abcl ;; ABCL: make it possible to use systems provided in the ABCL jar.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  (defun translate-jar-pathname (source wildcard)
    (declare (ignore wildcard))
    (flet ((normalize-device (pathname)
             (if (find :windows *features*)
                 pathname
                 (make-pathname :defaults pathname :device :unspecific))))
      (let* ((jar
               (pathname (first (pathname-device source))))
             (target-root-directory-namestring
               (format nil "/___jar___file___root___/~@[~A/~]"
                       (and (find :windows *features*)
                            (pathname-device jar))))
             (relative-source
               (relativize-pathname-directory source))
             (relative-jar
               (relativize-pathname-directory (ensure-directory-pathname jar)))
             (target-root-directory
               (normalize-device
                (pathname-directory-pathname
                 (parse-namestring target-root-directory-namestring))))
             (target-root
               (merge-pathnames* relative-jar target-root-directory))
             (target
               (merge-pathnames* relative-source target-root)))
        (normalize-device (apply-output-translations target))))))
344