upgrade.lisp 7.51 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 #: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
  ;; Important: define *p-a-v* /before/ *a-v* so that it initializes correctly.
33 34 35 36 37 38 39 40 41 42
  (defvar *previous-asdf-versions*
    (let ((previous (asdf-version)))
      (when previous
        ;; Punt on hard package upgrade: from ASDF1 or ASDF2
        (when (version< previous "2.27") ;; 2.27 is the first to have the :asdf3 feature.
          (let ((away (format nil "~A-~A" :asdf previous)))
            (rename-package :asdf away)
            (when *load-verbose*
              (format t "~&; Renamed old ~A package away to ~A~%" :asdf away)))))
        (list previous)))
43
  (defvar *asdf-version* nil)
44
  ;; We need to clear systems from versions yet older than the below:
45
  (defparameter *oldest-forward-compatible-asdf-version* "2.33") ;; 2.32.13 renames a slot in component.
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 52 53
  (defun upgrading-p (&optional (oldest-compatible-version *oldest-forward-compatible-asdf-version*))
    (and *previous-asdf-versions*
         (version< (first *previous-asdf-versions*) oldest-compatible-version)))
54 55 56 57 58 59 60 61
  (defmacro defparameter* (var value &optional docstring (version *oldest-forward-compatible-asdf-version*))
    (let* ((name (string-trim "*" var))
           (valfun (intern (format nil "%~A-~A-~A" :compute name :value))))
      `(progn
         (defun ,valfun () ,value)
         (defvar ,var (,valfun) ,@(ensure-list docstring))
         (when (upgrading-p ,version)
           (setf ,var (,valfun))))))
62 63
  (defmacro when-upgrading ((&key (version *oldest-forward-compatible-asdf-version*)
                               (upgrading-p `(upgrading-p ,version)) when) &body body)
64
    "A wrapper macro for code that should only be run when upgrading a
65
previously-loaded version of ASDF."
66
    `(with-upgradability ()
67 68
       (when (and ,upgrading-p ,@(when when `(,when)))
         (handler-bind ((style-warning #'muffle-warning))
69
           (eval '(progn ,@body))))))
70
  (let* (;; For bug reporting sanity, please always bump this version when you modify this file.
71
         ;; Please also modify asdf.asd to reflect this change. make bump-version v=3.4.5.67.8
72
         ;; can help you do these changes in synch (look at the source for documentation).
73
         ;; Relying on its automation, the version is now redundantly present on top of asdf.lisp.
74 75
         ;; "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.
76
         ;; "3.4.5.67" would be a development version in the official branch, on top of 3.4.5.
77 78
         ;; "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
79
         (asdf-version "3.1.7.21")
80
         (existing-version (asdf-version)))
81
    (setf *asdf-version* asdf-version)
82 83
    (when (and existing-version (not (equal asdf-version existing-version)))
      (push existing-version *previous-asdf-versions*)
84 85
      (when (or *verbose-out* *load-verbose*)
        (format (or *verbose-out* *trace-output*)
86
                (compatfmt "~&~@<; ~@;Upgrading ASDF ~@[from version ~A ~]to version ~A~@:>~%")
87
                existing-version asdf-version)))))
88 89 90

(when-upgrading ()
  (let ((redefined-functions ;; gf signature and/or semantics changed incompatibly. Oops.
91 92
          ;; NB: it's too late to do anything about functions in UIOP!
          ;; If you introduce some critically incompatibility there, you must change name.
93
          '()) ;; empty now that we don't unintern, but wholly punt on ASDF 2.26 or earlier.
94
        (redefined-classes
95
          ;; redefining the classes causes interim circularities
96
          ;; with the old ASDF during upgrade, and many implementations bork
97
          '((#:compile-concatenated-source-op (#:operation) ()))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
98
    (loop :for name :in redefined-functions
99 100
          :for sym = (find-symbol* name :asdf nil) :do
            (when sym
101 102
              ;; On CLISP we seem to be unable to fmakunbound and define a function in the same fasl. Sigh.
              #-clisp (fmakunbound sym)))
103
    (labels ((asym (x) (multiple-value-bind (s p) (if (consp x) (values (car x) (cadr x)) (values x :asdf))
104 105
                         (find-symbol* s p nil)))
             (asyms (l) (mapcar #'asym l)))
106
      (loop* :for (name superclasses slots) :in redefined-classes
107 108 109
             :for sym = (find-symbol* name :asdf nil)
             :when (and sym (find-class sym))
             :do (eval `(defclass ,sym ,(asyms superclasses) ,(asyms slots)))))))
110

111

112
;;; Self-upgrade functions
113

114 115 116 117 118 119
(with-upgradability ()
  (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
120 121 122 123 124
          (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))
125 126 127 128 129 130
          ;; 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*)))
131
          t))))
132

133 134
  (defun upgrade-asdf ()
    "Try to upgrade of ASDF. If a different version was used, return T.
135
   We need do that before we operate on anything that may possibly depend on ASDF."
136 137
    (let ((*load-print* nil)
          (*compile-print* nil))
138
      (handler-bind (((or style-warning) #'muffle-warning))
139 140 141
        (symbol-call :asdf :load-system :asdf :verbose nil))))

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