upgrade.lisp 8.21 KB
Newer Older
1 2 3 4
;;;; -------------------------------------------------------------------------
;;;; Handle upgrade as forward- and backward-compatibly as possible
;; See https://bugs.launchpad.net/asdf/+bug/485687

5
(uiop/package:define-package :asdf/upgrade
6
  (:recycle :asdf/upgrade :asdf)
7
  (:use :uiop/common-lisp :uiop)
8
  (:export
9
   #:asdf-version #:*previous-asdf-versions* #:*asdf-version*
10
   #:asdf-message #:*verbose-out*
11
   #:upgrading-p #:when-upgrading #:upgrade-asdf #:asdf-upgrade-error #:defparameter*
12
   #:*post-upgrade-cleanup-hook* #:*post-upgrade-restart-hook* #:cleanup-upgraded-asdf
13
   ;; There will be no symbol left behind!
14
   #:intern*)
15
  (:import-from :uiop/package #:intern* #:find-symbol*))
16 17 18 19
(in-package :asdf/upgrade)

;;; Special magic to detect if this is an upgrade

20
(with-upgradability ()
21 22 23 24 25
  (defun asdf-version ()
    "Exported interface to the version of ASDF currently installed. A string.
You can compare this string with e.g.: (ASDF:VERSION-SATISFIES (ASDF:ASDF-VERSION) \"3.4.5.67\")."
    (when (find-package :asdf)
      (or (symbol-value (find-symbol (string :*asdf-version*) :asdf))
26 27 28 29 30
          (let* ((revsym (find-symbol (string :*asdf-revision*) :asdf))
                 (rev (and revsym (boundp revsym) (symbol-value revsym))))
            (etypecase rev
              (string rev)
              (cons (format nil "~{~D~^.~}" rev))
31
              (null "1.0"))))))
32 33
  ;; Important: define *p-a-v* /before/ *a-v* so that it initializes correctly.
  (defvar *previous-asdf-versions* (if-let (previous (asdf-version)) (list previous)))
34
  (defvar *asdf-version* nil)
35
  ;; We need to clear systems from versions yet older than the below:
36
  (defparameter *oldest-forward-compatible-asdf-version* "2.33") ;; 2.32.13 renames a slot in component.
37 38 39 40 41 42 43 44 45
  (defmacro defparameter* (var value &optional docstring)
    (let* ((name (string-trim "*" var))
           (valfun (intern (format nil "%~A-~A-~A" :compute name :value)))
           (clearfun (intern (format nil "%~A-~A" :clear name))))
      `(progn
         (defun ,valfun () ,value)
         (defvar ,var (,valfun) ,@(ensure-list docstring))
         (defun ,clearfun () (setf ,var (,valfun)))
         (register-hook-function '*post-upgrade-cleanup-hook* ',clearfun))))
46
  (defvar *verbose-out* nil)
47
  (defun asdf-message (format-string &rest format-args)
48
    (when *verbose-out* (apply 'format *verbose-out* format-string format-args)))
49 50
  (defvar *post-upgrade-cleanup-hook* ())
  (defvar *post-upgrade-restart-hook* ())
51
  (defun upgrading-p ()
52 53
    (and *previous-asdf-versions* (not (equal *asdf-version* (first *previous-asdf-versions*)))))
  (defmacro when-upgrading ((&key (upgrading-p '(upgrading-p)) when) &body body)
54
    `(with-upgradability ()
55 56
       (when (and ,upgrading-p ,@(when when `(,when)))
         (handler-bind ((style-warning #'muffle-warning))
57
           (eval '(progn ,@body))))))
58
  (let* (;; For bug reporting sanity, please always bump this version when you modify this file.
59
         ;; Please also modify asdf.asd to reflect this change. make bump-version v=3.4.5.67.8
60
         ;; can help you do these changes in synch (look at the source for documentation).
61
         ;; Relying on its automation, the version is now redundantly present on top of asdf.lisp.
62 63
         ;; "3.4" would be the general branch for major version 3, minor version 4.
         ;; "3.4.5" would be an official release in the 3.4 branch.
64
         ;; "3.4.5.67" would be a development version in the official branch, on top of 3.4.5.
65 66
         ;; "3.4.5.0.8" would be your eighth local modification of official release 3.4.5
         ;; "3.4.5.67.8" would be your eighth local modification of development version 3.4.5.67
67
         (asdf-version "3.1.0.56")
68
         (existing-version (asdf-version)))
69
    (setf *asdf-version* asdf-version)
70 71
    (when (and existing-version (not (equal asdf-version existing-version)))
      (push existing-version *previous-asdf-versions*)
72 73
      (when (or *verbose-out* *load-verbose*)
        (format (or *verbose-out* *trace-output*)
74
                (compatfmt "~&~@<; ~@;Upgrading ASDF ~@[from version ~A ~]to version ~A~@:>~%")
75
                existing-version asdf-version)))))
76 77 78 79 80 81

(when-upgrading ()
  (let ((redefined-functions ;; gf signature and/or semantics changed incompatibly. Oops.
          '(#:component-relative-pathname #:component-parent-pathname ;; component
            #:source-file-type
            #:find-system #:system-source-file #:system-relative-pathname ;; system
82 83
             #:find-component ;; find-component
             #:explain #:perform #:perform-with-restarts #:input-files #:output-files ;; action
84
             #:component-depends-on #:operation-done-p #:component-depends-on
85
             #:traverse ;; backward-interface
86
             #:operate  ;; operate
87
             #:parse-component-form ;; defsystem
88
             #:apply-output-translations ;; output-translations
89
             #:process-output-translations-directive
90
             #:inherit-source-registry #:process-source-registry ;; source-registry
91
             #:process-source-registry-directive
92
             #:trivial-system-p ;; bundle
93
             ;; NB: it's too late to do anything about uiop functions!
94 95 96 97
             ))
         (uninterned-symbols
           '(#:*asdf-revision* #:around #:asdf-method-combination
             #:split #:make-collector #:do-dep #:do-one-dep
98
             #:component-self-dependencies
99 100
             #:resolve-relative-location-component #:resolve-absolute-location-component
             #:output-files-for-system-and-operation))) ; obsolete ASDF-BINARY-LOCATION function
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
101
    (loop :for name :in redefined-functions
102 103
          :for sym = (find-symbol* name :asdf nil) :do
            (when sym
104 105
              ;; On CLISP we seem to be unable to fmakunbound and define a function in the same fasl. Sigh.
              #-clisp (fmakunbound sym)))
106
    (loop :with asdf = (find-package :asdf)
107
          :for name :in uninterned-symbols
108 109 110 111 112 113 114 115 116 117 118
          :for sym = (find-symbol* name :asdf nil)
          :for base-pkg = (and sym (symbol-package sym)) :do
            (when sym
              (cond
                ((or (eq base-pkg asdf) (not base-pkg))
                 (unintern* sym asdf)
                 (intern* sym asdf))
                (t
                 (unintern* sym base-pkg)
                 (let ((new (intern* sym base-pkg)))
                   (shadowing-import new asdf))))))))
119

120

121
;;; Self-upgrade functions
122

123 124 125 126 127
(with-upgradability ()
  (defun asdf-upgrade-error ()
    ;; 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.
    (error "When a system transitively depends on ASDF, it must :defsystem-depends-on (:asdf)~%~
128
          Otherwise, when you upgrade from ASDF 2, you must do it before you operate on any system.~%"))
129

130 131 132 133 134
  (defun cleanup-upgraded-asdf (&optional (old-version (first *previous-asdf-versions*)))
    (let ((new-version (asdf-version)))
      (unless (equal old-version new-version)
        (push new-version *previous-asdf-versions*)
        (when old-version
135 136 137 138 139
          (if (version<= new-version old-version)
              (error (compatfmt "~&~@<; ~@;Downgraded ASDF from version ~A to version ~A~@:>~%")
                     old-version new-version)
              (asdf-message (compatfmt "~&~@<; ~@;Upgraded ASDF from version ~A to version ~A~@:>~%")
                            old-version new-version))
140 141 142 143 144 145
          ;; In case the previous version was too old to be forward-compatible, clear systems.
          ;; TODO: if needed, we may have to define a separate hook to run
          ;; in case of forward-compatible upgrade.
          ;; Or to move the tests forward-compatibility test inside each hook function?
          (unless (version<= *oldest-forward-compatible-asdf-version* old-version)
            (call-functions (reverse *post-upgrade-cleanup-hook*)))
146
          t))))
147

148 149
  (defun upgrade-asdf ()
    "Try to upgrade of ASDF. If a different version was used, return T.
150
   We need do that before we operate on anything that may possibly depend on ASDF."
151 152
    (let ((*load-print* nil)
          (*compile-print* nil))
153
      (handler-bind (((or style-warning) #'muffle-warning))
154 155 156
        (symbol-call :asdf :load-system :asdf :verbose nil))))

  (register-hook-function '*post-upgrade-cleanup-hook* 'upgrade-configuration))
157